solve.ml solves series of queries and ouptut onlt selected nodes + examples from...
authorLucca Hirschi <lucca.hirschi@gmail.com>
Fri, 13 Jul 2012 11:30:09 +0000 (13:30 +0200)
committerLucca Hirschi <lucca.hirschi@gmail.com>
Fri, 13 Jul 2012 11:30:09 +0000 (13:30 +0200)
correct_test [new file with mode: 0755]
src/solve.ml [new file with mode: 0644]
tests/docs/XPath-FT.xml [new file with mode: 0644]
tests/queries/XPath-FT.queries [new file with mode: 0644]

diff --git a/correct_test b/correct_test
new file mode 100755 (executable)
index 0000000..d28ee73
--- /dev/null
@@ -0,0 +1 @@
+./solve.native ./tests/docs/XPath-FT.xml -f ./tests/queries/XPath-FT.queries
\ No newline at end of file
diff --git a/src/solve.ml b/src/solve.ml
new file mode 100644 (file)
index 0000000..d898bab
--- /dev/null
@@ -0,0 +1,77 @@
+(***********************************************************************)
+(*                                                                     *)
+(*                               TAToo                                 *)
+(*                                                                     *)
+(*                     Kim Nguyen, LRI UMR8623                         *)
+(*                   Université Paris-Sud & CNRS                       *)
+(*                                                                     *)
+(*  Copyright 2010-2012 Université Paris-Sud and Centre National de la *)
+(*  Recherche Scientifique. All rights reserved.  This file is         *)
+(*  distributed under the terms of the GNU Lesser General Public       *)
+(*  License, with the special exception on linking described in file   *)
+(*  ../LICENSE.                                                        *)
+(*                                                                     *)
+(***********************************************************************)
+
+
+(** use: [./test xml_file -f XPath_queries_file]
+    one query per line [XPath_querie_file]
+*)
+
+open Format
+
+let doc () =
+  let fd = open_in Sys.argv.(1) in
+  let d = Tree.load_xml_file fd in
+  close_in fd;
+  fprintf err_formatter "Parse Tree OK ! ";
+  d
+
+
+let query () = 
+  let arg2 = Sys.argv.(2) in
+  if arg2 = "-f"
+  then  let fq = open_in Sys.argv.(3) in
+        let rec list_qu fq list =
+          try
+            (match XPath.parse_file fq with
+              | q -> list_qu fq (q::list)
+              | _ -> list)
+          with _ -> list in
+        let list = list_qu fq [] in
+        close_in fq;
+        fprintf err_formatter "Parse query OK ! ";
+        list
+  else failwith "Use -f"
+
+let build_asta query =
+  let asta = Compil.trans query in
+  fprintf err_formatter "Compil OK ! ";
+  asta
+
+let compute_run doc query = 
+  let run = Run.compute doc query in
+  fprintf err_formatter "Run OK ! \n";
+  run
+
+let () =
+  Format.pp_set_margin err_formatter 80;
+  let doc = doc () in
+  output_string stderr "##### Doc with positions #####\n";
+  Tree.print_xml_preorder stderr doc (Tree.root doc);
+  let queries = query () in
+  let rec solve_queries = function
+    | [] -> ()
+    | query :: tl ->
+      let asta = build_asta query in
+      let selected_nodes = Run.selected_nodes doc asta in
+      fprintf err_formatter "Query: %a\n"
+        XPath.Ast.print query;
+      let rec print_selec fmt l = match l with
+        | [x] -> fprintf fmt "%s" (string_of_int x)
+        | x :: tl -> fprintf fmt "%s" ((string_of_int x)^"; ");print_selec fmt tl
+        | [] -> fprintf fmt "%s" "ø" in
+      fprintf err_formatter "@.@.  # Selected nodes: {%a}@."
+        print_selec selected_nodes in
+  solve_queries queries;
+  exit 0
diff --git a/tests/docs/XPath-FT.xml b/tests/docs/XPath-FT.xml
new file mode 100644 (file)
index 0000000..90aef04
--- /dev/null
@@ -0,0 +1 @@
+<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE A SYSTEM "alphabet.dtd"><A id="n1" pre="1" post="26" xml:lang="en"><B id="n2" pre="2" post="3"><C id="n3" pre="3" post="1">clergywoman</C><D id="n4" pre="4" post="2">decadent</D></B><E id="n5" pre="5" post="22"><F id="n6" pre="6" post="6"><G id="n7" pre="7" post="4">gentility</G><H id="n8" pre="8" post="5" idrefs="n17 n26">happy-go-lucky man</H></F><I id="n9" pre="9" post="9"><J id="n10" pre="10" post="7">jigsaw</J><K id="n11" pre="11" post="8">kerchief</K></I><L id="n12" pre="12" post="15"><!--L is the twelve-th letter of the English alphabet-->The letter L is followed by the letter:<M id="n13" pre="13" post="10"/>which is followed by the letter:<N id="n14" pre="14" post="13"><O id="n15" pre="15" post="11">ovenware</O><P id="n16" pre="16" post="12">plentiful</P></N><?myPI value="XPath is nice"?><Q id="n17" pre="17" post="14" idrefs="n8 n26">quarrelsome</Q></L><R id="n18" pre="18" post="18"><S id="n19" pre="19" post="16">sage</S><T id="n20" pre="20" post="17">tattered</T></R><U id="n21" pre="21" post="21"><V id="n22" pre="22" post="19">voluptuary</V><W id="n23" pre="23" post="20">wriggle</W></U></E><X id="n24" pre="24" post="25"><Y id="n25" pre="25" post="23">yawn</Y><Z id="n26" pre="26" post="24" idrefs="n8 n17" xml:lang="it">zuzzurellone</Z></X></A>
diff --git a/tests/queries/XPath-FT.queries b/tests/queries/XPath-FT.queries
new file mode 100644 (file)
index 0000000..bf29a0d
--- /dev/null
@@ -0,0 +1,8 @@
+/descendant::L
+/descendant::L/descendant::* 
+/descendant::L/following-sibling::*
+/descendant::L/self::*
+/descendant::*[L]
+/descendant::*[descendant::L]
+/descendant::*[following-sibling::L]
+/descendant::*[self::L]
\ No newline at end of file