1 package org.smartcomps.twister.engine.priv.core.dynamic.impl;
2
3 import org.smartcomps.twister.engine.priv.core.dynamic.SwitchEC;
4 import org.smartcomps.twister.engine.priv.core.dynamic.ExecutionContext;
5 import org.smartcomps.twister.engine.priv.core.dynamic.ExecutionContextFactory;
6 import org.smartcomps.twister.engine.priv.core.definition.Switch;
7 import org.smartcomps.twister.engine.priv.core.definition.Activity;
8 import org.smartcomps.twister.engine.priv.expression.ExpressionProcessorFactory;
9 import org.smartcomps.twister.engine.exception.XPathEvaluationException;
10 import org.smartcomps.twister.engine.exception.ExecutionException;
11 import org.smartcomps.twister.common.persistence.DBSessionException;
12
13 /***
14 * Persistent implementation of the SwitchEC interface.
15 */
16 public class SwitchECImpl extends StructuredECImpl implements SwitchEC {
17
18 private int executedActivityIndex = -1;
19
20 public int getExecutedActivityIndex() {
21 return executedActivityIndex;
22 }
23
24 public void setExecutedActivityIndex(int executedActivityIndex) {
25 this.executedActivityIndex = executedActivityIndex;
26 }
27
28 public void execute() {
29 // Making the SwitchEC active
30 notifyExecution(this);
31
32 Switch origin = (Switch) getActivity();
33 Activity nextActivity = null;
34 int m;
35 for (m = 0; m < origin.getActivities().size(); m++) {
36 if (m < origin.getConditions().size()) {
37 // As long as we have a condition, evaluating it
38 String condition = origin.getCondition(m);
39 boolean conditionEval = false;
40 try {
41 conditionEval = ExpressionProcessorFactory.getBPELExpressionProcessor()
42 .evaluateAsBoolean(fetchInstance(), condition);
43 } catch (XPathEvaluationException e) {
44 throw new ExecutionException("An error occured when evaluating a condition in a Switch execution" +
45 "context : " + condition, e);
46 }
47 // If condition is true, we found the activity to execute
48 if (conditionEval) {
49 nextActivity = (Activity) origin.getActivities().get(m);
50 break;
51 }
52 } else {
53 // If no conditions left, we are in otherwise
54 nextActivity = (Activity) origin.getActivities().get(m);
55 break;
56 }
57 }
58
59 if (nextActivity != null) {
60 ExecutionContext context = null;
61 try {
62 context = ExecutionContextFactory.createExecutionContext(nextActivity, this);
63 } catch (DBSessionException e) {
64 throw new ExecutionException("An error occured when trying to create an execution context " +
65 "for activity " + nextActivity + " in sequence execution context " + this, e);
66 }
67 executedActivityIndex = m;
68 context.execute();
69 } else {
70 // If no activity has been found, the switch just completes
71 notifyTermination(this);
72 }
73 }
74
75 public Activity getActivityForChildContext(ExecutionContext ec) {
76 if (executedActivityIndex == -1) return null;
77 else return (Activity) getActivity().getActivities().get(executedActivityIndex);
78 }
79
80 public void notifyExecution(ExecutionContext ec) {
81 if (this.getStatus() != ACTIVE) {
82 this.setStatus(ACTIVE);
83 if (getInstance() == null) {
84 getContainer().notifyExecution(this);
85 } else {
86 getInstance().notifyExecution(this);
87 }
88 }
89 }
90
91 public void notifyTermination(ExecutionContext ec) {
92 this.setStatus(COMPLETED);
93 if (getInstance() == null) {
94 getContainer().notifyTermination(this);
95 } else {
96 getInstance().notifyTermination(this);
97 }
98 }
99
100 public ExecutionContext getExecution(Activity childActivity) {
101 if (executedActivityIndex == -1) {
102 return null;
103 } else {
104 int index = getActivity().getActivities().indexOf(childActivity);
105 if (executedActivityIndex != index) {
106 return null;
107 } else {
108 return (ExecutionContext) getExecutionContexts().get(0);
109 }
110 }
111 }
112
113 }
This page was automatically generated by Maven