changeset 108:9ece61d918b1

Improved the StateEngine and the TransitionEngine to retrieve the reachable states of a current state. artifacts/trunk@1297 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 07 Feb 2011 11:20:31 +0000
parents 39d9391059bd
children ab646e0f5569
files ChangeLog artifact-database/src/main/java/de/intevation/artifactdatabase/state/StateEngine.java artifact-database/src/main/java/de/intevation/artifactdatabase/transition/TransitionEngine.java
diffstat 3 files changed, 93 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Fri Feb 04 16:52:39 2011 +0000
+++ b/ChangeLog	Mon Feb 07 11:20:31 2011 +0000
@@ -1,3 +1,12 @@
+2011-02-07  Ingo Weinzierl <ingo@intevation.de>
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/state/StateEngine.java:
+	  There is a new method that returns a state based on its ID.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/transition/TransitionEngine.java:
+	  There is a new method that retrieves the reachable states of a specific
+	  state.
+
 2011-02-04  Ingo Weinzierl <ingo@intevation.de>
 
 	* artifact-database/src/main/java/de/intevation/artifactdatabase/ProtocolUtils.java:
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/state/StateEngine.java	Fri Feb 04 16:52:39 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/state/StateEngine.java	Mon Feb 07 11:20:31 2011 +0000
@@ -25,10 +25,40 @@
 
 
     /**
+     * A map that contains all existing states. The key of this map is the ID of
+     * the state, its value is the state itself.
+     */
+    protected Map<String, State> allStates;
+
+
+    /**
      * The default constructor.
      */
     public StateEngine() {
-        states = new HashMap<String, List>();
+        states    = new HashMap<String, List>();
+        allStates = new HashMap<String, State>();
+    }
+
+
+    /**
+     * This method adds a state into the map <i>allStates</i>.
+     *
+     * @param state The state to add.
+     */
+    protected void addState(State state) {
+        allStates.put(state.getID(), state);
+    }
+
+
+    /**
+     * Returns the state based on its ID.
+     *
+     * @param stateId The ID of the desired state.
+     *
+     * @return the state.
+     */
+    public State getState(String stateId) {
+        return allStates.get(stateId);
     }
 
 
@@ -40,7 +70,7 @@
      *
      * @return true, if the states were added, otherwise false.
      */
-    public boolean addStates(String artifact, List states) {
+    public boolean addStates(String artifact, List<State> states) {
         List tmp = this.states.get(artifact);
 
         if (tmp != null) {
@@ -50,6 +80,11 @@
             return false;
         }
 
+        // add the state to the map with all existing states
+        for (State s: states) {
+            addState(s);
+        }
+
         logger.debug("Add new states for the artifact '" + artifact + "'");
         return this.states.put(artifact, states) != null;
     }
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/transition/TransitionEngine.java	Fri Feb 04 16:52:39 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/transition/TransitionEngine.java	Mon Feb 07 11:20:31 2011 +0000
@@ -1,11 +1,16 @@
 package de.intevation.artifactdatabase.transition;
 
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
 import org.apache.log4j.Logger;
 
+import de.intevation.artifactdatabase.state.State;
+import de.intevation.artifactdatabase.state.StateEngine;
+
+
 /**
  * The TransitionEngine stores all transitions for each Artifact and should be
  * used to determine, if an Artifact is able to advance from one to another
@@ -41,20 +46,52 @@
      *
      * @return true, if the transitions were added, otherwise false.
      */
-    public boolean addTransition(String stateId, List transitions) {
-        List tmp = this.transitions.get(stateId);
+    public boolean addTransition(String stateId, Transition transition) {
+        List tmp = transitions.get(stateId);
 
-        if (tmp != null) {
-            logger.info(
-                "Transitions for the state '"
-                + stateId
-                + "' already stored.");
-
-            return false;
+        if (tmp == null) {
+            tmp = new ArrayList<Transition>();
         }
 
+        tmp.add(transition);
+
         logger.debug("Add new transitions for state '" + stateId + "'");
-        return this.transitions.put(stateId, transitions) != null;
+
+        return transitions.put(stateId, tmp) != null;
+    }
+
+
+    /**
+     * This method returns all existing transitions of a state.
+     *
+     * @param state The state
+     *
+     * @return the existing transition of <i>state</i>.
+     */
+    public List<Transition> getTransitions(State state) {
+        return transitions.get(state.getID());
+    }
+
+
+    /**
+     * This method returns the reachable states of <i>state</i>.
+     *
+     * @param state The current state.
+     * @param engine The state engine.
+     *
+     * @return a list of reachable states.
+     */
+    public List<State> getReachableStates(State state, StateEngine engine) {
+        List<Transition> transitions = getTransitions(state);
+        List<State>      reachable   = new ArrayList<State>();
+
+        for (Transition t: transitions) {
+            if (t.isValid(state)) {
+                reachable.add(engine.getState(t.getTo()));
+            }
+        }
+
+        return reachable;
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org