Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 408e3c76ac8b72f36cddd9fc5204837988e611a8 (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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
/*****************************************************************************
 * Copyright (c) 2013, 2017 CEA LIST, Christian W. Damus, 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:
 *  Laurent Wouters laurent.wouters@cea.fr - Initial API and implementation
 *  Christian W. Damus (CEA) - bug 422257
 *  Christian W. Damus - bugs 463156, 493030
 *  Thanh Liem PHAN (ALL4TEC) thanhliem.phan@all4tec.net - Bug 519409
 *****************************************************************************/
package org.eclipse.papyrus.infra.viewpoints.policy;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.papyrus.infra.architecture.ArchitectureDescriptionUtils;
import org.eclipse.papyrus.infra.architecture.ArchitectureDomainManager;
import org.eclipse.papyrus.infra.architecture.representation.ModelAutoCreate;
import org.eclipse.papyrus.infra.architecture.representation.ModelRule;
import org.eclipse.papyrus.infra.architecture.representation.OwningRule;
import org.eclipse.papyrus.infra.architecture.representation.PapyrusRepresentationKind;
import org.eclipse.papyrus.infra.core.architecture.RepresentationKind;
import org.eclipse.papyrus.infra.core.architecture.merged.MergedArchitectureContext;
import org.eclipse.papyrus.infra.core.architecture.merged.MergedArchitectureViewpoint;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.gmfdiag.representation.AssistantRule;
import org.eclipse.papyrus.infra.gmfdiag.representation.ChildRule;
import org.eclipse.papyrus.infra.gmfdiag.representation.PaletteRule;
import org.eclipse.papyrus.infra.gmfdiag.representation.PapyrusDiagram;

/**
 * The <code>PolicyChecker</code> enforces the viewpoints description as a policy in the user interface
 *
 * @author Laurent Wouters
 */
public class PolicyChecker {
	/**
	 * Default result when the current policy cannot determine whether an element can be added to a view
	 */
	private static final boolean DEFAULT_POLICY_UNKNOWN_CHILD = false;
	/**
	 * Default result when the current policy cannot determine whether a palette item should be exposed
	 */
	private static final boolean DEFAULT_POLICY_UNKNWON_PALETTE = true;
	/**
	 * Policy check result allowing an action
	 */
	private static final int RESULT_PERMIT = 1;
	/**
	 * Policy check result neither allowing not denying an action
	 */
	private static final int RESULT_UNKNOWN = 0;
	/**
	 * Policy check result denying an action
	 */
	private static final int RESULT_DENY = -1;

	/**
	 * @since 2.0
	 */
	public static PolicyChecker getFor(EObject object) {
		if (object.eResource() != null)
			return getFor(object.eResource());
		else
			return getFor(ArchitectureDomainManager.getInstance().getDefaultArchitectureContext());
	}

	/**
	 * @since 2.0
	 */
	public static PolicyChecker getFor(Resource resource) {
		if (resource.getResourceSet() != null)
			return getFor(resource.getResourceSet());
		else
			return getFor(ArchitectureDomainManager.getInstance().getDefaultArchitectureContext());
	}

	/**
	 * @since 2.0
	 */
	public static PolicyChecker getFor(ResourceSet resourceSet) {
		if (resourceSet instanceof ModelSet)
			return getFor((ModelSet) resourceSet);
		else
			return getFor(ArchitectureDomainManager.getInstance().getDefaultArchitectureContext());
	}

	/**
	 * @since 2.0
	 */
	public static PolicyChecker getFor(ModelSet modelSet) {
		Collection<MergedArchitectureViewpoint> viewpoints = new ArchitectureDescriptionUtils(modelSet).getArchitectureViewpoints();
		return getFor(viewpoints);
	}

	/**
	 * @since 2.0
	 */
	public static PolicyChecker getFor(MergedArchitectureContext context) {
		if (context == null) {
			return new PolicyChecker(Collections.emptyList());
		}
		return new PolicyChecker(context.getViewpoints());
	}

	/**
	 * @since 2.0
	 */
	public static PolicyChecker getFor(Collection<MergedArchitectureViewpoint> viewpoints) {
		return new PolicyChecker(viewpoints);
	}

	/**
	 * The current profile helper
	 */
	private IProfileHelper profileHelper;

	/**
	 * The architecture viewpoints
	 */
	private Collection<MergedArchitectureViewpoint> viewpoints;

	/**
	 * Gets the viewpoint enforced by this object
	 *
	 * @return The enforced viewpoint
	 * @since 2.0
	 */
	public Collection<MergedArchitectureViewpoint> getViewpoints() {
		return viewpoints;
	}

	/**
	 * Initializes this instance from the current preferences
	 */
	private PolicyChecker(Collection<MergedArchitectureViewpoint> viewpoints) {
		this.profileHelper = ProfileUtils.getProfileHelper();
		this.viewpoints = viewpoints;
	}

	/**
	 * Determines whether the given element can be the root of a view owned by the given object
	 *
	 * @param element
	 *            The possible root element
	 * @param owner
	 *            The possible owner
	 * @param prototype
	 *            The view prototype
	 * @return <code>true</code> if the element can be the root
	 */
	public boolean canHaveNewView(EObject element, EObject owner, ViewPrototype prototype) {
		if (prototype == null) {
			return false;
		}
		if (!matchesProfiles(prototype.representationKind, profileHelper.getAppliedProfiles(owner))) {
			return false;
		}
		if (!matchesProfiles(prototype.representationKind, profileHelper.getAppliedProfiles(element))) {
			return false;
		}
		if (!matchesCreationRoot(prototype.representationKind, element, profileHelper.getAppliedStereotypes(element), prototype.getViewCountOn(element))) {
			return false;
		}
		return true;
	}

	/**
	 * Gets the insertion data of the given element in the given diagram
	 *
	 * @param diagram
	 *            The diagram
	 * @param parent
	 *            The element to be edited
	 * @param child
	 *            The element to be added through the diagram
	 * @return The insertion data
	 */
	public ModelAddData getChildAddData(Diagram diagram, EObject parent, EObject child) {
		ViewPrototype prototype = ViewPrototype.get(diagram);
		if (prototype == null) {
			// This diagram is not in the current policy
			return new ModelAddData(false);
		}

		PapyrusDiagram config = (PapyrusDiagram) prototype.representationKind;
		Collection<EClass> stereotypes = profileHelper.getAppliedStereotypes(child);
		while (config != null) {
			for (ChildRule rule : config.getChildRules()) {
				int result = allows(rule, parent.eClass(), child.eClass(), stereotypes);
				if (result != RESULT_UNKNOWN) {
					return new ModelAddData((result == RESULT_PERMIT), rule.getInsertionPath());
				}
			}
			config = (PapyrusDiagram) config.getParent();
		}
		return new ModelAddData(DEFAULT_POLICY_UNKNOWN_CHILD);
	}

	/**
	 * Gets the insertion data of the given element in the given diagram
	 *
	 * @param diagram
	 *            The diagram
	 * @param parentType
	 *            The type of the element to be edited
	 * @param childType
	 *            The type of element to be added through the diagram
	 * @return The insertion data
	 */
	public ModelAddData getChildAddData(Diagram diagram, EClass parentType, EClass childType) {
		ViewPrototype prototype = ViewPrototype.get(diagram);
		if (prototype == null) {
			// This diagram is not in the current policy
			return new ModelAddData(false);
		}

		PapyrusDiagram config = (PapyrusDiagram) prototype.representationKind;
		while (config != null) {
			for (ChildRule rule : config.getChildRules()) {
				int result = allows(rule, parentType, childType, new ArrayList<EClass>(0));
				if (result != RESULT_UNKNOWN) {
					return new ModelAddData((result == RESULT_PERMIT), rule.getInsertionPath());
				}
			}
			config = (PapyrusDiagram) config.getParent();
		}
		return new ModelAddData(DEFAULT_POLICY_UNKNOWN_CHILD);
	}

	/**
	 * Determines whether the given diagram can have the palette element with the given entry ID
	 *
	 * @param diagram
	 *            The diagram
	 * @param entryID
	 *            A palette element entry ID
	 * @return <code>true</code> if the palette element is allowed
	 */
	public boolean isInPalette(Diagram diagram, String entryID) {
		ViewPrototype prototype = ViewPrototype.get(diagram);
		if (prototype == null) {
			// This diagram is not in the current policy
			return false;
		}

		PapyrusDiagram config = (PapyrusDiagram) prototype.representationKind;
		while (config != null) {
			for (PaletteRule rule : config.getPaletteRules()) {
				int result = allows(rule, entryID);
				if (result != RESULT_UNKNOWN) {
					return (result == RESULT_PERMIT);
				}
			}
			config = (PapyrusDiagram) config.getParent();
		}
		return DEFAULT_POLICY_UNKNWON_PALETTE;
	}

	/**
	 * Determines whether the given diagram can have a modeling assistant creating the specified element type.
	 *
	 * @param diagram
	 *            The diagram
	 * @param elementType
	 *            A modeling assistant element type
	 * @return whether the modeling assistant is allowed
	 */
	public boolean isInModelingAssistants(Diagram diagram, IElementType elementType) {
		ViewPrototype prototype = ViewPrototype.get(diagram);
		if (prototype == null) {
			// This diagram is not in the current policy
			return false;
		}

		PapyrusDiagram config = (PapyrusDiagram) prototype.representationKind;
		while (config != null) {
			for (AssistantRule rule : config.getAssistantRules()) {
				int result = allows(rule, elementType);
				if (result != RESULT_UNKNOWN) {
					return (result == RESULT_PERMIT);
				}
			}
			config = (PapyrusDiagram) config.getParent();
		}
		return DEFAULT_POLICY_UNKNWON_PALETTE;
	}

	/**
	 * Determines whether the given view description element is part of the current viewpoint
	 *
	 * @param config
	 *            A view description element
	 * @return <code>true</code> if the element is part of the current viewpoint
	 * @since 2.0
	 */
	public boolean isInViewpoint(PapyrusRepresentationKind kind) {
		return kind!=null && getViewpoints().stream()
				.flatMap(viewpoint -> viewpoint.getRepresentationKinds().stream())
		        .anyMatch(representationKinds -> representationKinds.getQualifiedName().equals(kind.getQualifiedName()));
	}

	/**
	 * Gets a collection of all the view prototypes allowed by the active policy
	 *
	 * @return A collection of view prototypes
	 */
	public Collection<ViewPrototype> getAllPrototypes() {
		Collection<ViewPrototype> result = new ArrayList<>();
		for (MergedArchitectureViewpoint viewpoint : getViewpoints()) {
			for (RepresentationKind kind : viewpoint.getRepresentationKinds()) {
				PapyrusRepresentationKind view = (PapyrusRepresentationKind) kind;
				ViewPrototype proto = ViewPrototype.get(view);
				if (null != proto && !result.contains(proto)) {
					result.add(proto);
				}
			}
		}
		return result;
	}

	/**
	 * Gets a list of the prototypes that can be instantiated with the given element as owner according to the policy
	 *
	 * @param element
	 *            The element to test
	 * @return A list of the prototypes that can be instantiated
	 */
	public Collection<ViewPrototype> getPrototypesFor(EObject element) {
		Collection<ViewPrototype> result = new LinkedHashSet<>();
		Collection<EPackage> profiles = profileHelper.getAppliedProfiles(element);
		Collection<EClass> stereotypes = profileHelper.getAppliedStereotypes(element);
		for (MergedArchitectureViewpoint viewpoint : getViewpoints()) {
			for (RepresentationKind kind : viewpoint.getRepresentationKinds()) {
				PapyrusRepresentationKind view = (PapyrusRepresentationKind) kind;
				if (!matchesProfiles(view, profiles)) {
					continue;
				}
				ViewPrototype proto = ViewPrototype.get(view);
				if (proto == null) {
					continue;
				}
				int count = proto.getOwnedViewCount(element);
				OwningRule rule = matchesCreationOwner(view, element, stereotypes, count);
				if (rule == null) {
					continue;
				}
				if (rule.getNewModelPath() != null && !rule.getNewModelPath().isEmpty()) {
					// Auto-created root => always OK
					result.add(proto);
				} else if (rule.getSelectDiagramRoot() != null && !rule.getSelectDiagramRoot().isEmpty()) {
					result.add(proto);
				} else {
					// We have to check if the owner can also be a root
					count = proto.getViewCountOn(element);
					if (matchesCreationRoot(view, element, stereotypes, count)) {
						// The owner can also be the root => OK
						result.add(proto);
					}
				}
			}
		}
		return result;
	}

	/**
	 * Gets the owning rule relevant for the given view prototype and owner
	 *
	 * @param prototype
	 *            The view prototype
	 * @param owner
	 *            The view's owner
	 * @return The owning rule
	 * @since 2.0
	 */
	public OwningRule getOwningRuleFor(ViewPrototype prototype, EObject owner) {
		Collection<EClass> stereotypes = profileHelper.getAppliedStereotypes(owner);
		int count = prototype.getOwnedViewCount(owner);
		return matchesCreationOwner(prototype.representationKind, owner, stereotypes, count);
	}

	/**
	 * Tries to match a view description from the given info
	 *
	 * @param implem
	 *            The implementation ID
	 * @param owner
	 *            The owner
	 * @param root
	 *            The root element
	 * @return The matching view, or <code>null</code> if none was found
	 * @since 2.0
	 */
	protected PapyrusRepresentationKind getRepresentationKindFrom(String implem, EObject owner, EObject root) {
		for (MergedArchitectureViewpoint viewpoint : getViewpoints()) {
			for (RepresentationKind kind : viewpoint.getRepresentationKinds()) {
				PapyrusRepresentationKind view = (PapyrusRepresentationKind) kind;
				if (matches(view, implem, owner, root)) {
					return view;
				}
			}
		}
		return null;
	}

	/**
	 * Tries to match a view description with the given info
	 *
	 * @param view
	 *            A view description
	 * @param implem
	 *            The implementation ID
	 * @param owner
	 *            The owner
	 * @param root
	 *            The root element
	 * @return <code>true</code> if the description matches
	 */
	private boolean matches(PapyrusRepresentationKind view, String implem, EObject owner, EObject root) {
		if (!view.getImplementationID().equals(implem)) {
			return false;
		}
		if (owner != null) {
			if (!matchesProfiles(view, profileHelper.getAppliedProfiles(owner))) {
				return false;
			}
			if (!matchesExistingOwner(view, owner, profileHelper.getAppliedStereotypes(owner))) {
				return false;
			}
		}
		if (root != null) {
			if (!matchesProfiles(view, profileHelper.getAppliedProfiles(root))) {
				return false;
			}
			if (!matchesExistingRoot(view, root, profileHelper.getAppliedStereotypes(root))) {
				return false;
			}
		}
		return true;
	}

	/**
	 * Checks whether the given set of profiles matches the requirements of the given view
	 *
	 * @param view
	 *            The view to check against
	 * @param profiles
	 *            The applied profiles
	 * @return <code>true</code> if the prototype is matching
	 */
	private boolean matchesProfiles(PapyrusRepresentationKind view, Collection<EPackage> profiles) {
		PapyrusRepresentationKind current = view;
		while (current != null) {
			for (EPackage profile : view.getLanguage().getProfiles()) {
				if (!profiles.contains(profile)) {
					return false;
				}
			}
			current = current.getParent();
		}
		return true;
	}

	/**
	 * Checks whether the given type of owning element with applied stereotypes is allowed for the given view
	 *
	 * @param view
	 *            The view to check against
	 * @param type
	 *            The owning element's type
	 * @param stereotypes
	 *            The stereotypes applied on the owning element
	 * @return <code>true</code> if the prototype is matching
	 */
	private boolean matchesExistingOwner(PapyrusRepresentationKind view, EObject owner, Collection<EClass> stereotypes) {
		PapyrusRepresentationKind current = view;
		while (current != null) {
			for (OwningRule rule : current.getOwningRules()) {
				int result = allows(rule, owner, stereotypes);
				if (result == RESULT_DENY) {
					return false;
				}
				if (result == RESULT_PERMIT) {
					return true;
				}
			}
			current = current.getParent();
		}
		return false;
	}

	/**
	 * Checks whether the given view can be owned by an element of the given type, applied with the given stereotypes if the cardinality is already the given amount
	 *
	 * @param view
	 *            The view to check against
	 * @param owner
	 *            The owning element
	 * @param stereotypes
	 *            The stereotypes applied on the owning element
	 * @param count
	 *            The current cardinality for the owning element
	 * @return The matching rule that allows the owner
	 */
	private OwningRule matchesCreationOwner(PapyrusRepresentationKind view, EObject owner, Collection<EClass> stereotypes, int count) {
		PapyrusRepresentationKind current = view;
		while (current != null) {
			for (OwningRule rule : current.getOwningRules()) {
				int allow = allows(rule, owner, stereotypes);
				if (allow == RESULT_DENY) {
					return null;
				}
				if (allow == RESULT_UNKNOWN) {
					continue;
				}
				int multiplicity = rule.getMultiplicity();
				if (multiplicity == -1 || count < multiplicity) {
					if (allows(rule, owner)) {
						return rule;
					}
				}
			}
			current = current.getParent();
		}
		return null;
	}

	/**
	 * Checks whether the given type of root element with applied stereotypes is allowed for the given view
	 *
	 * @param view
	 *            The view to check against
	 * @param root
	 *            The root element
	 * @param stereotypes
	 *            The stereotypes applied on the root element
	 * @return <code>true</code> if the prototype is matching
	 */
	private boolean matchesExistingRoot(PapyrusRepresentationKind view, EObject root, Collection<EClass> stereotypes) {
		PapyrusRepresentationKind current = view;
		while (current != null) {
			for (ModelRule rule : current.getModelRules()) {
				int result = allows(rule, root, stereotypes);
				if (result == RESULT_DENY) {
					return false;
				}
				if (result == RESULT_PERMIT) {
					return true;
				}
			}
			current = current.getParent();
		}
		return false;
	}

	/**
	 * Checks whether the given view can have the given root element of the given type, applied with the given stereotypes if the cardinality is already the given amount
	 *
	 * @param view
	 *            The view to check against
	 * @param root
	 *            The root element
	 * @param stereotypes
	 *            The stereotypes applied on the root element
	 * @param count
	 *            The current cardinality for the root element
	 * @return <code>true</code> if the prototype is matching
	 */
	private boolean matchesCreationRoot(PapyrusRepresentationKind view, EObject root, Collection<EClass> stereotypes, int count) {
		PapyrusRepresentationKind current = view;
		while (current != null) {
			for (ModelRule rule : current.getModelRules()) {
				int allow = allows(rule, root, stereotypes);
				if (allow == RESULT_DENY) {
					return false;
				}
				if (allow == RESULT_UNKNOWN) {
					continue;
				}
				int multiplicity = rule.getMultiplicity();
				if (multiplicity == -1 || count < multiplicity) {
					return true;
				}
			}
			current = current.getParent();
		}
		return false;
	}

	/**
	 * Checks an owner's type against a rule
	 *
	 * @param rule
	 *            The owning rule
	 * @param owner
	 *            The owner's type
	 * @param stereotypes
	 *            The stereotypes applied on <code>owner</code>
	 * @return The check result
	 */
	private int allows(OwningRule rule, EObject owner, Collection<EClass> stereotypes) {
		EClass c = rule.getElement();
		if (c == null || c.isSuperTypeOf(owner.eClass())) {
			// matching type => check the application of the required stereotypes
			for (EClass stereotype : rule.getStereotypes()) {
				if (!stereotypes.contains(stereotype)) {
					return RESULT_UNKNOWN;
				}
			}
			return rule.isPermit() ? RESULT_PERMIT : RESULT_DENY;
		} else {
			// type is not matching => unknown
			return RESULT_UNKNOWN;
		}
	}

	/**
	 * Checks whether the given owning rule will allow to derive an auto-created root
	 *
	 * @param rule
	 *            The owning rule
	 * @param owner
	 *            The owner
	 * @return <code>true</code> if it is possible
	 */
	private boolean allows(OwningRule rule, EObject owner) {
		List<ModelAutoCreate> modelAutoCreateList = rule.getNewModelPath();
		if (modelAutoCreateList == null || modelAutoCreateList.isEmpty()) {
			return true;
		}
		EObject current = owner;
		for (ModelAutoCreate elem : modelAutoCreateList) {
			EReference ref = elem.getFeature();
			if (ref.isMany()) {
				return true;
			}
			Object e = current.eGet(ref);
			if (e == null) {
				return true;
			}
			current = (EObject) e;
		}
		return false; // by default a rule is not allowed
	}

	/**
	 * Checks a root element's type against a rule
	 *
	 * @param rule
	 *            The modeling rule
	 * @param element
	 *            The root element's type
	 * @param stereotypes
	 *            The stereotypes applied on <code>element</code>
	 * @return The check result
	 */
	private int allows(ModelRule rule, EObject element, Collection<EClass> stereotypes) {
		EClass c = rule.getElement();
		if (c == null || c.isSuperTypeOf(element.eClass())) {
			// matching type => check the application of the required stereotypes
			// each stereotype required to match the model rule should be applied on the element
			for (EClass stereotype : rule.getStereotypes()) {
				if (!stereotypes.contains(stereotype)) {
					return RESULT_UNKNOWN;
				}
			}
			// check the rule
			boolean ruleMatches = RuleConstraintManager.getInstance().matchRule(rule, element);
			if (!ruleMatches) {
				return RESULT_UNKNOWN;
			}
			return rule.isPermit() ? RESULT_PERMIT : RESULT_DENY;
		} else {
			// type is not matching => unknown
			return RESULT_UNKNOWN;
		}
	}

	/**
	 * Checks a child insertion against a rule
	 *
	 * @param rule
	 *            The insertion rule
	 * @param origin
	 *            The root element's type
	 * @param element
	 *            The child's type
	 * @param stereotypes
	 *            The stereotypes applied on <code>element</code>
	 * @return The check result
	 */
	private int allows(ChildRule rule, EClass origin, EClass element, Collection<EClass> stereotypes) {
		EClass ce = rule.getElement();
		EClass co = rule.getOrigin();
		if ((ce == null || ce.isSuperTypeOf(element))
				&& (co == null || co.isSuperTypeOf(origin))) {
			// matching type => check the application of the required stereotypes
			for (EClass stereotype : rule.getStereotypes()) {
				if (!stereotypes.contains(stereotype)) {
					return RESULT_UNKNOWN;
				}
			}
			return rule.isPermit() ? RESULT_PERMIT : RESULT_DENY;
		} else {
			// type is not matching => unknown
			return RESULT_UNKNOWN;
		}
	}

	/**
	 * Checks a palette element against a rule
	 *
	 * @param rule
	 *            The palette rule
	 * @param toolID
	 *            The palette element's ID
	 * @return The check result
	 */
	private int allows(PaletteRule rule, String toolID) {
		String elem = rule.getElement();
		boolean applies = (elem == null);
		applies = applies || (elem.length() == 0);
		applies = applies || (elem != null && elem.equals(toolID));
		applies = applies || (elem != null && elem.endsWith("*") && toolID.startsWith(elem.substring(0, elem.length() - 1)));
		if (applies) {
			return rule.isPermit() ? RESULT_PERMIT : RESULT_DENY;
		}
		return RESULT_UNKNOWN;
	}

	/**
	 * Checks a modeling assistant element against a rule.
	 *
	 * @param rule
	 *            The assistant rule
	 * @param elementType
	 *            The modeling assistant element type
	 * @return The check result
	 */
	private int allows(AssistantRule rule, IElementType elementType) {
		return rule.matches(elementType)
				? (rule.isPermit() ? RESULT_PERMIT : RESULT_DENY)
						: RESULT_UNKNOWN;
	}
}

Back to the top