Modified: lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/workflow/impl/AbstractCondition.java
URL: http://svn.apache.org/viewvc/lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/workflow/impl/AbstractCondition.java?rev=617035&r1=617034&r2=617035&view=diff
==============================================================================
--- lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/workflow/impl/AbstractCondition.java (original)
+++ lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/workflow/impl/AbstractCondition.java Wed Jan 30 23:44:03 2008
@@ -14,41 +14,33 @@
* limitations under the License.
*
*/
-
/* $Id$ */
-
package org.apache.lenya.workflow.impl;
-
import org.apache.lenya.workflow.Condition;
import org.apache.lenya.workflow.WorkflowException;
-
/**
* Abstract base class for workflow conditions.
*/
public abstract class AbstractCondition implements Condition {
-
- private String expression;
-
- /**
- * @see org.apache.lenya.workflow.impl.AbstractCondition#setExpression(java.lang.String)
- */
- public void setExpression(String expression) throws WorkflowException {
- this.expression = expression.trim();
- }
-
- /**
- * Returns the expression of this condition.
- * @return A string.
- */
- public String getExpression() {
- return expression;
- }
-
- /**
- * @see java.lang.Object#toString()
- */
- public String toString() {
- return getExpression();
- }
-
+ private String expression;
+ /**
+ * @see org.apache.lenya.workflow.impl.AbstractCondition#setExpression(java.lang.String)
+ */
+ public void setExpression(String expression) throws WorkflowException {
+ this.expression = expression.trim();
+ }
+ /**
+ * Returns the expression of this condition.
+ *
+ * @return A string.
+ */
+ public String getExpression() {
+ return expression;
+ }
+ /**
+ * @see java.lang.Object#toString()
+ */
+ public String toString() {
+ return getExpression();
+ }
}
Modified: lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/workflow/impl/BooleanVariableAssignmentImpl.java
URL: http://svn.apache.org/viewvc/lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/workflow/impl/BooleanVariableAssignmentImpl.java?rev=617035&r1=617034&r2=617035&view=diff
==============================================================================
--- lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/workflow/impl/BooleanVariableAssignmentImpl.java (original)
+++ lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/workflow/impl/BooleanVariableAssignmentImpl.java Wed Jan 30 23:44:03 2008
@@ -14,56 +14,50 @@
* limitations under the License.
*
*/
-
/* $Id$ */
-
package org.apache.lenya.workflow.impl;
-
import org.apache.lenya.workflow.BooleanVariableAssignment;
import org.apache.lenya.workflow.WorkflowException;
import org.apache.lenya.workflow.WorkflowInstance;
-
-
/**
* Implementation of a boolean variable assignment.
*/
public class BooleanVariableAssignmentImpl implements BooleanVariableAssignment {
-
- /**
- * Ctor.
- * @param variable The variable.
- * @param value The value.
- */
- protected BooleanVariableAssignmentImpl(BooleanVariableImpl variable, boolean value) {
- this.variable = variable;
-
- this.value = value;
- }
-
- private BooleanVariableImpl variable;
- private boolean value;
-
- /**
- * @see org.apache.lenya.workflow.BooleanVariableAssignment#execute(org.apache.lenya.workflow.WorkflowInstance instance)
- */
- public void execute(WorkflowInstance instance) throws WorkflowException {
- WorkflowInstanceImpl impl = (WorkflowInstanceImpl) instance;
- impl.getVariableInstance(getVariable()).setValue(getValue());
- }
-
- /**
- * Returns the value of this assignment.
- * @return A boolean value.
- */
- public boolean getValue() {
- return value;
- }
-
- /**
- * Returns the variable of this assignment.
- * @return A variable.
- */
- public BooleanVariableImpl getVariable() {
- return variable;
- }
+ /**
+ * Ctor.
+ *
+ * @param variable
+ * The variable.
+ * @param value
+ * The value.
+ */
+ protected BooleanVariableAssignmentImpl(BooleanVariableImpl variable, boolean value) {
+ this.variable = variable;
+ this.value = value;
+ }
+ private BooleanVariableImpl variable;
+ private boolean value;
+ /**
+ * @see org.apache.lenya.workflow.BooleanVariableAssignment#execute(org.apache.lenya.workflow.WorkflowInstance instance)
+ */
+ public void execute(WorkflowInstance instance) throws WorkflowException {
+ WorkflowInstanceImpl impl = (WorkflowInstanceImpl) instance;
+ impl.getVariableInstance(getVariable()).setValue(getValue());
+ }
+ /**
+ * Returns the value of this assignment.
+ *
+ * @return A boolean value.
+ */
+ public boolean getValue() {
+ return value;
+ }
+ /**
+ * Returns the variable of this assignment.
+ *
+ * @return A variable.
+ */
+ public BooleanVariableImpl getVariable() {
+ return variable;
+ }
}
Modified: lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/workflow/impl/BooleanVariableCondition.java
URL: http://svn.apache.org/viewvc/lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/workflow/impl/BooleanVariableCondition.java?rev=617035&r1=617034&r2=617035&view=diff
==============================================================================
--- lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/workflow/impl/BooleanVariableCondition.java (original)
+++ lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/workflow/impl/BooleanVariableCondition.java Wed Jan 30 23:44:03 2008
@@ -14,72 +14,60 @@
* limitations under the License.
*
*/
-
/* $Id$ */
-
package org.apache.lenya.workflow.impl;
-
import org.apache.lenya.workflow.Situation;
import org.apache.lenya.workflow.WorkflowException;
import org.apache.lenya.workflow.WorkflowInstance;
-import org.apache.log4j.Category;
-
+import org.apache.log4j.Logger;
/**
* Implementation of a boolean variable condition.
*/
public class BooleanVariableCondition extends AbstractCondition {
-
- private static final Category log = Category.getInstance(BooleanVariableCondition.class);
-
- private String variableName;
- private boolean value;
-
- /**
- * Returns the variable value to check.
- * @return A boolean value.
- */
- protected boolean getValue() {
- return value;
- }
-
- /**
- * Returns the variable name to check.
- * @return A string.
- */
- protected String getVariableName() {
- return variableName;
- }
-
- /**
- * @see org.apache.lenya.workflow.Condition#setExpression(java.lang.String)
- */
- public void setExpression(String expression) throws WorkflowException {
- super.setExpression(expression);
- String[] sides = expression.split("=");
- if (sides.length != 2) {
- throw new WorkflowException(
- "The expression '" + expression + "' must be of the form 'name = [true|false]'");
- }
-
- variableName = sides[0].trim();
- value = Boolean.valueOf(sides[1].trim()).booleanValue();
-
- if (log.isDebugEnabled()) {
- log.debug("Expression: [" + sides[1].trim() + "]");
- log.debug("Setting value: [" + value + "]");
- }
- }
-
- /**
- * @see org.apache.lenya.workflow.Condition#isComplied(org.apache.lenya.workflow.Situation, org.apache.lenya.workflow.WorkflowInstance)
- */
- public boolean isComplied(Situation situation, WorkflowInstance instance) throws WorkflowException {
- if (log.isDebugEnabled()) {
- log.debug("Checking boolean variable condition");
- log.debug(" Condition value: [" + getValue() + "]");
- log.debug(" Variable value: [" + instance.getValue(getVariableName()) + "]");
- }
- return instance.getValue(getVariableName()) == getValue();
- }
-
+ private static Logger log = Logger.getLogger(BooleanVariableCondition.class);
+ private String variableName;
+ private boolean value;
+ /**
+ * Returns the variable value to check.
+ *
+ * @return A boolean value.
+ */
+ protected boolean getValue() {
+ return value;
+ }
+ /**
+ * Returns the variable name to check.
+ *
+ * @return A string.
+ */
+ protected String getVariableName() {
+ return variableName;
+ }
+ /**
+ * @see org.apache.lenya.workflow.Condition#setExpression(java.lang.String)
+ */
+ public void setExpression(String expression) throws WorkflowException {
+ super.setExpression(expression);
+ String[] sides = expression.split("=");
+ if(sides.length != 2){
+ throw new WorkflowException("The expression '" + expression + "' must be of the form 'name = [true|false]'");
+ }
+ variableName = sides[0].trim();
+ value = Boolean.valueOf(sides[1].trim()).booleanValue();
+ if(log.isDebugEnabled()){
+ log.debug("Expression: [" + sides[1].trim() + "]");
+ log.debug("Setting value: [" + value + "]");
+ }
+ }
+ /**
+ * @see org.apache.lenya.workflow.Condition#isComplied(org.apache.lenya.workflow.Situation, org.apache.lenya.workflow.WorkflowInstance)
+ */
+ public boolean isComplied(Situation situation, WorkflowInstance instance) throws WorkflowException {
+ if(log.isDebugEnabled()){
+ log.debug("Checking boolean variable condition");
+ log.debug(" Condition value: [" + getValue() + "]");
+ log.debug(" Variable value: [" + instance.getValue(getVariableName()) + "]");
+ }
+ return instance.getValue(getVariableName()) == getValue();
+ }
}
Modified: lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/workflow/impl/BooleanVariableImpl.java
URL: http://svn.apache.org/viewvc/lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/workflow/impl/BooleanVariableImpl.java?rev=617035&r1=617034&r2=617035&view=diff
==============================================================================
--- lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/workflow/impl/BooleanVariableImpl.java (original)
+++ lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/workflow/impl/BooleanVariableImpl.java Wed Jan 30 23:44:03 2008
@@ -14,45 +14,37 @@
* limitations under the License.
*
*/
-
/* $Id$ */
-
package org.apache.lenya.workflow.impl;
-
import org.apache.lenya.workflow.BooleanVariable;
-
-
/**
* Implementation of a boolean variable.
*/
public class BooleanVariableImpl implements BooleanVariable {
-
- /**
- * Creates a new instance of BooleanVariableImpl.
- * @param variableName The variable name.
- * @param initialValue The initial value of the corresponding variable instances.
- */
- protected BooleanVariableImpl(String variableName, boolean initialValue) {
- name = variableName;
-
- this.initialValue = initialValue;
- }
-
- private String name;
-
- /**
- * @see org.apache.lenya.workflow.BooleanVariable#getName()
- */
- public String getName() {
- return name;
- }
-
- private boolean initialValue;
-
- /**
- * @see org.apache.lenya.workflow.BooleanVariable#getInitialValue()
- */
- public boolean getInitialValue() {
- return initialValue;
- }
+ /**
+ * Creates a new instance of BooleanVariableImpl.
+ *
+ * @param variableName
+ * The variable name.
+ * @param initialValue
+ * The initial value of the corresponding variable instances.
+ */
+ protected BooleanVariableImpl(String variableName, boolean initialValue) {
+ name = variableName;
+ this.initialValue = initialValue;
+ }
+ private String name;
+ /**
+ * @see org.apache.lenya.workflow.BooleanVariable#getName()
+ */
+ public String getName() {
+ return name;
+ }
+ private boolean initialValue;
+ /**
+ * @see org.apache.lenya.workflow.BooleanVariable#getInitialValue()
+ */
+ public boolean getInitialValue() {
+ return initialValue;
+ }
}
Modified: lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/workflow/impl/BooleanVariableInstanceImpl.java
URL: http://svn.apache.org/viewvc/lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/workflow/impl/BooleanVariableInstanceImpl.java?rev=617035&r1=617034&r2=617035&view=diff
==============================================================================
--- lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/workflow/impl/BooleanVariableInstanceImpl.java (original)
+++ lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/workflow/impl/BooleanVariableInstanceImpl.java Wed Jan 30 23:44:03 2008
@@ -14,35 +14,27 @@
* limitations under the License.
*
*/
-
/* $Id$ */
-
package org.apache.lenya.workflow.impl;
-
import org.apache.lenya.workflow.BooleanVariableInstance;
-
-
/**
* Implementation of a boolean variable instance.
*/
public class BooleanVariableInstanceImpl implements BooleanVariableInstance {
- private boolean value;
-
- /** Creates a new instance of BooleanVariableInstanceImpl */
- protected BooleanVariableInstanceImpl() {
- }
-
- /**
- * @see org.apache.lenya.workflow.BooleanVariableInstance#getValue()
- */
- public boolean getValue() {
- return value;
- }
-
- /**
- * @see org.apache.lenya.workflow.BooleanVariableInstance#setValue(boolean)
- */
- public void setValue(boolean variableValue) {
- value = variableValue;
- }
+ private boolean value;
+ /** Creates a new instance of BooleanVariableInstanceImpl */
+ protected BooleanVariableInstanceImpl() {
+ }
+ /**
+ * @see org.apache.lenya.workflow.BooleanVariableInstance#getValue()
+ */
+ public boolean getValue() {
+ return value;
+ }
+ /**
+ * @see org.apache.lenya.workflow.BooleanVariableInstance#setValue(boolean)
+ */
+ public void setValue(boolean variableValue) {
+ value = variableValue;
+ }
}
Modified: lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/workflow/impl/SynchronizedWorkflowInstancesImpl.java
URL: http://svn.apache.org/viewvc/lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/workflow/impl/SynchronizedWorkflowInstancesImpl.java?rev=617035&r1=617034&r2=617035&view=diff
==============================================================================
--- lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/workflow/impl/SynchronizedWorkflowInstancesImpl.java (original)
+++ lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/workflow/impl/SynchronizedWorkflowInstancesImpl.java Wed Jan 30 23:44:03 2008
@@ -14,149 +14,127 @@
* limitations under the License.
*
*/
-
/* $Id$ */
-
package org.apache.lenya.workflow.impl;
-
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
-
import org.apache.lenya.workflow.Event;
import org.apache.lenya.workflow.Situation;
import org.apache.lenya.workflow.State;
import org.apache.lenya.workflow.SynchronizedWorkflowInstances;
import org.apache.lenya.workflow.WorkflowException;
import org.apache.lenya.workflow.WorkflowInstance;
-import org.apache.log4j.Category;
-
+import org.apache.log4j.Logger;
/**
- * An object of this class encapsulates a set of synchronized
- * workflow instances.
+ * An object of this class encapsulates a set of synchronized workflow instances.
*/
public class SynchronizedWorkflowInstancesImpl implements SynchronizedWorkflowInstances {
-
- private static final Category log = Category.getInstance(SynchronizedWorkflowInstancesImpl.class);
-
- /**
- * Ctor.
- */
- public SynchronizedWorkflowInstancesImpl() {
- }
-
- /**
- * Ctor.
- * @param instances The set of workflow instances to synchronize.
- * @param mainInstance The main workflow instance to invoke for non-synchronized transitions.
- */
- public SynchronizedWorkflowInstancesImpl(
- WorkflowInstance[] instances,
- WorkflowInstance mainInstance) {
- setInstances(instances);
- setMainInstance(mainInstance);
- }
-
- /**
- * Sets the main workflow instance.
- * @param mainInstance The main workflow instance to invoke for non-synchronized transitions.
- */
- public void setMainInstance(WorkflowInstance mainInstance) {
- this.mainInstance = mainInstance;
- }
-
- private WorkflowInstance[] instances;
- private WorkflowInstance mainInstance;
-
- public void setInstances(WorkflowInstance[] instances) {
- this.instances = instances;
- }
-
- public WorkflowInstance[] getInstances() {
- return instances;
- }
-
- /**
- * Returns all executable events.
- * @see org.apache.lenya.workflow.WorkflowInstance#getExecutableEvents(org.apache.lenya.workflow.Situation)
- */
- public Event[] getExecutableEvents(Situation situation) throws WorkflowException {
- if (log.isDebugEnabled()) {
- log.debug("Resolving executable events");
- }
-
- WorkflowInstance[] instances = getInstances();
- if (instances.length == 0) {
- throw new WorkflowException("The set must contain at least one workflow instance!");
- }
-
- Event[] events = mainInstance.getExecutableEvents(situation);
- Set executableEvents = new HashSet(Arrays.asList(events));
-
- for (int i = 0; i < events.length; i++) {
- Event event = events[i];
- if (mainInstance.isSynchronized(event)) {
-
- boolean canFire = true;
- if (log.isDebugEnabled()) {
- log.debug(" Transition for event [" + event + "] is synchronized.");
- }
-
- boolean sameState = true;
- State currentState = mainInstance.getCurrentState();
- int j = 0;
- while (j < instances.length && sameState) {
- sameState = instances[j].getCurrentState().equals(currentState);
- j++;
- }
- if (log.isDebugEnabled()) {
- log.debug(" All instances are in the same state: [" + sameState + "]");
- }
-
- if (sameState) {
- for (int k = 0; k < instances.length; k++) {
- WorkflowInstanceImpl instance = (WorkflowInstanceImpl) instances[k];
- if (instance != mainInstance && !instance.getNextTransition(event).canFire(situation, instance)) {
- canFire = false;
- if (log.isDebugEnabled()) {
- log.debug(" Workflow instance [" + instance + "] can not fire.");
- }
- }
- }
- } else {
- canFire = false;
- }
-
- if (!canFire) {
- executableEvents.remove(event);
- if (log.isDebugEnabled()) {
- log.debug(" Event [" + event + "] can not fire - removing from executable events.");
- }
- }
+ private static Logger log = Logger.getLogger(SynchronizedWorkflowInstancesImpl.class);
+ /**
+ * Ctor.
+ */
+ public SynchronizedWorkflowInstancesImpl() {
+ }
+ /**
+ * Ctor.
+ *
+ * @param instances
+ * The set of workflow instances to synchronize.
+ * @param mainInstance
+ * The main workflow instance to invoke for non-synchronized transitions.
+ */
+ public SynchronizedWorkflowInstancesImpl(WorkflowInstance[] instances, WorkflowInstance mainInstance) {
+ setInstances(instances);
+ setMainInstance(mainInstance);
+ }
+ /**
+ * Sets the main workflow instance.
+ *
+ * @param mainInstance
+ * The main workflow instance to invoke for non-synchronized transitions.
+ */
+ public void setMainInstance(WorkflowInstance mainInstance) {
+ this.mainInstance = mainInstance;
+ }
+ private WorkflowInstance[] instances;
+ private WorkflowInstance mainInstance;
+ public void setInstances(WorkflowInstance[] instances) {
+ this.instances = instances;
+ }
+ public WorkflowInstance[] getInstances() {
+ return instances;
+ }
+ /**
+ * Returns all executable events.
+ *
+ * @see org.apache.lenya.workflow.WorkflowInstance#getExecutableEvents(org.apache.lenya.workflow.Situation)
+ */
+ public Event[] getExecutableEvents(Situation situation) throws WorkflowException {
+ if(log.isDebugEnabled()){
+ log.debug("Resolving executable events");
+ }
+ WorkflowInstance[] instances = getInstances();
+ if(instances.length == 0){
+ throw new WorkflowException("The set must contain at least one workflow instance!");
+ }
+ Event[] events = mainInstance.getExecutableEvents(situation);
+ Set executableEvents = new HashSet(Arrays.asList(events));
+ for(int i = 0; i < events.length; i++){
+ Event event = events[i];
+ if(mainInstance.isSynchronized(event)){
+ boolean canFire = true;
+ if(log.isDebugEnabled()){
+ log.debug(" Transition for event [" + event + "] is synchronized.");
}
- }
-
- if (log.isDebugEnabled()) {
- log.debug(" Resolving executable events completed.");
- }
-
- return (Event[]) executableEvents.toArray(new Event[executableEvents.size()]);
- }
-
- /**
- * Invokes an event on all documents.
- * @see org.apache.lenya.workflow.WorkflowInstance#invoke(org.apache.lenya.workflow.Situation, org.apache.lenya.workflow.Event)
- */
- public void invoke(Situation situation, Event event) throws WorkflowException {
-
- if (mainInstance.isSynchronized(event)) {
- for (int i = 0; i < instances.length; i++) {
- instances[i].invoke(situation, event);
+ boolean sameState = true;
+ State currentState = mainInstance.getCurrentState();
+ int j = 0;
+ while(j < instances.length && sameState){
+ sameState = instances[j].getCurrentState().equals(currentState);
+ j++;
}
- }
- else {
- mainInstance.invoke(situation, event);
- }
- }
-
+ if(log.isDebugEnabled()){
+ log.debug(" All instances are in the same state: [" + sameState + "]");
+ }
+ if(sameState){
+ for(int k = 0; k < instances.length; k++){
+ WorkflowInstanceImpl instance = (WorkflowInstanceImpl) instances[k];
+ if(instance != mainInstance && !instance.getNextTransition(event).canFire(situation, instance)){
+ canFire = false;
+ if(log.isDebugEnabled()){
+ log.debug(" Workflow instance [" + instance + "] can not fire.");
+ }
+ }
+ }
+ }else{
+ canFire = false;
+ }
+ if(!canFire){
+ executableEvents.remove(event);
+ if(log.isDebugEnabled()){
+ log.debug(" Event [" + event + "] can not fire - removing from executable events.");
+ }
+ }
+ }
+ }
+ if(log.isDebugEnabled()){
+ log.debug(" Resolving executable events completed.");
+ }
+ return (Event[]) executableEvents.toArray(new Event[executableEvents.size()]);
+ }
+ /**
+ * Invokes an event on all documents.
+ *
+ * @see org.apache.lenya.workflow.WorkflowInstance#invoke(org.apache.lenya.workflow.Situation, org.apache.lenya.workflow.Event)
+ */
+ public void invoke(Situation situation, Event event) throws WorkflowException {
+ if(mainInstance.isSynchronized(event)){
+ for(int i = 0; i < instances.length; i++){
+ instances[i].invoke(situation, event);
+ }
+ }else{
+ mainInstance.invoke(situation, event);
+ }
+ }
}
Modified: lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/workflow/impl/TransitionImpl.java
URL: http://svn.apache.org/viewvc/lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/workflow/impl/TransitionImpl.java?rev=617035&r1=617034&r2=617035&view=diff
==============================================================================
--- lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/workflow/impl/TransitionImpl.java (original)
+++ lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/workflow/impl/TransitionImpl.java Wed Jan 30 23:44:03 2008
@@ -14,11 +14,10 @@
* limitations under the License.
*
*/
-
/* $Id$ */
-
package org.apache.lenya.workflow.impl;
-
+import java.util.ArrayList;
+import java.util.List;
import org.apache.lenya.workflow.Action;
import org.apache.lenya.workflow.Condition;
import org.apache.lenya.workflow.Event;
@@ -26,175 +25,161 @@
import org.apache.lenya.workflow.Transition;
import org.apache.lenya.workflow.WorkflowException;
import org.apache.lenya.workflow.WorkflowInstance;
-import org.apache.log4j.Category;
-
-import java.util.ArrayList;
-import java.util.List;
-
-
+import org.apache.log4j.Logger;
/**
* Implementation of a transition.
*/
public class TransitionImpl implements Transition {
-
- private static final Category log = Category.getInstance(TransitionImpl.class);
-
- /**
- * Ctor.
- * @param sourceState The source state.
- * @param destinationState The destination state.
- */
- protected TransitionImpl(StateImpl sourceState, StateImpl destinationState) {
- source = sourceState;
- destination = destinationState;
- }
-
- private List actions = new ArrayList();
- private boolean isSynchronized = false;
-
- /**
- * Returns the actions which are assigned tothis transition.
- * @return An array of actions.
- */
- public Action[] getActions() {
- return (Action[]) actions.toArray(new Action[actions.size()]);
- }
-
- /**
- * Assigns an action to this transition.
- * @param action The action.
- */
- public void addAction(Action action) {
- actions.add(action);
- }
-
- private List conditions = new ArrayList();
-
- /**
- * Returns the conditions which are assigned to this transition.
- * @return An array of conditions.
- */
- public Condition[] getConditions() {
- return (Condition[]) conditions.toArray(new Condition[conditions.size()]);
- }
-
- /**
- * Assigns a condition to this transition.
- * @param condition The condition.
- */
- public void addCondition(Condition condition) {
- conditions.add(condition);
- }
-
- private Event event;
-
- /**
- * Returns the event which invokes this transition.
- * @return An event.
- */
- public Event getEvent() {
- return event;
- }
-
- /**
- * Sets the event to invoke this transition.
- * @param anEvent An event.
- */
- public void setEvent(Event anEvent) {
- event = anEvent;
- }
-
- private StateImpl source;
-
- /**
- * Returns the source state of this transition.
- * @return A state.
- */
- public StateImpl getSource() {
- return source;
- }
-
- private StateImpl destination;
-
- /**
- * Returns the destination state of this transition.
- * @return A state.
- */
- public StateImpl getDestination() {
- return destination;
- }
-
- /**
- * Returns if the transition can fire in a certain situation.
- * @param situation The situation.
- * @param instance The workflow instance.
- * @throws WorkflowException when an error occurs.
- * @return A boolean value.
- */
- public boolean canFire(Situation situation, WorkflowInstance instance) throws WorkflowException {
- Condition[] conditions = getConditions();
- boolean canFire = true;
-
- int i = 0;
- while (canFire && i < conditions.length) {
- canFire = canFire && conditions[i].isComplied(situation, instance);
- if (log.isDebugEnabled()) {
- log.debug("Condition [" + conditions[i] + "] returns [" + canFire + "]");
- }
- i++;
- }
-
- return canFire;
- }
-
- /**
- * @see java.lang.Object#toString()
- */
- public String toString() {
- String string = getEvent().getName() + " [";
- Condition[] conditions = getConditions();
-
- for (int i = 0; i < conditions.length; i++) {
- if (i > 0) {
- string += ", ";
- }
-
- string += conditions[i].toString();
- }
-
- string += "]";
-
- Action[] actions = getActions();
-
- if (actions.length > 0) {
- string += " / ";
-
- for (int i = 0; i < actions.length; i++) {
- if (i > 0) {
- string += ", ";
- }
-
- string += actions[i].toString();
+ private static Logger log = Logger.getLogger(TransitionImpl.class);
+ /**
+ * Ctor.
+ *
+ * @param sourceState
+ * The source state.
+ * @param destinationState
+ * The destination state.
+ */
+ protected TransitionImpl(StateImpl sourceState, StateImpl destinationState) {
+ source = sourceState;
+ destination = destinationState;
+ }
+ private List actions = new ArrayList();
+ private boolean isSynchronized = false;
+ /**
+ * Returns the actions which are assigned tothis transition.
+ *
+ * @return An array of actions.
+ */
+ public Action[] getActions() {
+ return (Action[]) actions.toArray(new Action[actions.size()]);
+ }
+ /**
+ * Assigns an action to this transition.
+ *
+ * @param action
+ * The action.
+ */
+ public void addAction(Action action) {
+ actions.add(action);
+ }
+ private List conditions = new ArrayList();
+ /**
+ * Returns the conditions which are assigned to this transition.
+ *
+ * @return An array of conditions.
+ */
+ public Condition[] getConditions() {
+ return (Condition[]) conditions.toArray(new Condition[conditions.size()]);
+ }
+ /**
+ * Assigns a condition to this transition.
+ *
+ * @param condition
+ * The condition.
+ */
+ public void addCondition(Condition condition) {
+ conditions.add(condition);
+ }
+ private Event event;
+ /**
+ * Returns the event which invokes this transition.
+ *
+ * @return An event.
+ */
+ public Event getEvent() {
+ return event;
+ }
+ /**
+ * Sets the event to invoke this transition.
+ *
+ * @param anEvent
+ * An event.
+ */
+ public void setEvent(Event anEvent) {
+ event = anEvent;
+ }
+ private StateImpl source;
+ /**
+ * Returns the source state of this transition.
+ *
+ * @return A state.
+ */
+ public StateImpl getSource() {
+ return source;
+ }
+ private StateImpl destination;
+ /**
+ * Returns the destination state of this transition.
+ *
+ * @return A state.
+ */
+ public StateImpl getDestination() {
+ return destination;
+ }
+ /**
+ * Returns if the transition can fire in a certain situation.
+ *
+ * @param situation
+ * The situation.
+ * @param instance
+ * The workflow instance.
+ * @throws WorkflowException
+ * when an error occurs.
+ * @return A boolean value.
+ */
+ public boolean canFire(Situation situation, WorkflowInstance instance) throws WorkflowException {
+ Condition[] conditions = getConditions();
+ boolean canFire = true;
+ int i = 0;
+ while(canFire && i < conditions.length){
+ canFire = canFire && conditions[i].isComplied(situation, instance);
+ if(log.isDebugEnabled()){
+ log.debug("Condition [" + conditions[i] + "] returns [" + canFire + "]");
+ }
+ i++;
+ }
+ return canFire;
+ }
+ /**
+ * @see java.lang.Object#toString()
+ */
+ public String toString() {
+ String string = getEvent().getName() + " [";
+ Condition[] conditions = getConditions();
+ for(int i = 0; i < conditions.length; i++){
+ if(i > 0){
+ string += ", ";
+ }
+ string += conditions[i].toString();
+ }
+ string += "]";
+ Action[] actions = getActions();
+ if(actions.length > 0){
+ string += " / ";
+ for(int i = 0; i < actions.length; i++){
+ if(i > 0){
+ string += ", ";
}
- }
-
- return string;
- }
-
- /**
- * Returns if this transition is synchronized.
- * @return A boolean value.
- */
- public boolean isSynchronized() {
- return isSynchronized;
- }
-
- /**
- * Sets if this transition is synchronized.
- * @param isSynchronized A boolean value.
- */
- protected void setSynchronized(boolean isSynchronized) {
- this.isSynchronized = isSynchronized;
- }
-
+ string += actions[i].toString();
+ }
+ }
+ return string;
+ }
+ /**
+ * Returns if this transition is synchronized.
+ *
+ * @return A boolean value.
+ */
+ public boolean isSynchronized() {
+ return isSynchronized;
+ }
+ /**
+ * Sets if this transition is synchronized.
+ *
+ * @param isSynchronized
+ * A boolean value.
+ */
+ protected void setSynchronized(boolean isSynchronized) {
+ this.isSynchronized = isSynchronized;
+ }
}
Modified: lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/workflow/impl/WorkflowBuilder.java
URL: http://svn.apache.org/viewvc/lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/workflow/impl/WorkflowBuilder.java?rev=617035&r1=617034&r2=617035&view=diff
==============================================================================
--- lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/workflow/impl/WorkflowBuilder.java (original)
+++ lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/workflow/impl/WorkflowBuilder.java Wed Jan 30 23:44:03 2008
@@ -14,314 +14,273 @@
* limitations under the License.
*
*/
-
/* $Id$ */
-
package org.apache.lenya.workflow.impl;
-
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
-
import javax.xml.parsers.ParserConfigurationException;
-
import org.apache.lenya.workflow.Action;
import org.apache.lenya.workflow.Condition;
import org.apache.lenya.workflow.Event;
import org.apache.lenya.workflow.Workflow;
import org.apache.lenya.workflow.WorkflowException;
import org.apache.lenya.xml.DocumentHelper;
-import org.apache.log4j.Category;
+import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
-
/**
* Utility class to build a workflow schema from a file.
*/
public class WorkflowBuilder {
-
- private static final Category log = Category.getInstance(WorkflowBuilder.class);
-
- /**
- * Ctor.
- */
- protected WorkflowBuilder() {
- }
-
- /**
- * Builds a workflow schema from a file.
- * @param file The file.
- * @return A workflow schema implementation.
- * @throws WorkflowException if the file does not represent a valid workflow schema.
- */
- public static WorkflowImpl buildWorkflow(File file) throws WorkflowException {
- WorkflowImpl workflow;
-
- try {
- Document document = DocumentHelper.readDocument(file);
- workflow = buildWorkflow(document);
- } catch (Exception e) {
- throw new WorkflowException(e);
- }
-
- return workflow;
- }
-
- /**
- * Builds a workflow object from an XML document.
- * @param document The XML document.
- * @return A workflow implementation.
- * @throws ParserConfigurationException when something went wrong.
- * @throws SAXException when something went wrong.
- * @throws IOException when something went wrong.
- * @throws WorkflowException when something went wrong.
- */
- protected static WorkflowImpl buildWorkflow(Document document)
- throws ParserConfigurationException, SAXException, IOException, WorkflowException {
-
- Element root = document.getDocumentElement();
- StateImpl initialState = null;
-
- Map states = new HashMap();
- Map events = new HashMap();
- Map variables = new HashMap();
-
- // load states
- NodeList stateElements = root.getElementsByTagNameNS(Workflow.NAMESPACE, STATE_ELEMENT);
-
- for (int i = 0; i < stateElements.getLength(); i++) {
- Element element = (Element) stateElements.item(i);
- StateImpl state = buildState(element);
- String id = state.getId();
- states.put(id, state);
-
- if (isInitialStateElement(element)) {
- initialState = state;
- }
- }
-
- WorkflowImpl workflow = new WorkflowImpl(initialState);
-
- // load variables
- NodeList variableElements =
- root.getElementsByTagNameNS(Workflow.NAMESPACE, VARIABLE_ELEMENT);
-
- for (int i = 0; i < variableElements.getLength(); i++) {
- Element element = (Element) variableElements.item(i);
- BooleanVariableImpl variable = buildVariable(element);
- variables.put(variable.getName(), variable);
- workflow.addVariable(variable);
- }
-
- // load events
- NodeList eventElements = root.getElementsByTagNameNS(Workflow.NAMESPACE, EVENT_ELEMENT);
-
- for (int i = 0; i < eventElements.getLength(); i++) {
- EventImpl event = buildEvent((Element) eventElements.item(i));
- String id = event.getName();
- events.put(id, event);
- workflow.addEvent(event);
- }
-
- // load transitions
- NodeList transitionElements =
- root.getElementsByTagNameNS(Workflow.NAMESPACE, TRANSITION_ELEMENT);
-
- for (int i = 0; i < transitionElements.getLength(); i++) {
- TransitionImpl transition =
- buildTransition((Element) transitionElements.item(i), states, events, variables);
- workflow.addTransition(transition);
- }
-
- return workflow;
- }
-
- /**
- * Checks if a state element contains the initial state.
- * @param element An XML element.
- * @return A boolean value.
- */
- protected static boolean isInitialStateElement(Element element) {
- String initialAttribute = element.getAttribute(INITIAL_ATTRIBUTE);
-
- return (initialAttribute != null)
- && (initialAttribute.equals("yes") || initialAttribute.equals("true"));
- }
-
- protected static final String STATE_ELEMENT = "state";
- protected static final String TRANSITION_ELEMENT = "transition";
- protected static final String EVENT_ELEMENT = "event";
- protected static final String CONDITION_ELEMENT = "condition";
- protected static final String ACTION_ELEMENT = "action";
- protected static final String ID_ATTRIBUTE = "id";
- protected static final String INITIAL_ATTRIBUTE = "initial";
- protected static final String SOURCE_ATTRIBUTE = "source";
- protected static final String DESTINATION_ATTRIBUTE = "destination";
- protected static final String CLASS_ATTRIBUTE = "class";
- protected static final String VARIABLE_ELEMENT = "variable";
- protected static final String ASSIGNMENT_ELEMENT = "assign";
- protected static final String VARIABLE_ATTRIBUTE = "variable";
- protected static final String VALUE_ATTRIBUTE = "value";
- protected static final String NAME_ATTRIBUTE = "name";
- protected static final String SYNCHRONIZED_ATTRIBUTE = "synchronized";
-
- /**
- * Builds a state from an XML element.
- * @param element An XML element.
- * @return A state.
- */
- protected static StateImpl buildState(Element element) {
- String id = element.getAttribute(ID_ATTRIBUTE);
- StateImpl state = new StateImpl(id);
-
- return state;
- }
-
- /**
- * Builds a transition from an XML element.
- * @param element An XML element.
- * @param states A map from state IDs to states.
- * @param events A map from event IDs to events.
- * @param variables A map from variable names to variables.
- * @return A transition.
- * @throws WorkflowException when something went wrong.
- */
- protected static TransitionImpl buildTransition(
- Element element,
- Map states,
- Map events,
- Map variables)
- throws WorkflowException {
-
- if (log.isDebugEnabled()) {
- log.debug("Building transition");
- }
-
- String sourceId = element.getAttribute(SOURCE_ATTRIBUTE);
- String destinationId = element.getAttribute(DESTINATION_ATTRIBUTE);
-
- StateImpl source = (StateImpl) states.get(sourceId);
- StateImpl destination = (StateImpl) states.get(destinationId);
-
- TransitionImpl transition = new TransitionImpl(source, destination);
-
- // set event
- Element eventElement =
- (Element) element.getElementsByTagNameNS(Workflow.NAMESPACE, EVENT_ELEMENT).item(0);
- String id = eventElement.getAttribute(ID_ATTRIBUTE);
- Event event = (Event) events.get(id);
- transition.setEvent(event);
-
- if (log.isDebugEnabled()) {
- log.debug(" Event: [" + event + "]");
- }
-
- // load conditions
- NodeList conditionElements =
- element.getElementsByTagNameNS(Workflow.NAMESPACE, CONDITION_ELEMENT);
-
- for (int i = 0; i < conditionElements.getLength(); i++) {
- Condition condition = buildCondition((Element) conditionElements.item(i));
- transition.addCondition(condition);
- }
-
- // load assignments
- NodeList assignmentElements =
- element.getElementsByTagNameNS(Workflow.NAMESPACE, ASSIGNMENT_ELEMENT);
-
- for (int i = 0; i < assignmentElements.getLength(); i++) {
- BooleanVariableAssignmentImpl action =
- buildAssignment(variables, (Element) assignmentElements.item(i));
- transition.addAction(action);
- }
-
- // load actions
- NodeList actionElements =
- element.getElementsByTagNameNS(Workflow.NAMESPACE, ACTION_ELEMENT);
-
- for (int i = 0; i < actionElements.getLength(); i++) {
- Action action = buildAction((Element) actionElements.item(i));
- transition.addAction(action);
- }
-
- // set synchronization
- if (element.hasAttribute(SYNCHRONIZED_ATTRIBUTE)) {
- Boolean isSynchronized = Boolean.valueOf(element.getAttribute(SYNCHRONIZED_ATTRIBUTE));
- transition.setSynchronized(isSynchronized.booleanValue());
- }
-
- return transition;
- }
-
- /**
- * Builds an event from an XML element.
- * @param element An XML element.
- * @return An event.
- */
- protected static EventImpl buildEvent(Element element) {
- String id = element.getAttribute(ID_ATTRIBUTE);
- EventImpl event = new EventImpl(id);
-
- return event;
- }
-
- /**
- * Builds a condition from an XML element.
- * @param element An XML element.
- * @return A condition.
- * @throws WorkflowException when something went wrong.
- */
- protected static Condition buildCondition(Element element) throws WorkflowException {
- String className = element.getAttribute(CLASS_ATTRIBUTE);
- String expression = DocumentHelper.getSimpleElementText(element);
- Condition condition = ConditionFactory.createCondition(className, expression);
-
- return condition;
- }
-
- /**
- * Builds an action from an XML element.
- * @param element An XML element.
- * @return An action.
- */
- protected static Action buildAction(Element element) {
- String id = element.getAttribute(ID_ATTRIBUTE);
- Action action = new ActionImpl(id);
-
- return action;
- }
-
- /**
- * Builds a boolean variable from an XML element.
- * @param element An XML element.
- * @return A boolean variable.
- */
- protected static BooleanVariableImpl buildVariable(Element element) {
- String name = element.getAttribute(NAME_ATTRIBUTE);
- String value = element.getAttribute(VALUE_ATTRIBUTE);
-
- return new BooleanVariableImpl(name, Boolean.getBoolean(value));
- }
-
- /**
- * Builds an assignment object from an XML element.
- * @param variables A map from variable names to variables.
- * @param element An XML element.
- * @return An assignment object.
- * @throws WorkflowException when something went wrong.
- */
- protected static BooleanVariableAssignmentImpl buildAssignment(Map variables, Element element)
- throws WorkflowException {
- String variableName = element.getAttribute(VARIABLE_ATTRIBUTE);
-
- String valueString = element.getAttribute(VALUE_ATTRIBUTE);
- boolean value = Boolean.valueOf(valueString).booleanValue();
-
- BooleanVariableImpl variable = (BooleanVariableImpl) variables.get(variableName);
-
- return new BooleanVariableAssignmentImpl(variable, value);
- }
+ private static Logger log = Logger.getLogger(WorkflowBuilder.class);
+ /**
+ * Ctor.
+ */
+ protected WorkflowBuilder() {
+ }
+ /**
+ * Builds a workflow schema from a file.
+ *
+ * @param file
+ * The file.
+ * @return A workflow schema implementation.
+ * @throws WorkflowException
+ * if the file does not represent a valid workflow schema.
+ */
+ public static WorkflowImpl buildWorkflow(File file) throws WorkflowException {
+ WorkflowImpl workflow;
+ try{
+ Document document = DocumentHelper.readDocument(file);
+ workflow = buildWorkflow(document);
+ }catch(Exception e){
+ throw new WorkflowException(e);
+ }
+ return workflow;
+ }
+ /**
+ * Builds a workflow object from an XML document.
+ *
+ * @param document
+ * The XML document.
+ * @return A workflow implementation.
+ * @throws ParserConfigurationException
+ * when something went wrong.
+ * @throws SAXException
+ * when something went wrong.
+ * @throws IOException
+ * when something went wrong.
+ * @throws WorkflowException
+ * when something went wrong.
+ */
+ protected static WorkflowImpl buildWorkflow(Document document) throws ParserConfigurationException, SAXException, IOException, WorkflowException {
+ Element root = document.getDocumentElement();
+ StateImpl initialState = null;
+ Map states = new HashMap();
+ Map events = new HashMap();
+ Map variables = new HashMap();
+ // load states
+ NodeList stateElements = root.getElementsByTagNameNS(Workflow.NAMESPACE, STATE_ELEMENT);
+ for(int i = 0; i < stateElements.getLength(); i++){
+ Element element = (Element) stateElements.item(i);
+ StateImpl state = buildState(element);
+ String id = state.getId();
+ states.put(id, state);
+ if(isInitialStateElement(element)){
+ initialState = state;
+ }
+ }
+ WorkflowImpl workflow = new WorkflowImpl(initialState);
+ // load variables
+ NodeList variableElements = root.getElementsByTagNameNS(Workflow.NAMESPACE, VARIABLE_ELEMENT);
+ for(int i = 0; i < variableElements.getLength(); i++){
+ Element element = (Element) variableElements.item(i);
+ BooleanVariableImpl variable = buildVariable(element);
+ variables.put(variable.getName(), variable);
+ workflow.addVariable(variable);
+ }
+ // load events
+ NodeList eventElements = root.getElementsByTagNameNS(Workflow.NAMESPACE, EVENT_ELEMENT);
+ for(int i = 0; i < eventElements.getLength(); i++){
+ EventImpl event = buildEvent((Element) eventElements.item(i));
+ String id = event.getName();
+ events.put(id, event);
+ workflow.addEvent(event);
+ }
+ // load transitions
+ NodeList transitionElements = root.getElementsByTagNameNS(Workflow.NAMESPACE, TRANSITION_ELEMENT);
+ for(int i = 0; i < transitionElements.getLength(); i++){
+ TransitionImpl transition = buildTransition((Element) transitionElements.item(i), states, events, variables);
+ workflow.addTransition(transition);
+ }
+ return workflow;
+ }
+ /**
+ * Checks if a state element contains the initial state.
+ *
+ * @param element
+ * An XML element.
+ * @return A boolean value.
+ */
+ protected static boolean isInitialStateElement(Element element) {
+ String initialAttribute = element.getAttribute(INITIAL_ATTRIBUTE);
+ return (initialAttribute != null) && (initialAttribute.equals("yes") || initialAttribute.equals("true"));
+ }
+ protected static final String STATE_ELEMENT = "state";
+ protected static final String TRANSITION_ELEMENT = "transition";
+ protected static final String EVENT_ELEMENT = "event";
+ protected static final String CONDITION_ELEMENT = "condition";
+ protected static final String ACTION_ELEMENT = "action";
+ protected static final String ID_ATTRIBUTE = "id";
+ protected static final String INITIAL_ATTRIBUTE = "initial";
+ protected static final String SOURCE_ATTRIBUTE = "source";
+ protected static final String DESTINATION_ATTRIBUTE = "destination";
+ protected static final String CLASS_ATTRIBUTE = "class";
+ protected static final String VARIABLE_ELEMENT = "variable";
+ protected static final String ASSIGNMENT_ELEMENT = "assign";
+ protected static final String VARIABLE_ATTRIBUTE = "variable";
+ protected static final String VALUE_ATTRIBUTE = "value";
+ protected static final String NAME_ATTRIBUTE = "name";
+ protected static final String SYNCHRONIZED_ATTRIBUTE = "synchronized";
+ /**
+ * Builds a state from an XML element.
+ *
+ * @param element
+ * An XML element.
+ * @return A state.
+ */
+ protected static StateImpl buildState(Element element) {
+ String id = element.getAttribute(ID_ATTRIBUTE);
+ StateImpl state = new StateImpl(id);
+ return state;
+ }
+ /**
+ * Builds a transition from an XML element.
+ *
+ * @param element
+ * An XML element.
+ * @param states
+ * A map from state IDs to states.
+ * @param events
+ * A map from event IDs to events.
+ * @param variables
+ * A map from variable names to variables.
+ * @return A transition.
+ * @throws WorkflowException
+ * when something went wrong.
+ */
+ protected static TransitionImpl buildTransition(Element element, Map states, Map events, Map variables) throws WorkflowException {
+ if(log.isDebugEnabled()){
+ log.debug("Building transition");
+ }
+ String sourceId = element.getAttribute(SOURCE_ATTRIBUTE);
+ String destinationId = element.getAttribute(DESTINATION_ATTRIBUTE);
+ StateImpl source = (StateImpl) states.get(sourceId);
+ StateImpl destination = (StateImpl) states.get(destinationId);
+ TransitionImpl transition = new TransitionImpl(source, destination);
+ // set event
+ Element eventElement = (Element) element.getElementsByTagNameNS(Workflow.NAMESPACE, EVENT_ELEMENT).item(0);
+ String id = eventElement.getAttribute(ID_ATTRIBUTE);
+ Event event = (Event) events.get(id);
+ transition.setEvent(event);
+ if(log.isDebugEnabled()){
+ log.debug(" Event: [" + event + "]");
+ }
+ // load conditions
+ NodeList conditionElements = element.getElementsByTagNameNS(Workflow.NAMESPACE, CONDITION_ELEMENT);
+ for(int i = 0; i < conditionElements.getLength(); i++){
+ Condition condition = buildCondition((Element) conditionElements.item(i));
+ transition.addCondition(condition);
+ }
+ // load assignments
+ NodeList assignmentElements = element.getElementsByTagNameNS(Workflow.NAMESPACE, ASSIGNMENT_ELEMENT);
+ for(int i = 0; i < assignmentElements.getLength(); i++){
+ BooleanVariableAssignmentImpl action = buildAssignment(variables, (Element) assignmentElements.item(i));
+ transition.addAction(action);
+ }
+ // load actions
+ NodeList actionElements = element.getElementsByTagNameNS(Workflow.NAMESPACE, ACTION_ELEMENT);
+ for(int i = 0; i < actionElements.getLength(); i++){
+ Action action = buildAction((Element) actionElements.item(i));
+ transition.addAction(action);
+ }
+ // set synchronization
+ if(element.hasAttribute(SYNCHRONIZED_ATTRIBUTE)){
+ Boolean isSynchronized = Boolean.valueOf(element.getAttribute(SYNCHRONIZED_ATTRIBUTE));
+ transition.setSynchronized(isSynchronized.booleanValue());
+ }
+ return transition;
+ }
+ /**
+ * Builds an event from an XML element.
+ *
+ * @param element
+ * An XML element.
+ * @return An event.
+ */
+ protected static EventImpl buildEvent(Element element) {
+ String id = element.getAttribute(ID_ATTRIBUTE);
+ EventImpl event = new EventImpl(id);
+ return event;
+ }
+ /**
+ * Builds a condition from an XML element.
+ *
+ * @param element
+ * An XML element.
+ * @return A condition.
+ * @throws WorkflowException
+ * when something went wrong.
+ */
+ protected static Condition buildCondition(Element element) throws WorkflowException {
+ String className = element.getAttribute(CLASS_ATTRIBUTE);
+ String expression = DocumentHelper.getSimpleElementText(element);
+ Condition condition = ConditionFactory.createCondition(className, expression);
+ return condition;
+ }
+ /**
+ * Builds an action from an XML element.
+ *
+ * @param element
+ * An XML element.
+ * @return An action.
+ */
+ protected static Action buildAction(Element element) {
+ String id = element.getAttribute(ID_ATTRIBUTE);
+ Action action = new ActionImpl(id);
+ return action;
+ }
+ /**
+ * Builds a boolean variable from an XML element.
+ *
+ * @param element
+ * An XML element.
+ * @return A boolean variable.
+ */
+ protected static BooleanVariableImpl buildVariable(Element element) {
+ String name = element.getAttribute(NAME_ATTRIBUTE);
+ String value = element.getAttribute(VALUE_ATTRIBUTE);
+ return new BooleanVariableImpl(name, Boolean.getBoolean(value));
+ }
+ /**
+ * Builds an assignment object from an XML element.
+ *
+ * @param variables
+ * A map from variable names to variables.
+ * @param element
+ * An XML element.
+ * @return An assignment object.
+ * @throws WorkflowException
+ * when something went wrong.
+ */
+ protected static BooleanVariableAssignmentImpl buildAssignment(Map variables, Element element) throws WorkflowException {
+ String variableName = element.getAttribute(VARIABLE_ATTRIBUTE);
+ String valueString = element.getAttribute(VALUE_ATTRIBUTE);
+ boolean value = Boolean.valueOf(valueString).booleanValue();
+ BooleanVariableImpl variable = (BooleanVariableImpl) variables.get(variableName);
+ return new BooleanVariableAssignmentImpl(variable, value);
+ }
}
Modified: lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/workflow/impl/WorkflowImpl.java
URL: http://svn.apache.org/viewvc/lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/workflow/impl/WorkflowImpl.java?rev=617035&r1=617034&r2=617035&view=diff
==============================================================================
--- lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/workflow/impl/WorkflowImpl.java (original)
+++ lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/workflow/impl/WorkflowImpl.java Wed Jan 30 23:44:03 2008
@@ -14,199 +14,199 @@
* limitations under the License.
*
*/
-
/* $Id$ */
-
package org.apache.lenya.workflow.impl;
-
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
-
import org.apache.lenya.workflow.State;
import org.apache.lenya.workflow.Transition;
import org.apache.lenya.workflow.Workflow;
import org.apache.lenya.workflow.WorkflowException;
-
-
/**
* Implementation of a workflow schema.
*/
public class WorkflowImpl implements Workflow {
-
- /**
- * Creates a new instance of WorkflowImpl.
- * @param initialState the initial state of the workflow.
- */
- protected WorkflowImpl(StateImpl initialState) {
- this.initialState = initialState;
- addState(initialState);
- }
-
- private State initialState;
-
- /** Returns the initial state of this workflow.
- * @return The initial state.
- *
- */
- public State getInitialState() {
- return initialState;
- }
-
- private Set transitions = new HashSet();
- private Map states = new HashMap();
-
- /**
- * Adds a state.
- * @param state A state.
- */
- private void addState(StateImpl state) {
- states.put(state.getId(), state);
- }
-
- /**
- * Adds a transition.
- * @param transition The transition.
- */
- protected void addTransition(TransitionImpl transition) {
- transitions.add(transition);
- addState(transition.getSource());
- addState(transition.getDestination());
- }
-
- /**
- * Returns the transitions.
- * @return An array of transitions.
- */
- protected TransitionImpl[] getTransitions() {
- return (TransitionImpl[]) transitions.toArray(new TransitionImpl[transitions.size()]);
- }
-
- /** Returns the destination state of a transition.
- * @param transition A transition.
- * @return The destination state.
- *
- */
- protected State getDestination(Transition transition) {
- return ((TransitionImpl) transition).getDestination();
- }
-
- /** Returns the transitions that leave a state.
- * @param state A state.
- * @return The transitions that leave the state.
- *
- */
- public Transition[] getLeavingTransitions(State state) {
- Set leavingTransitions = new HashSet();
- TransitionImpl[] transitions = getTransitions();
-
- for (int i = 0; i < transitions.length; i++) {
- if (transitions[i].getSource() == state) {
- leavingTransitions.add(transitions[i]);
- }
- }
-
- return (Transition[]) leavingTransitions.toArray(new Transition[leavingTransitions.size()]);
- }
-
- /**
- * Checks if this workflow contains a state.
- * @param state The state to check.
- * @return <code>true</code> if the state is contained, <code>false</code> otherwise.
- */
- protected boolean containsState(State state) {
- return states.containsValue(state);
- }
-
- /**
- * Returns the states.
- * @return An array of states.
- */
- protected StateImpl[] getStates() {
- return (StateImpl[]) states.values().toArray(new StateImpl[states.size()]);
- }
-
- /**
- * Returns the state with a certain name.
- * @param name The state name.
- * @return A state.
- * @throws WorkflowException when the state does not exist.
- */
- protected StateImpl getState(String name) throws WorkflowException {
- if (!states.containsKey(name)) {
- throw new WorkflowException("Workflow does not contain the state '" + name + "'!");
- }
-
- return (StateImpl) states.get(name);
- }
-
- private Map events = new HashMap();
-
- /**
- * Adds an event.
- * @param event An event.
- */
- protected void addEvent(EventImpl event) {
- events.put(event.getName(), event);
- }
-
- /**
- * Returns the event for a certain event name.
- * @param name A string.
- * @return The event with this name.
- * @throws WorkflowException when no event with the given name exists.
- */
- public EventImpl getEvent(String name) throws WorkflowException {
- if (!events.containsKey(name)) {
- throw new WorkflowException("Workflow does not contain the event '" + name + "'!");
- }
-
- return (EventImpl) events.get(name);
- }
-
- private Map variables = new HashMap();
-
- /**
- * Adds a variable.
- * @param variable A variable.
- */
- protected void addVariable(BooleanVariableImpl variable) {
- variables.put(variable.getName(), variable);
- }
-
- /**
- * Returns the variable for a certain name.
- * @param name The name of the variable.
- * @return A variable.
- * @throws WorkflowException if no variable with the given name exists.
- */
- public BooleanVariableImpl getVariable(String name)
- throws WorkflowException {
- if (!variables.containsKey(name)) {
- throw new WorkflowException("Workflow does not contain the variable '" + name + "'!");
- }
-
- return (BooleanVariableImpl) variables.get(name);
- }
-
- /**
- * Returns the variables.
- * @return An array of variables.
- */
- protected BooleanVariableImpl[] getVariables() {
- return (BooleanVariableImpl[]) variables.values().toArray(new BooleanVariableImpl[variables.size()]);
- }
-
- /**
- * @see org.apache.lenya.workflow.Workflow#getVariableNames()
- */
- public String[] getVariableNames() {
- BooleanVariableImpl[] variables = getVariables();
- String[] names = new String[variables.length];
- for (int i = 0; i <names.length; i++) {
- names[i] = variables[i].getName();
- }
- return names;
- }
+ /**
+ * Creates a new instance of WorkflowImpl.
+ *
+ * @param initialState
+ * the initial state of the workflow.
+ */
+ protected WorkflowImpl(StateImpl initialState) {
+ this.initialState = initialState;
+ addState(initialState);
+ }
+ private State initialState;
+ /**
+ * Returns the initial state of this workflow.
+ *
+ * @return The initial state.
+ *
+ */
+ public State getInitialState() {
+ return initialState;
+ }
+ private Set transitions = new HashSet();
+ private Map states = new HashMap();
+ /**
+ * Adds a state.
+ *
+ * @param state
+ * A state.
+ */
+ private void addState(StateImpl state) {
+ states.put(state.getId(), state);
+ }
+ /**
+ * Adds a transition.
+ *
+ * @param transition
+ * The transition.
+ */
+ protected void addTransition(TransitionImpl transition) {
+ transitions.add(transition);
+ addState(transition.getSource());
+ addState(transition.getDestination());
+ }
+ /**
+ * Returns the transitions.
+ *
+ * @return An array of transitions.
+ */
+ protected TransitionImpl[] getTransitions() {
+ return (TransitionImpl[]) transitions.toArray(new TransitionImpl[transitions.size()]);
+ }
+ /**
+ * Returns the destination state of a transition.
+ *
+ * @param transition
+ * A transition.
+ * @return The destination state.
+ *
+ */
+ protected State getDestination(Transition transition) {
+ return ((TransitionImpl) transition).getDestination();
+ }
+ /**
+ * Returns the transitions that leave a state.
+ *
+ * @param state
+ * A state.
+ * @return The transitions that leave the state.
+ *
+ */
+ public Transition[] getLeavingTransitions(State state) {
+ Set leavingTransitions = new HashSet();
+ TransitionImpl[] transitions = getTransitions();
+ for(int i = 0; i < transitions.length; i++){
+ if(transitions[i].getSource() == state){
+ leavingTransitions.add(transitions[i]);
+ }
+ }
+ return (Transition[]) leavingTransitions.toArray(new Transition[leavingTransitions.size()]);
+ }
+ /**
+ * Checks if this workflow contains a state.
+ *
+ * @param state
+ * The state to check.
+ * @return <code>true</code> if the state is contained, <code>false</code> otherwise.
+ */
+ protected boolean containsState(State state) {
+ return states.containsValue(state);
+ }
+ /**
+ * Returns the states.
+ *
+ * @return An array of states.
+ */
+ protected StateImpl[] getStates() {
+ return (StateImpl[]) states.values().toArray(new StateImpl[states.size()]);
+ }
+ /**
+ * Returns the state with a certain name.
+ *
+ * @param name
+ * The state name.
+ * @return A state.
+ * @throws WorkflowException
+ * when the state does not exist.
+ */
+ protected StateImpl getState(String name) throws WorkflowException {
+ if(!states.containsKey(name)){
+ throw new WorkflowException("Workflow does not contain the state '" + name + "'!");
+ }
+ return (StateImpl) states.get(name);
+ }
+ private Map events = new HashMap();
+ /**
+ * Adds an event.
+ *
+ * @param event
+ * An event.
+ */
+ protected void addEvent(EventImpl event) {
+ events.put(event.getName(), event);
+ }
+ /**
+ * Returns the event for a certain event name.
+ *
+ * @param name
+ * A string.
+ * @return The event with this name.
+ * @throws WorkflowException
+ * when no event with the given name exists.
+ */
+ public EventImpl getEvent(String name) throws WorkflowException {
+ if(!events.containsKey(name)){
+ throw new WorkflowException("Workflow does not contain the event '" + name + "'!");
+ }
+ return (EventImpl) events.get(name);
+ }
+ private Map variables = new HashMap();
+ /**
+ * Adds a variable.
+ *
+ * @param variable
+ * A variable.
+ */
+ protected void addVariable(BooleanVariableImpl variable) {
+ variables.put(variable.getName(), variable);
+ }
+ /**
+ * Returns the variable for a certain name.
+ *
+ * @param name
+ * The name of the variable.
+ * @return A variable.
+ * @throws WorkflowException
+ * if no variable with the given name exists.
+ */
+ public BooleanVariableImpl getVariable(String name) throws WorkflowException {
+ if(!variables.containsKey(name)){
+ throw new WorkflowException("Workflow does not contain the variable '" + name + "'!");
+ }
+ return (BooleanVariableImpl) variables.get(name);
+ }
+ /**
+ * Returns the variables.
+ *
+ * @return An array of variables.
+ */
+ protected BooleanVariableImpl[] getVariables() {
+ return (BooleanVariableImpl[]) variables.values().toArray(new BooleanVariableImpl[variables.size()]);
+ }
+ /**
+ * @see org.apache.lenya.workflow.Workflow#getVariableNames()
+ */
+ public String[] getVariableNames() {
+ BooleanVariableImpl[] variables = getVariables();
+ String[] names = new String[variables.length];
+ for(int i = 0; i < names.length; i++){
+ names[i] = variables[i].getName();
+ }
+ return names;
+ }
}
Modified: lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/workflow/impl/WorkflowInstanceImpl.java
URL: http://svn.apache.org/viewvc/lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/workflow/impl/WorkflowInstanceImpl.java?rev=617035&r1=617034&r2=617035&view=diff
==============================================================================
--- lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/workflow/impl/WorkflowInstanceImpl.java (original)
+++ lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/workflow/impl/WorkflowInstanceImpl.java Wed Jan 30 23:44:03 2008
@@ -14,11 +14,8 @@
* limitations under the License.
*
*/
-
/* $Id$ */
-
package org.apache.lenya.workflow.impl;
-
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
@@ -27,7 +24,6 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
-
import org.apache.lenya.workflow.Action;
import org.apache.lenya.workflow.BooleanVariable;
import org.apache.lenya.workflow.BooleanVariableInstance;
@@ -39,272 +35,259 @@
import org.apache.lenya.workflow.WorkflowException;
import org.apache.lenya.workflow.WorkflowInstance;
import org.apache.lenya.workflow.WorkflowListener;
-import org.apache.log4j.Category;
-
-
+import org.apache.log4j.Logger;
/**
* Implementation of a workflow instance.
*/
public abstract class WorkflowInstanceImpl implements WorkflowInstance {
-
- private static final Category log = Category.getInstance(WorkflowInstanceImpl.class);
-
- /**
- * Creates a new instance of WorkflowInstanceImpl.
- */
- protected WorkflowInstanceImpl() {
- }
-
- private WorkflowImpl workflow;
-
- /**
- * Returns the workflow object of this instance.
- * @return A workflow object.
- */
- public Workflow getWorkflow() {
- return getWorkflowImpl();
- }
-
- /**
- * Returns the workflow object of this instance.
- * @return A workflow object.
- */
- protected WorkflowImpl getWorkflowImpl() {
- return workflow;
- }
-
- /** Returns the events that can be invoked in a certain situation.
- * @param situation The situation to check.
- * @return The events that can be invoked.
- * @throws WorkflowException when something went wrong.
- */
- public Event[] getExecutableEvents(Situation situation) throws WorkflowException {
-
- if (log.isDebugEnabled()) {
- log.debug("Resolving executable events");
- }
-
- Transition[] transitions = getWorkflow().getLeavingTransitions(getCurrentState());
- Set executableEvents = new HashSet();
-
- for (int i = 0; i < transitions.length; i++) {
- if (transitions[i].canFire(situation, this)) {
- executableEvents.add(transitions[i].getEvent());
- if (log.isDebugEnabled()) {
- log.debug(" [" + transitions[i].getEvent() + "] can fire.");
- }
+ private static Logger log = Logger.getLogger(WorkflowInstanceImpl.class);
+ /**
+ * Creates a new instance of WorkflowInstanceImpl.
+ */
+ protected WorkflowInstanceImpl() {
+ }
+ private WorkflowImpl workflow;
+ /**
+ * Returns the workflow object of this instance.
+ *
+ * @return A workflow object.
+ */
+ public Workflow getWorkflow() {
+ return getWorkflowImpl();
+ }
+ /**
+ * Returns the workflow object of this instance.
+ *
+ * @return A workflow object.
+ */
+ protected WorkflowImpl getWorkflowImpl() {
+ return workflow;
+ }
+ /**
+ * Returns the events that can be invoked in a certain situation.
+ *
+ * @param situation
+ * The situation to check.
+ * @return The events that can be invoked.
+ * @throws WorkflowException
+ * when something went wrong.
+ */
+ public Event[] getExecutableEvents(Situation situation) throws WorkflowException {
+ if(log.isDebugEnabled()){
+ log.debug("Resolving executable events");
+ }
+ Transition[] transitions = getWorkflow().getLeavingTransitions(getCurrentState());
+ Set executableEvents = new HashSet();
+ for(int i = 0; i < transitions.length; i++){
+ if(transitions[i].canFire(situation, this)){
+ executableEvents.add(transitions[i].getEvent());
+ if(log.isDebugEnabled()){
+ log.debug(" [" + transitions[i].getEvent() + "] can fire.");
}
- else {
- if (log.isDebugEnabled()) {
- log.debug(" [" + transitions[i].getEvent() + "] can not fire.");
- }
+ }else{
+ if(log.isDebugEnabled()){
+ log.debug(" [" + transitions[i].getEvent() + "] can not fire.");
}
- }
-
- if (log.isDebugEnabled()) {
- log.debug(" Resolving executable events completed.");
- }
-
- return (Event[]) executableEvents.toArray(new Event[executableEvents.size()]);
- }
-
- /** Invoke an event on this workflow instance.
- * @param situation The situation when the event was invoked.
- * @param event The event that was invoked.
- * @throws WorkflowException when the event may not be invoked.
- */
- public void invoke(Situation situation, Event event)
- throws WorkflowException {
- if (!Arrays.asList(getExecutableEvents(situation)).contains(event)) {
- throw new WorkflowException("The event '" + event +
- "' cannot be invoked in the situation '" + situation + "'.");
- }
-
- fire(getNextTransition(event));
-
- for (Iterator iter = listeners.iterator(); iter.hasNext();) {
- WorkflowListener listener = (WorkflowListener) iter.next();
- listener.transitionFired(this, situation, event);
- }
- }
-
- /**
- * Returns the transition that would fire for a given event.
- * @param event The event.
- * @return A transition.
- * @throws WorkflowException if no single transition would fire.
- */
- protected TransitionImpl getNextTransition(Event event) throws WorkflowException {
- TransitionImpl nextTransition = null;
- Transition[] transitions = getWorkflow().getLeavingTransitions(getCurrentState());
-
- for (int i = 0; i < transitions.length; i++) {
- if (transitions[i].getEvent().equals(event)) {
-
- if (nextTransition != null) {
- throw new WorkflowException("More than one transition found for event [" + event + "]!");
- }
-
- nextTransition = (TransitionImpl) transitions[i];
+ }
+ }
+ if(log.isDebugEnabled()){
+ log.debug(" Resolving executable events completed.");
+ }
+ return (Event[]) executableEvents.toArray(new Event[executableEvents.size()]);
+ }
+ /**
+ * Invoke an event on this workflow instance.
+ *
+ * @param situation
+ * The situation when the event was invoked.
+ * @param event
+ * The event that was invoked.
+ * @throws WorkflowException
+ * when the event may not be invoked.
+ */
+ public void invoke(Situation situation, Event event) throws WorkflowException {
+ if(!Arrays.asList(getExecutableEvents(situation)).contains(event)){
+ throw new WorkflowException("The event '" + event + "' cannot be invoked in the situation '" + situation + "'.");
+ }
+ fire(getNextTransition(event));
+ for(Iterator iter = listeners.iterator(); iter.hasNext();){
+ WorkflowListener listener = (WorkflowListener) iter.next();
+ listener.transitionFired(this, situation, event);
+ }
+ }
+ /**
+ * Returns the transition that would fire for a given event.
+ *
+ * @param event
+ * The event.
+ * @return A transition.
+ * @throws WorkflowException
+ * if no single transition would fire.
+ */
+ protected TransitionImpl getNextTransition(Event event) throws WorkflowException {
+ TransitionImpl nextTransition = null;
+ Transition[] transitions = getWorkflow().getLeavingTransitions(getCurrentState());
+ for(int i = 0; i < transitions.length; i++){
+ if(transitions[i].getEvent().equals(event)){
+ if(nextTransition != null){
+ throw new WorkflowException("More than one transition found for event [" + event + "]!");
}
- }
-
- if (nextTransition == null) {
- throw new WorkflowException("No transition found for event [" + event + "]!");
- }
-
- return nextTransition;
- }
-
- /**
- * Invokes a transition.
- * @param transition The transition to invoke.
- * @throws WorkflowException if something goes wrong.
- */
- protected void fire(TransitionImpl transition) throws WorkflowException {
- Action[] actions = transition.getActions();
-
- for (int i = 0; i < actions.length; i++) {
- actions[i].execute(this);
- }
-
- setCurrentState(transition.getDestination());
- }
-
- private State currentState;
-
- /**
- * Sets the current state of this instance.
- * @param state The state to set.
- */
- protected void setCurrentState(State state) {
- this.currentState = state;
- }
-
- /** Returns the current state of this WorkflowInstance.
- * @return A state object.
- */
- public State getCurrentState() {
- return currentState;
- }
-
- /**
- * Sets the workflow of this instance.
- * @param workflow A workflow object.
- */
- protected void setWorkflow(WorkflowImpl workflow) {
- this.workflow = workflow;
- setCurrentState(getWorkflow().getInitialState());
- initVariableInstances();
- }
-
- /**
- * Sets the workflow of this instance.
- * @param workflowName The identifier of the workflow.
- * @throws WorkflowException if something goes wrong.
- */
- protected void setWorkflow(String workflowName) throws WorkflowException {
- setWorkflow(getWorkflow(workflowName));
- }
-
- /**
- * Factory method to create a workflow object for a given identifier.
- * @param workflowName The workflow identifier.
- * @return A workflow object.
- * @throws WorkflowException when the workflow could not be created.
- */
- protected abstract WorkflowImpl getWorkflow(String workflowName)
- throws WorkflowException;
-
- /**
- * Returns a workflow state for a given name.
- * @param id The state id.
- * @return A workflow object.
- * @throws WorkflowException when the state was not found.
- */
- protected State getState(String id) throws WorkflowException {
- return getWorkflowImpl().getState(id);
- }
-
- private Map variableInstances = new HashMap();
-
- /**
- * Initializes the variable instances in the initial state.
- */
- protected void initVariableInstances() {
- variableInstances.clear();
-
- BooleanVariable[] variables = getWorkflowImpl().getVariables();
-
- for (int i = 0; i < variables.length; i++) {
- BooleanVariableInstance instance = new BooleanVariableInstanceImpl();
- instance.setValue(variables[i].getInitialValue());
- variableInstances.put(variables[i], instance);
- }
- }
-
- /**
- * Returns the corresponding instance of a workflow variable.
- * @param variable A variable of the corresponding workflow.
- * @return A variable instance object.
- * @throws WorkflowException when the variable instance was not found.
- */
- protected BooleanVariableInstance getVariableInstance(BooleanVariable variable)
- throws WorkflowException {
- if (!variableInstances.containsKey(variable)) {
- throw new WorkflowException("No instance for variable '" + variable.getName() + "'!");
- }
-
- return (BooleanVariableInstance) variableInstances.get(variable);
- }
-
- /**
- * @see org.apache.lenya.workflow.WorkflowInstance#getValue(java.lang.String)
- */
- public boolean getValue(String variableName) throws WorkflowException {
- BooleanVariable variable = getWorkflowImpl().getVariable(variableName);
- BooleanVariableInstance instance = getVariableInstance(variable);
-
- return instance.getValue();
- }
-
- /**
- * Sets the value of a state variable.
- * @param variableName The variable name.
- * @param value The value to set.
- * @throws WorkflowException when the variable was not found.
- */
- protected void setValue(String variableName, boolean value)
- throws WorkflowException {
- BooleanVariable variable = getWorkflowImpl().getVariable(variableName);
- BooleanVariableInstance instance = getVariableInstance(variable);
- instance.setValue(value);
- }
-
- private List listeners = new ArrayList();
-
- /**
- * @see org.apache.lenya.workflow.WorkflowInstance#addWorkflowListener(org.apache.lenya.workflow.WorkflowListener)
- */
- public void addWorkflowListener(WorkflowListener listener) {
- if (!listeners.contains(listener)) {
- listeners.add(listener);
- }
- }
-
- /**
- * @see org.apache.lenya.workflow.WorkflowInstance#removeWorkflowListener(org.apache.lenya.workflow.WorkflowListener)
- */
- public void removeWorkflowListener(WorkflowListener listener) {
- listeners.remove(listener);
- }
-
- /**
- * @see org.apache.lenya.workflow.WorkflowInstance#isSynchronized(org.apache.lenya.workflow.Event)
- */
- public boolean isSynchronized(Event event) throws WorkflowException {
- Transition nextTransition = getNextTransition(event);
- return nextTransition.isSynchronized();
- }
-
+ nextTransition = (TransitionImpl) transitions[i];
+ }
+ }
+ if(nextTransition == null){
+ throw new WorkflowException("No transition found for event [" + event + "]!");
+ }
+ return nextTransition;
+ }
+ /**
+ * Invokes a transition.
+ *
+ * @param transition
+ * The transition to invoke.
+ * @throws WorkflowException
+ * if something goes wrong.
+ */
+ protected void fire(TransitionImpl transition) throws WorkflowException {
+ Action[] actions = transition.getActions();
+ for(int i = 0; i < actions.length; i++){
+ actions[i].execute(this);
+ }
+ setCurrentState(transition.getDestination());
+ }
+ private State currentState;
+ /**
+ * Sets the current state of this instance.
+ *
+ * @param state
+ * The state to set.
+ */
+ protected void setCurrentState(State state) {
+ this.currentState = state;
+ }
+ /**
+ * Returns the current state of this WorkflowInstance.
+ *
+ * @return A state object.
+ */
+ public State getCurrentState() {
+ return currentState;
+ }
+ /**
+ * Sets the workflow of this instance.
+ *
+ * @param workflow
+ * A workflow object.
+ */
+ protected void setWorkflow(WorkflowImpl workflow) {
+ this.workflow = workflow;
+ setCurrentState(getWorkflow().getInitialState());
+ initVariableInstances();
+ }
+ /**
+ * Sets the workflow of this instance.
+ *
+ * @param workflowName
+ * The identifier of the workflow.
+ * @throws WorkflowException
+ * if something goes wrong.
+ */
+ protected void setWorkflow(String workflowName) throws WorkflowException {
+ setWorkflow(getWorkflow(workflowName));
+ }
+ /**
+ * Factory method to create a workflow object for a given identifier.
+ *
+ * @param workflowName
+ * The workflow identifier.
+ * @return A workflow object.
+ * @throws WorkflowException
+ * when the workflow could not be created.
+ */
+ protected abstract WorkflowImpl getWorkflow(String workflowName) throws WorkflowException;
+ /**
+ * Returns a workflow state for a given name.
+ *
+ * @param id
+ * The state id.
+ * @return A workflow object.
+ * @throws WorkflowException
+ * when the state was not found.
+ */
+ protected State getState(String id) throws WorkflowException {
+ return getWorkflowImpl().getState(id);
+ }
+ private Map variableInstances = new HashMap();
+ /**
+ * Initializes the variable instances in the initial state.
+ */
+ protected void initVariableInstances() {
+ variableInstances.clear();
+ BooleanVariable[] variables = getWorkflowImpl().getVariables();
+ for(int i = 0; i < variables.length; i++){
+ BooleanVariableInstance instance = new BooleanVariableInstanceImpl();
+ instance.setValue(variables[i].getInitialValue());
+ variableInstances.put(variables[i], instance);
+ }
+ }
+ /**
+ * Returns the corresponding instance of a workflow variable.
+ *
+ * @param variable
+ * A variable of the corresponding workflow.
+ * @return A variable instance object.
+ * @throws WorkflowException
+ * when the variable instance was not found.
+ */
+ protected BooleanVariableInstance getVariableInstance(BooleanVariable variable) throws WorkflowException {
+ if(!variableInstances.containsKey(variable)){
+ throw new WorkflowException("No instance for variable '" + variable.getName() + "'!");
+ }
+ return (BooleanVariableInstance) variableInstances.get(variable);
+ }
+ /**
+ * @see org.apache.lenya.workflow.WorkflowInstance#getValue(java.lang.String)
+ */
+ public boolean getValue(String variableName) throws WorkflowException {
+ BooleanVariable variable = getWorkflowImpl().getVariable(variableName);
+ BooleanVariableInstance instance = getVariableInstance(variable);
+ return instance.getValue();
+ }
+ /**
+ * Sets the value of a state variable.
+ *
+ * @param variableName
+ * The variable name.
+ * @param value
+ * The value to set.
+ * @throws WorkflowException
+ * when the variable was not found.
+ */
+ protected void setValue(String variableName, boolean value) throws WorkflowException {
+ BooleanVariable variable = getWorkflowImpl().getVariable(variableName);
+ BooleanVariableInstance instance = getVariableInstance(variable);
+ instance.setValue(value);
+ }
+ private List listeners = new ArrayList();
+ /**
+ * @see org.apache.lenya.workflow.WorkflowInstance#addWorkflowListener(org.apache.lenya.workflow.WorkflowListener)
+ */
+ public void addWorkflowListener(WorkflowListener listener) {
+ if(!listeners.contains(listener)){
+ listeners.add(listener);
+ }
+ }
+ /**
+ * @see org.apache.lenya.workflow.WorkflowInstance#removeWorkflowListener(org.apache.lenya.workflow.WorkflowListener)
+ */
+ public void removeWorkflowListener(WorkflowListener listener) {
+ listeners.remove(listener);
+ }
+ /**
+ * @see org.apache.lenya.workflow.WorkflowInstance#isSynchronized(org.apache.lenya.workflow.Event)
+ */
+ public boolean isSynchronized(Event event) throws WorkflowException {
+ Transition nextTransition = getNextTransition(event);
+ return nextTransition.isSynchronized();
+ }
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.