[PATCH] 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)

Wald Commits scm-commit at wald.intevation.org
Tue Aug 27 15:48:43 CEST 2013


# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1377611306 -7200
# Node ID cbe3dfd3e1a6f86bfdee27022f0aaa96200d5385
# Parent  baffc9b6f3790f5639b8c8a1c83b30cb4a21b646
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).

diff -r baffc9b6f379 -r cbe3dfd3e1a6 artifact-database/src/main/java/org/dive4elements/artifactdatabase/transition/TransitionEngine.java
--- 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


More information about the Dive4elements-commits mailing list