Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1558d2f64a3d1ebbf0263e4e6b3b7984db5bc817 (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
/*****************************************************************************
 * Copyright (c) 2014, 2016 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:
 *  CEA LIST - Initial API and implementation
 *  Christian W. Damus (CEA) - bug 417409
 *  Christian W. Damus - bug 485220
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.viewpoints.policy;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.papyrus.infra.constraints.Activator;
import org.eclipse.papyrus.infra.constraints.ConstraintDescriptor;
import org.eclipse.papyrus.infra.constraints.constraints.Constraint;
import org.eclipse.papyrus.infra.constraints.runtime.ConstraintEngine;
import org.eclipse.papyrus.infra.constraints.runtime.ConstraintFactory;
import org.eclipse.papyrus.infra.constraints.runtime.DefaultConstraintEngine;
import org.eclipse.papyrus.infra.architecture.representation.ModelRule;

/**
 * {@link ConstraintEngine} for viewpoint {@link ModelRule}
 */
public class ModelRuleConstraintEngine extends DefaultConstraintEngine<ModelRule> {

	protected final Map<ModelRule, List<Constraint>> modelRule2Constraints = new HashMap<ModelRule, List<Constraint>>();

	/** singleton instance */
	private static ModelRuleConstraintEngine instance;

	/**
	 * Not instantiable by clients.
	 */
	public ModelRuleConstraintEngine() {
		super(ModelRule.class);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void refresh() {
		modelRule2Constraints.clear();
		fireConstraintsChanged();
	}

	public boolean matchesRule(ModelRule rule, EObject element) {
		Collection<EObject> selection = Collections.singletonList(element);
		List<Constraint> constraints = getConstraintsFor(rule);
		if (constraints == null || constraints.size() == 0) {
			return true;
		}
		for (Constraint c : constraints) {
			try {
				if (!(c.match(selection))) {
					return false;
				}
			} catch (Throwable ex) {
				Activator.log.error(ex);
			}
		}
		return true;
	}

	/**
	 * Initialize and return the list of constraints for a given rule
	 *
	 * @param rule
	 *            the rule to check
	 * @return the list of constraints for the specified rule or an empty list if no rule is registered for the rule
	 */
	protected List<Constraint> getConstraintsFor(ModelRule rule) {
		if (!modelRule2Constraints.containsKey(rule)) {
			List<Constraint> constraints = initializeConstraints(rule);
			modelRule2Constraints.put(rule, constraints);
		}
		return modelRule2Constraints.get(rule);
	}

	/**
	 * Initialize constraints for a given rule
	 *
	 * @param rule
	 *            the rule that contains the constraints
	 * @return the list of constraints for the given rule or an empty list if no constraints were found for the given rule
	 */
	protected List<Constraint> initializeConstraints(ModelRule rule) {
		List<ConstraintDescriptor> descriptors = rule.getConstraints();
		if (descriptors == null || descriptors.size() == 0) {
			return Collections.emptyList();
		}

		List<Constraint> constraints = new ArrayList<Constraint>();
		for (ConstraintDescriptor descriptor : descriptors) {
			try {
				Constraint constraint = ConstraintFactory.getInstance().createFromModel(descriptor);
				if (constraint != null) {
					constraints.add(constraint);
				}
			} catch (Throwable e) {
				Activator.log.error(e);
			}

		}
		return constraints;
	}

	public static synchronized ModelRuleConstraintEngine getInstance() {
		if (instance == null) {
			instance = new ModelRuleConstraintEngine();
		}
		return instance;
	}

}

Back to the top