.
[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 let a = ref None
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   Printf.eprintf "Parsing document : %!";
27   let v = time Tree.Binary.parse_xml_uri filename in
28     MM(v,__LOCATION__);
29     a := Some (v);
30     a := None;
31     Printf.eprintf "Parsing query : ";    
32     let query = try
33       time
34         XPath.Parser.parse_string query
35     with
36         Ulexer.Loc.Exc_located ((x,y),e) -> Printf.eprintf "character %i-%i %s\n" x y (Printexc.to_string e);exit 1
37     in
38       Printf.eprintf "Compiling query : ";
39       let auto = time XPath.Compile.compile  query in
40         XPath.Ast.print Format.err_formatter query;
41         Format.eprintf "\n%!";
42 (*              Format.eprintf "Internal rep of the tree is :\n%!";
43                 Tree.Binary.dump v;                           *)
44         Printf.eprintf "TopDown (No BackTrack) : \n";
45         time (fun v -> ignore (TopDown.accept auto v)) v;
46         Printf.eprintf "Number of nodes in the result set : %i\n" (BST.cardinal auto.result);
47         begin
48           match output with
49             | None -> ()
50             | Some f ->
51                 
52                 Printf.eprintf "Serializing results : ";
53                 time( fun () ->
54                         let oc = open_out f in
55                           output_string oc "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
56                           BST.iter (fun t -> Tree.Binary.print_xml_fast oc t;
57                                       output_char oc '\n') auto.result) ();
58         end;
59         Printf.eprintf "Total time : %fms\n Coherence : %i\n%!" (total_time())
60 ;;
61                 
62 let argc = Array.length Sys.argv;;
63 if (argc < 3 || argc >4)
64 then
65   (prerr_endline ("usage : " ^ Sys.argv.(0) ^ " <document> \'query\'[ <output> ]");
66    exit 1)
67 ;;
68
69
70 main Sys.argv.(1) Sys.argv.(2) (if argc == 4 then Some Sys.argv.(3) else None) ;; 
71
72 Printf.eprintf "\n=================================================\nDEBUGGING\n%!";
73 Tree.DEBUGTREE.print_stats Format.err_formatter;;
74
75