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