1 (***********************************************************************)
5 (* Kim Nguyen, LRI UMR8623 *)
6 (* Université Paris-Sud & CNRS *)
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 *)
14 (***********************************************************************)
21 let t1 = Unix.gettimeofday () in
23 let t2 = Unix.gettimeofday () in
24 let time = (t2 -. t1) *. 1000. in
25 Logger.msg `STATS "%s: %fms" msg time;
29 let compose_parallel run auto_list tree nodes () =
31 [ auto ] -> [run auto tree nodes]
34 let compose_sequential run auto_list tree nodes () =
35 [ List.fold_left (fun acc auto ->
36 run auto tree acc) nodes auto_list ]
39 let restart_parallel run auto_list tree nodes () =
41 [ auto ] -> List.map snd (run auto tree nodes)
44 let restart_sequential run auto_list tree nodes () =
45 List.map (fun auto -> run auto tree nodes) auto_list
48 let () = Options.parse () in
50 let fd, close_fd = match !Options.input_file with
51 None | Some "-" | Some "/dev/stdin" -> stdin, ignore
53 let fd = open_in input in fd, fun () -> close_in fd
55 let d = time Naive_tree.load_xml_file fd "parsing xml document" in
63 (Ulexing.from_utf8_string q)) l)
65 "parsing XPath queries"
67 (* parallel, compose -> action
68 true, true -> Ata.concat of all automata and single run
69 true, false -> Ata.merge of all automata and single run
70 false, true -> Eval first, then run on results then ...
71 false, false -> Eval first on root, then second on root then ...
76 List.map (fun query -> Xpath.Compile.path query) l)
78 "compiling XPath queries"
81 if !Options.parallel then
85 if !Options.compose then
90 let big_auto = List.fold_left f fst rest in
98 match !Options.output_file with
99 | None | Some "-" | Some "/dev/stdout" -> stdout
100 | Some f -> open_out f
102 if !Options.stats then begin
103 List.iter (fun query ->
104 Logger.msg `STATS "Query: %a " Xpath.Ast.print_path query) queries;
105 List.iter (fun auto ->
106 Logger.msg `STATS "@[Automaton: @\n%a@]" Ata.print auto) auto_list;
109 let module Naive = Run.Make(Naive_tree) in
111 let root = [ Naive_tree.root doc] in
113 match !Options.parallel, !Options.compose with
115 compose_parallel Naive.eval auto_list doc root, "parallel/compose"
117 restart_parallel Naive.full_eval auto_list doc root, "parallel/restart"
119 compose_sequential Naive.eval auto_list doc root , "sequential/compose"
121 restart_sequential Naive.eval auto_list doc root, "sequential/restart"
123 time f () ("evaluating query in " ^ msg ^ " mode")
125 let s = Naive.stats () in
128 "@[tree size: %d@\ntraversals: %d@\ntransition fetch cache hit ratio: %f@\ntransition eval cache hit ratio: %f@]"
130 (float s.fetch_trans_cache_hit /. float s.fetch_trans_cache_access)
131 (float s.eval_trans_cache_hit /. float s.eval_trans_cache_access));
134 List.iter (fun results ->
135 output_string output "<xml_result num=\"";
136 output_string output (string_of_int !count);
137 output_string output "\" >\n";
138 if !Options.count then begin
139 output_string output (string_of_int (List.length results));
140 output_char output '\n';
143 Naive_tree.print_xml output doc n;
144 output_char output '\n'
146 output_string output "</xml_result>\n";
150 if output != stdout then close_out output
152 ) () "serializing results"
159 Arg.Bad msg -> eprintf "Error: %s\n%!" msg; Options.usage (); exit 1
160 | Sys_error msg -> eprintf "Error: %s\n%!" msg; exit 2
161 | Tree.Parse_error msg ->
162 eprintf "Error: %s, %s\n%!"
163 (match !Options.input_file with
164 Some s -> ("file " ^ s)
165 | None -> "[stdin]") msg; exit 3
166 | Xpath.Ulexer.Error (s, e, msg) -> eprintf "Error: character %i-%i: %s\n%!" s e msg; exit 4
167 | e -> eprintf "FATAL ERROR: %s\n%!" (Printexc.to_string e); exit 128