c94967db1449fc60d51772fb9d23d1d97e20755e
[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]
19 *)
20
21 open Format
22
23 let doc () =
24   let fd = open_in Sys.argv.(1) in
25   let d = Tree.load_xml_file fd in
26   close_in fd;
27   fprintf err_formatter "Parse Tree OK ! ";
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         fprintf err_formatter "Parse query OK !\n %!";
42         list
43   else failwith "Use -f"
44
45 let build_asta query =
46   let asta = Compil.trans query in
47   asta
48
49 let compute_run doc query = 
50   let run = Run.compute doc query in
51   run
52
53 let () =
54   let flag  = Array.length Sys.argv = 5 in
55   Format.pp_set_margin err_formatter 80;
56   let doc = doc () in
57   output_string stderr "##### Doc with positions #####\n";
58   Tree.print_xml_preorder stderr doc (Tree.root doc);
59   output_string stderr "\n";
60   let queries = query () in
61   let rec print_selec fmt l = match l with
62     | [x] -> fprintf fmt "%s" (string_of_int x)
63     | x :: tl -> fprintf fmt "%s" ((string_of_int x)^"; ");print_selec fmt tl
64     | [] -> fprintf fmt "%s" "ø" in
65   let rec solve_queries = function
66     | [] -> ()
67     | query :: tl ->
68       let asta = build_asta query in
69       let selected_nodes = Run.selected_nodes doc asta in
70       let run = compute_run doc asta in
71       fprintf err_formatter "\n  ### Query: %a"
72         XPath.Ast.print query;      
73       fprintf err_formatter "@.  ### Selected nodes: {%a}@."
74         print_selec selected_nodes;
75       if flag
76       then begin
77         Asta.print err_formatter asta;
78         Run.print err_formatter run;
79       end
80       else ();
81       solve_queries tl in
82   solve_queries queries;
83   exit 0