Skip to main content
summaryrefslogtreecommitdiffstats
blob: 25f6560b0112d6f5369811d75e955b31d7cf5041 (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
/*******************************************************************************
 * Copyright (c) 2016, 2017 Obeo.
 * 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:
 *    Obeo - initial API and implementation
 *******************************************************************************/
package org.eclipse.eef.core.api.controllers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.eef.EEFRuleAuditDescription;
import org.eclipse.eef.EEFValidationRuleDescription;
import org.eclipse.eef.EefPackage;
import org.eclipse.eef.core.api.EEFExpressionUtils;
import org.eclipse.eef.core.api.EditingContextAdapter;
import org.eclipse.eef.core.api.utils.EvalFactory;
import org.eclipse.eef.core.api.utils.EvalFactory.Eval;
import org.eclipse.eef.core.internal.controllers.InvalidValidationRuleResult;
import org.eclipse.eef.core.internal.controllers.ValidationRuleResult;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.sirius.common.interpreter.api.IInterpreter;
import org.eclipse.sirius.common.interpreter.api.IVariableManager;

/**
 * Superclass of all the controllers containing some utility methods.
 *
 * @author sbegaudeau
 */
public abstract class AbstractEEFController implements IEEFController {
	/**
	 * The interpreter.
	 */
	protected IInterpreter interpreter;

	/**
	 * The variable manager.
	 */
	protected IVariableManager variableManager;

	/**
	 * The editing context adapter.
	 */
	protected EditingContextAdapter editingContextAdapter;

	/**
	 * The consumer of the validation messages.
	 */
	private Consumer<List<IValidationRuleResult>> validationConsumer;

	/**
	 * The constructor.
	 *
	 * @param variableManager
	 *            The variable manager
	 * @param interpreter
	 *            The interpreter
	 * @param editingContextAdapter
	 *            The editing context adapter
	 */
	public AbstractEEFController(IVariableManager variableManager, IInterpreter interpreter, EditingContextAdapter editingContextAdapter) {
		this.variableManager = variableManager;
		this.interpreter = interpreter;
		this.editingContextAdapter = editingContextAdapter;
	}

	/**
	 * Returns a new {@link Eval} instance initialized with the {@link IInterpreter} and the {@link IVariableManager}.
	 *
	 * @return a new Eval.
	 */
	protected Eval<Object> newEval() {
		return EvalFactory.of(this.interpreter, this.variableManager);
	}

	/**
	 * {@inheritDoc}
	 *
	 * @see org.eclipse.eef.core.api.controllers.IEEFController#onValidation(java.util.function.Consumer)
	 */
	@Override
	public void onValidation(Consumer<List<IValidationRuleResult>> consumer) {
		this.validationConsumer = consumer;
	}

	/**
	 * {@inheritDoc}
	 *
	 * @see org.eclipse.eef.core.api.controllers.IEEFController#removeValidationConsumer()
	 */
	@Override
	public void removeValidationConsumer() {
		this.validationConsumer = null;
	}

	/**
	 * {@inheritDoc}
	 *
	 * @see org.eclipse.eef.core.api.controllers.IEEFController#refresh()
	 */
	@Override
	public void refresh() {
		List<IValidationRuleResult> validationRuleResults = this.getValidationRuleResults(this.getValidationRulesContainer(),
				this.getValidationRulesReference());
		if (this.validationConsumer != null) {
			this.validationConsumer.accept(validationRuleResults);
		}
	}

	/**
	 * Returns the EObject containing the validation rules.
	 *
	 * @return The EObject containing the validation rules
	 */
	protected abstract EObject getValidationRulesContainer();

	/**
	 * Returns the EReference containing the validation rules.
	 *
	 * @return The EReference containing the validation rules.
	 */
	protected abstract EReference getValidationRulesReference();

	/**
	 * Computes the result of the execution of all the validation rules.
	 *
	 * @param eObject
	 *            The EObject containing the validation rules
	 * @param validationRulesReference
	 *            The reference used to contain the validation rules (semantic or property based)
	 * @return The list of the validation rule results
	 */
	private List<IValidationRuleResult> getValidationRuleResults(EObject eObject, EReference validationRulesReference) {
		List<IValidationRuleResult> validationRuleResults = new ArrayList<IValidationRuleResult>();

		List<EEFValidationRuleDescription> descriptions = new ArrayList<EEFValidationRuleDescription>();
		Object validationRules = eObject.eGet(validationRulesReference);
		if (validationRules instanceof Iterable<?>) {
			for (Object validationRule : (Iterable<?>) validationRules) {
				if (validationRule instanceof EEFValidationRuleDescription) {
					descriptions.add((EEFValidationRuleDescription) validationRule);
				}
			}
		}

		EAttribute auditEAttribute = EefPackage.Literals.EEF_RULE_AUDIT_DESCRIPTION__AUDIT_EXPRESSION;
		EAttribute messageEAttribute = EefPackage.Literals.EEF_VALIDATION_RULE_DESCRIPTION__MESSAGE_EXPRESSION;

		for (EEFValidationRuleDescription validationRule : descriptions) {
			Object result = null;

			for (EEFRuleAuditDescription audit : validationRule.getAudits()) {
				String auditExpression = audit.getAuditExpression();
				result = this.newEval().logIfBlank(auditEAttribute).evaluate(auditExpression);

				if (!this.isValid(result)) {
					break;
				}
			}

			if (this.isValid(result)) {
				validationRuleResults.add(new ValidationRuleResult(validationRule));
			} else {
				Map<String, Object> variables = new HashMap<String, Object>();
				variables.putAll(this.variableManager.getVariables());
				variables.put(EEFExpressionUtils.AUDIT_RESULT, result);

				Eval<Object> eval = EvalFactory.of(this.interpreter, variables);
				String message = eval.logIfBlank(messageEAttribute).logIfInvalidType(String.class).evaluate(validationRule.getMessageExpression());

				validationRuleResults.add(new InvalidValidationRuleResult(validationRule, message, eval, this.editingContextAdapter,
						validationRule.getSeverity().getValue()));
			}
		}

		return validationRuleResults;
	}

	/**
	 * Indicates if the result of the evaluation of the audit expression is valid or not. A valid result is either:
	 * <ul>
	 * <li>A boolean with the value true</li>
	 * <li>An IStatus with a severity OK</li>
	 * <li>A diagnostic with a severity OK</li>
	 * </ul>
	 *
	 * @param result
	 *            The result of the evaluation of the audit expression
	 * @return <code>true</code> if the result is valid, <code>false</code> otherwise
	 */
	private boolean isValid(Object result) {
		boolean isValid = false;
		if (result instanceof Boolean) {
			isValid = ((Boolean) result).booleanValue();
		} else if (result instanceof IStatus) {
			isValid = ((IStatus) result).isOK();
		} else if (result instanceof Diagnostic) {
			isValid = ((Diagnostic) result).getSeverity() == Diagnostic.OK;
		}
		return isValid;
	}
}

Back to the top