Resurect the HTML trace. Now generates a single HTML file containing the SVG.
[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 () = Gc.compact () in
57   let queries =
58     time
59       (fun l ->
60         List.map (fun q ->
61           Xpath.Parser.parse
62             (Ulexing.from_utf8_string q)) l)
63       !Options.queries
64       "parsing XPath queries"
65   in
66   (* parallel, compose  ->     action
67      true, true -> Ata.concat of all automata and single run
68      true, false -> Ata.merge of all automata and single run
69      false, true -> Eval first, then run on results then ...
70      false, false -> Eval first on root, then second on root then ...
71   *)
72   let auto_list =
73     time
74       (fun l ->
75         List.map (fun query -> Xpath.Compile.path query) l)
76       queries
77       "compiling XPath queries"
78   in
79   let auto_list =
80     if !Options.parallel then
81       match auto_list with
82         fst :: rest ->
83           let f =
84             if !Options.compose then
85               Ata.concat
86             else
87               Ata.merge
88           in
89           let big_auto = List.fold_left f fst rest in
90           [big_auto]
91       | _ -> assert false
92
93     else
94       auto_list
95   in
96   let output =
97     match !Options.output_file with
98     | None | Some "-" | Some "/dev/stdout" -> stdout
99     | Some f -> open_out f
100   in
101   if !Options.stats then begin
102     List.iter (fun query ->
103       Logger.msg `STATS "Query: %a " Xpath.Ast.print_path query) queries;
104     List.iter (fun auto ->
105       Logger.msg `STATS "@[Automaton: @\n%a@]" Ata.print auto) auto_list;
106   end;
107
108   let module Naive = Run.Make(Naive_tree) in
109   let result_list =
110     let root = [ Naive_tree.root doc] 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 hit ratio: %f@\ntransition eval cache hit ratio: %f@]"
128     s.tree_size s.run
129     (float s.fetch_trans_cache_hit /. float s.fetch_trans_cache_access)
130     (float s.eval_trans_cache_hit /. float s.eval_trans_cache_access));
131   time (fun () ->
132     let count = ref 1 in
133     List.iter (fun results ->
134       output_string output "<xml_result num=\"";
135       output_string output (string_of_int !count);
136       output_string output "\" >\n";
137       if !Options.count then begin
138         output_string output (string_of_int (List.length results));
139         output_char output '\n';
140       end else
141         List.iter (fun n ->
142           Naive_tree.print_xml output doc n;
143           output_char output '\n'
144         ) results;
145       output_string output "</xml_result>\n";
146       incr count
147     ) result_list;
148     flush output;
149     if output != stdout then close_out output
150
151   ) () "serializing results"
152
153
154 let () =
155   try
156     main ()
157   with
158     Arg.Bad msg -> eprintf "Error: %s\n%!" msg; Options.usage (); exit 1
159   | Sys_error msg -> eprintf "Error: %s\n%!" msg; exit 2
160   | Tree.Parse_error msg ->
161       eprintf "Error: %s, %s\n%!"
162         (match !Options.input_file with
163           Some s -> ("file " ^ s)
164         | None -> "[stdin]") msg; exit 3
165   | Xpath.Ulexer.Error (s, e, msg) -> eprintf "Error: character %i-%i: %s\n%!" s e msg; exit 4
166   | e -> eprintf "FATAL ERROR: %s\n%!" (Printexc.to_string e); exit 128