comparison artifact-database/src/main/java/org/dive4elements/artifactdatabase/state/StateEngine.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/state/StateEngine.java@708de1779232
children 415df0fc4fa1
comparison
equal deleted inserted replaced
472:783cc1b6b615 473:d0ac790a6c89
1 package de.intevation.artifactdatabase.state;
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.artifactdatabase.data.StateData;
11
12
13 /**
14 * The StateEngine stores all states and associated information about
15 * outputs and facets for each Artifact.
16 *
17 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
18 */
19 public class StateEngine {
20
21 /** The logger used in this class. */
22 private static Logger logger = Logger.getLogger(StateEngine.class);
23
24 /**
25 * A map that contains the states of the artifacts. The key of this map is
26 * the name of an artifact, its value is a list of all states the artifact
27 * can reach.
28 */
29 protected Map<String, List<State>> states;
30
31
32 /**
33 * A map that contains all existing states. The key of this map is the ID of
34 * the state, its value is the state itself.
35 */
36 protected Map<String, State> allStates;
37
38
39 /**
40 * The default constructor.
41 */
42 public StateEngine() {
43 states = new HashMap<String, List<State>>();
44 allStates = new HashMap<String, State>();
45 }
46
47
48 /**
49 * This method adds a state into the map <i>allStates</i>.
50 *
51 * @param state The state to add.
52 */
53 protected void addState(State state) {
54 allStates.put(state.getID(), state);
55 }
56
57
58 /**
59 * Returns the state based on its ID.
60 *
61 * @param stateId The ID of the desired state.
62 *
63 * @return the state.
64 */
65 public State getState(String stateId) {
66 return allStates.get(stateId);
67 }
68
69
70 public StateData getStateData(String artifact, String dataName) {
71 List<State> artifactStates = getStates(artifact);
72
73 if (artifactStates == null || artifactStates.size() == 0) {
74 logger.warn("No States for Artifact '" + artifact + "' existing.");
75 return null;
76 }
77
78 for (State state: artifactStates) {
79 StateData sd = state.getData(dataName);
80
81 if (sd != null) {
82 return sd;
83 }
84 }
85
86 logger.warn(
87 "No StateData for Artifact '" + artifact +
88 "' with name '" + dataName + "' existing.");
89
90 return null;
91 }
92
93
94 /**
95 * Add new states for a specific artifact.
96 *
97 * @param artifact The name of the artifact.
98 * @param states A list of states that the artifact can reach.
99 *
100 * @return true, if the states were added, otherwise false.
101 */
102 public boolean addStates(String artifact, List<State> states) {
103 List tmp = this.states.get(artifact);
104
105 if (tmp != null) {
106 logger.info(
107 "States for the artifact '" + artifact + "' already stored.");
108
109 return false;
110 }
111
112 // add the state to the map with all existing states
113 for (State s: states) {
114 addState(s);
115 }
116
117 logger.debug("Add new states for the artifact '" + artifact + "'");
118 return this.states.put(artifact, states) != null;
119 }
120
121
122 /**
123 * Returns the state list of an artifact specified by its name.
124 *
125 * @param artifact The name of the artifact (e.g. "winfo").
126 *
127 * @return the list of states of this artifact or <i>null</i> if no states
128 * are existing for this <i>artifact</i>.
129 */
130 public List<State> getStates(String artifact) {
131 return states.get(artifact);
132 }
133
134
135 /**
136 * Return mapping of output to facets for an artifact in its states.
137 */
138 public Map<String, List<String>> getCompatibleFacets(List<String> aStates) {
139 Map<String, List<String>> compatibilityMatrix =
140 new HashMap<String, List<String>>();
141
142 // For all states that the artifact had seen, add outputs facets.
143 logger.debug("Searching in " + aStates);
144 for (String stateId: aStates) {
145
146 State state = allStates.get(stateId);
147 if (state == null) {
148 logger.debug("No state found for id " + stateId);
149 continue;
150 }
151
152 for (Output output: state.getOutputs()) {
153 List<Facet> outFacets = output.getFacets();
154 logger.debug("Facets for output " + output.getName() + " :" + outFacets);
155
156 List<String> oldFacets = compatibilityMatrix.get(output.getName());
157
158 if (oldFacets == null) {
159 oldFacets = new ArrayList<String>();
160 }
161
162 for (Facet facet: outFacets) {
163 oldFacets.add(facet.getName());
164 }
165
166 compatibilityMatrix.put(output.getName(), oldFacets);
167 }
168 }
169 return compatibilityMatrix;
170 }
171 }
172 // vim:set ts=4 sw=4 et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org