ingo@104: package de.intevation.artifactdatabase.state; ingo@104: felix@347: import java.util.ArrayList; ingo@104: import java.util.HashMap; ingo@104: import java.util.List; ingo@104: import java.util.Map; ingo@104: ingo@104: import org.apache.log4j.Logger; ingo@104: ingo@374: import de.intevation.artifactdatabase.data.StateData; ingo@374: ingo@374: ingo@104: /** felix@347: * The StateEngine stores all states and associated information about felix@347: * outputs and facets for each Artifact. ingo@104: * ingo@104: * @author Ingo Weinzierl ingo@104: */ ingo@104: public class StateEngine { ingo@104: ingo@104: /** The logger used in this class. */ ingo@104: private static Logger logger = Logger.getLogger(StateEngine.class); ingo@104: ingo@104: /** ingo@104: * A map that contains the states of the artifacts. The key of this map is ingo@104: * the name of an artifact, its value is a list of all states the artifact ingo@104: * can reach. ingo@104: */ sascha@272: protected Map> states; ingo@104: ingo@104: ingo@104: /** ingo@108: * A map that contains all existing states. The key of this map is the ID of ingo@108: * the state, its value is the state itself. ingo@108: */ ingo@108: protected Map allStates; ingo@108: ingo@108: ingo@108: /** ingo@104: * The default constructor. ingo@104: */ ingo@104: public StateEngine() { sascha@272: states = new HashMap>(); ingo@108: allStates = new HashMap(); ingo@108: } ingo@108: ingo@108: ingo@108: /** ingo@108: * This method adds a state into the map allStates. ingo@108: * ingo@108: * @param state The state to add. ingo@108: */ ingo@108: protected void addState(State state) { ingo@108: allStates.put(state.getID(), state); ingo@108: } ingo@108: ingo@108: ingo@108: /** ingo@108: * Returns the state based on its ID. ingo@108: * ingo@108: * @param stateId The ID of the desired state. ingo@108: * ingo@108: * @return the state. ingo@108: */ ingo@108: public State getState(String stateId) { ingo@108: return allStates.get(stateId); ingo@104: } ingo@104: ingo@104: ingo@374: public StateData getStateData(String artifact, String dataName) { ingo@374: List artifactStates = getStates(artifact); ingo@374: ingo@374: if (artifactStates == null || artifactStates.size() == 0) { ingo@374: logger.warn("No States for Artifact '" + artifact + "' existing."); ingo@374: return null; ingo@374: } ingo@374: ingo@374: for (State state: artifactStates) { ingo@374: StateData sd = state.getData(dataName); ingo@374: ingo@374: if (sd != null) { ingo@374: return sd; ingo@374: } ingo@374: } ingo@374: ingo@374: logger.warn( ingo@374: "No StateData for Artifact '" + artifact + ingo@374: "' with name '" + dataName + "' existing."); ingo@374: ingo@374: return null; ingo@374: } ingo@374: ingo@374: ingo@104: /** ingo@104: * Add new states for a specific artifact. ingo@104: * ingo@104: * @param artifact The name of the artifact. ingo@104: * @param states A list of states that the artifact can reach. ingo@104: * ingo@104: * @return true, if the states were added, otherwise false. ingo@104: */ ingo@108: public boolean addStates(String artifact, List states) { ingo@104: List tmp = this.states.get(artifact); ingo@104: ingo@104: if (tmp != null) { ingo@104: logger.info( ingo@104: "States for the artifact '" + artifact + "' already stored."); ingo@104: ingo@104: return false; ingo@104: } ingo@104: ingo@108: // add the state to the map with all existing states ingo@108: for (State s: states) { ingo@108: addState(s); ingo@108: } ingo@108: ingo@104: logger.debug("Add new states for the artifact '" + artifact + "'"); ingo@104: return this.states.put(artifact, states) != null; ingo@104: } ingo@106: ingo@106: ingo@106: /** ingo@106: * Returns the state list of an artifact specified by its name. ingo@106: * felix@347: * @param artifact The name of the artifact (e.g. "winfo"). ingo@106: * ingo@106: * @return the list of states of this artifact or null if no states ingo@106: * are existing for this artifact. ingo@106: */ ingo@106: public List getStates(String artifact) { ingo@106: return states.get(artifact); ingo@106: } felix@347: felix@347: felix@347: /** felix@347: * Return mapping of output to facets for an artifact in its states. felix@347: */ felix@347: public Map> getCompatibleFacets(List aStates) { felix@347: Map> compatibilityMatrix = felix@347: new HashMap>(); felix@347: felix@347: // For all states that the artifact had seen, add outputs facets. bjoern@449: logger.debug("Searching in " + aStates); felix@347: for (String stateId: aStates) { felix@347: felix@347: State state = allStates.get(stateId); felix@347: if (state == null) { bjoern@449: logger.debug("No state found for id " + stateId); felix@347: continue; felix@347: } felix@347: felix@347: for (Output output: state.getOutputs()) { felix@347: List outFacets = output.getFacets(); bjoern@449: logger.debug("Facets for output " + output.getName() + " :" + outFacets); felix@347: felix@347: List oldFacets = compatibilityMatrix.get(output.getName()); felix@347: felix@347: if (oldFacets == null) { felix@347: oldFacets = new ArrayList(); felix@347: } felix@347: felix@347: for (Facet facet: outFacets) { felix@347: oldFacets.add(facet.getName()); felix@347: } felix@347: felix@347: compatibilityMatrix.put(output.getName(), oldFacets); felix@347: } felix@347: } felix@347: return compatibilityMatrix; felix@347: } ingo@104: } felix@347: // vim:set ts=4 sw=4 et sta sts=4 fenc=utf8 :