KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > workflow > impl > WorkflowImpl


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: WorkflowImpl.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.workflow.impl;
21
22 import java.util.HashMap JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Set JavaDoc;
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 /**
34  * Implementation of a workflow schema.
35  */

36 public class WorkflowImpl implements Workflow {
37     
38     /**
39      * Creates a new instance of WorkflowImpl.
40      * @param initialState the initial state of the workflow.
41      */

42     protected WorkflowImpl(StateImpl initialState) {
43         this.initialState = initialState;
44         addState(initialState);
45     }
46
47     private State initialState;
48
49     /** Returns the initial state of this workflow.
50      * @return The initial state.
51      *
52      */

53     public State getInitialState() {
54         return initialState;
55     }
56
57     private Set JavaDoc transitions = new HashSet JavaDoc();
58     private Map JavaDoc states = new HashMap JavaDoc();
59
60     /**
61      * Adds a state.
62      * @param state A state.
63      */

64     private void addState(StateImpl state) {
65         states.put(state.getId(), state);
66     }
67
68     /**
69      * Adds a transition.
70      * @param transition The transition.
71      */

72     protected void addTransition(TransitionImpl transition) {
73         transitions.add(transition);
74         addState(transition.getSource());
75         addState(transition.getDestination());
76     }
77
78     /**
79      * Returns the transitions.
80      * @return An array of transitions.
81      */

82     protected TransitionImpl[] getTransitions() {
83         return (TransitionImpl[]) transitions.toArray(new TransitionImpl[transitions.size()]);
84     }
85
86     /** Returns the destination state of a transition.
87      * @param transition A transition.
88      * @return The destination state.
89      *
90      */

91     protected State getDestination(Transition transition) {
92         return ((TransitionImpl) transition).getDestination();
93     }
94
95     /** Returns the transitions that leave a state.
96      * @param state A state.
97      * @return The transitions that leave the state.
98      *
99      */

100     public Transition[] getLeavingTransitions(State state) {
101         Set JavaDoc leavingTransitions = new HashSet JavaDoc();
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     /**
114      * Checks if this workflow contains a state.
115      * @param state The state to check.
116      * @return <code>true</code> if the state is contained, <code>false</code> otherwise.
117      */

118     protected boolean containsState(State state) {
119         return states.containsValue(state);
120     }
121
122     /**
123      * Returns the states.
124      * @return An array of states.
125      */

126     protected StateImpl[] getStates() {
127         return (StateImpl[]) states.values().toArray(new StateImpl[states.size()]);
128     }
129
130     /**
131      * Returns the state with a certain name.
132      * @param name The state name.
133      * @return A state.
134      * @throws WorkflowException when the state does not exist.
135      */

136     protected StateImpl getState(String JavaDoc 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 JavaDoc events = new HashMap JavaDoc();
145
146     /**
147      * Adds an event.
148      * @param event An event.
149      */

150     protected void addEvent(EventImpl event) {
151         events.put(event.getName(), event);
152     }
153
154     /**
155      * Returns the event for a certain event name.
156      * @param name A string.
157      * @return The event with this name.
158      * @throws WorkflowException when no event with the given name exists.
159      */

160     public EventImpl getEvent(String JavaDoc 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 JavaDoc variables = new HashMap JavaDoc();
169
170     /**
171      * Adds a variable.
172      * @param variable A variable.
173      */

174     protected void addVariable(BooleanVariableImpl variable) {
175         variables.put(variable.getName(), variable);
176     }
177
178     /**
179      * Returns the variable for a certain name.
180      * @param name The name of the variable.
181      * @return A variable.
182      * @throws WorkflowException if no variable with the given name exists.
183      */

184     public BooleanVariableImpl getVariable(String JavaDoc 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     /**
194      * Returns the variables.
195      * @return An array of variables.
196      */

197     protected BooleanVariableImpl[] getVariables() {
198         return (BooleanVariableImpl[]) variables.values().toArray(new BooleanVariableImpl[variables.size()]);
199     }
200
201     /**
202      * @see org.apache.lenya.workflow.Workflow#getVariableNames()
203      */

204     public String JavaDoc[] getVariableNames() {
205         BooleanVariableImpl[] variables = getVariables();
206         String JavaDoc[] names = new String JavaDoc[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