teichmann@475: package org.dive4elements.artifactdatabase.transition; ingo@104: teichmann@500: import java.util.ArrayDeque; ingo@108: import java.util.ArrayList; teichmann@500: import java.util.Deque; ingo@104: import java.util.HashMap; teichmann@500: import java.util.HashSet; ingo@104: import java.util.List; ingo@104: import java.util.Map; teichmann@500: import java.util.Set; ingo@104: ingo@104: import org.apache.log4j.Logger; ingo@104: teichmann@475: import org.dive4elements.artifacts.Artifact; ingo@257: teichmann@475: import org.dive4elements.artifactdatabase.state.State; teichmann@475: import org.dive4elements.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. felix@400: * @param transition 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: teichmann@500: /** Returns all recursive reachable state ids for teichmann@500: * a given pair of artifact id and state id. teichmann@500: */ teichmann@500: public Set allRecursiveSuccessorStateIds( teichmann@500: String artifactIdentifier, teichmann@500: String stateId teichmann@500: ) { teichmann@500: HashSet result = new HashSet(); teichmann@500: teichmann@500: List trans = transitions.get(artifactIdentifier); teichmann@500: teichmann@500: if (trans == null) { teichmann@500: return result; teichmann@500: } teichmann@500: teichmann@500: Map> succs = new HashMap>(); teichmann@500: teichmann@500: for (Transition t: trans) { teichmann@500: String from = t.getFrom(); teichmann@500: String to = t.getTo(); teichmann@500: teichmann@500: Set s = succs.get(from); teichmann@500: if (s == null) { teichmann@500: s = new HashSet(); teichmann@500: succs.put(from, s); teichmann@500: } teichmann@500: s.add(to); teichmann@500: } teichmann@500: teichmann@500: Set start = succs.get(stateId); teichmann@500: teichmann@500: if (start == null) { teichmann@500: return result; teichmann@500: } teichmann@500: teichmann@500: Deque open = new ArrayDeque(start); teichmann@500: teichmann@500: while (!open.isEmpty()) { teichmann@500: String cand = open.pop(); teichmann@500: if (result.add(cand)) { teichmann@500: Set s = succs.get(cand); teichmann@500: if (s != null) { teichmann@500: open.addAll(s); teichmann@500: } teichmann@500: } teichmann@500: } teichmann@500: teichmann@500: return result; teichmann@500: } teichmann@500: 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 :