remedier a la liste desordonné par l'ajout de sort a la fin de la fonction eval_query...
[tatoo.git] / src / query_tree.ml
index 018036b..02ed498 100644 (file)
@@ -5,8 +5,10 @@ let compteur = ref 0
 let all_nodes tree = let root = Naive_tree.root tree in
                     eval_axis tree [root] (Descendant true)
 
-let element_by_tag tree tagset = let dom = all_nodes tree in
-                             List.filter (fun c -> QNameSet.mem (Naive_tree.tag tree c) tagset ) dom
+let element_by_tag tree tagset kind = let dom = all_nodes tree in
+                             List.filter (fun c ->
+                               Tree.NodeKind.is_a (Naive_tree.kind tree c) kind &&
+                               QNameSet.mem (Naive_tree.tag tree c) tagset ) dom
 
 let rec compile_single_path p =
   let open Xpath.Ast in
@@ -16,11 +18,11 @@ let rec compile_single_path p =
 and compile_step_list p = 
     match p with
       | [] -> Start
-      | (a,(test,_),el) :: r ->
+      | (a,(test,kind),el) :: r ->
        let qtree = compile_step_list r in
-       let res = Binop ( Inter,Axis (a,qtree), Tag (test) ) in
+       let res = Binop ( Inter,Axis (a,qtree), Tag (test,kind) ) in
        List.fold_left (fun acc e ->
-         Binop(Inter, acc, compile_expr e)) res el  (*avant j'ai utilise une function compile_expr_list ,c'est pas genial*)
+         Binop(Inter, acc, compile_expr e)) res el  
 
   and compile_expr  (e : Xpath.Ast.expr )  = match e with
     | Fun_call (f, [ e0 ]) when (QName.to_string f) = "not" ->
@@ -49,10 +51,10 @@ and compile_step_list p =
       | Absolute p | Relative p -> compile_step_list_rev p (*(List.rev p)*)
 
   and compile_step_list_rev p = match p with
-    | [] -> Dom         (*assert false*) (*on fait rien , mais comment signifer ???*)
-    | (a,(test,_),el) :: r -> 
+    | [] -> Dom        
+    | (a,(test,kind),el) :: r -> 
       let qtree = compile_step_list_rev r in
-      let res = Binop (Inter , qtree, Tag(test)) in
+      let res = Binop (Inter , qtree, Tag(test,kind)) in
       let qtree2 = List.fold_left (fun acc e ->
        Binop(Inter, acc, compile_expr e)) res el in
       let a_rev = axis_rev a in
@@ -63,7 +65,7 @@ and compile_step_list p =
       let open Xpath.Ast in
          match a with
              Self -> Self
-           | Attribute -> assert false
+           | Attribute -> Parent
            | Child -> Parent
            | Descendant b -> 
              if not b then (Ancestor false)
@@ -117,10 +119,12 @@ let debug tree q l =
   if !do_debug then begin
     Format.fprintf Format.std_formatter "Evaluation de: ";
     print_query_tree Format.std_formatter q;
-    Format.fprintf Format.std_formatter "\nResultat: %i"
+    Format.fprintf Format.std_formatter "\nResultat: %i\n"
     (List.length l);
     Format.pp_print_flush Format.std_formatter ();
     print_node_list tree l;
+    (*List.iter
+      (fun n -> Format.fprintf Format.std_formatter "%i, " (Naive_tree.preorder tree n)) l;*)
     Format.fprintf Format.std_formatter "\n----------------\n";
     Format.pp_print_flush Format.std_formatter ();
   end
@@ -138,7 +142,7 @@ let rec eval_query_tree tree start q =
        match q with
          | Start -> start
          | Dom ->  all_nodes tree          
-         | Tag t -> element_by_tag tree t                   
+         | Tag (t,k) -> element_by_tag tree t k                     
          | Axis (a,q1) -> let ls = eval_query_tree tree start q1 in
                           eval_axis tree ls a                     
          | Binop (op,q1,q2)-> begin
@@ -150,6 +154,7 @@ let rec eval_query_tree tree start q =
              | Diff -> diff_list tree ls1 ls2
          end
        in
+       let res = List.sort (Table.compare_node tree) res in
        Hashtbl.add table_query_tree q res;
        compteur := !compteur + (List.length res);
        res