Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1fc821792a7363fdd868af60c4439076b246c43c (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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/**
 * <copyright>
 *
 * Copyright (c) 2013 CEA LIST and others.
 * 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:
 *	E.D.Willink (CEA LIST) - initial API and implementation
 *  Obeo - Optimize View Input Refresh
 * </copyright>
 */
package org.eclipse.ocl.examples.emf.validation.validity.manager;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.util.BasicDiagnostic;
import org.eclipse.emf.common.util.BasicMonitor;
import org.eclipse.emf.common.util.Monitor;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.ENamedElement;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.ETypedElement;
import org.eclipse.emf.ecore.provider.EcoreItemProviderAdapterFactory;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.Diagnostician;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
import org.eclipse.emf.edit.provider.IItemLabelProvider;
import org.eclipse.emf.edit.provider.ReflectiveItemProviderAdapterFactory;
import org.eclipse.emf.edit.provider.resource.ResourceItemProviderAdapterFactory;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.ocl.examples.common.utils.TracingOption;
import org.eclipse.ocl.examples.domain.elements.Nameable;
import org.eclipse.ocl.examples.emf.validation.validity.ConstrainingNode;
import org.eclipse.ocl.examples.emf.validation.validity.Result;
import org.eclipse.ocl.examples.emf.validation.validity.ResultConstrainingNode;
import org.eclipse.ocl.examples.emf.validation.validity.ResultSet;
import org.eclipse.ocl.examples.emf.validation.validity.ResultValidatableNode;
import org.eclipse.ocl.examples.emf.validation.validity.RootNode;
import org.eclipse.ocl.examples.emf.validation.validity.ValidatableNode;
import org.eclipse.ocl.examples.emf.validation.validity.locator.ConstraintLocator;
import org.eclipse.ocl.examples.emf.validation.validity.plugin.ValidityPlugin;

public class ValidityManager
{	
	private static final @NonNull Map<String, List<ConstraintLocator.Descriptor>> constraintLocatorDescriptors = new HashMap<String, List<ConstraintLocator.Descriptor>>();
	private static final @NonNull Map<String, List<ConstraintLocator>> constraintLocators = new HashMap<String, List<ConstraintLocator>>();

	public static final @NonNull TracingOption ANALYZE_RESOURCE = new TracingOption(ValidityPlugin.PLUGIN_ID, "analyze/resource");
	public static final @NonNull TracingOption CREATE_CONSTRAINING = new TracingOption(ValidityPlugin.PLUGIN_ID, "create/constraining");
	public static final @NonNull TracingOption CREATE_RESULT = new TracingOption(ValidityPlugin.PLUGIN_ID, "create/result");
	public static final @NonNull TracingOption CREATE_VALIDATABLE = new TracingOption(ValidityPlugin.PLUGIN_ID, "create/validatable");
	public static final @NonNull TracingOption LOCATE_RESOURCE = new TracingOption(ValidityPlugin.PLUGIN_ID, "locate/resource");

	private final @NonNull LinkedHashSet<Resource> newResources = new LinkedHashSet<Resource>();

	private final @NonNull Set<Resource> oldResources = new HashSet<Resource>();
	
	/**
	 * This add the corresponding constraint locator if it exists in the list of
	 * defined descriptors.
	 * 
	 * @param nsURI
	 *            the nsURI of the validated resource
	 * @param constraintLocator
	 *            the corresponding constraint locator
	 */
	public static synchronized void addConstraintLocator(/*@NonNull*/ String nsURI, @NonNull ConstraintLocator.Descriptor constraintLocator) {
		List<ConstraintLocator.Descriptor> list = constraintLocatorDescriptors.get(nsURI);
		if (list == null) {
			list = new ArrayList<ConstraintLocator.Descriptor>();
			constraintLocatorDescriptors.put(nsURI, list);
		}
		if (!list.contains(constraintLocator)) {
			list.add(constraintLocator);
			constraintLocators.remove(nsURI);
		}
	}

	public static synchronized @Nullable ConstraintLocator getConstraintLocator(@NonNull EObject eObject) {
		return getConstraintLocator(eObject.eResource());
	}

	public static synchronized @Nullable ConstraintLocator getConstraintLocator(@Nullable Resource resource) {
		if (resource != null) {
			for (EObject eObject : resource.getContents()) {
				EClass eClass = eObject.eClass();
				if (eClass != null) {
					EPackage ePackage = eClass.getEPackage();
					if (ePackage != null) {
						String nsURI = ePackage.getNsURI();
						if (nsURI != null) {
							List<ConstraintLocator> list = getConstraintLocators(nsURI);
							if ((list != null) && (list.size() > 0)) {
								return list.get(0);
							}
						}
					}
				}
			}
		}
		return null;
	}

	public static synchronized @Nullable List<ConstraintLocator> getConstraintLocators(@NonNull String nsURI) {
		List<ConstraintLocator> list = constraintLocators.get(nsURI);
		if (list == null) {
			List<ConstraintLocator.Descriptor> descriptors = constraintLocatorDescriptors.get(nsURI);
			if (descriptors == null) {
				return null;
			}
			list = new ArrayList<ConstraintLocator>();
			constraintLocators.put(nsURI, list);
			for (ConstraintLocator.Descriptor descriptor : descriptors) {
				list.add(descriptor.getConstraintLocator());
			}
		}
		return list;
	}
	
	protected final @NonNull ComposedAdapterFactory adapterFactory;
	protected final @NonNull Map<ResultValidatableNode, Result> resultsMap = new HashMap<ResultValidatableNode, Result>();
	protected final @SuppressWarnings("null")@NonNull Map<Object, Object> context = Diagnostician.INSTANCE.createDefaultContext();
	private @Nullable ValidityModel model = null;
	protected @Nullable ResultSet lastResultSet = null;

	public ValidityManager() {
	    adapterFactory = new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE);
	    adapterFactory.addAdapterFactory(new ResourceItemProviderAdapterFactory());
	    adapterFactory.addAdapterFactory(new EcoreItemProviderAdapterFactory());
	    adapterFactory.addAdapterFactory(new ReflectiveItemProviderAdapterFactory());
	}

	public @NonNull Map<Object, Object> createDefaultContext() {
		return context;
	}

	@SuppressWarnings("null")
	public @NonNull BasicDiagnostic createDefaultDiagnostic(@NonNull EObject eObject) {
		return Diagnostician.INSTANCE.createDefaultDiagnostic(eObject);
	}

	protected @NonNull ValidityModel createModel(@NonNull Collection<Resource> newResources) {
		return new ValidityModel(this, newResources);
	}

	public /*synchronized*/ @Nullable ResultSet createResultSet(@Nullable IProgressMonitor monitor) {
		ValidityModel model2 = model;
		return model2 != null ? model2.createResultSet(monitor) : null;
	}

	public void dispose() {
		model = null;
		lastResultSet = null;
		resultsMap.clear();
	}
	
	protected @Nullable Set<ConstraintLocator> gatherConstraintLocators(@Nullable Set<ConstraintLocator> set, @NonNull List<ConstraintLocator> list) {
		if (set == null) {
			set = new HashSet<ConstraintLocator>();
		}
		set.addAll(list);
		return set;
	}

	public @NonNull AdapterFactory getAdapterFactory() {
		return adapterFactory;
	}

	/**
	 * Return the ConstrainingNode node for EObject creating any ConstrainingNodes that
	 * are required to ensure that the returned ConstrainingNode is installed in the root.
	 */
	public @NonNull ConstrainingNode getConstrainingNode(@NonNull EObject eObject) {
		ValidityModel model2 = model;
		if (model2 == null) {
			throw new IllegalStateException();
		}
		return model2.getConstrainingNode(eObject);
	}

	public @NonNull List<Result> getConstrainingNodeResults(@NonNull ConstrainingNode element) {
		List<Result> results = new ArrayList<Result>();
		if (element.getLabel().startsWith("EOperation")) {
			getAllConstrainingNodeResults(results, element);
		}
		else {
			getAllConstrainingNodeResults(results, element);
		}
		return results;
	}
	
	/**
	 * Returns the eObject uri
	 * 
	 * @param eObject
	 * @return the eObject uri
	 */
	public @NonNull ConstrainingURI getConstrainingURI(@NonNull EObject eObject) {
		ConstraintLocator constraintLocator = ValidityManager.getConstraintLocator(eObject);
		if (constraintLocator != null) {
			URI uri = constraintLocator.getURI(eObject);
			if (uri != null) {
				return new ConstrainingURI(trimDuplicateContextSuffix(uri));		// FIXME should not be needed
			}
		}
		URI uri = EcoreUtil.getURI(eObject);
		assert uri != null;
		return new ConstrainingURI(trimDuplicateContextSuffix(uri));		// FIXME should not be needed
	}

	/**
	 * It is possible to have multiple "identical" contexts defined in an OCL file :
	 * <p>
	 * <pre>
	 * context EClass
	 *   inv invariant1 : not name.oclIsUndefined()
	 * 
	 * context EClass
	 *   inv invariant2 : if interface then name.startsWith('I') else true endif;
	 * </pre>
	 * </p>
	 * 
	 * In such a case, the URI of the first will be <code>http://www.eclipse.org/emf/2002/Ecore#//EClass</code> while the URI of the second will be <code>http://www.eclipse.org/emf/2002/Ecore#//EClass.1</code>. We wish to "regroup" both invariants
	 * under the same context in the validity results.
	 * 
	 * @param uri
	 * @return
	 */
	private @NonNull URI trimDuplicateContextSuffix(URI uri) {
		String fragment = uri.fragment();
		// This should always be called on types, so we should be able to safely remove the trailing ".1" from the fragment
		if (fragment.matches(".*\\.[0-9]+$")){
			String trimmedFragment = fragment.replaceFirst("\\.[0-9]+$", "");
			URI trimmedURI = uri.trimFragment().appendFragment(trimmedFragment);
			assert trimmedURI != null;
			return trimmedURI;
		}
		return uri;
	}

	private void getAllConstrainingNodeResults(List<Result> results, @NonNull ConstrainingNode element) {
		if (element instanceof ResultConstrainingNode) {
			ResultValidatableNode resultValidatableNode = ((ResultConstrainingNode)element).getResultValidatableNode();
			Result result = resultsMap.get(resultValidatableNode);
			if (result != null) {
				results.add(result);
			}
		}
		else {
			for (@SuppressWarnings("null")@NonNull ConstrainingNode child : element.getChildren()) {
				getAllConstrainingNodeResults(results, child);
			}
		}
	}

	private void getAllValidatableNodeResults(List<Result> results, @NonNull ValidatableNode element) {
		if (element instanceof ResultValidatableNode) {
			ResultValidatableNode resultValidatableNode = (ResultValidatableNode)element;
			Result result = resultsMap.get(resultValidatableNode);
			if (result != null) {
				results.add(result);
			}
		}
		else {
			for (@SuppressWarnings("null")@NonNull ValidatableNode child : element.getChildren()) {
				getAllValidatableNodeResults(results, child);
			}
		}
	}

	public @NonNull String getConstrainingLabel(@NonNull EObject eObject) {
		StringBuilder s = new StringBuilder();
		if (eObject instanceof ENamedElement) {
			s.append(((ENamedElement)eObject).getName());
		}
		else if (eObject instanceof Nameable) {
			s.append(((Nameable)eObject).getName());
		}
		else {
		    IItemLabelProvider itemLabelProvider = (IItemLabelProvider)adapterFactory.adapt(eObject, IItemLabelProvider.class);
			String label = itemLabelProvider != null ? itemLabelProvider.getText(eObject) : eObject.toString();
			s.append(label != null ? label : "");
		}
/*		EClass eClass = eObject.eClass();
		if (eClass != null) {
			s.append(" : " + eClass.getName());
		} */
		EObject eContainer = eObject.eContainer();
		if (eContainer == null) {
			Resource eResource = eObject.eResource();
			if (eResource != null) {
				URI uri = eResource.getURI();
				if (uri != null) {
					s.append(" in " + uri);
				}
			}
		}
		@SuppressWarnings("null")@NonNull String string = s.toString();
		return string;
	}
	
	public @Nullable ValidityModel getModel() {
		return model;
	}

	public @Nullable RootNode getRootNode() {
		ValidityModel model2 = model;
		return model2 != null ? model2.getRootNode() : null;
	}

	/**
	 * Returns the eObject uri
	 * 
	 * @param eObject
	 * @return the eObject uri
	 */
	public @NonNull TypeURI getTypeURI(@NonNull EObject eObject) {
		String nsURI = null;
		for (EObject eContainer = eObject; eContainer != null; eContainer = eContainer.eContainer()) {
			if (eContainer instanceof EPackage) {
				EPackage ePackage = (EPackage) eContainer;
				nsURI = ePackage.getNsURI();
				if ((nsURI != null) && !"".equals(nsURI)) {
					Resource eResource = ePackage.eResource();
					if (eResource != null) {
						String fragment = eResource.getURIFragment(eObject);
						@SuppressWarnings("null")@NonNull URI uri = URI.createURI(nsURI).appendFragment(fragment);
						return new TypeURI(uri);
					}
				}
			}
		}
		@SuppressWarnings("null")@NonNull URI uri = EcoreUtil.getURI(eObject);
		return new TypeURI(uri);
	}

	public @NonNull String getValidatableLabel(@NonNull EObject eObject) {
		StringBuilder s = new StringBuilder();
		if (eObject instanceof ENamedElement) {
			s.append(((ENamedElement)eObject).getName());
		}
		else if (eObject instanceof Nameable) {
			s.append(((Nameable)eObject).getName());
		}
		else {
			IItemLabelProvider itemLabelProvider = (IItemLabelProvider)adapterFactory.adapt(eObject, IItemLabelProvider.class);
			String label = itemLabelProvider != null ? itemLabelProvider.getText(eObject) : eObject.toString();
			s.append(label != null ? label : "");
		}
		if (eObject instanceof ETypedElement) {
			EClassifier eType = ((ETypedElement)eObject).getEType();
			s.append(" : " + eType.getName());
		}
		else {
			EClass eClass = eObject.eClass();
			if (eClass != null) {
				s.append(" | " + eClass.getName());
			}
		}
		EObject eContainer = eObject.eContainer();
		if (eContainer == null) {
			Resource eResource = eObject.eResource();
			if (eResource != null) {
				URI uri = eResource.getURI();
				if (uri != null) {
					s.append(" in " + uri);
				}
			}
		}
		@SuppressWarnings("null")@NonNull String string = s.toString();
		return string;
	}

	public List<Result> getValidatableNodeResults(@NonNull ValidatableNode element) {
		List<Result> results = new ArrayList<Result>();
		getAllValidatableNodeResults(results, element);
		return results;
	}

	/**
	 * Returns the eObject uri
	 * 
	 * @param eObject
	 * @return the eObject uri
	 */
	public @NonNull ValidatableURI getValidatableURI(@NonNull EObject eObject) {
		@SuppressWarnings("null")@NonNull URI uri = EcoreUtil.getURI(eObject);
		return new ValidatableURI(uri);
	}

	public void setInput(Object newInput) {
		setInput(newInput, new BasicMonitor());
	}

	public void setInput(Object newInput, @NonNull Monitor monitor) {
		monitor.beginTask("Selective Validation", ValidityModel.WORK_FOR_ALL_SET_INPUT);
		monitor.setTaskName("Clean Up");
		ResourceSet selectedResourceSet = null;
		Resource selectedResource = null;
		EObject selectedObject = null;
		newResources.clear();
		
		if (newInput == null) {
			oldResources.clear();
			model = null;
			return;
		}
		
		if (newInput instanceof ResourceSet) {
			selectedResourceSet = (ResourceSet) newInput;
		} else if (newInput instanceof Resource) {
			selectedResource = (Resource) newInput;
			selectedResourceSet = selectedResource.getResourceSet();
			if (selectedResourceSet == null) {
				List<EObject> eContents = selectedResource.getContents();
				for (int j = 0; j < eContents.size(); j++) {		// Tolerate domain growth without a CME
					EObject eObject = eContents.get(j);
					EcoreUtil.resolveAll(eObject);
				}
				newResources.add(selectedResource);
			}
		} else if (newInput instanceof EObject) {
			selectedObject = (EObject) newInput;
			selectedResource = selectedObject.eResource();
			if (selectedResource != null) {
				selectedResourceSet = selectedResource.getResourceSet();
			}
		}

		if (selectedResourceSet != null) {
			List<Resource> selectedResources = selectedResourceSet.getResources();
			for (int i = 0; i < selectedResources.size(); i++) {	// Tolerate domain growth without a CME
				Resource eResource = selectedResources.get(i);
				List<EObject> eContents = eResource.getContents();
				for (int j = 0; j < eContents.size(); j++) {		// Tolerate domain growth without a CME
					EObject eObject = eContents.get(j);
					EcoreUtil.resolveAll(eObject);
				}
			}
			newResources.addAll(selectedResourceSet.getResources());
		}
		
		if (newResources.isEmpty()) {
			return;
		}
		
		if (!oldResources.isEmpty() && oldResources.equals(newResources)) {
			return;
		}

		monitor.worked(ValidityModel.WORK_FOR_CLEAN_UP);
		monitor.setTaskName("Creating model");
		ValidityModel model2 = model = createModel(newResources);
		monitor.worked(ValidityModel.WORK_FOR_CREATE_MODEL);
		model2.init(monitor);

		oldResources.clear();
		if (!monitor.isCanceled()) {
			oldResources.addAll(newResources);
		}
	}
}

Back to the top