1 17 18 19 20 package org.apache.lenya.workflow.impl; 21 22 import java.util.HashMap ; 23 import java.util.HashSet ; 24 import java.util.Map ; 25 import java.util.Set ; 26 27 import org.apache.lenya.workflow.State; 28 import org.apache.lenya.workflow.Transition; 29 import org.apache.lenya.workflow.Workflow; 30 import org.apache.lenya.workflow.WorkflowException; 31 32 33 36 public class WorkflowImpl implements Workflow { 37 38 42 protected WorkflowImpl(StateImpl initialState) { 43 this.initialState = initialState; 44 addState(initialState); 45 } 46 47 private State initialState; 48 49 53 public State getInitialState() { 54 return initialState; 55 } 56 57 private Set transitions = new HashSet (); 58 private Map states = new HashMap (); 59 60 64 private void addState(StateImpl state) { 65 states.put(state.getId(), state); 66 } 67 68 72 protected void addTransition(TransitionImpl transition) { 73 transitions.add(transition); 74 addState(transition.getSource()); 75 addState(transition.getDestination()); 76 } 77 78 82 protected TransitionImpl[] getTransitions() { 83 return (TransitionImpl[]) transitions.toArray(new TransitionImpl[transitions.size()]); 84 } 85 86 91 protected State getDestination(Transition transition) { 92 return ((TransitionImpl) transition).getDestination(); 93 } 94 95 100 public Transition[] getLeavingTransitions(State state) { 101 Set leavingTransitions = new HashSet (); 102 TransitionImpl[] transitions = getTransitions(); 103 104 for (int i = 0; i < transitions.length; i++) { 105 if (transitions[i].getSource() == state) { 106 leavingTransitions.add(transitions[i]); 107 } 108 } 109 110 return (Transition[]) leavingTransitions.toArray(new Transition[leavingTransitions.size()]); 111 } 112 113 118 protected boolean containsState(State state) { 119 return states.containsValue(state); 120 } 121 122 126 protected StateImpl[] getStates() { 127 return (StateImpl[]) states.values().toArray(new StateImpl[states.size()]); 128 } 129 130 136 protected StateImpl getState(String name) throws WorkflowException { 137 if (!states.containsKey(name)) { 138 throw new WorkflowException("Workflow does not contain the state '" + name + "'!"); 139 } 140 141 return (StateImpl) states.get(name); 142 } 143 144 private Map events = new HashMap (); 145 146 150 protected void addEvent(EventImpl event) { 151 events.put(event.getName(), event); 152 } 153 154 160 public EventImpl getEvent(String name) throws WorkflowException { 161 if (!events.containsKey(name)) { 162 throw new WorkflowException("Workflow does not contain the event '" + name + "'!"); 163 } 164 165 return (EventImpl) events.get(name); 166 } 167 168 private Map variables = new HashMap (); 169 170 174 protected void addVariable(BooleanVariableImpl variable) { 175 variables.put(variable.getName(), variable); 176 } 177 178 184 public BooleanVariableImpl getVariable(String name) 185 throws WorkflowException { 186 if (!variables.containsKey(name)) { 187 throw new WorkflowException("Workflow does not contain the variable '" + name + "'!"); 188 } 189 190 return (BooleanVariableImpl) variables.get(name); 191 } 192 193 197 protected BooleanVariableImpl[] getVariables() { 198 return (BooleanVariableImpl[]) variables.values().toArray(new BooleanVariableImpl[variables.size()]); 199 } 200 201 204 public String [] getVariableNames() { 205 BooleanVariableImpl[] variables = getVariables(); 206 String [] names = new String [variables.length]; 207 for (int i = 0; i <names.length; i++) { 208 names[i] = variables[i].getName(); 209 } 210 return names; 211 } 212 } 213 | Popular Tags |