.
[tatoo.git] / src / tatoo.ml
1 (***********************************************************************)
2 (*                                                                     *)
3 (*                               TAToo                                 *)
4 (*                                                                     *)
5 (*                     Kim Nguyen, LRI UMR8623                         *)
6 (*                   Université Paris-Sud & CNRS                       *)
7 (*                                                                     *)
8 (*  Copyright 2010-2012 Université Paris-Sud and Centre National de la *)
9 (*  Recherche Scientifique. All rights reserved.  This file is         *)
10 (*  distributed under the terms of the GNU Lesser General Public       *)
11 (*  License, with the special exception on linking described in file   *)
12 (*  ../LICENSE.                                                        *)
13 (*                                                                     *)
14 (***********************************************************************)
15
16 open Format
17
18 let time f arg msg =
19   let t1 = Unix.gettimeofday () in
20   let r = f arg in
21   let t2 = Unix.gettimeofday () in
22   let time = (t2 -. t1) *. 1000. in
23   Logger.msg `STATS "%s: %fms" msg time;
24   r
25
26
27 let compose_parallel run auto_list tree nodes () =
28   match auto_list with
29     [ auto ] -> [run auto tree nodes]
30   | _ -> assert false
31
32 let compose_sequential run auto_list tree nodes () =
33   [ List.fold_left (fun acc auto ->
34     run auto tree acc) nodes auto_list ]
35
36
37 let restart_parallel run auto_list tree nodes () =
38   match auto_list with
39     [ auto ] -> List.map snd (run auto tree nodes)
40   | _ -> assert false
41
42 let restart_sequential run auto_list tree nodes () =
43   List.map (fun auto -> run auto tree nodes) auto_list
44
45 let main () =
46   let () = Options.parse () in
47   let doc =
48     let fd, close_fd = match !Options.input_file with
49       None | Some "-" | Some "/dev/stdin" -> stdin, ignore
50     | Some input ->
51         let fd = open_in input in fd, fun () -> close_in fd
52     in
53     let d = time Naive_tree.load_xml_file fd "parsing xml document" in
54     close_fd (); d
55   in
56   let queries =
57     time
58       (fun l ->
59         List.map (fun q ->
60           Xpath.Parser.parse
61             (Ulexing.from_utf8_string q)) l)
62       !Options.queries
63       "parsing XPath queries"
64   in
65   (* parallel, compose  ->     action
66      true, true -> Ata.concat of all automata and single run
67      true, false -> Ata.merge of all automata and single run
68      false, true -> Eval first, then run on results then ...
69      false, false -> Eval first on root, then second on root then ...
70   *)
71   let auto_list =
72     time
73       (fun l ->
74         List.map (fun query -> Xpath.Compile.path query) l)
75       queries
76       "compiling XPath queries"
77   in
78   let auto_list =
79     if !Options.parallel then
80       match auto_list with
81         fst :: rest ->
82           let f =
83             if !Options.compose then
84               Ata.concat
85             else
86               Ata.merge
87           in
88           let big_auto = List.fold_left f fst rest in
89           [big_auto]
90       | _ -> assert false
91
92     else
93       auto_list
94   in
95   let output =
96     match !Options.output_file with
97     | None | Some "-" | Some "/dev/stdout" -> stdout
98     | Some f -> open_out f
99   in
100   if !Options.stats then begin
101     List.iter (fun query ->
102       Logger.msg `STATS "Query: %a " Xpath.Ast.print_path query) queries;
103     List.iter (fun auto ->
104       Logger.msg `STATS "@[Automaton: @\n%a@]" Ata.print auto) auto_list;
105   end;
106
107   let module Naive = Run.Make(Naive_tree)(Naive_node_list) in
108   let result_list =
109     let root = Naive_node_list.create () in
110     let () = Naive_node_list.add (Naive_tree.root doc) root in
111     let f, msg =
112       match !Options.parallel, !Options.compose with
113         true, true ->
114           compose_parallel Naive.eval auto_list doc root, "parallel/compose"
115       | true, false ->
116           restart_parallel Naive.full_eval auto_list doc root, "parallel/restart"
117       | false, true ->
118           compose_sequential Naive.eval auto_list doc root , "sequential/compose"
119       | false, false ->
120           restart_sequential Naive.eval auto_list doc root, "sequential/restart"
121     in
122     time f () (Printf.sprintf "evaluating quer%s in %s mode"
123                               (match auto_list with
124                                  [] | [ _ ] -> "y" |  _ -> "ies" )
125                               msg)
126   in
127   let sl = Naive.stats () in
128   Run.(
129     List.iter (fun s -> 
130   Logger.msg `STATS
131     "@[automaton: %d@\ntree size: %d@\ntraversals: %d@\ntransition fetch cache hit ratio: %f@\ntransition eval cache hit ratio: %f@\nNumber of visited nodes per pass: %a@]"
132     (s.auto :> int) s.tree_size s.pass
133     (float s.fetch_trans_cache_hit /. float s.fetch_trans_cache_access)
134     (float s.eval_trans_cache_hit /. float s.eval_trans_cache_access)
135     (let i = ref 0 in
136      Pretty.print_list ~sep:"," (fun fmt n -> Format.fprintf fmt "%i: %i" !i n;incr i))
137     s.nodes_per_run)) sl;
138   time (fun () ->
139     let count = ref 1 in
140     List.iter (fun results ->
141       output_string output "<xml_result num=\"";
142       output_string output (string_of_int !count);
143       output_string output "\" >\n";
144       if !Options.count then begin
145         output_string output (string_of_int (Naive_node_list.length results));
146         output_char output '\n';
147       end else
148         Naive_node_list.iter (fun n ->
149           Naive_tree.print_xml output doc n;
150           output_char output '\n'
151         ) results;
152       output_string output "</xml_result>\n";
153       incr count
154     ) result_list;
155     flush output;
156     if output != stdout then close_out output
157
158   ) () "serializing results"
159
160
161 let () =
162   try
163     main ()
164   with
165     Arg.Bad msg -> eprintf "Error: %s\n%!" msg; Options.usage (); exit 1
166   | Sys_error msg -> eprintf "Error: %s\n%!" msg; exit 2
167   | Tree.Parse_error msg ->
168       eprintf "Error: %s, %s\n%!"
169         (match !Options.input_file with
170           Some s -> ("file " ^ s)
171         | None -> "[stdin]") msg; exit 3
172   | Xpath.Ulexer.Error (s, e, msg) -> eprintf "Error: character %i-%i: %s\n%!" s e msg; exit 4
173   | e -> eprintf "FATAL ERROR: %s\n%!" (Printexc.to_string e); exit 128