comparison artifact-database/src/main/java/org/dive4elements/artifactdatabase/transition/TransitionEngine.java @ 473:d0ac790a6c89 dive4elements-move

Moved directories to org.dive4elements
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 10:57:18 +0200
parents artifact-database/src/main/java/de/intevation/artifactdatabase/transition/TransitionEngine.java@f367be55dd35
children 415df0fc4fa1
comparison
equal deleted inserted replaced
472:783cc1b6b615 473:d0ac790a6c89
1 package de.intevation.artifactdatabase.transition;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7
8 import org.apache.log4j.Logger;
9
10 import de.intevation.artifacts.Artifact;
11
12 import de.intevation.artifactdatabase.state.State;
13 import de.intevation.artifactdatabase.state.StateEngine;
14
15
16 /**
17 * The TransitionEngine stores all transitions for each Artifact and should be
18 * used to determine, if an Artifact is able to advance from one to another
19 * state.
20 *
21 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
22 */
23 public class TransitionEngine {
24
25 /** The logger used in this class. */
26 private static Logger logger = Logger.getLogger(TransitionEngine.class);
27
28 /**
29 * A map that contains the transitions of the artifacts. The key is the name
30 * of the artifact, its value is a list of all transitions of this artifact.
31 */
32 protected Map<String, List> transitions;
33
34
35 /**
36 * The default constructor.
37 */
38 public TransitionEngine() {
39 transitions = new HashMap<String, List>();
40 }
41
42
43 /**
44 * Add new transitions for a specific artifact.
45 *
46 * @param stateId the name of the Artifact.
47 * @param transition the list of transition of the artifact.
48 *
49 * @return true, if the transitions were added, otherwise false.
50 */
51 public boolean addTransition(String stateId, Transition transition) {
52 List tmp = transitions.get(stateId);
53
54 if (tmp == null) {
55 tmp = new ArrayList<Transition>();
56 }
57
58 tmp.add(transition);
59
60 logger.debug("Add new transitions for state '" + stateId + "'");
61
62 return transitions.put(stateId, tmp) != null;
63 }
64
65
66 /**
67 * This method returns all existing transitions of a state.
68 *
69 * @param state The state
70 *
71 * @return the existing transition of <i>state</i>.
72 */
73 public List<Transition> getTransitions(State state) {
74 return transitions.get(state.getID());
75 }
76
77
78 /**
79 * This method returns the reachable states of <i>state</i>.
80 *
81 * @param state The current state.
82 * @param engine The state engine.
83 *
84 * @return a list of reachable states.
85 */
86 public List<State> getReachableStates(
87 Artifact artifact,
88 State state,
89 StateEngine engine) {
90 List<Transition> transitions = getTransitions(state);
91 List<State> reachable = new ArrayList<State>();
92
93 if (transitions == null) {
94 return reachable;
95 }
96
97 for (Transition t: transitions) {
98 State target = engine.getState(t.getTo());
99
100 if (t.isValid(artifact, state, target)) {
101 reachable.add(target);
102 }
103 }
104
105 return reachable;
106 }
107
108
109 /**
110 * Determines if a state with a given identifier is reachable from a current
111 * state.
112 *
113 * @param artifact The owner artifact of state <i>state</i>.
114 * @param targetId The identifier of the target state.
115 * @param state The start state.
116 * @param stateEngine The StateEngine.
117 *
118 * @return true, if the target state is reachable, otherwise false.
119 */
120 public boolean isStateReachable(
121 Artifact artifact,
122 String targetId,
123 State state,
124 StateEngine stateEngine)
125 {
126 List<State> reachable = getReachableStates(artifact, state,stateEngine);
127
128 if (reachable == null || reachable.size() == 0) {
129 return false;
130 }
131
132 for (State s: reachable) {
133 if (targetId.equals(s.getID())) {
134 return true;
135 }
136 }
137
138 return false;
139 }
140 }
141 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org