ingo@104: package de.intevation.artifactdatabase.transition;
ingo@104:
ingo@108: 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@108: import de.intevation.artifactdatabase.state.State;
ingo@108: import de.intevation.artifactdatabase.state.StateEngine;
ingo@108:
ingo@108:
ingo@104: /**
ingo@104: * The TransitionEngine stores all transitions for each Artifact and should be
ingo@104: * used to determine, if an Artifact is able to advance from one to another
ingo@104: * state.
ingo@104: *
ingo@104: * @author Ingo Weinzierl
ingo@104: */
ingo@104: public class TransitionEngine {
ingo@104:
ingo@104: /** The logger used in this class. */
ingo@104: private static Logger logger = Logger.getLogger(TransitionEngine.class);
ingo@104:
ingo@104: /**
ingo@104: * A map that contains the transitions of the artifacts. The key is the name
ingo@104: * of the artifact, its value is a list of all transitions of this artifact.
ingo@104: */
ingo@104: protected Map transitions;
ingo@104:
ingo@104:
ingo@104: /**
ingo@104: * The default constructor.
ingo@104: */
ingo@104: public TransitionEngine() {
ingo@104: transitions = new HashMap();
ingo@104: }
ingo@104:
ingo@104:
ingo@104: /**
ingo@104: * Add new transitions for a specific artifact.
ingo@104: *
ingo@104: * @param stateId the name of the Artifact.
ingo@104: * @param transitions the list of transition of the artifact.
ingo@104: *
ingo@104: * @return true, if the transitions were added, otherwise false.
ingo@104: */
ingo@108: public boolean addTransition(String stateId, Transition transition) {
ingo@108: List tmp = transitions.get(stateId);
ingo@104:
ingo@108: if (tmp == null) {
ingo@108: tmp = new ArrayList();
ingo@104: }
ingo@104:
ingo@108: tmp.add(transition);
ingo@108:
ingo@104: logger.debug("Add new transitions for state '" + stateId + "'");
ingo@108:
ingo@108: return transitions.put(stateId, tmp) != null;
ingo@108: }
ingo@108:
ingo@108:
ingo@108: /**
ingo@108: * This method returns all existing transitions of a state.
ingo@108: *
ingo@108: * @param state The state
ingo@108: *
ingo@108: * @return the existing transition of state.
ingo@108: */
ingo@108: public List getTransitions(State state) {
ingo@108: return transitions.get(state.getID());
ingo@108: }
ingo@108:
ingo@108:
ingo@108: /**
ingo@108: * This method returns the reachable states of state.
ingo@108: *
ingo@108: * @param state The current state.
ingo@108: * @param engine The state engine.
ingo@108: *
ingo@108: * @return a list of reachable states.
ingo@108: */
ingo@108: public List getReachableStates(State state, StateEngine engine) {
ingo@108: List transitions = getTransitions(state);
ingo@108: List reachable = new ArrayList();
ingo@108:
ingo@108: for (Transition t: transitions) {
ingo@108: if (t.isValid(state)) {
ingo@108: reachable.add(engine.getState(t.getTo()));
ingo@108: }
ingo@108: }
ingo@108:
ingo@108: return reachable;
ingo@104: }
ingo@104: }
ingo@104: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :