Skip to main content
summaryrefslogtreecommitdiffstats
blob: 56c3ee900df35820d6ee388f38b3169c0c4a9ffc (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
/*****************************************************************************
 * 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.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.Loci.LociL1.Locus;
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.ActivityEdge;

public class ActivityEdgeInstance {

	/*
	 * The activity edge of which this is an instance. [This is optional to
	 * allow for an implicit fork node execution to be connected to its action
	 * execution by an edge instance which does not have a corresponding node in
	 * the model.]
	 */
	public ActivityEdge edge;

	/*
	 * The activity group that contains this activity edge instance.
	 */
	public ActivityNodeActivationGroup group;

	/*
	 * The source of this activity edge instance. The node of the source must be
	 * the same as the source of the edge of this edge instance.
	 */
	public ActivityNodeActivation source;

	/*
	 * The target of this activity edge instance. The node of the target must be
	 * the same as the target of the edge of this edge instance.
	 */
	public ActivityNodeActivation target;

	public List<Offer> offers = new ArrayList<Offer>();

	public void sendOffer(List<Token> tokens) {
		// Send an offer from the source to the target.
		// Keep the offered tokens until taken by the target.
		// (Note that any one edge should only be handling either all object
		// tokens or all control tokens.)
		Offer offer = new Offer();
		for(int i = 0; i < tokens.size(); i++) {
			Token token = tokens.get(i);
			// Debug.println("[sendOffer] token value = " + token.get());
			offer.offeredTokens.add(token);
		}
		this.offers.add(offer);

		// Connection with the debug api
		if (this.group != null) { // TODO : Understand why group may be null
			Locus locus = this.group.getActivityExecution().locus ;
			if (locus.isInDebugMode) {
				if (locus.engine.isTerminated())
					return ;
				boolean animationMarkerNeedsToBeRemoved = false ;
				long date = 0 ;
				if (MokaConstants.MOKA_AUTOMATIC_ANIMATION) {
					date = System.currentTimeMillis() ;
					AnimationUtils.getInstance().addAnimationMarker(edge) ;
					animationMarkerNeedsToBeRemoved = true ;
				}
				MokaStackFrame stackFrame = FUMLPresentationUtils.getMokaStackFrame(this) ;
				//stackFrame.setModelElement(this.edge) ;
				stackFrame.setThread(locus.mainThread) ;
				stackFrame.setName(this.edge.getName()) ;
				StackFrameManager.getInstance().setStackFrame(this.group.getActivityExecution(), stackFrame) ;
				int reasonForSuspending = FUMLExecutionEngine.controlDelegate.shallSuspend(this.group.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.group.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(edge) ;
				}
				if (locus.engine.isTerminated())
					return ;
			}
		}

		// End: Connection with the debug api

		this.target.receiveOffer();
	}

	public Integer countOfferedValues() {
		// Return the number of values being offered in object tokens.
		int count = 0;
		List<Offer> offers = this.offers;
		for(int i = 0; i < offers.size(); i++) {
			count = count + offers.get(i).countOfferedValues();
		}
		return count;
	}

	public List<Token> takeOfferedTokens() {
		// Take all the offered tokens and return them.
		List<Token> tokens = new ArrayList<Token>();
		while(this.offers.size() > 0) {
			List<Token> offeredTokens = this.offers.get(0).getOfferedTokens();
			for(int i = 0; i < offeredTokens.size(); i++) {
				tokens.add(offeredTokens.get(i));
			}
			this.offers.remove(0);
		}
		return tokens;
	}

	public List<Token> takeOfferedTokens(Integer maxCount) {
		// Take all the offered tokens, up to the given maximum count of
		// non-null object tokens, and return them.
		List<Token> tokens = new ArrayList<Token>();
		int remainingCount = maxCount;
		while(this.offers.size() > 0 & remainingCount > 0) {
			Offer offer = this.offers.get(0);
			List<Token> offeredTokens = offer.getOfferedTokens();
			int count = offer.countOfferedValues();
			if(count <= remainingCount) {
				for(int i = 0; i < offeredTokens.size(); i++) {
					tokens.add(offeredTokens.get(i));
				}
				remainingCount = remainingCount - count;
				this.offers.remove(0);
			} else {
				for(int i = 0; i < remainingCount; i++) {
					Token token = offeredTokens.get(i);
					if(token.getValue() != null) {
						tokens.add(token);
					}
				}
				offer.removeOfferedValues(remainingCount);
				remainingCount = 0;
			}
		}
		return tokens;
	}

	public List<Token> getOfferedTokens() {
		// Get the offered tokens (after which the tokens will still be
		// offered).
		List<Token> tokens = new ArrayList<Token>();
		List<Offer> offers = this.offers;
		for(int i = 0; i < offers.size(); i++) {
			List<Token> offeredTokens = offers.get(i).getOfferedTokens();
			for(int j = 0; j < offeredTokens.size(); j++) {
				tokens.add(offeredTokens.get(j));
			}
		}
		return tokens;
	}

	public Boolean hasOffer() {
		// Return true if there are any pending offers.
		boolean hasTokens = false;
		int i = 1;
		while(!hasTokens & i <= this.offers.size()) {
			hasTokens = this.offers.get(i - 1).hasTokens();
			i = i + 1;
		}
		return hasTokens;
	}
}

Back to the top