# HG changeset patch # User Ingo Weinzierl # Date 1297077631 0 # Node ID 9ece61d918b1b6d0e67ea58d2788e633618b57f7 # Parent 39d9391059bd04b122f6a85d836c6addd0f091be Improved the StateEngine and the TransitionEngine to retrieve the reachable states of a current state. artifacts/trunk@1297 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 39d9391059bd -r 9ece61d918b1 ChangeLog --- 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 + + * 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 * artifact-database/src/main/java/de/intevation/artifactdatabase/ProtocolUtils.java: diff -r 39d9391059bd -r 9ece61d918b1 artifact-database/src/main/java/de/intevation/artifactdatabase/state/StateEngine.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 allStates; + + + /** * The default constructor. */ public StateEngine() { - states = new HashMap(); + states = new HashMap(); + allStates = new HashMap(); + } + + + /** + * This method adds a state into the map allStates. + * + * @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 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; } diff -r 39d9391059bd -r 9ece61d918b1 artifact-database/src/main/java/de/intevation/artifactdatabase/transition/TransitionEngine.java --- 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(); } + 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 state. + */ + public List getTransitions(State state) { + return transitions.get(state.getID()); + } + + + /** + * This method returns the reachable states of state. + * + * @param state The current state. + * @param engine The state engine. + * + * @return a list of reachable states. + */ + public List getReachableStates(State state, StateEngine engine) { + List transitions = getTransitions(state); + List reachable = new ArrayList(); + + 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 :