Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ba72f5e13ea487c15cf9b08092f8441e1fb7dfb1 (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
/*****************************************************************************
 * 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
 *  	Mathieu Velten (Atos Origin) mathieu.velten@atosorigin.com - remove linked messages too
 *
 *****************************************************************************/
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.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.DestroyDependentsRequest;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.uml.diagram.common.helper.InteractionFragmentHelper;
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.Element;
import org.eclipse.uml2.uml.ExecutionOccurrenceSpecification;
import org.eclipse.uml2.uml.ExecutionSpecification;
import org.eclipse.uml2.uml.Interaction;
import org.eclipse.uml2.uml.InteractionFragment;
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.OccurrenceSpecification;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.UMLFactory;

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

	/**
	 * Create an execution Occurrence
	 *
	 * @param execution the execution that references the execution occurrences  always !=null
	 * @param lifeline the lifeLine that is covered by the execution occurrences ,always !=null
	 */
	public static ExecutionOccurrenceSpecification createOccurenceSpecification(ExecutionSpecification execution, Lifeline lifeline) {
		ExecutionOccurrenceSpecification occurrenceSpecification=UMLFactory.eINSTANCE.createExecutionOccurrenceSpecification();
		occurrenceSpecification.setCovered(lifeline);
		((Interaction)execution.getOwner()).getFragments().add(occurrenceSpecification);
		return occurrenceSpecification;
	}

	/**
	 * <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 ExecutionSpecification execution = (ExecutionSpecification) request.getElementToConfigure();
		IElementType elementType = request.getTypeToConfigure();
		return new ConfigureElementCommand(request) {

			@Override
			protected CommandResult doExecuteWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException {
				Object coveredParam = request.getParameters().get(SequenceRequestConstant.COVERED);

				Lifeline coveredLifeline = null;
				if (coveredParam instanceof Lifeline) {
					coveredLifeline = (Lifeline) coveredParam;
				}

				final ExecutionSpecification execution = (ExecutionSpecification) request.getElementToConfigure();

				Object replaceStart= request.getParameters().get(SequenceRequestConstant.REPLACE_EXECUTION_SPECIFICATION_START);
				if(replaceStart instanceof MessageOccurrenceSpecification ){
					execution.setStart((MessageOccurrenceSpecification)replaceStart);
				}
				else{
					//create Occurrence SpecStart
					ExecutionOccurrenceSpecification start=createOccurenceSpecification(execution, coveredLifeline);
					start.setName(execution.getName()+"Start");
					start.setExecution(execution);
					execution.setStart(start);
				}
				//add covered for the execution
				coveredLifeline.getCoveredBys().add(execution);
				execution.getCovereds().add(coveredLifeline);

				//create Occurrence SpecFinish
				Object replaceFinish= request.getParameters().get(SequenceRequestConstant.REPLACE_EXECUTION_SPECIFICATION_FINISH);
				if(replaceFinish instanceof MessageOccurrenceSpecification ){
					execution.setFinish((MessageOccurrenceSpecification)replaceFinish);
				}
				else{
					ExecutionOccurrenceSpecification finish=createOccurenceSpecification(execution, coveredLifeline);
					finish.setName(execution.getName()+"Finish");
					finish.setExecution(execution);
					execution.setFinish(finish);
				}
				return CommandResult.newOKCommandResult(execution);
			}

		};
	}

	/**
	 * <pre>
	 * Add a command to associated {@link OccurrenceSpecification} and {@link Message}.
	 * This command is only added if the start - finish referenced {@link OccurrenceSpecification} is not 
	 * referenced by another element or the start/finish references are of type {@link ExecutionOccurrenceSpecification}.
	 * </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>();

		ExecutionSpecification es = (ExecutionSpecification)request.getElementToDestroy();

		// Check whether start - finish referenced OccurrenceSpecification should be added to the dependents list
		OccurrenceSpecification osStart = es.getStart();
		if(shouldDestroyOccurrenceSpecification(es, osStart)) {
			dependentsToDestroy.add(osStart);
		}

		OccurrenceSpecification osFinish = es.getFinish();
		if(shouldDestroyOccurrenceSpecification(es, osFinish)) {
			dependentsToDestroy.add(osFinish);
		}

		Set<Lifeline> coveredLifelines = new HashSet<Lifeline>(es.getCovereds());

		// find initiating MOS of a synch message
		InteractionFragment previousIft = InteractionFragmentHelper.findPreviousFragment(osStart, es.getOwner());
		while(previousIft != null) {
			// keep the first ift with the same lifelines, and check it
			if(coveredLifelines.equals(new HashSet<Lifeline>(previousIft.getCovereds()))) {
				if(previousIft instanceof MessageOccurrenceSpecification) {
					Message msg = ((MessageOccurrenceSpecification)previousIft).getMessage();
					if(msg != null && MessageSort.SYNCH_CALL_LITERAL.equals(msg.getMessageSort())) {
						dependentsToDestroy.add(previousIft);
					}
				}
				break;
			}
			previousIft = InteractionFragmentHelper.findPreviousFragment(previousIft, es.getOwner());
		}

		// find MOS between the start and finish
		InteractionFragment fragment = osStart;
		while(fragment != null && !fragment.equals(osFinish)) {
			// remove MOS if it have the same covered lifelines as the ES
			if(fragment instanceof MessageOccurrenceSpecification && coveredLifelines.equals(new HashSet<Lifeline>(fragment.getCovereds()))) {
				dependentsToDestroy.add(fragment);
			}

			fragment = InteractionFragmentHelper.findNextFragment(fragment, es.getOwner());
		}

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

		return null;
	}

	/**
	 * <pre>
	 * Check that given {@link OccurrenceSpecification} should be destroyed along with {@link ExecutionSpecification} which references it.
	 * It should be destroyed in case:
	 * It is of type {@link ExecutionOccurrenceSpecification} (since the opposite reference 
	 *   'ExecutionOccurrenceSpecification::execution[1]' which designates given {@link ExecutionSpecification} is mandatory).
	 *   or
	 * It is not used by another element.
	 * </pre>
	 * 
	 * @param es
	 *        {@link ExecutionSpecification} which references {@link OccurrenceSpecification} (by means of #start/#finish references)
	 * @param os
	 *        start or finish {@link OccurrenceSpecification} which defines the duration of {@link ExecutionSpecification}
	 * @return true in case {@link OccurrenceSpecification} should be destroyed 
	 */
	private boolean shouldDestroyOccurrenceSpecification(ExecutionSpecification es, OccurrenceSpecification os) {
		return os instanceof ExecutionOccurrenceSpecification
				|| (os != null && EMFHelper.isOnlyUsage(os, es));
	}

}

Back to the top