typo test.ml + Why the old asta is taken for building Compil.trans qu ?
[tatoo.git] / src / test.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 "XPath querie"]
18     or : [./test xml_file -f XPath_querie_file]
19     only the first line of [XPath_querie_file] is read 
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   fprintf err_formatter "Parse Tree OK ! ";
29   d
30
31
32 let query () = 
33   let arg2 = Sys.argv.(2) in
34   if arg2 = "-f"
35   then  let fq = open_in Sys.argv.(3) in
36         let q = XPath.parse_file fq in
37         close_in fq;
38         fprintf err_formatter "Parse query OK ! ";
39         q
40   else let q = XPath.parse_string arg2 in
41        fprintf err_formatter "Parse query OK ! ";
42        q
43
44 let build_asta query =
45   let asta = Compil.trans query in
46   fprintf err_formatter "Compil OK ! ";
47   asta
48
49 let compute_run doc query = 
50   let run = Run.compute doc query in
51   fprintf err_formatter "Run OK ! \n";
52   run
53
54 let () =
55   let doc = doc () in
56   let query = query () in
57   let asta = build_asta query in
58   let run = compute_run doc asta in
59   let selected_nodes = Run.selected_nodes doc asta in
60   Format.pp_set_margin err_formatter 80;
61   fprintf err_formatter "@[<v 0>##### Query #####@.  %a@]\n"
62     XPath.Ast.print query;
63   output_string stderr "\n##### Doc #####\n";
64   Tree.print_xml stderr doc (Tree.root doc);
65   output_string stderr "\n";
66   Asta.print err_formatter asta;
67   Run.print err_formatter run;
68   output_string stderr "\n  # Doc with positions: \n";
69   Tree.print_xml_preorder stderr doc (Tree.root doc);
70   let rec print_selec fmt l = match l with
71     | [x] -> fprintf fmt "%s" (string_of_int x)
72     | x :: tl -> fprintf fmt "%s" ((string_of_int x)^"; ");print_selec fmt tl
73     | [] -> fprintf fmt "%s" "ø" in
74   fprintf err_formatter "@.@.  # Selected nodes: {%a}@."
75     print_selec selected_nodes;
76   exit 0