summaryrefslogtreecommitdiffstats
path: root/rba.tool.editor/src/rba/tool/editor/validation/CircularContainmentValidationHelper.xtend
blob: ba4205af312f525b52d98d8ab078a1dd19d0d6d8 (plain)
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
package rba.tool.editor.validation

import com.google.common.collect.Maps
import com.google.common.collect.Sets
import com.google.inject.Inject
import java.util.LinkedHashSet
import java.util.Map
import java.util.Set
import org.eclipse.emf.ecore.EClass
import org.eclipse.emf.ecore.EObject
import org.eclipse.emf.ecore.EStructuralFeature
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.emf.ecore.resource.ResourceSet
import org.eclipse.xtext.EcoreUtil2
import org.eclipse.xtext.naming.QualifiedName
import org.eclipse.xtext.resource.IEObjectDescription
import org.eclipse.xtext.resource.IResourceServiceProvider
import org.eclipse.xtext.resource.impl.EObjectDescriptionLookUp
import org.eclipse.xtext.resource.impl.ResourceDescriptionsProvider
import org.eclipse.xtext.service.OperationCanceledManager
import org.eclipse.xtext.util.CancelIndicator
import org.eclipse.xtext.validation.ValidationMessageAcceptor
import org.eclipse.xtext.xbase.lib.Functions.Function1
import rba.core.AbstractAllocatable
import rba.core.AbstractContent
import rba.core.AllocatableSet
import rba.core.ContentSet
import rba.core.RBACorePackage
import rba.tool.editor.messages.Messages
import rba.tool.editor.model.manager.ResourceManager

class CircularContainmentValidationHelper {

	private final static Function1<? super IEObjectDescription, Boolean> containmentPredicate = [ d |
		d.EObjectOrProxy instanceof AbstractAllocatable || d.EObjectOrProxy instanceof AbstractContent
	];

	private final static Function1<? super EObject, Boolean> containerPredicate = [ eObject |
		eObject instanceof AllocatableSet || eObject instanceof ContentSet
	];

	@Inject
	private IResourceServiceProvider.Registry resourceServiceProviderRegistry = IResourceServiceProvider.Registry.INSTANCE;

	@Inject
	private ResourceDescriptionsProvider resourceDescriptionsProvider;

	@Inject
	private OperationCanceledManager operationCanceledManager = new OperationCanceledManager();

	private String CIRCULAR_CONTAINMENT_DETECTE = Messages.CIRCULAR_CONTAINMENT_DETECTE;

	def public void checkCircularContainment(Resource resource, CancelIndicator cancelIndicator, ValidationMessageAcceptor acceptor) {
		val resourceServiceProvider = resourceServiceProviderRegistry.getResourceServiceProvider(resource.getURI());
		if (resourceServiceProvider === null) {
			return;
		}

		val clusterToNames = Maps.newHashMap();
		val resourceDescriptions = resourceDescriptionsProvider.getResourceDescriptions(resource.resourceSet);
		var descriptionLookUp = new EObjectDescriptionLookUp(resourceDescriptions.exportedObjects.filter(containmentPredicate).toList);

		val descriptions = getAllObjectDescriptions(resource, descriptionLookUp);
		val currentIter = descriptions.iterator();
		if (!currentIter.hasNext()) {
			return;
		}

		initDescriptionForCircularContainment(resource.resourceSet, descriptionLookUp, clusterToNames, acceptor);

		while (currentIter.hasNext()) {
			val objectDescription = currentIter.next();
			checkDescriptionForCircularContainment(resource.resourceSet, descriptionLookUp, objectDescription, clusterToNames, acceptor);
			operationCanceledManager.checkCanceled(cancelIndicator);
		}
	}

	def private Iterable<IEObjectDescription> getAllObjectDescriptions(Resource resource, EObjectDescriptionLookUp descriptionLookUp) {
		val allObjectDescriptions = resource.allContents.toIterable.filter(containerPredicate).map(o|findEObjectDescription(o, descriptionLookUp));
		return allObjectDescriptions;
	}

	def protected void initDescriptionForCircularContainment(ResourceSet resourceSet, EObjectDescriptionLookUp descriptionLookUp,
		Map<EClass, Map<QualifiedName, Set<IEObjectDescription>>> clusterTypeToName, ValidationMessageAcceptor acceptor) {
		initDescriptionForAllocatableSet(resourceSet, descriptionLookUp, clusterTypeToName, acceptor);
		initDescriptionForContentSet(resourceSet, descriptionLookUp, clusterTypeToName, acceptor);
	}

