view artifact-database/src/main/java/de/intevation/artifactdatabase/transition/TransitionEngine.java @ 261:f3d970a832ff

Bugfix: the document that is used to trigger the OUT operation of a collection will now contain the name of the output mode and its concrete subtype. artifacts/trunk@1933 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 17 May 2011 09:57:10 +0000
parents 8c2b6cdf22ad
children f367be55dd35
line wrap: on
line source
package de.intevation.artifactdatabase.transition;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import de.intevation.artifacts.Artifact;

import de.intevation.artifactdatabase.state.State;
import de.intevation.artifactdatabase.state.StateEngine;


/**
 * The TransitionEngine stores all transitions for each Artifact and should be
 * used to determine, if an Artifact is able to advance from one to another
 * state.
 *
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class TransitionEngine {

    /** The logger used in this class. */
    private static Logger logger = Logger.getLogger(TransitionEngine.class);

    /**
     * A map that contains the transitions of the artifacts. The key is the name
     * of the artifact, its value is a list of all transitions of this artifact.
     */
    protected Map<String, List> transitions;


    /**
     * The default constructor.
     */
    public TransitionEngine() {
        transitions = new HashMap<String, List>();
    }


    /**
     * Add new transitions for a specific artifact.
     *
     * @param stateId the name of the Artifact.
     * @param transitions the list of transition of the artifact.
     *
     * @return true, if the transitions were added, otherwise false.
     */
    public boolean addTransition(String stateId, Transition transition) {
        List tmp = transitions.get(stateId);

        if (tmp == null) {
            tmp = new ArrayList<Transition>();
        }

        tmp.add(transition);

        logger.debug("Add new transitions for state '" + stateId + "'");

        return transitions.put(stateId, tmp) != null;
    }


    /**
     * This method returns all existing transitions of a state.
     *
     * @param state The state
     *
     * @return the existing transition of <i>state</i>.
     */
    public List<Transition> getTransitions(State state) {
        return transitions.get(state.getID());
    }


    /**
     * This method returns the reachable states of <i>state</i>.
     *
     * @param state The current state.
     * @param engine The state engine.
     *
     * @return a list of reachable states.
     */
    public List<State> getReachableStates(
        Artifact    artifact,
        State       state,
        StateEngine engine) {
        List<Transition> transitions = getTransitions(state);
        List<State>      reachable   = new ArrayList<State>();

        if (transitions == null) {
            return reachable;
        }

        for (Transition t: transitions) {
            State target = engine.getState(t.getTo());

            if (t.isValid(artifact, state, target)) {
                reachable.add(target);
            }
        }

        return reachable;
    }


    /**
     * Determines if a state with a given identifier is reachable from a current
     * state.
     *
     * @param artifact The owner artifact of state <i>state</i>.
     * @param targetId The identifier of the target state.
     * @param state The start state.
     * @param stateEngine The StateEngine.
     *
     * @return true, if the target state is reachable, otherwise false.
     */
    public boolean isStateReachable(
        Artifact    artifact,
        String      targetId,
        State       state,
        StateEngine stateEngine)
    {
        List<State> reachable = getReachableStates(artifact, state,stateEngine);

        if (reachable == null || reachable.size() == 0) {
            return false;
        }

        for (State s: reachable) {
            if (targetId.equals(s.getID())) {
                return true;
            }
        }

        return false;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org