Merge branch 'lucca-tests-bench' into lucca-optim
[tatoo.git] / src / solve.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
17 (** use: [./test xml_file -f XPath_queries_file]
18     one query per line [XPath_querie_file], output
19     a solution per line.
20 *)
21
22 open Format
23
24 let doc () =
25   let fd = open_in Sys.argv.(1) in
26   let d = Tree.load_xml_file fd in
27   close_in fd;
28   d
29
30
31 let query () = 
32   let arg2 = Sys.argv.(2) in
33   if arg2 = "-f"
34   then  let fq = open_in Sys.argv.(3) in
35         let rec list_qu fq list =          
36           try let q =  XPath.parse_file fq in
37               list_qu fq (q::list)
38           with _ -> list in
39         let list = list_qu fq [] in
40         close_in fq;
41         list
42   else failwith "Use ./test xml_file -f XPath_queries_file"
43
44 let compute_run doc query = 
45   let run = Run.compute doc query in
46   run
47
48 let () =
49   let flag  = Array.length Sys.argv = 5 in
50   let flag2 =
51     if flag
52     then int_of_string Sys.argv.(4) = 1
53     else false in
54   Format.pp_set_margin err_formatter 80;
55   let doc = doc () in
56   let queries = query () in
57   let rec print_selec fmt l = match l with
58     | [x] -> fprintf fmt "%s" (string_of_int x)
59     | x :: tl -> fprintf fmt "%s" ((string_of_int x)^"; ");print_selec fmt tl
60     | [] -> fprintf fmt "%s" "ø" in
61   let rec solve_queries = function
62     | [] -> ()
63     | query :: tl ->
64       let asta = Compil.trans query in
65       let selected_nodes = Run.selected_nodes doc asta in
66       let run = compute_run doc asta in
67       if flag
68       then
69         fprintf err_formatter "  ### Query: %a"
70           XPath.Ast.print query
71       else ();
72       if flag2 then
73         fprintf err_formatter "@.  ### Selected nodes: {%a}@."
74           print_selec selected_nodes
75       else ();
76       if flag
77       then begin
78         Asta.print err_formatter asta;
79         Run.print err_formatter run;
80       end
81       else ();
82       solve_queries tl in
83   solve_queries queries;
84   exit 0