	def protected void initDescriptionForAllocatableSet(ResourceSet resourceSet, EObjectDescriptionLookUp descriptionLookUp,
		Map<EClass, Map<QualifiedName, Set<IEObjectDescription>>> clusterTypeToName, ValidationMessageAcceptor acceptor) {
		val allocatableSetDescriptionsMap = Maps.newHashMap();
		clusterTypeToName.put(RBACorePackage.Literals.ALLOCATABLE_SET, allocatableSetDescriptionsMap);

		ResourceManager.INSTANCE.getRbaAreaSets(resourceSet).forEach [ areaSet |
			if (!areaSet.target.isEmpty) {
				val IEObjectDescription eObjectDescription = findEObjectDescription(areaSet, descriptionLookUp);
				if (eObjectDescription !== null) {
					val targets = Sets.newLinkedHashSet();
					var IEObjectDescription targetDescription;
					for (target : areaSet.target) {
						targetDescription = findEObjectDescription(target, descriptionLookUp);
						if (targetDescription !== null) {
							targets.add(targetDescription);
						}
					}
					allocatableSetDescriptionsMap.put(eObjectDescription.qualifiedName, targets);
				}
			}
		];
		ResourceManager.INSTANCE.getRbaZoneSets(resourceSet).forEach [ zoneSet |
			if (!zoneSet.target.isEmpty) {
				val IEObjectDescription eObjectDescription = findEObjectDescription(zoneSet, descriptionLookUp);
				if (eObjectDescription !== null) {
					val targets = Sets.newLinkedHashSet();
					var IEObjectDescription targetDescription;
					for (target : zoneSet.target) {
						targetDescription = findEObjectDescription(target, descriptionLookUp);
						if (targetDescription !== null) {
							targets.add(targetDescription);
						}
					}
					allocatableSetDescriptionsMap.put(eObjectDescription.qualifiedName, targets);
				}
			}
		];
	}

	def protected void initDescriptionForContentSet(ResourceSet resourceSet, EObjectDescriptionLookUp descriptionLookUp,
		Map<EClass, Map<QualifiedName, Set<IEObjectDescription>>> clusterTypeToName, ValidationMessageAcceptor acceptor) {
		val allocatableSetDescriptionsMap = Maps.newHashMap();
		clusterTypeToName.put(RBACorePackage.Literals.CONTENT_SET, allocatableSetDescriptionsMap);

		ResourceManager.INSTANCE.getRbaViewContentSets(resourceSet).forEach [ viewContentSet |
			if (!viewContentSet.target.isEmpty) {
				val IEObjectDescription eObjectDescription = findEObjectDescription(viewContentSet, descriptionLookUp);
				if (eObjectDescription !== null) {
					val targets = Sets.newLinkedHashSet();
					var IEObjectDescription targetDescription;
					for (target : viewContentSet.target) {
						targetDescription = findEObjectDescription(target, descriptionLookUp);
						if (targetDescription !== null) {
							targets.add(targetDescription);
						}
					}
					allocatableSetDescriptionsMap.put(eObjectDescription.qualifiedName, targets);
				}
			}
		];
		ResourceManager.INSTANCE.getRbaSoundContentSets(resourceSet).forEach [ soundContentSet |
			if (!soundContentSet.target.isEmpty) {
				val IEObjectDescription eObjectDescription = findEObjectDescription(soundContentSet, descriptionLookUp);
				if (eObjectDescription !== null) {
					val targets = Sets.newLinkedHashSet();
					var IEObjectDescription targetDescription;
					for (target : soundContentSet.target) {
						targetDescription = findEObjectDescription(target, descriptionLookUp);
						if (targetDescription !== null) {
							targets.add(targetDescription);
						}
					}
					allocatableSetDescriptionsMap.put(eObjectDescription.qualifiedName, targets);
				}
			}
		];
	}

	def private IEObjectDescription findEObjectDescription(EObject eObject, EObjectDescriptionLookUp descriptionLookUp) {
		val candidates = descriptionLookUp.getExportedObjectsByObject(eObject);
		if (!candidates.isEmpty) {
			return candidates.get(0);
		}
		return null;
	}

