1 package org.smartcomps.twister.engine.priv.core.definition;
2
3 import org.smartcomps.twister.common.persistence.DBSessionException;
4 import org.smartcomps.twister.common.configuration.EngineConfiguration;
5 import org.smartcomps.twister.engine.exception.InstantiationException;
6 import org.smartcomps.twister.engine.exception.LinkAlreadyBoundException;
7 import org.smartcomps.twister.engine.priv.core.definition.impl.ActivityImpl;
8 import org.smartcomps.twister.engine.priv.core.definition.impl.AlarmEventImpl;
9 import org.smartcomps.twister.engine.priv.core.definition.impl.AssignmentImpl;
10 import org.smartcomps.twister.engine.priv.core.definition.impl.CorrelationRefImpl;
11 import org.smartcomps.twister.engine.priv.core.definition.impl.MessageEventImpl;
12 import org.smartcomps.twister.engine.priv.core.definition.impl.ProcessImpl;
13 import org.smartcomps.twister.engine.priv.core.definition.impl.dao.ActivityDAO;
14
15 import java.util.Set;
16 import java.util.List;
17
18 /***
19 * Creates appropriate activities or activity specific objects, setting default values.
20 */
21 public class ActivityFactory {
22
23 /***
24 * Adds an assignment to an assign activity, creating the associated Assign persistent
25 * instance.
26 * @param assign the Assign activity to add assignment for
27 * @param fromType the type of the "from" part of the assignment (see Assignment interface)
28 * @param toType the type of the "to" part of the assignment (see Assignment interface)
29 * @return the newly create Assignment instance (usually not useful)
30 * @throws DBSessionException
31 * @throws DBSessionException
32 * @see org.smartcomps.twister.engine.priv.core.definition.Assignment
33 */
34 public static Assignment addAssignment(Assign assign, int fromType, int toType) throws DBSessionException {
35 AssignmentImpl instance = new AssignmentImpl();
36 instance.setFromType(fromType);
37 instance.setToType(toType);
38 assign.addAssignment(instance);
39
40 instance = (AssignmentImpl) ActivityDAO.create(instance);
41 ActivityDAO.update(assign);
42
43 return instance;
44 }
45
46 /***
47 * Adds a correlation reference (CorrelationRef) to an Invoke activity.
48 * @param invoke
49 * @param set
50 * @param initiate
51 * @param pattern
52 * @return
53 * @throws DBSessionException
54 */
55 public static CorrelationRef addCorrelationRef(Invoke invoke, String set, boolean initiate, int pattern) throws DBSessionException {
56 CorrelationRef ref = createCorrelationRef(set, initiate, pattern);
57 invoke.addCorrelation(ref);
58 ActivityDAO.update(invoke);
59 return ref;
60 }
61
62 /***
63 * Adds a correlation reference (CorrelationRef) to a Receive activity.
64 * @param receive
65 * @param set
66 * @param initiate
67 * @param pattern
68 * @return
69 * @throws DBSessionException
70 */
71 public static CorrelationRef addCorrelationRef(Receive receive, String set, boolean initiate, int pattern) throws DBSessionException {
72 CorrelationRef ref = createCorrelationRef(set, initiate, pattern);
73 receive.addCorrelation(ref);
74 ActivityDAO.update(receive);
75 return ref;
76 }
77
78 /***
79 * Adds a MessageEvent to a Pick structured activity triggering the execution of
80 * the provided activity when an appropriate message is received.
81 * @param pick
82 * @param activity
83 * @param partnerLink
84 * @param portType
85 * @param operation
86 * @param variable
87 * @param correlations
88 * @return
89 * @throws DBSessionException
90 */
91 public static MessageEvent addMessageEvent(Pick pick, Activity activity, String partnerLink, String portType, String operation, String variable, Set correlations) throws DBSessionException {
92 MessageEventImpl instance = new MessageEventImpl();
93 instance.setOperation(operation);
94 instance.setPartnerLink(partnerLink);
95 instance.setPortType(portType);
96 instance.setVariable(variable);
97 instance.setCorrelations(correlations);
98
99 instance = (MessageEventImpl) ActivityDAO.create(instance);
100 pick.addMessageEvent(instance, activity);
101 return instance;
102 }
103
104 /***
105 * Adds an AlarmEvent to a Pick structured activity triggering the execution of
106 * the provided activity when the corresponding alarm is reached.
107 * @param pick
108 * @param activity
109 * @param timeExpression
110 * @param type
111 * @return
112 * @throws DBSessionException
113 */
114 public static AlarmEvent addAlarmEvent(Pick pick, Activity activity, String timeExpression, int type) throws DBSessionException {
115 AlarmEventImpl instance = new AlarmEventImpl();
116 instance.setTimeExpression(timeExpression);
117 instance.setType(type);
118
119 instance = (AlarmEventImpl) ActivityDAO.create(instance);
120 pick.addAlarmEvent(instance, activity);
121 return instance;
122 }
123
124 /***
125 * Creates a new persistent activity instance implementing the provided interface and
126 * assigns it to the provided container. <br>
127 * IMPORTANT : when you create a container and then create several activities using
128 * this container, the order used to create activities is the order of addition in the
129 * container. For example when you create a Sequence, the order in which you create
130 * sub-activities will be the order of activities in the sequence.
131 * @param activityInterface the interface the returned instance has to implement
132 * @param container the container to register the created activity under
133 * @return a persistent implementation of the provided interface
134 */
135 public static Activity createActivity(Class activityInterface, StructuredActivity container) throws DBSessionException {
136 if (!activityInterface.isInterface()) {
137 throw new java.lang.IllegalArgumentException("Method ActivityFactory.createActivity takes an interface for parameter, not a class.");
138 }
139
140 ActivityImpl instance = null;
141 try {
142 instance = getActivityInstance(activityInterface);
143 } catch (InstantiationException e) {
144 throw new DBSessionException(e);
145 }
146 container.addActivity(instance);
147 instance.setContainer(container);
148 instance = (ActivityImpl) ActivityDAO.create(instance);
149
150 return instance;
151 }
152
153 /***
154 * Creates a new persistent activity instance implementing the provided interface and
155 * assigns it to the provided process, only needed once in a process creation. <br>
156 * IMPORTANT : when you create a container and then create several activities using
157 * this container, the order used to create activities is the order of addition in the
158 * container. For example when you create a Sequence, the order in which you create
159 * sub-activities will be the order of activities in the sequence.
160 * @param activityInterface the interface the returned instance has to implement
161 * @param process the process to register the created activity under
162 * @return a persistent implementation of the provided interface
163 */
164 public static Activity createActivity(Class activityInterface, TwisterProcess process) throws DBSessionException {
165 if (!activityInterface.isInterface()) {
166 throw new java.lang.IllegalArgumentException("Method ActivityFactory.createActivity takes an interface for parameter, not a class.");
167 }
168
169 ActivityImpl instance = null;
170 try {
171 instance = getActivityInstance(activityInterface);
172 } catch (InstantiationException e) {
173 throw new DBSessionException(e);
174 }
175 ((ProcessImpl)process).setActivity(instance);
176 instance.setProcess(process);
177 instance = (ActivityImpl) ActivityDAO.create(instance);
178
179 return instance;
180 }
181
182 /***
183 * Creates a MessageEvent persistent implementation initializing it with the provided values.
184 * @param partnerLink
185 * @param portType
186 * @param operation
187 * @param variable
188 * @param correlations
189 * @return a MessageEvent persistent implementation
190 * @throws DBSessionException
191 * @throws DBSessionException
192 * @deprecated use addMessageEvent on Pick instead
193 */
194 public static MessageEvent createMessageEvent(String partnerLink, String portType, String operation, String variable, Set correlations) throws DBSessionException {
195 MessageEventImpl instance = new MessageEventImpl();
196 instance.setOperation(operation);
197 instance.setPartnerLink(partnerLink);
198 instance.setPortType(portType);
199 instance.setVariable(variable);
200 instance.setCorrelations(correlations);
201
202 instance = (MessageEventImpl) ActivityDAO.create(instance);
203 return instance;
204 }
205
206 /***
207 * Creates an AlarmEvent persistent implementation initializing it with the provided values.
208 * @param timeExpression
209 * @param type
210 * @return an AlarmEvent persistent implementation
211 * @throws DBSessionException
212 * @throws DBSessionException
213 * @deprecated use addAlarmEvent on Pick instead
214 */
215 public static AlarmEvent createAlarmEvent(String timeExpression, int type) throws DBSessionException, DBSessionException {
216 AlarmEventImpl instance = new AlarmEventImpl();
217 instance.setTimeExpression(timeExpression);
218 instance.setType(type);
219
220 instance = (AlarmEventImpl) ActivityDAO.create(instance);
221 return instance;
222 }
223
224 public static CorrelationRef createCorrelationRef(String set, boolean initiate, int pattern) throws DBSessionException {
225 CorrelationRefImpl instance = new CorrelationRefImpl();
226 instance.setInitiate(initiate);
227 instance.setPattern(pattern);
228 instance.setSet(set);
229
230 instance = (CorrelationRefImpl) ActivityDAO.create(instance);
231 return instance;
232 }
233
234 /***
235 * TODO Implement me with Flow
236 * Binds an activity as the source of a Link, eventually creating that Link if
237 * it doesn't exist.
238 * @param linkName
239 * @param sourceActivity
240 */
241 public static void bindSource(String linkName, Activity sourceActivity) throws LinkAlreadyBoundException {
242
243 }
244
245 /***
246 * TODO Implement me with Flow
247 * Binds an activity as the destination of a Link, eventually creating that Link if
248 * it doesn't exist.
249 * @param linkName
250 * @param sourceActivity
251 */
252 public static void bindDestination(String linkName, Activity sourceActivity) throws LinkAlreadyBoundException {
253
254 }
255
256 /***
257 * Find receives interested in the provided invoker paramaters.
258 * @param partnerLink
259 * @param portType
260 * @param operation
261 * @return List of Receive
262 * @throws DBSessionException
263 */
264 public static List findReceivesByInvoker(String partnerLink, String portType, String operation) throws DBSessionException {
265 return ActivityDAO.findReceivesByInvoker(partnerLink, portType, operation);
266 }
267
268 /***
269 * Finds Picks and according MessageEvents interested in the provided
270 * invoker paramters.
271 * @param partnerLink
272 * @param portType
273 * @param operation
274 * @return List of Object[] containing the Pick as obj[0] and the MessageEvent as obj[1]
275 * @throws DBSessionException
276 */
277 public static List findPickEventsByInvoker(String partnerLink, String portType, String operation) throws DBSessionException {
278 return ActivityDAO.findPickEventsByInvoker(partnerLink, portType, operation);
279 }
280
281 /***
282 * Does the bulk work of finding the right class from the interface and instantiating it.
283 * @param activityInterface
284 * @return
285 * @throws InstantiationException
286 */
287 private static ActivityImpl getActivityInstance(Class activityInterface) throws InstantiationException {
288 String activityClassName = EngineConfiguration.getActivityImplementation(activityInterface.getName());
289
290 ActivityImpl instance = null;
291 try {
292 instance = (ActivityImpl) Class.forName(activityClassName).newInstance();
293 } catch (java.lang.InstantiationException e) {
294 throw new InstantiationException("Could not instantiate an activity implementation " + activityClassName + " for interface " + activityInterface, e);
295 } catch (IllegalAccessException e) {
296 throw new InstantiationException("Could not instantiate an activity implementation " + activityClassName + " for interface " + activityInterface, e);
297 } catch (ClassNotFoundException e) {
298 throw new InstantiationException("The implementation class " + activityClassName + " for the interface " + activityInterface + " could not be found", e);
299 } catch (ClassCastException e) {
300 throw new InstantiationException("The instance returned from the interface " + activityInterface + " is not an Activity implementation : " + activityClassName, e);
301 }
302 return instance;
303 }
304
305 }
This page was automatically generated by Maven