X-Git-Url: http://git.nguyen.vg/gitweb/?a=blobdiff_plain;f=src%2Fquery_tree.ml;h=9297d3a29248c857b8916f507e00a488d3d485d1;hb=f0406bb418d8b742ffdb4b001ecd178f5b7d1271;hp=018036bf82cb63ae84e5ebcd58d9252d6645353e;hpb=8fbdb25170c3376272f7eec7ef2be8fc008d11f7;p=tatoo.git diff --git a/src/query_tree.ml b/src/query_tree.ml index 018036b..9297d3a 100644 --- a/src/query_tree.ml +++ b/src/query_tree.ml @@ -5,8 +5,12 @@ 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 mk_node q = {desc = q; id = -1; hash = -1} let rec compile_single_path p = let open Xpath.Ast in @@ -15,24 +19,24 @@ let rec compile_single_path p = and compile_step_list p = match p with - | [] -> Start - | (a,(test,_),el) :: r -> + | [] -> mk_node Start + | (a,(test,kind),el) :: r -> let qtree = compile_step_list r in - let res = Binop ( Inter,Axis (a,qtree), Tag (test) ) in + let res = mk_node ( Binop ( Inter,mk_node( Axis (a,qtree)),mk_node (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*) + mk_node (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" -> let qtree = compile_expr e0 in - Binop (Diff , Dom, qtree) + mk_node (Binop (Diff , mk_node (Dom), qtree)) | Binop (e1,op,e2) -> let qtree1 = compile_expr e1 in let qtree2 = compile_expr e2 in begin match op with - | Or -> Binop (Union , qtree1,qtree2) - | And -> Binop (Inter ,qtree1,qtree2) + | Or -> mk_node (Binop (Union , qtree1,qtree2)) + | And -> mk_node (Binop (Inter ,qtree1,qtree2)) | _ -> failwith "Unknown operator" end | Path p -> compile_path_rev p @@ -42,28 +46,28 @@ and compile_step_list p = match p with | [] -> assert false | [p] -> compile_single_path_rev p - | p::r -> List.fold_left (fun acc p -> Binop (Union , acc, compile_single_path_rev p) ) (compile_single_path_rev p) r + | p::r -> List.fold_left (fun acc p -> mk_node (Binop (Union , acc, compile_single_path_rev p)) ) (compile_single_path_rev p) r and compile_single_path_rev p = match p with - | Absolute p | Relative p -> compile_step_list_rev p (*(List.rev p)*) + | Absolute p | Relative p -> compile_step_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 -> + | [] -> mk_node Dom + | (a,(test,kind),el) :: r -> let qtree = compile_step_list_rev r in - let res = Binop (Inter , qtree, Tag(test)) in + let res = mk_node (Binop (Inter , qtree,mk_node (Tag(test,kind)))) in let qtree2 = List.fold_left (fun acc e -> - Binop(Inter, acc, compile_expr e)) res el in + mk_node (Binop(Inter, acc, compile_expr e))) res el in let a_rev = axis_rev a in - Axis (a_rev , qtree2) + mk_node (Axis (a_rev , qtree2)) and axis_rev a = 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) @@ -81,7 +85,7 @@ and compile_step_list p = let compile_xpath p = match p with | [] -> assert false | [p] -> compile_single_path p - | p::r -> List.fold_left (fun acc p -> Binop (Union , acc, compile_single_path p) ) (compile_single_path p) r + | p::r -> List.fold_left (fun acc p -> mk_node (Binop (Union , acc, compile_single_path p) )) (compile_single_path p) r let comp_node t n1 n2 = (Naive_tree.preorder t n1) < (Naive_tree.preorder t n2) @@ -117,17 +121,39 @@ 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 let table_query_tree = Hashtbl.create 97 - + +let rec compare_query_tree q1 q2 = + q1.id==q2.id|| match q1.desc,q2.desc with + | Binop(op1,qt1,qt2),Binop(op2,qt3,qt4)->op1==op2&& ((compare_query_tree qt1 qt3 && compare_query_tree qt2 qt4) + || (compare_query_tree qt1 qt4 && compare_query_tree qt2 qt3)) + | Axis(a1,qt1),Axis(a2,qt2) -> compare_axis a1 a2 && compare_query_tree qt1 qt2 + | Tag(t1,k1),Tag(t2,k2) ->t1==t2&& k1==k2 + | Dom,Dom | Start,Start ->true + | _,_ ->false + +and compare_axis a1 a2 = + match a1,a2 with + Self ,Self | Attribute, Attribute | Child , Child | Parent , Parent + | FollowingSibling , FollowingSibling + | PrecedingSibling , PrecedingSibling + | Preceding , Preceding | Following , Following -> true + | Descendant b1, Descendant b2 -> b1==b2 + | Ancestor b1, Ancestor b2 -> b1==b2 + | _,_ -> false + + let rec eval_query_tree tree start q = let resultat = begin @@ -135,10 +161,10 @@ let rec eval_query_tree tree start q = Hashtbl.find table_query_tree q with Not_found -> let res = - match q with + match q.desc 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 +176,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