	def protected void checkDescriptionForCircularContainment(ResourceSet resourceSet, EObjectDescriptionLookUp descriptionLookUp, IEObjectDescription description,
		Map<EClass, Map<QualifiedName, Set<IEObjectDescription>>> clusterTypeToName, ValidationMessageAcceptor acceptor) {
		val object = description.getEObjectOrProxy();
		val eClass = object.eClass();
		val qualifiedName = description.getName();
		val clusterType = getAssociatedClusterType(eClass);
		if (clusterType === null) {
			return;
		}

		val nameToDescription = clusterTypeToName.get(clusterType);
		if (nameToDescription.containsKey(qualifiedName)) {
			val targetsDescription = nameToDescription.get(qualifiedName);

			val filtedTargets = targetsDescription.filter(d|!description.qualifiedName.equals(d.qualifiedName) && nameToDescription.containsKey(d.qualifiedName));
			for (target : filtedTargets) {
				val index = filtedTargets.toList.indexOf(target);
				val cycleConsistsElement = Sets.newLinkedHashSet(#[target.qualifiedName.toString]);
				checkCircularContainment(resourceSet, description, target, nameToDescription, cycleConsistsElement, acceptor, index);
			}
		}
	}

	def protected void checkCircularContainment(ResourceSet resourceSet, IEObjectDescription rootDescription, IEObjectDescription description,
		Map<QualifiedName, Set<IEObjectDescription>> nameToDescription, LinkedHashSet<String> cycleConsistsElement, ValidationMessageAcceptor acceptor, int index) {
		val targetsDescription = nameToDescription.get(description.qualifiedName);
		val filtedTargets = targetsDescription.filter(d|!description.qualifiedName.equals(d.qualifiedName) && nameToDescription.containsKey(d.qualifiedName));
		if (filtedTargets === null) {
			return;
		}

		for (target : filtedTargets.filter(d|d.qualifiedName.equals(rootDescription.qualifiedName))) {
			createCircularContainmentError(resourceSet, rootDescription, index, acceptor, appendCycleConsists(cycleConsistsElement, target.qualifiedName.toString()));
		}

		for (target : filtedTargets.filter(d|!d.qualifiedName.equals(rootDescription.qualifiedName))) {
			val subCycleConsistsElement = Sets.newLinkedHashSet(cycleConsistsElement);
			if (!subCycleConsistsElement.add(target.qualifiedName.toString())) {
				return;
			}

			checkCircularContainment(resourceSet, rootDescription, target, nameToDescription, subCycleConsistsElement, acceptor, index);
		}
	}

	def protected void createCircularContainmentError(ResourceSet resourceSet, IEObjectDescription description, int index, ValidationMessageAcceptor acceptor,
		LinkedHashSet<String> cycleConsists) {
		var object = description.getEObjectOrProxy();
		if (object.eIsProxy) {
			object = EcoreUtil2.resolve(object, resourceSet);
			if (object.eIsProxy) {
				return;
			}
		}

		val feature = getContainmentFeature(object);
		if (feature !== null) {
			acceptor.acceptError(getCircularContainmentErrorMessage(description, cycleConsists), object, feature, index, getErrorCode());
		}
	}

	def private LinkedHashSet<String> appendCycleConsists(LinkedHashSet<String> cycleConsists, String... args) {
		val LinkedHashSet<String> newCycleConsists = Sets.newLinkedHashSet(cycleConsists);
		if (args !== null && args.length > 0) {
			newCycleConsists.addAll(args);
		}
		return newCycleConsists;
	}

	def String getCircularContainmentErrorMessage(IEObjectDescription description, LinkedHashSet<String> cycleConsists) {
		val qualifiedName = description.getQualifiedName().toString();
		val shortName = String.valueOf(if (qualifiedName !== null && qualifiedName !== "") qualifiedName else "<unnamed>");
		return String.format(CIRCULAR_CONTAINMENT_DETECTE, shortName, cycleConsists.toString());
	}

	def protected boolean isContainerInformationHelpful(IEObjectDescription description, EObject container, String containerTypeLabel, EStructuralFeature containerNameFeature) {
		return containerTypeLabel !== null && containerNameFeature !== null;
	}

	def protected boolean isContainerInformationHelpful(IEObjectDescription description, String shortName) {
		return true;
	}

	def protected EObject getContainerForErrorMessage(EObject object) {
		return object.eContainer();
	}

	def protected String getTypeLabel(EClass eClass) {
		return eClass.getName();
	}

	def protected EStructuralFeature getContainmentFeature(EObject object) {
		switch (object) {
			AllocatableSet:
				return RBACorePackage.Literals.ALLOCATABLE_SET__TARGET
			ContentSet:
				return RBACorePackage.Literals.CONTENT_SET__TARGET
			default:
				return null
		}
	}

	def protected EClass getAssociatedClusterType(EClass eClass) {
		val superTypes = eClass.getESuperTypes();
		if (superTypes.isEmpty()) {
			return null;
		}
		if (superTypes.contains(RBACorePackage.Literals.ALLOCATABLE_SET)) {
			return RBACorePackage.Literals.ALLOCATABLE_SET;
		}
		if (superTypes.contains(RBACorePackage.Literals.CONTENT_SET)) {
			return RBACorePackage.Literals.CONTENT_SET;
		}
		return null;
	}

	def protected String getErrorCode() {
		return null;
	}
}