git-svn-id: svn+ssh://idea.nguyen.vg/svn/sxsi/trunk/xpathcomp@84 3cdefd35-fc62-479d...
[SXSI/xpathcomp.git] / main.ml
1 (******************************************************************************)
2 (*  SXSI : XPath evaluator                                                    *)
3 (*  Kim Nguyen (Kim.Nguyen@nicta.com.au)                                      *)
4 (*  Copyright NICTA 2008                                                      *)
5 (*  Distributed under the terms of the LGPL (see LICENCE)                     *)
6 (******************************************************************************)
7 INCLUDE "debug.ml"
8
9 open Automaton
10
11
12 let l = ref [] ;;
13 let time f x =
14   let t1 = Unix.gettimeofday () in
15   let r = f x in
16   let t2 = Unix.gettimeofday () in 
17   let t = (1000. *.(t2 -. t1)) in
18     l:= t::!l;
19     Printf.eprintf "  %fms\n%!" t ;
20     r
21 ;;
22 let total_time () =  List.fold_left (+.) 0. !l;;
23
24
25 let main filename query output =
26     (* Just a trick to allow the C++ code to print debugging stuff first *)  
27     let v = time (fun () -> let v = Tree.Binary.parse_xml_uri filename;
28                   in Printf.eprintf "Parsing document : %!";v
29                  ) () 
30     in
31     let _ = Tag.init (Tree.Binary.tag_pool v) in
32       Printf.eprintf "Parsing query : ";    
33       let query = try
34         time
35           XPath.Parser.parse_string query
36       with
37           Ulexer.Loc.Exc_located ((x,y),e) -> Printf.eprintf "character %i-%i %s\n" x y (Printexc.to_string e);exit 1
38       in      
39         Printf.eprintf "Compiling query : ";
40         let auto = time XPath.Compile.compile  query in
41           XPath.Ast.print Format.err_formatter query;
42           Format.eprintf "\n%!";
43           (*            Format.eprintf "Internal rep of the tree is :\n%!";
44                         Tree.Binary.dump v;                           *)
45           Printf.eprintf "Execution time : ";
46           time (fun v -> ignore (TopDown.accept auto v)) v;
47           Printf.eprintf "Number of nodes in the result set : %i\n" (BST.cardinal auto.result);
48           begin
49             match output with
50               | None -> ()
51               | Some f ->
52                   
53                   Printf.eprintf "Serializing results : ";
54                 time( fun () ->
55                         let oc = open_out f in
56                           output_string oc "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
57                           BST.iter (fun t -> Tree.Binary.print_xml_fast oc t;
58                                       output_char oc '\n') auto.result) ();
59         end;
60         Printf.eprintf "Total time : %fms\n Coherence : %i\n%!" (total_time())
61 ;;
62                 
63
64 Options.parse_cmdline();;
65
66 main !Options.input_file !Options.query !Options.output_file;;
67
68 IFDEF DEBUG
69 THEN
70 Printf.eprintf "\n=================================================\nDEBUGGING\n%!";
71 Tree.DEBUGTREE.print_stats Format.err_formatter;;
72 Gc.full_major()
73 ENDIF