Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8a4cbf5b969e9f8affc373a0b0892cd0d40657a3 (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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*****************************************************************************
 * 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.Actions.BasicActions;

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

import org.eclipse.papyrus.moka.fuml.FUMLExecutionEngine;
import org.eclipse.papyrus.moka.fuml.Semantics.Activities.IntermediateActivities.ActivityEdgeInstance;
import org.eclipse.papyrus.moka.fuml.Semantics.Activities.IntermediateActivities.ActivityNodeActivation;
import org.eclipse.papyrus.moka.fuml.Semantics.Activities.IntermediateActivities.ControlToken;
import org.eclipse.papyrus.moka.fuml.Semantics.Activities.IntermediateActivities.ForkNodeActivation;
import org.eclipse.papyrus.moka.fuml.Semantics.Activities.IntermediateActivities.ObjectToken;
import org.eclipse.papyrus.moka.fuml.Semantics.Activities.IntermediateActivities.Token;
import org.eclipse.papyrus.moka.fuml.Semantics.Classes.Kernel.BooleanValue;
import org.eclipse.papyrus.moka.fuml.Semantics.Classes.Kernel.FeatureValue;
import org.eclipse.papyrus.moka.fuml.Semantics.Classes.Kernel.Link;
import org.eclipse.papyrus.moka.fuml.Semantics.Classes.Kernel.Value;
import org.eclipse.papyrus.moka.fuml.debug.Debug;
import org.eclipse.uml2.uml.Action;
import org.eclipse.uml2.uml.ActivityNode;
import org.eclipse.uml2.uml.ConditionalNode;
import org.eclipse.uml2.uml.InputPin;
import org.eclipse.uml2.uml.LiteralBoolean;
import org.eclipse.uml2.uml.LoopNode;
import org.eclipse.uml2.uml.OutputPin;
import org.eclipse.uml2.uml.Pin;
import org.eclipse.uml2.uml.UMLFactory;

public abstract class ActionActivation extends ActivityNodeActivation {

	/*
	 * The activations of the pins owned by the action of this action
	 * activation.
	 */
	public List<PinActivation> pinActivations = new ArrayList<PinActivation>();

	/*
	 * Whether this action activation is already firing. This attribute is only
	 * used if the action for this action activation has isLocallyReentrant =
	 * false (the default). If isLocallyReentrant=true, then firing always just
	 * remains false.
	 */
	public Boolean firing;

	public void run() {
		// Run this action activation and any outoging fork node attached to it.
		super.run();
		if(this.outgoingEdges.size() > 0) {
			this.outgoingEdges.get(0).target.run();
		}
		this.firing = false;
	}

	public List<Token> takeOfferedTokens() {
		// If the action is not locally reentrant, then mark this activation as
		// firing.
		// Take any incoming offers of control tokens, then concurrently fire
		// all input pin activations.
		// Note: This is included here to happen in the same isolation scope as
		// the isReady test.
		this.firing = !((Action)this.node).isLocallyReentrant();
		List<Token> offeredTokens = 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);
				token.withdraw();
				offeredTokens.add(token);
			}
		}
		Action action = (Action)(this.node);
		// *** Fire all input pins concurrently. ***
		List<InputPin> inputPins = getInputs(action); // CHANGED from: action.getInputs();
		for(Iterator<InputPin> i = inputPins.iterator(); i.hasNext();) {
			InputPin pin = i.next();
			PinActivation pinActivation = this.getPinActivation(pin);
			List<Token> tokens = pinActivation.takeOfferedTokens();
			if (FUMLExecutionEngine.eInstance.getControlDelegate().control(pinActivation)) // Added for connection with debug API
				pinActivation.fire(tokens);
			for(int j = 0; j < tokens.size(); j++) {
				Token token = tokens.get(j);
				offeredTokens.add(token);
			}
		}
		return offeredTokens;
	}

	public void fire(List<Token> incomingTokens) {
		// Do the main action behavior then concurrently fire all output pin
		// activations
		// and offer a single control token. Then activate the action again,
		// if it is still ready to fire and has at least one token actually
		// being
		// offered to it.
		do {
			Debug.println("[fire] Action " + this.node.getName() + "...");
			Debug.println("[event] Fire activity=" + this.getActivityExecution().getBehavior().getName() + " action=" + this.node.getName());
			this.doAction();
			incomingTokens = this.completeAction();
		} while(incomingTokens.size() > 0);
	}

	public void terminate() {
		// Terminate this action activation and any outgoing fork node attached
		// to it.
		super.terminate();
		if(this.outgoingEdges.size() > 0) {
			this.outgoingEdges.get(0).target.terminate();
		}
	}

	public List<Token> completeAction() {
		// Concurrently fire all output pin activations and offer a single
		// control token. Then check if the action should fire again
		// and, if so, return additional incoming tokens for this.
		this.sendOffers();
		Debug.println("[fire] Checking if " + this.node.getName() + " should fire again...");
		_beginIsolation();
		List<Token> incomingTokens = new ArrayList<Token>();
		this.firing = false;
		if(this.isReady()) {
			incomingTokens = this.takeOfferedTokens();
			this.firing = this.isFiring() & incomingTokens.size() > 0;
		}
		_endIsolation();
		return incomingTokens;
	}

	public Boolean isReady() {
		// In addition to the default condition, check that, if the action has
		// isLocallyReentrant=false, then the activation is not currently
		// firing,
		// and that the sources of all incoming edges (control flows) have
		// offers and all input pin activations are ready.
		// [This assumes that all edges directly incoming to the action are
		// control flows.]
		boolean ready = super.isReady() & (((Action)this.node).isLocallyReentrant() | !this.isFiring());
		int i = 1;
		while(ready & i <= this.incomingEdges.size()) {
			ready = this.incomingEdges.get(i - 1).hasOffer();
			i = i + 1;
		}
		List<InputPin> inputPins = getInputs((Action)this.node); // CHANGED from: ((Action)(this.node)).getInputs();
		int j = 1;
		while(ready & j <= inputPins.size()) {
			ready = this.getPinActivation(inputPins.get(j - 1)).isReady();
			j = j + 1;
		}
		return ready;
	}

	public Boolean isFiring() {
		// Indicate whether this action activation is currently firing or not.
		return this.firing == null? false: this.firing; // ADDED check for null
	}

	public abstract void doAction();

	public void sendOffers() {
		// Fire all output pins and send offers on all outgoing control flows.
		Action action = (Action)(this.node);
		// *** Send offers from all output pins concurrently. ***
		List<OutputPin> outputPins = getOutputs(action); // CHANGED from: action.getOutputs();
		for(Iterator<OutputPin> i = outputPins.iterator(); i.hasNext();) {
			OutputPin outputPin = i.next();
			PinActivation pinActivation = this.getPinActivation(outputPin);
			pinActivation.sendUnofferedTokens();
		}
		// Send offers on all outgoing control flows.
		if(this.outgoingEdges.size() > 0) {
			List<Token> tokens = new ArrayList<Token>();
			tokens.add(new ControlToken());
			this.addTokens(tokens);
			this.outgoingEdges.get(0).sendOffer(tokens);
		}
	}

	public void createNodeActivations() {
		// Create node activations for the input and output pins of the action
		// for this activation.
		// [Note: Pins are owned by their actions, not by the enclosing activity
		// (or group), so they must be activated through the action activation.]
		Action action = (Action)(this.node);
		List<ActivityNode> inputPinNodes = new ArrayList<ActivityNode>();
		List<InputPin> inputPins = getInputs(action); // CHANGED from: action.getInputs();
		for(int i = 0; i < inputPins.size(); i++) {
			InputPin inputPin = inputPins.get(i);
			inputPinNodes.add(inputPin);
		}
		this.group.createNodeActivations(inputPinNodes);
		for(int i = 0; i < inputPinNodes.size(); i++) {
			ActivityNode node = inputPinNodes.get(i);
			this.addPinActivation((PinActivation)(this.group.getNodeActivation(node)));
		}
		List<ActivityNode> outputPinNodes = new ArrayList<ActivityNode>();
		List<OutputPin> outputPins = getOutputs(action); // CHANGED from: action.getOutputs();
		for(int i = 0; i < outputPins.size(); i++) {
			OutputPin outputPin = outputPins.get(i);
			outputPinNodes.add(outputPin);
		}
		this.group.createNodeActivations(outputPinNodes);
		for(int i = 0; i < outputPinNodes.size(); i++) {
			ActivityNode node = outputPinNodes.get(i);
			this.addPinActivation((PinActivation)(this.group.getNodeActivation(node)));
		}
	}

	public void addOutgoingEdge(ActivityEdgeInstance edge) {
		// If there are no outgoing activity edge instances, create a single
		// activity edge instance with a fork node execution at the other end.
		// Add the give edge to the fork node execution that is the target of
		// the activity edge instance out of this action execution.
		// [This assumes that all edges directly outgoing from the action are
		// control flows, with an implicit fork for offers out of the action.]
		ActivityNodeActivation forkNodeActivation;
		if(this.outgoingEdges.size() == 0) {
			forkNodeActivation = new ForkNodeActivation();
			forkNodeActivation.running = false; // ADDED
			ActivityEdgeInstance newEdge = new ActivityEdgeInstance();
			super.addOutgoingEdge(newEdge);
			forkNodeActivation.addIncomingEdge(newEdge);
		} else {
			forkNodeActivation = this.outgoingEdges.get(0).target;
		}
		forkNodeActivation.addOutgoingEdge(edge);
	}

	public void addPinActivation(PinActivation pinActivation) {
		// Add a pin activation to this action activation.
		this.pinActivations.add(pinActivation);
		pinActivation.actionActivation = this;
	}

	public PinActivation getPinActivation(Pin pin) {
		// Precondition: The given pin is owned by the action of the action
		// activation.
		// Return the pin activation corresponding to the given pin.
		PinActivation pinActivation = null;
		int i = 1;
		while(pinActivation == null & i <= this.pinActivations.size()) {
			PinActivation thisPinActivation = this.pinActivations.get(i - 1);
			if(thisPinActivation.node == pin) {
				pinActivation = thisPinActivation;
			}
			i = i + 1;
		}
		return pinActivation;
	}

	public void putToken(OutputPin pin, Value value) {
		// Precondition: The action execution has fired and the given pin is
		// owned by the action of the action execution.
		// Place a token for the given value on the pin activation corresponding
		// to the given output pin.
		Debug.println("[putToken] node = " + this.node.getName());
		ObjectToken token = new ObjectToken();
		token.value = value;
		PinActivation pinActivation = this.getPinActivation(pin);
		pinActivation.addToken(token);
	}

	public void putTokens(OutputPin pin, List<Value> values) {
		// Precondition: The action execution has fired and the given pin is
		// owned by the action of the action execution.
		// Place tokens for the given values on the pin activation corresponding
		// to the given output pin.
		// Debug.println("[putTokens] node = " + this.node.getName());
		for(int i = 0; i < values.size(); i++) {
			Value value = values.get(i);
			this.putToken(pin, value);
		}
	}

	public List<Value> getTokens(InputPin pin) {
		// Precondition: The action execution has fired and the given pin is
		// owned by the action of the action execution.
		// Get any tokens held by the pin activation corresponding to the given
		// input pin and return them
		// (but leave the tokens on the pin).
		Debug.println("[getTokens] node = " + this.node.getName() + ", pin = " + pin.getName());
		PinActivation pinActivation = this.getPinActivation(pin);
		List<Token> tokens = pinActivation.getUnofferedTokens();
		List<Value> values = new ArrayList<Value>();
		for(int i = 0; i < tokens.size(); i++) {
			Token token = tokens.get(i);
			Value value = ((ObjectToken)token).value;
			if(value != null) {
				values.add(value);
			}
		}
		return values;
	}

	public List<Value> takeTokens(InputPin pin) {
		// Precondition: The action execution has fired and the given pin is
		// owned by the action of the action execution.
		// Take any tokens held by the pin activation corresponding to the given
		// input pin and return them.
		Debug.println("[takeTokens] node = " + this.node.getName() + ", pin = " + pin.getName());
		PinActivation pinActivation = this.getPinActivation(pin);
		List<Token> tokens = pinActivation.takeUnofferedTokens();
		List<Value> values = new ArrayList<Value>();
		for(int i = 0; i < tokens.size(); i++) {
			Token token = tokens.get(i);
			Value value = ((ObjectToken)token).value;
			if(value != null) {
				values.add(value);
			}
		}
		return values;
	}

	public Boolean isSourceFor(ActivityEdgeInstance edgeInstance) {
		// If this action has an outgoing fork node, check that the fork node is
		// the source of the given edge instance.
		boolean isSource = false;
		if(this.outgoingEdges.size() > 0) {
			isSource = this.outgoingEdges.get(0).target.isSourceFor(edgeInstance);
		}
		return isSource;
	}

	public Boolean valueParticipatesInLink(Value value, Link link) {
		// Test if the given value participates in the given link.
		List<FeatureValue> linkFeatureValues = link.getFeatureValues();
		boolean participates = false;
		int i = 1;
		while(!participates & i <= linkFeatureValues.size()) {
			participates = linkFeatureValues.get(i - 1).values.get(0).equals(value);
			i = i + 1;
		}
		return participates;
	}

	public BooleanValue makeBooleanValue(Boolean value) {
		// Make a Boolean value using the built-in Boolean primitive type.
		// [This ensures that Boolean values created internally are the same as
		// the default used for evaluating Boolean literals.]
		LiteralBoolean booleanLiteral = UMLFactory.eINSTANCE.createLiteralBoolean();
		booleanLiteral.setValue(value);
		return (BooleanValue)(this.getExecutionLocus().executor.evaluate(booleanLiteral));
	}
	
	// ADDED:
	protected static List<InputPin> getInputs(Action action) {
		return action instanceof LoopNode? ((LoopNode)action).getLoopVariableInputs():
			   action.getInputs();
	}
	
	protected static List<OutputPin> getOutputs(Action action) {
		return action instanceof LoopNode? ((LoopNode)action).getResults():
			   action instanceof ConditionalNode? ((ConditionalNode)action).getResults():
			   action.getOutputs();
	}
	//
}

Back to the top