001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: /* $Id: WorkflowImpl.java 479309 2006-11-26 08:24:35Z andreas $ */
020:
021: package org.apache.lenya.workflow.impl;
022:
023: import java.util.HashMap;
024: import java.util.HashSet;
025: import java.util.Map;
026: import java.util.Set;
027:
028: import org.apache.lenya.workflow.Transition;
029: import org.apache.lenya.workflow.Workflow;
030: import org.apache.lenya.workflow.WorkflowException;
031:
032: /**
033: * Implementation of a workflow schema.
034: */
035: public class WorkflowImpl implements Workflow {
036:
037: /**
038: * Creates a new instance of WorkflowImpl.
039: * @param _name The name.
040: * @param _initialState the initial state of the workflow.
041: */
042: protected WorkflowImpl(String _name, String _initialState) {
043: this .initialState = _initialState;
044: this .name = _name;
045: addState(_initialState);
046: }
047:
048: private String initialState;
049: private String name;
050:
051: /**
052: * Returns the initial state of this workflow.
053: * @return The initial state.
054: *
055: */
056: public String getInitialState() {
057: return this .initialState;
058: }
059:
060: private Set transitions = new HashSet();
061: private Set states = new HashSet();
062:
063: /**
064: * Adds a state.
065: * @param state A state.
066: */
067: private void addState(String state) {
068: this .states.add(state);
069: }
070:
071: /**
072: * Adds a transition.
073: * @param transition The transition.
074: */
075: protected void addTransition(TransitionImpl transition) {
076: this .transitions.add(transition);
077: addState(transition.getSource());
078: addState(transition.getDestination());
079: }
080:
081: /**
082: * Returns the transitions.
083: * @return An array of transitions.
084: */
085: protected TransitionImpl[] getTransitions() {
086: return (TransitionImpl[]) this .transitions
087: .toArray(new TransitionImpl[this .transitions.size()]);
088: }
089:
090: /**
091: * @see org.apache.lenya.workflow.Workflow#getLeavingTransitions(java.lang.String)
092: */
093: public Transition[] getLeavingTransitions(String state)
094: throws WorkflowException {
095: Set leavingTransitions = new HashSet();
096: TransitionImpl[] _transitions = getTransitions();
097: for (int i = 0; i < _transitions.length; i++) {
098: if (_transitions[i].getSource().equals(state)) {
099: leavingTransitions.add(_transitions[i]);
100: }
101: }
102:
103: return (Transition[]) leavingTransitions
104: .toArray(new Transition[leavingTransitions.size()]);
105: }
106:
107: /**
108: * Checks if this workflow contains a state.
109: * @param state The state to check.
110: * @return <code>true</code> if the state is contained, <code>false</code>
111: * otherwise.
112: */
113: protected boolean containsState(String state) {
114: return this .states.contains(state);
115: }
116:
117: public String[] getStates() {
118: return (String[]) this .states.toArray(new String[this .states
119: .size()]);
120: }
121:
122: private Set events = new HashSet();
123:
124: /**
125: * Adds an event.
126: * @param event An event.
127: */
128: protected void addEvent(String event) {
129: this .events.add(event);
130: }
131:
132: private Map variables = new HashMap();
133:
134: /**
135: * Adds a variable.
136: * @param variable A variable.
137: */
138: protected void addVariable(BooleanVariableImpl variable) {
139: this .variables.put(variable.getName(), variable);
140: }
141:
142: /**
143: * Returns the variable for a certain name.
144: * @param _name The name of the variable.
145: * @return A variable.
146: * @throws WorkflowException if no variable with the given name exists.
147: */
148: public BooleanVariableImpl getVariable(String _name)
149: throws WorkflowException {
150: if (!this .variables.containsKey(_name)) {
151: throw new WorkflowException(
152: "Workflow does not contain the variable '" + _name
153: + "'!");
154: }
155:
156: return (BooleanVariableImpl) this .variables.get(_name);
157: }
158:
159: /**
160: * Returns the variables.
161: * @return An array of variables.
162: */
163: protected BooleanVariableImpl[] getVariables() {
164: return (BooleanVariableImpl[]) this .variables.values().toArray(
165: new BooleanVariableImpl[this .variables.size()]);
166: }
167:
168: /**
169: * @see org.apache.lenya.workflow.Workflow#getVariableNames()
170: */
171: public String[] getVariableNames() {
172: BooleanVariableImpl[] _variables = getVariables();
173: String[] names = new String[_variables.length];
174: for (int i = 0; i < names.length; i++) {
175: names[i] = _variables[i].getName();
176: }
177: return names;
178: }
179:
180: /**
181: * @see org.apache.lenya.workflow.Workflow#getName()
182: */
183: public String getName() {
184: return this .name;
185: }
186:
187: /**
188: * @see org.apache.lenya.workflow.Workflow#getInitialValue(java.lang.String)
189: */
190: public boolean getInitialValue(String variableName)
191: throws WorkflowException {
192: BooleanVariableImpl[] variables = getVariables();
193: for (int i = 0; i < variables.length; i++) {
194: if (variables[i].getName().equals(variableName)) {
195: return variables[i].getInitialValue();
196: }
197: }
198: throw new WorkflowException("The variable [" + variableName
199: + "] does not exist.");
200: }
201:
202: /**
203: * @see org.apache.lenya.workflow.Workflow#getEvents()
204: */
205: public String[] getEvents() {
206: return (String[]) this .events.toArray(new String[this.events
207: .size()]);
208: }
209: }
|