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@257: import de.intevation.artifacts.Artifact;
ingo@257:
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@257: public List getReachableStates(
ingo@257: Artifact artifact,
ingo@257: State state,
ingo@257: StateEngine engine) {
ingo@108: List transitions = getTransitions(state);
ingo@108: List reachable = new ArrayList();
ingo@108:
ingo@201: if (transitions == null) {
ingo@201: return reachable;
ingo@201: }
ingo@201:
ingo@108: for (Transition t: transitions) {
ingo@257: State target = engine.getState(t.getTo());
ingo@257:
ingo@257: if (t.isValid(artifact, state, target)) {
ingo@257: reachable.add(target);
ingo@108: }
ingo@108: }
ingo@108:
ingo@108: return reachable;
ingo@104: }
ingo@201:
ingo@201:
ingo@201: /**
ingo@201: * Determines if a state with a given identifier is reachable from a current
ingo@201: * state.
ingo@201: *
ingo@257: * @param artifact The owner artifact of state state.
ingo@201: * @param targetId The identifier of the target state.
ingo@201: * @param state The start state.
ingo@201: * @param stateEngine The StateEngine.
ingo@201: *
ingo@201: * @return true, if the target state is reachable, otherwise false.
ingo@201: */
ingo@201: public boolean isStateReachable(
ingo@257: Artifact artifact,
ingo@201: String targetId,
ingo@201: State state,
ingo@201: StateEngine stateEngine)
ingo@201: {
ingo@257: List reachable = getReachableStates(artifact, state,stateEngine);
ingo@201:
ingo@201: if (reachable == null || reachable.size() == 0) {
ingo@201: return false;
ingo@201: }
ingo@201:
ingo@201: for (State s: reachable) {
ingo@201: if (targetId.equals(s.getID())) {
ingo@201: return true;
ingo@201: }
ingo@201: }
ingo@201:
ingo@201: return false;
ingo@201: }
ingo@104: }
ingo@104: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :