1 package org.smartcomps.twister.engine.priv.core.dynamic.impl;
2
3 import org.smartcomps.twister.common.persistence.DBSessionException;
4 import org.smartcomps.twister.engine.exception.EngineRuntimeException;
5 import org.smartcomps.twister.engine.exception.ExecutionException;
6 import org.smartcomps.twister.engine.priv.core.definition.Activity;
7 import org.smartcomps.twister.engine.priv.core.definition.Sequence;
8 import org.smartcomps.twister.engine.priv.core.definition.impl.FlowImpl;
9 import org.smartcomps.twister.engine.priv.core.dynamic.ExecutionContext;
10 import org.smartcomps.twister.engine.priv.core.dynamic.ExecutionContextFactory;
11 import org.smartcomps.twister.engine.priv.core.dynamic.FlowEC;
12
13 import java.util.Collection;
14 import java.util.Iterator;
15
16 /***
17 * Persistent implementation of the FlowEC interface.
18 */
19 public class FlowECImpl extends StructuredECImpl implements FlowEC {
20
21 public void execute() {
22 if (this.getStatus() != ACTIVE) {
23 this.setStatus(ACTIVE);
24 if (getInstance() == null) {
25 getContainer().notifyExecution(this);
26 } else {
27 getInstance().notifyExecution(this);
28 }
29 }
30 executeContext();
31 }
32
33 public void notifyExecution(ExecutionContext ec) {
34 if (this.getStatus() != ACTIVE) {
35 this.setStatus(ACTIVE);
36 notifyExecutionToContainer();
37 }
38 }
39
40 public void notifyTermination(ExecutionContext ec) {
41 this.setStatus(COMPLETED);
42 // Completing children of branches still waiting
43 completeChildren(this);
44
45 notifyTerminationToContainer();
46 }
47
48 private void executeContext() {
49 Collection activities = ((FlowImpl) getActivity()).getActivities();
50 for (Iterator iterator = activities.iterator(); iterator.hasNext();) {
51 Activity activity = (Activity) iterator.next();
52 if (getStatus() == ACTIVE) {
53 ExecutionContext context = null;
54 try {
55 context = ExecutionContextFactory.createExecutionContext(activity, this);
56 } catch (DBSessionException e) {
57 throw new ExecutionException("An error occured when trying to create an execution context " +
58 "for activity " + activity + " in sequence execution context " + this, e);
59 }
60 context.execute();
61 }
62 }
63 }
64
65 public Activity getActivityForChildContext(ExecutionContext ec) {
66 int ecIndex = getExecutionContexts().indexOf(ec);
67 Activity result = null;
68 if (ecIndex >= 0) {
69 Sequence correspondingContainer = null;
70 try {
71 correspondingContainer = (Sequence) this.getActivity();
72 result = (Activity) correspondingContainer.getActivities().get(ecIndex);
73 } catch (IndexOutOfBoundsException e) {
74 throw new EngineRuntimeException("There are no activity in activity container " +
75 correspondingContainer + " at position " + ecIndex + " when trying to look for activity " +
76 "corresponding to ec " + ec + " in structuredEC " + this, e);
77 }
78 } else {
79 throw new EngineRuntimeException("The execution context " + ec + " could not be found inside its " +
80 "container with id : " + getId());
81 }
82 return result;
83 }
84
85 public ExecutionContext getExecution(Activity childActivity) {
86 int index = getActivity().getActivities().indexOf(childActivity);
87 if (index < 0 || index + 1 > getExecutionContexts().size()) {
88 return null;
89 } else {
90 return (ExecutionContext) getExecutionContexts().get(index);
91 }
92 }
93 }
This page was automatically generated by Maven