Preliminary work for multiple starters evaluation.
[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 main () =
28   let () = Options.parse () in
29   let doc =
30     let fd = open_in !Options.input_file in
31     let d = time Naive_tree.load_xml_file fd "parsing xml document" in
32     close_in fd; d
33   in
34   let query =
35     time
36       Xpath.Parser.parse
37       (Ulexing.from_latin1_string !Options.query)
38       "parsing XPath query"
39   in
40   let auto =
41     time Xpath.Compile.path query "compiling XPath query"
42   in
43   let output =
44     match !Options.output_file with
45     | None | Some "-" | Some "/dev/stdout" -> stdout
46     | Some f -> open_out f
47   in
48   if !Options.stats then begin
49     Logger.msg `STATS "Query: %a " Xpath.Ast.print_path query;
50     Logger.msg `STATS "@[Automaton: @\n%a@]" Ata.print auto;
51   end;
52
53   let module Naive = Run.Make(Naive_tree) in
54   let results =
55     time (Naive.eval auto doc) ([Naive_tree.root doc]) "evaluating query"
56   in
57   time (fun () ->
58   output_string output "<xml_result>\n";
59   if !Options.count then begin
60     output_string output (string_of_int (List.length results));
61     output_char output '\n';
62   end else
63     List.iter (fun n ->
64       Naive_tree.print_xml output doc n;
65       output_char output '\n'
66     ) results;
67   output_string output "</xml_result>\n";
68   flush output;
69   if output != stdout then close_out output
70  
71 ) () "serializing results"
72
73
74 let () =
75   try
76     main ()
77   with
78     Arg.Bad msg -> eprintf "Error: %s\n%!" msg; Options.usage (); exit 1
79   | Sys_error msg -> eprintf "Error: %s\n%!" msg; exit 2
80   | Tree.Parse_error msg -> eprintf "Error: file %s, %s\n%!" !Options.input_file msg; exit 3
81   | Xpath.Ulexer.Error (s, e, msg) -> eprintf "Error: character %i-%i: %s\n%!" s e msg; exit 4
82   | e -> eprintf "FATAL ERROR: %s\n%!" (Printexc.to_string e); exit 128