From 58c30aeeefa822548d00dc0a511ed7356438ca82 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Thorsten=20Wi=C3=9Fmann?= <uni@thorsten-wissmann.de>
Date: Fri, 11 Apr 2014 01:23:57 +0200
Subject: [PATCH] Implement tree printing

---
 src/CoolUtils.ml            |  2 ++
 src/CoolUtils.mli           |  2 ++
 src/OWLFunctionalParser.ml  | 14 ++++++++++++--
 src/OWLFunctionalParser.mli |  4 ++++
 src/coalg.ml                | 18 +++++++++++++++++-
 5 files changed, 37 insertions(+), 3 deletions(-)

diff --git a/src/CoolUtils.ml b/src/CoolUtils.ml
index 937064c..608526f 100644
--- a/src/CoolUtils.ml
+++ b/src/CoolUtils.ml
@@ -50,4 +50,6 @@ let intlist_of_string str = List.map int_of_string (Str.split (Str.regexp "[ \t,
 
 let compose f g x = f (g (x))
 let flip f y x = f x y
+let eval a f = f a
+let id a = a
 
diff --git a/src/CoolUtils.mli b/src/CoolUtils.mli
index 9e2a544..fb6fc5c 100644
--- a/src/CoolUtils.mli
+++ b/src/CoolUtils.mli
@@ -24,3 +24,5 @@ val intlist_of_string : string -> int list
 
 val compose : ('b -> 'c) -> ('a -> 'b) -> ('a -> 'c)
 val flip : ('a -> 'b -> 'c) -> ('b -> 'a -> 'c)
+val eval : 'a -> ('a -> 'b) -> 'b
+val id : 'a -> 'a
diff --git a/src/OWLFunctionalParser.ml b/src/OWLFunctionalParser.ml
index 68f882d..bfb6334 100644
--- a/src/OWLFunctionalParser.ml
+++ b/src/OWLFunctionalParser.ml
@@ -1,6 +1,7 @@
 
 module L = List
 
+open CoolUtils
 open Str
 open Stream
 
@@ -42,7 +43,7 @@ let line_stream_of_string string =
 
 let list_of_stream stream =
     let cons a b = a::b in
-    stream_fold cons stream []
+    L.rev (stream_fold cons stream [])
 
 let tokens_from_string (buf:string) : string annotated list =
     let lastline = ref 0 in
@@ -71,6 +72,9 @@ let tokens_from_string (buf:string) : string annotated list =
 let string_of_annotation (file,line,col) =
     file ^ ":" ^ (string_of_int line) ^ ":" ^ (string_of_int col)
 
+let string_of_annotated str_of_a (obj,anno) : string =
+    string_of_annotation anno ^ " " ^ str_of_a obj
+
 let tree_of_tokens lst : string annotated tree =
     let stream:string annotated Stream.t = Stream.of_list lst in
     (* returns the next tree in our forest *)
@@ -93,5 +97,11 @@ let tree_of_tokens lst : string annotated tree =
     let forest = list_of_stream (Stream.from (fun _ -> tree_of_token_stream ())) in
     Node forest
 
-
+let string_of_tree str_of_a atree : string =
+    let indent str = "  "^str in
+    let rec lines_of_tree t = match t with
+        | (Leaf a) -> [str_of_a a]
+        | (Node lst) -> L.map indent (L.concat (L.map lines_of_tree lst))
+    in
+    String.concat "\n" (lines_of_tree atree)
 
diff --git a/src/OWLFunctionalParser.mli b/src/OWLFunctionalParser.mli
index b157080..5c742b8 100644
--- a/src/OWLFunctionalParser.mli
+++ b/src/OWLFunctionalParser.mli
@@ -10,9 +10,13 @@ type 'a tree =
     | Leaf of 'a
     | Node of (('a tree) list)
 
+val list_of_stream : 'a Stream.t -> 'a list
+
 val string_of_annotation : annotation -> string
+val string_of_annotated : ('a -> string) -> 'a annotated -> string
 
 val tokens_from_string : string -> string annotated list
 val tree_of_tokens : string annotated list -> string annotated tree
+val string_of_tree : ('a -> string) -> 'a tree -> string
 
 (*val parse : string -> OWL.Ontology *)
diff --git a/src/coalg.ml b/src/coalg.ml
index 745191d..e10e999 100644
--- a/src/coalg.ml
+++ b/src/coalg.ml
@@ -6,6 +6,9 @@
 
 module CM = CoAlgMisc
 module CF = CoAlgFormula
+module OWLFP = OWLFunctionalParser
+
+open CoolUtils
 
 (* The type of formulae that are accepted. *)
 (*
@@ -37,7 +40,7 @@ let _ = Gc.set { (Gc.get()) with Gc.minor_heap_size = 4194304; major_heap_increm
 
 let printUsage () =
   print_endline "Usage: \"alc <task> <functor> [<flags>]\" where";
-  print_endline "       <task> in { sat print nnf prov (is »not.(sat ¬f)«) }";
+  print_endline "       <task> in { sat print nnf prov (is »not.(sat ¬f)«) owlcat }";
   print_endline "       <functor> in { MultiModalK MultiModalKD CoalitionLogic GML";
   print_endline "                      (or: K)     (or: KD)     (or: CL)       }";
   print_endline "       <flags> = a list of the following items";
@@ -142,6 +145,18 @@ let choiceNNF () =
       done
     with End_of_file -> ()
 
+let choiceOWLCat () =
+    let buf = ref "" in
+    try
+      while true do
+        let input = read_line () in
+        buf := !buf ^ input ^ "\n"
+      done
+    with End_of_file -> () ;
+    let t = OWLFP.tree_of_tokens (OWLFP.tokens_from_string !buf) in
+    print_endline (OWLFP.string_of_tree (OWLFP.string_of_annotated id) t)
+
+
 let rec parseFlags arr offs : unit =
     let offs = ref (offs) in
     let getParam () =
@@ -173,6 +188,7 @@ let _ =
     | "prov" -> choiceProvable ()
     | "print" -> choicePrint ()
     | "nnf" -> choiceNNF ()
+    | "owlcat" -> choiceOWLCat ()
     | _ -> printUsage ()
 
 
-- 
GitLab