Skip to main content
summaryrefslogtreecommitdiffstats
blob: d1bf5f9a1cf1f12940f1ecb32e3d8df3611bc779 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*****************************************************************************
 * Copyright (c) 2012 CEA LIST.
 *
 *    
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  CEA LIST - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.moka.fuml.Semantics.Activities.IntermediateActivities;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.papyrus.infra.core.Activator;
import org.eclipse.papyrus.moka.MokaConstants;
import org.eclipse.papyrus.moka.communication.event.isuspendresume.Suspend_Event;
import org.eclipse.papyrus.moka.debug.MokaStackFrame;
import org.eclipse.papyrus.moka.debug.MokaThread;
import org.eclipse.papyrus.moka.fuml.FUMLExecutionEngine;
import org.eclipse.papyrus.moka.fuml.Semantics.Classes.Kernel.Object_;
import org.eclipse.papyrus.moka.fuml.Semantics.Loci.LociL1.Locus;
import org.eclipse.papyrus.moka.fuml.Semantics.Loci.LociL1.SemanticVisitor;
import org.eclipse.papyrus.moka.fuml.debug.Debug;
import org.eclipse.papyrus.moka.fuml.debug.StackFrameManager;
import org.eclipse.papyrus.moka.fuml.presentation.FUMLPresentationUtils;
import org.eclipse.papyrus.moka.ui.presentation.AnimationUtils;
import org.eclipse.uml2.uml.ActivityNode;

public abstract class ActivityNodeActivation extends SemanticVisitor {

	/*
	 * The group that contains this activity node activation.
	 */
	public ActivityNodeActivationGroup group;

	/*
	 * The activity node being activated by this activity node activation. The
	 * node must be owned by the activity (type) of the activity execution of
	 * this node activation. [This is optional, to allow for fork node edge
	 * queues and implicit fork and join node activations for actions to not
	 * have nodes in the model.]
	 */
	public ActivityNode node;

	/*
	 * The set of activity edge instances for the incoming edges of the node.
	 */
	public List<ActivityEdgeInstance> incomingEdges = new ArrayList<ActivityEdgeInstance>();

	/*
	 * The set of activity edge instances for the outgoing edges of the node.
	 */
	public List<ActivityEdgeInstance> outgoingEdges = new ArrayList<ActivityEdgeInstance>();

	/*
	 * If true, this node activation is enabled for execution once all its other
	 * prerequesites are satisfied.
	 */
	public Boolean running;

	public List<Token> heldTokens = new ArrayList<Token>();

	public void run() {
		// Run the activation of this node.
		if(this.node != null) {
			Debug.println("[run] node = " + this.node.getName());
		} else {
			Debug.println("[run] Anonymous activation of type " + this.getClass().getName());
		}
		this.running = true;
	}

	public void receiveOffer() {
		// Receive an offer from an incoming edge.
		// Check if all prerequisites have been satisfied. If so, fire.
		Debug.println("[receiveOffer] " + (this.node == null ? "..." : "node = " + this.node.getName()));
		_beginIsolation();
		boolean ready = this.isReady();
		List<Token> tokens = new ArrayList<Token>();
		if(ready) {
			Debug.println("[receiveOffer] Firing.");
			tokens = this.takeOfferedTokens();
		}
		_endIsolation();
		if(ready) {
			this.fire(tokens);
		}
	}

	public List<Token> takeOfferedTokens() {
		// Get tokens from all incoming edges.
		List<Token> allTokens = new ArrayList<Token>();
		List<ActivityEdgeInstance> incomingEdges = this.incomingEdges;
		for(int i = 0; i < incomingEdges.size(); i++) {
			ActivityEdgeInstance incomingEdge = incomingEdges.get(i);
			List<Token> tokens = incomingEdge.takeOfferedTokens();
			for(int j = 0; j < tokens.size(); j++) {
				Token token = tokens.get(j);
				allTokens.add(token);
			}
		}
		return allTokens;
	}

	// public abstract void fire(List<Token> incomingTokens); 

