Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 771c13a67d84b6a702d20b11051ba15f53ccda11 (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
/*****************************************************************************
 * Copyright (c) 2010 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:
 * 
 * 		Yann Tanguy (CEA LIST) yann.tanguy@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.service.types.helper.advice;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EStructuralFeature.Setting;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.common.core.command.UnexecutableCommand;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.emf.type.core.commands.ConfigureElementCommand;
import org.eclipse.gmf.runtime.emf.type.core.edithelper.AbstractEditHelperAdvice;
import org.eclipse.gmf.runtime.emf.type.core.requests.ConfigureRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateRelationshipRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyDependentsRequest;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.uml.diagram.common.util.MessageDirection;
import org.eclipse.papyrus.uml.service.types.Activator;
import org.eclipse.papyrus.uml.service.types.command.SetMessageSort;
import org.eclipse.papyrus.uml.service.types.element.UMLElementTypes;
import org.eclipse.papyrus.uml.service.types.utils.ElementUtil;
import org.eclipse.papyrus.uml.service.types.utils.SequenceRequestConstant;
import org.eclipse.uml2.uml.Association;
import org.eclipse.uml2.uml.BehaviorExecutionSpecification;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.DestructionOccurrenceSpecification;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.ExecutionSpecification;
import org.eclipse.uml2.uml.Gate;
import org.eclipse.uml2.uml.Interaction;
import org.eclipse.uml2.uml.Lifeline;
import org.eclipse.uml2.uml.Message;
import org.eclipse.uml2.uml.MessageEnd;
import org.eclipse.uml2.uml.MessageOccurrenceSpecification;
import org.eclipse.uml2.uml.MessageSort;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.UMLFactory;
import org.eclipse.uml2.uml.UMLPackage;

/**
 * Helper advice for all {@link Message} elements.
 */
public class MessageHelperAdvice extends AbstractEditHelperAdvice {




	/**
	 * This method provides the source type provided as {@link ConfigureRequest} parameter.
	 * 
	 * @return the target role
	 * @since 3.0
	 */
	protected Element getSource(ConfigureRequest req) {
		Element result = null;
		Object paramObject = req.getParameter(CreateRelationshipRequest.SOURCE);
		if (paramObject instanceof Element) {
			result = (Element) paramObject;
		}

		return result;
	}

	/**
	 * This method provides the target type provided as {@link ConfigureRequest} parameter.
	 * 
	 * @return the target role
	 * @since 3.0
	 */
	protected Element getTarget(ConfigureRequest req) {
		Element result = null;
		Object paramObject = req.getParameter(CreateRelationshipRequest.TARGET);
		if (paramObject instanceof Element) {
			result = (Element) paramObject;
		}

		return result;
	}
	/**
	 * <pre>
	 * {@inheritDoc}
	 * 
	 * Complete the {@link Association} creation by:
	 * 		adding its {@link Property} ends in the model
	 * 		adding the UML Nature on the {@link Association}.
	 * 
	 * </pre>
	 */
	@Override
	protected ICommand getBeforeConfigureCommand(final ConfigureRequest request) {


		final Message message = (Message) request.getElementToConfigure();
		final Element source = getSource(request);
		final Element target = getTarget(request);
		IElementType elementType = request.getTypeToConfigure();
		if ((source == null) || (target == null)) {
			return UnexecutableCommand.INSTANCE;
		}

		if  ((!(source instanceof Lifeline))&&(!(source instanceof Interaction))&&(!(source instanceof Gate))&&(!(source instanceof ExecutionSpecification))){
			return UnexecutableCommand.INSTANCE;
		}
		if ((!(target instanceof Lifeline))&&(!(target instanceof Interaction))&&(!(target instanceof Gate))&&(!(target instanceof ExecutionSpecification))){
			return UnexecutableCommand.INSTANCE;
		}
		return new ConfigureElementCommand(request) {

			@Override
			protected CommandResult doExecuteWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException {
				final Message message = (Message) request.getElementToConfigure();
				final Element source = getSource(request);
				final Element target = getTarget(request);
				MessageEnd previousSentEvent = (MessageEnd)request.getParameters().get(SequenceRequestConstant.PREVIOUS_EVENT);
				MessageEnd previousReceiveEvent = (MessageEnd)request.getParameters().get(SequenceRequestConstant.SECOND_PREVIOUS_EVENT);
				IElementType elementType = request.getTypeToConfigure();
				if (ElementUtil.isTypeOf(elementType, UMLElementTypes.COMPLETE_ASYNCH_CALL)) {
					createSenEvent(message, source,previousSentEvent);
					createReceiveEvent(message, target,previousReceiveEvent);
					message.setMessageSort(MessageSort.ASYNCH_CALL_LITERAL);
				} else if (ElementUtil.isTypeOf(elementType, UMLElementTypes.COMPLETE_ASYNCH_SIGNAL)) {
					createSenEvent(message, source,previousSentEvent);
					createReceiveEvent(message, target,previousReceiveEvent);
					message.setMessageSort(MessageSort.ASYNCH_SIGNAL_LITERAL);
				} else if (ElementUtil.isTypeOf(elementType, UMLElementTypes.COMPLETE_CREATE_MESSAGE)) {
					createSenEvent(message, source,previousSentEvent);
					createReceiveEvent(message, target,previousReceiveEvent);
					message.setMessageSort(MessageSort.CREATE_MESSAGE_LITERAL);
				} else if (ElementUtil.isTypeOf(elementType, UMLElementTypes.COMPLETE_DELETE_MESSAGE)) {
					createSenEvent(message, source,previousSentEvent);
					createDestroyReceiveEvent(message, target);
					message.setMessageSort(MessageSort.DELETE_MESSAGE_LITERAL);
				} else if (ElementUtil.isTypeOf(elementType, UMLElementTypes.COMPLETE_REPLY)) {
					createSenEvent(message, source,previousSentEvent);
					createReceiveEvent(message, target,previousReceiveEvent);
					message.setMessageSort(MessageSort.REPLY_LITERAL);
				} else if (ElementUtil.isTypeOf(elementType, UMLElementTypes.COMPLETE_SYNCH_CALL)) {
					createSenEvent(message, source,previousSentEvent);
					createReceiveEvent(message, target,previousReceiveEvent);
					message.setMessageSort(MessageSort.SYNCH_CALL_LITERAL);
				} else if (ElementUtil.isTypeOf(elementType, UMLElementTypes.FOUND_ASYNCH_CALL)) {
					createReceiveEvent(message, target,previousReceiveEvent);
					message.setMessageSort(MessageSort.ASYNCH_CALL_LITERAL);
				} else if (ElementUtil.isTypeOf(elementType, UMLElementTypes.FOUND_ASYNCH_SIGNAL)) {
					createReceiveEvent(message, target,previousReceiveEvent);
					message.setMessageSort(MessageSort.ASYNCH_SIGNAL_LITERAL);
				} else if (ElementUtil.isTypeOf(elementType, UMLElementTypes.FOUND_CREATE_MESSAGE)) {
					createReceiveEvent(message, target,previousReceiveEvent);
					message.setMessageSort(MessageSort.CREATE_MESSAGE_LITERAL);
				} else if (ElementUtil.isTypeOf(elementType, UMLElementTypes.FOUND_DELETE_MESSAGE)) {
					createDestroyReceiveEvent(message, target);
					message.setMessageSort(MessageSort.DELETE_MESSAGE_LITERAL);
				} else if (ElementUtil.isTypeOf(elementType, UMLElementTypes.FOUND_REPLY)) {
					createReceiveEvent(message, target,previousReceiveEvent);
					message.setMessageSort(MessageSort.REPLY_LITERAL);
				}  else if (ElementUtil.isTypeOf(elementType, UMLElementTypes.LOST_ASYNCH_CALL)) {
					createSenEvent(message, source,previousSentEvent);
					message.setMessageSort(MessageSort.ASYNCH_CALL_LITERAL);
				} else if (ElementUtil.isTypeOf(elementType, UMLElementTypes.LOST_ASYNCH_SIGNAL)) {
					createSenEvent(message, source,previousSentEvent);
					message.setMessageSort(MessageSort.ASYNCH_SIGNAL_LITERAL);
				} else if (ElementUtil.isTypeOf(elementType, UMLElementTypes.LOST_CREATE_MESSAGE)) {
					createSenEvent(message, source,previousSentEvent);
					message.setMessageSort(MessageSort.CREATE_MESSAGE_LITERAL);
				} else if (ElementUtil.isTypeOf(elementType, UMLElementTypes.LOST_DELETE_MESSAGE)) {
					createSenEvent(message, source,previousSentEvent);
					message.setMessageSort(MessageSort.DELETE_MESSAGE_LITERAL);
				} else if (ElementUtil.isTypeOf(elementType, UMLElementTypes.LOST_REPLY)) {
					createSenEvent(message, source,previousSentEvent);
					message.setMessageSort(MessageSort.REPLY_LITERAL);
				} 

				return CommandResult.newOKCommandResult(message);
			}

			/**
			 * @param message
			 * @param source
			 */
			private void createDestroyReceiveEvent(final Message message, final Element source) {
				// Create source and target ends
				MessageEnd sendEvent = createDestroyMessageEnd(message,(Lifeline)source);
				sendEvent.setName(message.getName()+"ReceiveDestroyEvent");
				message.setReceiveEvent(sendEvent);
			}


			/**
			 * @param message
			 * @param source
			 */
			private void createSenEvent(final Message message, final Element source, final MessageEnd previous) {
				if( source instanceof Gate){
					message.setSendEvent((Gate)source);
				}
				else if(source instanceof ExecutionSpecification){
					if(((ExecutionSpecification)source).getCovereds().size()>0){
						Lifeline  lifeline=((ExecutionSpecification)source).getCovereds().get(0);
						MessageEnd sendEvent = createMessageEnd(message,lifeline,previous);
						sendEvent.setName(message.getName()+"SendEvent");
						message.setSendEvent(sendEvent);
					}
				}
				else if( source instanceof Lifeline){
				// Create source and target ends
				MessageEnd sendEvent = createMessageEnd(message,(Lifeline)source,previous);
				sendEvent.setName(message.getName()+"SendEvent");
				message.setSendEvent(sendEvent);
				}
			}

			/**
			 * @param message
			 * @param target
			 */
			private void createReceiveEvent(final Message message, final Element target, final MessageEnd previous) {
				if( target instanceof Gate){
					message.setReceiveEvent((Gate)target);
				}
				else if(target instanceof ExecutionSpecification){
					if(((ExecutionSpecification)target).getCovereds().size()>0){
						Lifeline  lifeline=((ExecutionSpecification)target).getCovereds().get(0);
						MessageEnd receiveEvent = createMessageEnd(message,lifeline,previous);
						receiveEvent.setName(message.getName()+"ReceiveEvent");
						message.setReceiveEvent(receiveEvent);
					}
				}
				else if( target instanceof Lifeline){
				MessageEnd receiveEvent = createMessageEnd(message,(Lifeline) target, previous);
				receiveEvent.setName(message.getName()+"ReceiveEvent");
				message.setReceiveEvent(receiveEvent);
				}
			}
		};
	}

	/**
	 * Create a MessageEnd
	 *
	 * @param message the message that reference the message end  always !=null
	 * @param lifeline the lifeLine where is set the message end ,always !=null
	 * @since 3.0
	 */
	public static MessageEnd createMessageEnd(Message message, Lifeline lifeline, final MessageEnd previous) {
		MessageOccurrenceSpecification messageOccurrenceSpecification=UMLFactory.eINSTANCE.createMessageOccurrenceSpecification();
		if(previous==null){
			messageOccurrenceSpecification.setCovered(lifeline);
		}else{
			lifeline.getCoveredBys().add(lifeline.getCoveredBys().indexOf(previous)+1, messageOccurrenceSpecification);
		}
		messageOccurrenceSpecification.setMessage(message);
		messageOccurrenceSpecification.setMessage(message);
		((Interaction)message.getOwner()).getFragments().add(messageOccurrenceSpecification);
		return messageOccurrenceSpecification;
	}

	/**
	 * Create a MessageEnd
	 *
	 * @param message the message that reference the message end  always !=null
	 * @param lifeline the lifeLine where is set the message end ,always !=null
	 * @since 3.0
	 */
	public static MessageEnd createDestroyMessageEnd(Message message, Lifeline lifeline) {
		DestructionOccurrenceSpecification messageOccurrenceSpecification=UMLFactory.eINSTANCE.createDestructionOccurrenceSpecification();
		messageOccurrenceSpecification.setCovered(lifeline);
		messageOccurrenceSpecification.setMessage(message);
		((Interaction)message.getOwner()).getFragments().add(messageOccurrenceSpecification);
		return messageOccurrenceSpecification;
	}

	/**
	 * <pre>
	 * Add a command to destroy {@link MessageEnd} referenced by the {@link Message} 
	 * to delete.
	 * This command is only added if the send - receive event referenced is not 
	 * referenced by another element.
	 * </pre>
	 * 
	 * @see org.eclipse.gmf.runtime.emf.type.core.edithelper.AbstractEditHelperAdvice#getBeforeDestroyDependentsCommand(org.eclipse.gmf.runtime.emf.type.core.requests.DestroyDependentsRequest)
	 * 
	 * @param request
	 *        the request
	 * @return the command to execute before the edit helper work is done
	 */
	@Override
	protected ICommand getBeforeDestroyDependentsCommand(DestroyDependentsRequest request) {

		List<EObject> dependentsToDestroy = new ArrayList<EObject>();

		Message message = (Message)request.getElementToDestroy();

		// Add send - receive referenced MessageEnd to the dependents list
		// if they are not used by another element.
		MessageEnd sendEvent = message.getSendEvent();
		if((sendEvent != null) && (!isSharedEvent(sendEvent, message))) {
			dependentsToDestroy.add(sendEvent);
		}

		MessageEnd recvEvent = message.getReceiveEvent();
		if((recvEvent != null) && (!isSharedEvent(recvEvent, message))) {
			dependentsToDestroy.add(recvEvent);
		}

		// return command to destroy dependents MessageEnd 
		if(!dependentsToDestroy.isEmpty()) {
			return request.getDestroyDependentsCommand(dependentsToDestroy);
		}

		return null;
	}

	/**
	 * <pre>
	 * Test if the used element is referenced by other elements than the known
	 * referencer (except its container). It ignores references from an other meta-model.
	 * </pre>
	 *
	 * @param usedObject
	 *            the used object
	 * @param knownReferencer
	 *            the known referencer
	 * @return true if the known referencer is the only referencer.
	 * @since 3.0
	 */
	public static boolean isSharedEvent(MessageEnd usedObject, EObject knownReferencer) {
		EPackage mmPackage = usedObject.eClass().getEPackage();

		// Retrieve the list of elements referencing the usedObject.
		Set<EObject> crossReferences = new HashSet<EObject>();
		for (Setting setting : EMFHelper.getUsages(usedObject)) {
			EObject eObj = setting.getEObject();
			if( !setting.getEStructuralFeature().equals(UMLPackage.eINSTANCE.getLifeline_CoveredBy())){
				if (eObj.eClass().getEPackage().equals(mmPackage)) {
					crossReferences.add(eObj);
				}
			}
		}

		// Remove the container of used object.
		crossReferences.remove(usedObject.eContainer());
		// Remove the knownReferencer from the list of references.
		crossReferences.remove(knownReferencer);

		// If no referencer remains in the list, the known element is the only
		// usage.
		return !(crossReferences.isEmpty());
	}
}

Back to the top