Add a kind element to the node tree. Improve support for XPath by
[tatoo.git] / src / auto / eval.ml
index 80df253..4f68da0 100644 (file)
@@ -14,7 +14,7 @@
 (***********************************************************************)
 
 (*
-  Time-stamp: <Last modified on 2013-03-08 16:25:12 CET by Kim Nguyen>
+  Time-stamp: <Last modified on 2013-03-10 23:31:33 CET by Kim Nguyen>
 *)
 
 INCLUDE "utils.ml"
@@ -30,11 +30,42 @@ module Make (T : Tree.Sig.S) = struct
     with Not_found -> StateSet.empty
 
   let set c t n v = Hashtbl.replace c (T.preorder t n) v
+  let eval_form  phi tree node fcs nss ps ss =
+    let rec loop phi =
+      match Ata.SFormula.expr phi with
+        Formula.True -> true
+      | Formula.False -> false
+      | Formula.Atom a ->
+          let p, b, q = Ata.Atom.node a in
+          let pos =
+            let open Ata in
+            match p with
+            | First_child -> StateSet.mem q fcs
+            | Next_sibling ->  StateSet.mem q nss
+            | Parent | Previous_sibling -> StateSet.mem q ps
+            | Stay -> StateSet.mem q ss
+            | Is_first_child ->
+                node == (T.first_child tree (T.parent tree node))
+            | Is_next_sibling ->
+                node == (T.next_sibling tree (T.parent tree node))
+            | Is k -> k == (T.kind tree node)
+            | Has_first_child ->
+                T.nil != T.first_child tree node
+            | Has_next_sibling ->
+                T.nil != T.next_sibling tree node
+          in
+          if Ata.is_move p && (not b) then
+            eprintf "Warning: Invalid negative atom %a" Ata.Atom.print a;
+          b == pos
+      | Formula.And(phi1, phi2) -> loop phi1 && loop phi2
+      | Formula.Or (phi1, phi2) -> loop phi1 || loop phi2
+    in
+    loop phi
 
-  let eval_trans l ctx acc =
+  let eval_trans l tree node fcs nss ps ss acc =
     List.fold_left (fun (acct, accs) ((q, phi) as trs) ->
       if StateSet.mem q accs then (acct, accs) else
-      if Ata.SFormula.eval ctx phi then
+      if eval_form phi tree node fcs nss ps ss then
         (acct, StateSet.add q accs)
       else
         (trs::acct, accs)
@@ -42,7 +73,7 @@ module Make (T : Tree.Sig.S) = struct
 
   let top_down_run auto tree node cache i =
     let redo = ref false in
-    let rec loop node is_left =
+    let rec loop node =
       if node != T.nil then begin
         let parent = T.parent tree node in
         let fc = T.first_child tree node in
@@ -50,34 +81,32 @@ module Make (T : Tree.Sig.S) = struct
         let states0 = get cache tree node in
         let tag = T.tag tree node in
         let trans0 = Ata.get_trans auto auto.Ata.states tag in
-        let parent_states = if parent == T.nil then auto.Ata.top_states else get cache tree parent in
-        let fc_states = if fc == T.nil then auto.Ata.bottom_states else get cache tree fc in
-        let ns_states = if ns == T.nil then auto.Ata.bottom_states else get cache tree ns in
-        let is_root = parent == T.nil in
-        let ctx0 =
-          if is_left then
-            Ata.make_ctx fc_states ns_states parent_states  StateSet.empty states0 is_left is_root
-          else
-            Ata.make_ctx fc_states ns_states StateSet.empty parent_states states0 is_left is_root
-        in
-        eprintf "-- [Iteration % 4d] --\n node: %a\n context: %a\n%!"
-          i T.print_node node Ata.print_ctx ctx0;
-        List.iter (fun (q, phi) -> eprintf " %a -> %a\n" State.print q Ata.SFormula.print phi) trans0;
+        let ps = get cache tree parent in
+        let fcs = get cache tree fc in
+        let nss = get cache tree ns in
+        eprintf "-- [Iteration % 4d] --\n node: %a\n%!" i T.print_node node;
+        List.iter (fun (q, phi) -> eprintf " %a -> %a\n"
+          State.print q Ata.SFormula.print phi) trans0;
         eprintf "----------------------\n%!";
-        let trans1, states1 = eval_trans trans0 ctx0 StateSet.empty in
+        let trans1, states1 =
+          eval_trans trans0 tree node fcs nss ps states0 states0
+        in
         if states1 != states0 then set cache tree node states1;
-        let () = loop fc true in
-        let ctx1 = {ctx0 with Ata.left = (get cache tree fc) ; Ata.epsilon = states1 } in
-        let trans2, states2 = eval_trans trans1 ctx1 states1 in
+        let () = loop fc in
+        let fcs1 = get cache tree fc in
+        let trans2, states2 =
+          eval_trans trans1 tree node fcs1 nss ps states1 states1
+        in
         if states2 != states1 then set cache tree node states2;
-        let () = loop ns false in
-        let ctx2 = { ctx1 with Ata.right = (get cache tree ns); Ata.epsilon = states2 } in
-        let _, states3 = eval_trans trans2 ctx2 states2 in
+        let () = loop ns in
+        let _, states3 =
+          eval_trans trans2 tree node fcs1 (get cache tree ns) ps states2 states2
+        in
         if states3 != states2 then set cache tree node states3;
         if states0 != states3 && (not !redo) then redo := true
       end
     in
-    loop node true;
+    loop node;
     !redo
 
   let get_results auto tree node cache =