	// This property is introduced for connection to the debug API
	public List<Token> incomingTokens_DEBUG ;

	public void fire(List<Token> incomingTokens) {
		Locus locus = this.getExecutionLocus() ;
		if (locus.isInDebugMode) {
			this.incomingTokens_DEBUG = incomingTokens ;
			if (locus.engine.isTerminated())
				return ;
			boolean animationMarkerNeedsToBeRemoved = false ;
			long date = 0 ;
			if (MokaConstants.MOKA_AUTOMATIC_ANIMATION) {
				date = System.currentTimeMillis() ;
				AnimationUtils.getInstance().addAnimationMarker(node) ;
				animationMarkerNeedsToBeRemoved = true ;
			}
			MokaStackFrame stackFrame = FUMLPresentationUtils.getMokaStackFrame(this) ;
			stackFrame.setThread(locus.mainThread) ;
			stackFrame.setName(this.node.getName()) ;
			StackFrameManager.getInstance().setStackFrame(this.getActivityExecution(), stackFrame) ;
			int reasonForSuspending = FUMLExecutionEngine.controlDelegate.shallSuspend(this.getActivityExecution(), this) ; 
			if (reasonForSuspending != -1) {
				locus.mainThread.setSuspended(true) ;
				locus.mainThread.setStackFrames(null) ;
				//locus.stackFrames = new MokaStackFrame[]{stackFrame} ;
				locus.stackFrames = StackFrameManager.getInstance().getStackFrames() ;
				Suspend_Event breakpointEvent = new Suspend_Event(locus.mainThread, reasonForSuspending, new MokaThread[]{locus.mainThread}) ;
				locus.engine.sendEvent(breakpointEvent) ;
				try {
					FUMLExecutionEngine.controlDelegate.suspend(this.getActivityExecution()) ;
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				locus.mainThread.setSuspended(false) ;
			}
			if (animationMarkerNeedsToBeRemoved) {
				try {
					long ellapsed = System.currentTimeMillis() - date ;
					long delay = Math.max(1,MokaConstants.MOKA_ANIMATION_DELAY - ellapsed) ;
					Thread.sleep(delay) ;
				} catch (InterruptedException e1) {
					Activator.log.error(e1);
				}	
				AnimationUtils.getInstance().removeAnimationMarker(node) ;
			}
			if (locus.engine.isTerminated())
				return ;
		}
	}
	//

	public void sendOffers(List<Token> tokens) {
		// Send offers for the given set of tokens over all outgoing edges (if
		// there are any tokens actually being offered).
		if(tokens.size() > 0) {
			// *** Send all outgoing offers concurrently. ***
			List<ActivityEdgeInstance> outgoingEdges = this.outgoingEdges;
			for(Iterator<ActivityEdgeInstance> i = outgoingEdges.iterator(); i.hasNext();) {
				ActivityEdgeInstance outgoingEdge = i.next();
				// Debug.println("[sendOffers] Sending offer to " +
				// outgoingEdge.target.node.getName() + ".");
				outgoingEdge.sendOffer(tokens);
			}
		}
	}

	public void terminate() {
		// Terminate the activation of this node.
		if(this.running) {
			if(this.node != null) {
				Debug.println("[terminate] node = " + this.node.getName());
			} else {
				Debug.println("[terminate] Anonymous activation of type " + this.getClass().getName());
			}
		}
		this.running = false;
	}

	public Boolean isReady() {
		// Check if all the prerequisites for this node have been satisfied.
		// By default, check that this node is running.
		return this.isRunning();
	}

	public Boolean isRunning() {
		// Test whether this node activation is running.
		return this.running;
	}

	public void addOutgoingEdge(ActivityEdgeInstance edge) {
		// Add an activity edge instance as an outgoing edge of this activity
		// node activation.
		edge.source = this;
		this.outgoingEdges.add(edge);
	}

	public void addIncomingEdge(ActivityEdgeInstance edge) {
		// Add an activity edge instance as an incoming edge of this activity
		// node activation.
		edge.target = this;
		this.incomingEdges.add(edge);
	}

	public void createNodeActivations() {
		// Create node activations for any subnodes of the node for this
		// activation.
		// For most kinds of nodes, this does nothing.
		return;
	}

	public void createEdgeInstances() {
		// Create edge instances for any edge instances owned by the node for
		// this activation.
		// For most kinds of nodes, this does nothing.
		return;
	}

	public Boolean isSourceFor(ActivityEdgeInstance edgeInstance) {
		// Check if this node activation is the effective source for the given
		// edge instance.
		return edgeInstance.source == this;
	}

	public ActivityExecution getActivityExecution() {
		// Return the activity execution that contains this activity node
		// activation, directly or indirectly.
		return this.group.getActivityExecution();
	}

	public Object_ getExecutionContext() {
		// Get the context object for the containing activity execution.
		return this.getActivityExecution().context;
	}

	public Locus getExecutionLocus() {
		// Get the locus of the containing activity execution.
		return this.getActivityExecution().locus;
	}

	public ActivityNodeActivation getNodeActivation(ActivityNode node) {
		// Get the activity node activation corresponding to the given activity
		// node, in the context of this activity node activation.
		// By default, return this activity node activation, if it is for the
		// given node, otherwise return nothing.
		ActivityNodeActivation activation = null;
		if(node == this.node) {
			activation = this;
		}
		return activation;
	}

	public void addToken(Token token) {
		// Transfer the given token to be held by this node.
		if(this.node == null) {
			Debug.println("[addToken] ...");
		} else {
			Debug.println("[addToken] node = " + this.node.getName());
		}
		Token transferredToken = token.transfer(this);
		// Debug.println("[addToken] Adding token with value = " +
		// transferredToken.get());
		this.heldTokens.add(transferredToken);
	}

	public Integer removeToken(Token token) {
		// Remove the given token, if it is held by this node activation.
		// Return the position (counting from 1) of the removed token (0 if
		// there is none removed).
		boolean notFound = true;
		int i = 1;
		while(notFound & i <= this.heldTokens.size()) {
			if(this.heldTokens.get(i - 1) == token) {
				if(this.node == null) {
					Debug.println("[removeToken] ...");
				} else {
					Debug.println("[removeToken] node = " + this.node.getName());
				}
				this.heldTokens.remove(i - 1);
				notFound = false;
			}
			i = i + 1;
		}
		if(notFound) {
			i = 0;
		} else {
			i = i - 1;
		}
		return i;
	}

	public void addTokens(List<Token> tokens) {
		// Transfer the given tokens to be the held tokens for this node.
		// if (this.node == null) {
		// Debug.println("[addTokens] ...");
		// } else {
		// Debug.println("[addTokens] node = " + this.node.getName());
		// }
		for(int i = 0; i < tokens.size(); i++) {
			Token token = tokens.get(i);
			this.addToken(token);
		}
	}

	public List<Token> takeTokens() {
		// Take the tokens held by this node activation.
		List<Token> tokens = this.getTokens();
		this.clearTokens();
		return tokens;
	}

	public void clearTokens() {
		// Remove all held tokens.
		while(this.heldTokens.size() > 0) {
			this.heldTokens.get(0).withdraw();
		}
	}

	public List<Token> getTokens() {
		// Get the tokens held by this node activation.
		// Debug.println("[getTokens] node = " + this.node.getName());
		List<Token> tokens = new ArrayList<Token>();
		List<Token> heldTokens = this.heldTokens;
		for(int i = 0; i < heldTokens.size(); i++) {
			Token heldToken = heldTokens.get(i);
			// Debug.println("[getTokens] token value = " +
			// heldTokens.get());
			tokens.add(heldToken);
		}
		return tokens;
	}

	public void suspend() {
		// Suspend this activation within the activation group that contains it.
		this.group.suspend(this);
	}

	public void resume() {
		// Resume this activation within the activation group that contains it.
		this.group.resume(this);
	}
}

Back to the top