Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bddf7eac0f95aeeec243fa5332be46e6825fba01 (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
/*****************************************************************************
 * 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:
 *  CEA LIST - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.textedit.state.xtext.validation;

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

import org.eclipse.emf.ecore.EObject;
import org.eclipse.papyrus.infra.gmfdiag.xtext.glue.edit.part.PopupXtextEditorHelper;
import org.eclipse.papyrus.uml.textedit.state.xtext.umlState.BehaviorKind;
import org.eclipse.papyrus.uml.textedit.state.xtext.umlState.DoRule;
import org.eclipse.papyrus.uml.textedit.state.xtext.umlState.EntryRule;
import org.eclipse.papyrus.uml.textedit.state.xtext.umlState.ExitRule;
import org.eclipse.papyrus.uml.textedit.state.xtext.umlState.StateRule;
import org.eclipse.papyrus.uml.textedit.state.xtext.umlState.SubmachineRule;
import org.eclipse.papyrus.uml.textedit.state.xtext.umlState.UmlStatePackage;
import org.eclipse.papyrus.uml.xtext.integration.core.ContextElementUtil;
import org.eclipse.uml2.uml.Activity;
import org.eclipse.uml2.uml.Behavior;
import org.eclipse.uml2.uml.OpaqueBehavior;
import org.eclipse.uml2.uml.StateMachine;
import org.eclipse.uml2.uml.Vertex;
import org.eclipse.xtext.validation.Check;
 

public class UmlStateJavaValidator extends AbstractUmlStateJavaValidator {

	/**
	 * First checks if the new name being attributed to the edited state is already used by another state in the region.
	 * Then, notifies (via warning) any of the potential Behavior deletion implied by the textual specification 
	 * (either DoActivity, Enty, or Exit behaviors)
	 * 
	 * @param stateRule
	 */
	@Check
	public void checkStateName (StateRule stateRule) {
		if (PopupXtextEditorHelper.context == null)
			return ;
		
		if (stateRule.getName() == null || stateRule.getName().equals(""))
			return ;
		
		//
		// first, checks if the new name of the State is already used by another state in the region
		//
		org.eclipse.uml2.uml.State editedState = (org.eclipse.uml2.uml.State) PopupXtextEditorHelper.context ;
		List<String> alreadyUsedNames = new ArrayList<String>() ;
		
		for (Vertex v : editedState.getContainer().getSubvertices()) {
			if (v instanceof org.eclipse.uml2.uml.State) {
				org.eclipse.uml2.uml.State s = (org.eclipse.uml2.uml.State) v ;
				if (s != editedState) {
					alreadyUsedNames.add("" + s.getName()) ;
				}
			}
		}
		
		String newName = "" + stateRule.getName() ;
		
		if (alreadyUsedNames.contains("" + newName))
			warning("Name " + newName + " is already used by another State in this Region", UmlStatePackage.eINSTANCE.getStateRule_Name()) ;
		
		
		// Check if ConnectionPointReference exist when one delete the submachine reference: not allowed!
		if((stateRule.getSubmachine() == null) && !editedState.getConnections().isEmpty()){
			error(getErrorMessageForSubmachineState(), UmlStatePackage.eINSTANCE.getStateRule_Submachine()) ;
		}
		//
		// Then, checks if the textual specification implies deletion of the DoActivity, Entry or Exit behavior
		// and raises warnings accordingly
		//
		
		boolean deletionOfDoActivity = editedState.getDoActivity()!=null && stateRule.getDo()==null ;
		boolean deletionOfExit = editedState.getExit()!=null && stateRule.getExit()==null ;
		boolean deletionOfEntry = editedState.getEntry()!=null && stateRule.getEntry()==null;
		
		if (deletionOfDoActivity) {
			warning(getBehaviorKindAsString(
						getBehaviorKind(editedState.getDoActivity())
					)
					+ " "
					+ editedState.getDoActivity().getName()
					+ " will be deleted", UmlStatePackage.eINSTANCE.getStateRule_Name()) ;
		}
		
		if (deletionOfExit) {
			warning(getBehaviorKindAsString(
						getBehaviorKind(editedState.getExit())
					)
					+ " "
					+ editedState.getExit().getName()
					+ " will be deleted", UmlStatePackage.eINSTANCE.getStateRule_Name()) ;
		}
		
		if (deletionOfEntry) {
			warning(getBehaviorKindAsString(
						getBehaviorKind(editedState.getEntry())
					)
					+ " "
					+ editedState.getEntry().getName()
					+ " will be deleted", UmlStatePackage.eINSTANCE.getStateRule_Name()) ;
		}
	}
	
	/**
	 * Notifies (via a Warning) the potential impact of changing the kind (i.e., Activity, StateMachine or OpaqueBehavior) 
	 * of the DoActivity behavior.
	 * 
	 * @param doRule
	 */
	@Check
	public void checkDoRule (DoRule doRule) {
		if (PopupXtextEditorHelper.context == null)
			return ;
		if (doRule.getKind() == null)
			return ;
		if (doRule.getBehaviorName() == null || doRule.getBehaviorName().equals(""))
			return ;
		
		org.eclipse.uml2.uml.State editedState = (org.eclipse.uml2.uml.State) PopupXtextEditorHelper.context ;
		BehaviorKind oldDoKind = getBehaviorKind(editedState.getDoActivity()) ;
		BehaviorKind newDoKind = doRule.getKind() ;
		if (oldDoKind != null) {
			if (oldDoKind != newDoKind) {
				warning("Changing the kind of " 
						+ doRule.getBehaviorName() 
						+ " from <<" 
						+ getBehaviorKindAsString(oldDoKind)
						+ ">> to <<"
						+ getBehaviorKindAsString(newDoKind)
						+ ">> will cause the deletion of "
						+ getBehaviorKindAsString(oldDoKind)
						+ " "
						+ doRule.getBehaviorName()
						+ ". Any changes made to "
						+ getBehaviorKindAsString(oldDoKind)
						+ " "
						+ doRule.getBehaviorName()
						+ " will be lost", UmlStatePackage.eINSTANCE.getDoRule_Kind()) ;
			}
		}
	}
	
	/**
	 * Notifies (via a Warning) the potential impact of changing the kind (i.e., Activity, StateMachine or OpaqueBehavior) 
	 * of the Entry behavior.
	 * 
	 * @param entryRule
	 */
	@Check
	public void checkEntryRule (EntryRule entryRule) {
		if (PopupXtextEditorHelper.context == null)
			return ;
		if (entryRule.getKind() == null)
			return ;
		if (entryRule.getBehaviorName() == null || entryRule.getBehaviorName().equals(""))
			return ;
		
		org.eclipse.uml2.uml.State editedState = (org.eclipse.uml2.uml.State) PopupXtextEditorHelper.context ;
		BehaviorKind oldDoKind = getBehaviorKind(editedState.getEntry()) ;
		BehaviorKind newDoKind = entryRule.getKind() ;
		if (oldDoKind != null) {
			if (oldDoKind != newDoKind) {
				warning("Changing the kind of " 
						+ entryRule.getBehaviorName() 
						+ " from <<" 
						+ getBehaviorKindAsString(oldDoKind)
						+ ">> to <<"
						+ getBehaviorKindAsString(newDoKind)
						+ ">> will cause the deletion of "
						+ getBehaviorKindAsString(oldDoKind)
						+ " "
						+ entryRule.getBehaviorName()
						+ ". Any changes made to "
						+ getBehaviorKindAsString(oldDoKind)
						+ " "
						+ entryRule.getBehaviorName()
						+ " will be lost", UmlStatePackage.eINSTANCE.getEntryRule_Kind()) ;
			}
		}
	}
	
	/**
	 * Notifies (via a Warning) the potential impact of changing the kind (i.e., Activity, StateMachine or OpaqueBehavior) 
	 * of the Entry behavior.
	 * 
	 * @param exitRule
	 */
	@Check
	public void checkExitRule (ExitRule exitRule) {
		if (PopupXtextEditorHelper.context == null)
			return ;
		if (exitRule.getKind() == null)
			return ;
		if (exitRule.getBehaviorName() == null || exitRule.getBehaviorName().equals(""))
			return ;
		
		org.eclipse.uml2.uml.State editedState = (org.eclipse.uml2.uml.State) PopupXtextEditorHelper.context ;
		BehaviorKind oldDoKind = getBehaviorKind(editedState.getExit()) ;
		BehaviorKind newDoKind = exitRule.getKind() ;
		if (oldDoKind != null) {
			if (oldDoKind != newDoKind) {
				warning("Changing the kind of " 
						+ exitRule.getBehaviorName() 
						+ " from <<" 
						+ getBehaviorKindAsString(oldDoKind)
						+ ">> to <<"
						+ getBehaviorKindAsString(newDoKind)
						+ ">> will cause the deletion of "
						+ getBehaviorKindAsString(oldDoKind)
						+ " "
						+ exitRule.getBehaviorName()
						+ ". Any changes made to "
						+ getBehaviorKindAsString(oldDoKind)
						+ " "
						+ exitRule.getBehaviorName()
						+ " will be lost", UmlStatePackage.eINSTANCE.getExitRule_Kind()) ;
			}
		}
	}
	
	@Check
	public void checkSubmachineRule(SubmachineRule rule) {
		EObject contextElement = ContextElementUtil.getContextElement(rule.eResource());
		if (contextElement == null || ! (contextElement instanceof org.eclipse.uml2.uml.State))
			return ;
		org.eclipse.uml2.uml.State contextState = (org.eclipse.uml2.uml.State)contextElement ;
		if (contextState.isOrthogonal()) {
			error(getErrorMessageForOrthogonalState(), UmlStatePackage.eINSTANCE.getSubmachineRule_Submachine()) ;
		}
		if (contextState.isComposite()) {
			error(getErrorMessageForCompositeState(), UmlStatePackage.eINSTANCE.getSubmachineRule_Submachine()) ;
		}
	}
	
	//*****************//
	// Utility methods //
	//*****************//
	
	private String getErrorMessageForOrthogonalState() {
		return "An orthogonal state cannot reference a submachine." ;
	}
	
	private String getErrorMessageForCompositeState() {
		return "A composite state cannot reference a submachine." ;
	}

	private String getErrorMessageForSubmachineState() {
		return "A simple state cannot have ConnectionPointReferences. You should delete them before removing the reference to the submachine." ;
	}

	private static BehaviorKind getBehaviorKind(Behavior behavior) {
		if (behavior == null)
			return null ;
		if (behavior instanceof Activity)
			return BehaviorKind.ACTIVITY ;
		if (behavior instanceof OpaqueBehavior)
			return BehaviorKind.OPAQUE_BEHAVIOR ;
		if (behavior instanceof StateMachine)
			return BehaviorKind.STATE_MACHINE ;
		return null ;
	}
	
	private static String getBehaviorKindAsString(BehaviorKind behaviorKind) {
		if (behaviorKind == BehaviorKind.ACTIVITY)
			return "Activity" ;
		if (behaviorKind == BehaviorKind.OPAQUE_BEHAVIOR)
			return "OpaqueBehavior" ;
		if (behaviorKind == BehaviorKind.STATE_MACHINE)
			return "StateMachine" ;
		return "" ;
	}
}

Back to the top