20bc903dc02620dc1e46d669f3f8c8c75b8c3ba4
[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 () ("evaluating query in " ^ msg ^ " mode")
123   in
124   let s = Naive.stats () in
125   Run.(
126   Logger.msg `STATS
127     "@[tree size: %d@\ntraversals: %d@\ntransition fetch cache miss ratio: %f@\ntransition eval cache miss ratio: %f@\nNumber of visited nodes per pass: %a@]"
128     s.tree_size s.pass
129     (float s.fetch_trans_cache_miss /. float s.fetch_trans_cache_access)
130     (float s.eval_trans_cache_miss /. float s.eval_trans_cache_access)
131     (let i = ref 0 in
132      Pretty.print_list ~sep:"," (fun fmt n -> Format.fprintf fmt "%i: %i" !i n;incr i))
133     s.nodes_per_run);
134   time (fun () ->
135     let count = ref 1 in
136     List.iter (fun results ->
137       output_string output "<xml_result num=\"";
138       output_string output (string_of_int !count);
139       output_string output "\" >\n";
140       if !Options.count then begin
141         output_string output (string_of_int (Naive_node_list.length results));
142         output_char output '\n';
143       end else
144         Naive_node_list.iter (fun n ->
145           Naive_tree.print_xml output doc n;
146           output_char output '\n'
147         ) results;
148       output_string output "</xml_result>\n";
149       incr count
150     ) result_list;
151     flush output;
152     if output != stdout then close_out output
153
154   ) () "serializing results"
155
156
157 let () =
158   try
159     main ()
160   with
161     Arg.Bad msg -> eprintf "Error: %s\n%!" msg; Options.usage (); exit 1
162   | Sys_error msg -> eprintf "Error: %s\n%!" msg; exit 2
163   | Tree.Parse_error msg ->
164       eprintf "Error: %s, %s\n%!"
165         (match !Options.input_file with
166           Some s -> ("file " ^ s)
167         | None -> "[stdin]") msg; exit 3
168   | Xpath.Ulexer.Error (s, e, msg) -> eprintf "Error: character %i-%i: %s\n%!" s e msg; exit 4
169   | e -> eprintf "FATAL ERROR: %s\n%!" (Printexc.to_string e); exit 128