changeset 500:cbe3dfd3e1a6 3.0.15

Framework: Added a method to the transition engine to recursively collect all state id for a given artifact identifer and a state id. Goal is to create a predicate dc:state-successors-contains($artifict-type-identifer, 'winfo.calculation.select, $current-state-id).
author Sascha L. Teichmann <teichmann@intevation.de>
date Tue, 27 Aug 2013 15:48:26 +0200
parents baffc9b6f379
children d3dbb7b5a164
files artifact-database/src/main/java/org/dive4elements/artifactdatabase/transition/TransitionEngine.java
diffstat 1 files changed, 54 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/artifact-database/src/main/java/org/dive4elements/artifactdatabase/transition/TransitionEngine.java	Thu Aug 22 15:24:18 2013 +0200
+++ b/artifact-database/src/main/java/org/dive4elements/artifactdatabase/transition/TransitionEngine.java	Tue Aug 27 15:48:26 2013 +0200
@@ -1,9 +1,13 @@
 package org.dive4elements.artifactdatabase.transition;
 
+import java.util.ArrayDeque;
 import java.util.ArrayList;
+import java.util.Deque;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import org.apache.log4j.Logger;
 
@@ -105,6 +109,56 @@
         return reachable;
     }
 
+    /** Returns all recursive reachable state ids for
+     *  a given pair of artifact id and state id.
+     */
+    public Set<String> allRecursiveSuccessorStateIds(
+        String artifactIdentifier,
+        String stateId
+    ) {
+        HashSet<String> result = new HashSet<String>();
+
+        List<Transition> trans = transitions.get(artifactIdentifier);
+
+        if (trans == null) {
+            return result;
+        }
+
+        Map<String, Set<String>> succs = new HashMap<String, Set<String>>();
+
+        for (Transition t: trans) {
+            String from = t.getFrom();
+            String to   = t.getTo();
+
+            Set<String> s = succs.get(from);
+            if (s == null) {
+                s = new HashSet<String>();
+                succs.put(from, s);
+            }
+            s.add(to);
+        }
+
+        Set<String> start = succs.get(stateId);
+
+        if (start == null) {
+            return result;
+        }
+
+        Deque<String> open = new ArrayDeque<String>(start);
+
+        while (!open.isEmpty()) {
+            String cand = open.pop();
+            if (result.add(cand)) {
+                Set<String> s = succs.get(cand);
+                if (s != null) {
+                    open.addAll(s);
+                }
+            }
+        }
+
+        return result;
+    }
+
 
     /**
      * Determines if a state with a given identifier is reachable from a current

http://dive4elements.wald.intevation.org