1 package org.smartcomps.twister.engine.priv.core.definition.impl;
2
3 import org.smartcomps.twister.engine.exception.ProcessStructuralException;
4 import org.smartcomps.twister.engine.exception.EngineException;
5 import org.smartcomps.twister.engine.priv.core.definition.Activity;
6 import org.smartcomps.twister.engine.priv.core.definition.AlarmEvent;
7 import org.smartcomps.twister.engine.priv.core.definition.MessageEvent;
8 import org.smartcomps.twister.engine.priv.core.definition.Pick;
9 import org.smartcomps.twister.engine.priv.core.dynamic.ExecutionContext;
10
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
15
16 /***
17 * Persistent implementation of the Pick interface.
18 * @see org.smartcomps.twister.engine.priv.core.definition.Pick
19 */
20 public class PickImpl extends StructuredActivityImpl implements Pick {
21
22 private boolean createInstance;
23
24 private List messageEvents = new ArrayList();
25 private List alarmEvents = new ArrayList();
26
27 public boolean isCreateInstance() {
28 return createInstance;
29 }
30
31 public void setCreateInstance(boolean createInstance) {
32 this.createInstance = createInstance;
33 }
34
35 public List getMessageEvents() {
36 return messageEvents;
37 }
38
39 public void setMessageEvents(List messageEvents) {
40 this.messageEvents = messageEvents;
41 }
42
43 public void addMessageEvent(MessageEvent event, Activity activity) {
44 int actPos = getActivities().indexOf(activity);
45
46 System.out.println("actPos, size : " + actPos + ", " + messageEvents.size());
47 if (actPos < 0) throw new ProcessStructuralException("Activity has not been attributed to " +
48 "this Pick container at creation time.");
49 if (actPos != messageEvents.size()) throw new ProcessStructuralException("MessageEvent must be" +
50 "added in the same order as activities have been created and therefore assigned to this" +
51 "Pick container, please check the ActivityFactory.createActivity method.");
52
53 getMessageEvents().add(event);
54 }
55
56 public Map getActivityMessageEvents() {
57 // First activities in the activities List are associated with MessageEvent objects.
58 Map result = new HashMap(messageEvents.size());
59
60 for (int m = 0; m < getMessageEvents().size(); m++) {
61 try {
62 Activity activity = (Activity) getActivities().get(m);
63
64 if (messageEvents.get(m) == null) {
65 throw new ProcessStructuralException("An activity in this Pick container is not associated with " +
66 "any MessageEvent, please check that all activities created in this " +
67 "container is properly associated with an event.");
68 }
69 result.put(activity, messageEvents.get(m));
70 } catch (IndexOutOfBoundsException iobe) {
71 throw new ProcessStructuralException("There are more Activity objects in this Pick container " +
72 "than MessageEvent, activities and container have been wrongly created or associated", iobe);
73 }
74 }
75 return result;
76 }
77
78 public MessageEvent getMessageEvent(Activity activity) {
79 int actPos = getActivities().indexOf(activity);
80
81 if (actPos < 0) throw new ProcessStructuralException("Activity has not been attributed to " +
82 "this Pick container at creation time.");
83
84 return (MessageEvent) messageEvents.get(actPos);
85 }
86
87 public List getAlarmEvents() {
88 return alarmEvents;
89 }
90
91 public void setAlarmEvents(List alarmEvents) {
92 this.alarmEvents = alarmEvents;
93 }
94
95 public void addAlarmEvent(AlarmEvent event, Activity activity) {
96 int actPos = getActivities().indexOf(activity);
97
98 System.out.println("actPos, size : " + actPos + ", " + alarmEvents.size());
99
100 if (actPos < 0) throw new ProcessStructuralException("Activity has not been attributed to " +
101 "this Pick container at creation time.");
102 if (actPos != (alarmEvents.size() + messageEvents.size())) throw new ProcessStructuralException("AlarmEvent must be" +
103 "added in the same order as activities have been created and therefore assigned to this" +
104 "Pick container, please check the ActivityFactory.createActivity method.");
105
106 getAlarmEvents().add(event);
107 }
108
109 public Map getActivityAlarmEvents() {
110 // Activities after those associated to MessageEvent objects are associated with
111 // AlarmEvent objects.
112 Map result = new HashMap(alarmEvents.size());
113
114 for (int m = 0; m < alarmEvents.size(); m++) {
115 try {
116 Activity activity = (Activity) getActivities().get(m + messageEvents.size());
117
118 if (alarmEvents.get(m) == null) {
119 throw new ProcessStructuralException("An activity in this Pick container is not associated " +
120 "with any AlarmEvent, please check that all activities created in this container " +
121 "is properly associated with an event.");
122 }
123 result.put(activity, alarmEvents.get(m));
124 } catch (IndexOutOfBoundsException iobe) {
125 throw new ProcessStructuralException("There are more Activity objects in this Pick container " +
126 "than AlarmEvent, activities and container have been wrongly created or associated", iobe);
127 }
128 }
129 return result;
130 }
131
132 public AlarmEvent getAlarmEvent(Activity activity) {
133 int actPos = getActivities().indexOf(activity);
134
135 if (actPos < 0) throw new ProcessStructuralException("Activity has not been attributed to " +
136 "this Pick container at creation time.");
137 if (actPos - messageEvents.size() < 0) throw new ProcessStructuralException("There are more " +
138 "message events than activities in this Pick container.");
139
140 return (AlarmEvent) alarmEvents.get(actPos - messageEvents.size());
141 }
142
143 public ExecutionContext execute(String correlationSetName, Map correlation) throws EngineException {
144 ExecutionContext ec = createContextTree(correlationSetName, correlation);
145 ec.execute();
146 return ec;
147 }
148
149 }
This page was automatically generated by Maven