d8c3e0990a833fc66daacfda9805093e75715430
[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   time (fun () ->
124     let count = ref 1 in
125     List.iter (fun results ->
126       output_string output "<xml_result num=\"";
127       output_string output (string_of_int !count);
128       output_string output "\" >\n";
129       if !Options.count then begin
130         output_string output (string_of_int (List.length results));
131         output_char output '\n';
132       end else
133         List.iter (fun n ->
134           Naive_tree.print_xml output doc n;
135           output_char output '\n'
136         ) results;
137       output_string output "</xml_result>\n";
138       incr count
139     ) result_list;
140     flush output;
141     if output != stdout then close_out output
142
143   ) () "serializing results"
144
145
146 let () =
147   try
148     main ()
149   with
150     Arg.Bad msg -> eprintf "Error: %s\n%!" msg; Options.usage (); exit 1
151   | Sys_error msg -> eprintf "Error: %s\n%!" msg; exit 2
152   | Tree.Parse_error msg ->
153       eprintf "Error: %s, %s\n%!"
154         (match !Options.input_file with
155           Some s -> ("file " ^ s)
156         | None -> "[stdin]") msg; exit 3
157   | Xpath.Ulexer.Error (s, e, msg) -> eprintf "Error: character %i-%i: %s\n%!" s e msg; exit 4
158   | e -> eprintf "FATAL ERROR: %s\n%!" (Printexc.to_string e); exit 128