Mercurial > dive4elements > framework
comparison artifact-database/src/main/java/de/intevation/artifactdatabase/transition/TransitionEngine.java @ 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 | 26bfff409dd3 |
children | 973f244ed568 |
comparison
equal
deleted
inserted
replaced
107:39d9391059bd | 108:9ece61d918b1 |
---|---|
1 package de.intevation.artifactdatabase.transition; | 1 package de.intevation.artifactdatabase.transition; |
2 | 2 |
3 import java.util.ArrayList; | |
3 import java.util.HashMap; | 4 import java.util.HashMap; |
4 import java.util.List; | 5 import java.util.List; |
5 import java.util.Map; | 6 import java.util.Map; |
6 | 7 |
7 import org.apache.log4j.Logger; | 8 import org.apache.log4j.Logger; |
9 | |
10 import de.intevation.artifactdatabase.state.State; | |
11 import de.intevation.artifactdatabase.state.StateEngine; | |
12 | |
8 | 13 |
9 /** | 14 /** |
10 * The TransitionEngine stores all transitions for each Artifact and should be | 15 * The TransitionEngine stores all transitions for each Artifact and should be |
11 * used to determine, if an Artifact is able to advance from one to another | 16 * used to determine, if an Artifact is able to advance from one to another |
12 * state. | 17 * state. |
39 * @param stateId the name of the Artifact. | 44 * @param stateId the name of the Artifact. |
40 * @param transitions the list of transition of the artifact. | 45 * @param transitions the list of transition of the artifact. |
41 * | 46 * |
42 * @return true, if the transitions were added, otherwise false. | 47 * @return true, if the transitions were added, otherwise false. |
43 */ | 48 */ |
44 public boolean addTransition(String stateId, List transitions) { | 49 public boolean addTransition(String stateId, Transition transition) { |
45 List tmp = this.transitions.get(stateId); | 50 List tmp = transitions.get(stateId); |
46 | 51 |
47 if (tmp != null) { | 52 if (tmp == null) { |
48 logger.info( | 53 tmp = new ArrayList<Transition>(); |
49 "Transitions for the state '" | |
50 + stateId | |
51 + "' already stored."); | |
52 | |
53 return false; | |
54 } | 54 } |
55 | 55 |
56 tmp.add(transition); | |
57 | |
56 logger.debug("Add new transitions for state '" + stateId + "'"); | 58 logger.debug("Add new transitions for state '" + stateId + "'"); |
57 return this.transitions.put(stateId, transitions) != null; | 59 |
60 return transitions.put(stateId, tmp) != null; | |
61 } | |
62 | |
63 | |
64 /** | |
65 * This method returns all existing transitions of a state. | |
66 * | |
67 * @param state The state | |
68 * | |
69 * @return the existing transition of <i>state</i>. | |
70 */ | |
71 public List<Transition> getTransitions(State state) { | |
72 return transitions.get(state.getID()); | |
73 } | |
74 | |
75 | |
76 /** | |
77 * This method returns the reachable states of <i>state</i>. | |
78 * | |
79 * @param state The current state. | |
80 * @param engine The state engine. | |
81 * | |
82 * @return a list of reachable states. | |
83 */ | |
84 public List<State> getReachableStates(State state, StateEngine engine) { | |
85 List<Transition> transitions = getTransitions(state); | |
86 List<State> reachable = new ArrayList<State>(); | |
87 | |
88 for (Transition t: transitions) { | |
89 if (t.isValid(state)) { | |
90 reachable.add(engine.getState(t.getTo())); | |
91 } | |
92 } | |
93 | |
94 return reachable; | |
58 } | 95 } |
59 } | 96 } |
60 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : | 97 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |