Refactor the run module, moving out of the Make functor everything that can be moved...
[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) in
108   let result_list =
109     let root = [ Naive_tree.root doc] in
110     let f, msg =
111       match !Options.parallel, !Options.compose with
112         true, true ->
113           compose_parallel Naive.eval auto_list doc root, "parallel/compose"
114       | true, false ->
115           restart_parallel Naive.full_eval auto_list doc root, "parallel/restart"
116       | false, true ->
117           compose_sequential Naive.eval auto_list doc root , "sequential/compose"
118       | false, false ->
119           restart_sequential Naive.eval auto_list doc root, "sequential/restart"
120     in
121     time f () ("evaluating query in " ^ msg ^ " mode")
122   in
123   let s = Naive.stats () in
124   Run.(
125   Logger.msg `STATS
126     "@[tree size: %d@\ntraversals: %d@\ntransition fetch cache hit ratio: %f@\ntransition eval cache hit ratio: %f@]"
127     s.tree_size s.run
128     (float s.fetch_trans_cache_hit /. float s.fetch_trans_cache_access)
129     (float s.eval_trans_cache_hit /. float s.eval_trans_cache_access));
130   time (fun () ->
131     let count = ref 1 in
132     List.iter (fun results ->
133       output_string output "<xml_result num=\"";
134       output_string output (string_of_int !count);
135       output_string output "\" >\n";
136       if !Options.count then begin
137         output_string output (string_of_int (List.length results));
138         output_char output '\n';
139       end else
140         List.iter (fun n ->
141           Naive_tree.print_xml output doc n;
142           output_char output '\n'
143         ) results;
144       output_string output "</xml_result>\n";
145       incr count
146     ) result_list;
147     flush output;
148     if output != stdout then close_out output
149
150   ) () "serializing results"
151
152
153 let () =
154   try
155     main ()
156   with
157     Arg.Bad msg -> eprintf "Error: %s\n%!" msg; Options.usage (); exit 1
158   | Sys_error msg -> eprintf "Error: %s\n%!" msg; exit 2
159   | Tree.Parse_error msg ->
160       eprintf "Error: %s, %s\n%!"
161         (match !Options.input_file with
162           Some s -> ("file " ^ s)
163         | None -> "[stdin]") msg; exit 3
164   | Xpath.Ulexer.Error (s, e, msg) -> eprintf "Error: character %i-%i: %s\n%!" s e msg; exit 4
165   | e -> eprintf "FATAL ERROR: %s\n%!" (Printexc.to_string e); exit 128