Skip to content

Commit 531475f

Browse files
committed
add ALEDynamicExpressionEvaluator
This class offers services similar to AQLSiriusInterpreter but without the registration to the workspace listener If offers the possibility to parse and evaluate string expressions and return various kinds of objects It relies on the queryEnvironment of an existing ALEInterpreter. (ie. this is the responsibility of this ALEInterpreter to clean/dispose the query environment when not use anymore)
1 parent df15d52 commit 531475f

File tree

5 files changed

+268
-1
lines changed

5 files changed

+268
-1
lines changed

plugins/org.eclipse.emf.ecoretools.ale/META-INF/MANIFEST.MF

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ Require-Bundle: org.eclipse.acceleo.query,
1010
org.eclipse.emf.ecore.xmi,
1111
org.eclipse.emf.ecoretools.ale.core,
1212
org.eclipse.sirius.common,
13-
org.eclipse.core.resources
13+
org.eclipse.core.resources,
14+
com.google.guava;bundle-version="21.0.0",
15+
org.eclipse.osgi
1416
Bundle-Vendor: Inria/Obeo
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# ====================================================================
2+
# Copyright (c) 2019 Inria, Obeo
3+
# This program and the accompanying materials
4+
# are made available under the terms of the Eclipse Public License 2.0
5+
# which accompanies this distribution, and is available at
6+
# https://www.eclipse.org/legal/epl-2.0/
7+
#
8+
# SPDX-License-Identifier: EPL-2.0
9+
#
10+
# Contributors:
11+
# Obeo - initial API and implementation
12+
# ====================================================================
13+
14+
ALEInterpreter_errorWithExpression= An error has appeared during the evaluation of an expression:\n\
15+
Expression: {0}\n\
16+
Diagnostic: {1}\n\
17+
Target URI Fragment: {2}\n\
18+
Target: {3}\n\
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
package org.eclipse.emf.ecoretools.ale;
2+
3+
import java.text.MessageFormat;
4+
import java.util.Collection;
5+
import java.util.Collections;
6+
import java.util.HashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
import org.eclipse.acceleo.query.runtime.EvaluationResult;
11+
import org.eclipse.acceleo.query.runtime.IQueryBuilderEngine;
12+
import org.eclipse.acceleo.query.runtime.IQueryEnvironment;
13+
import org.eclipse.acceleo.query.runtime.IQueryEvaluationEngine;
14+
import org.eclipse.acceleo.query.runtime.QueryEvaluation;
15+
import org.eclipse.acceleo.query.runtime.QueryParsing;
16+
import org.eclipse.acceleo.query.runtime.IQueryBuilderEngine.AstResult;
17+
import org.eclipse.emf.common.util.BasicDiagnostic;
18+
import org.eclipse.emf.common.util.Diagnostic;
19+
import org.eclipse.emf.ecore.EObject;
20+
import org.eclipse.emf.ecore.util.EcoreUtil;
21+
import org.eclipse.emf.ecoretools.ale.ALEInterpreter;
22+
import org.eclipse.sirius.common.tools.api.interpreter.EvaluationException;
23+
import org.eclipse.sirius.common.tools.api.interpreter.IInterpreterWithDiagnostic.IEvaluationResult;
24+
25+
import com.google.common.collect.Iterables;
26+
import com.google.common.collect.Lists;
27+
28+
29+
/**
30+
* This class offers services similar to AQLSiriusInterpreter but without the registration to the workspace listener
31+
* If offers the possibility to parse and evaluate string expressions and return various kinds of objects
32+
* It relies on the queryEnvironment of an existing ALEInterpreter. (ie. this is the responsibility of this ALEInterpreter
33+
* to clean/dispose the query environment when not use anymore)
34+
*/
35+
public class ALEDynamicExpressionEvaluator {
36+
37+
ALEInterpreter aleInterpreter;
38+
39+
public ALEDynamicExpressionEvaluator(ALEInterpreter aleInterpreter) {
40+
super();
41+
this.aleInterpreter = aleInterpreter;
42+
}
43+
44+
public IEvaluationResult evaluateExpression(final EObject target, final String fullExpression)
45+
throws EvaluationException {
46+
// this.javaExtensions.reloadIfNeeded();
47+
String expression = fullExpression.replaceFirst("ale:", "");
48+
Map<String, Object> variables = new HashMap<>();
49+
variables.put("self", target); //$NON-NLS-1$
50+
51+
IQueryEnvironment queryEnv = aleInterpreter.getQueryEnvironment();
52+
if (queryEnv != null) {
53+
final IQueryBuilderEngine builder = QueryParsing.newBuilder(queryEnv);
54+
AstResult build = builder.build(expression);
55+
IQueryEvaluationEngine evaluationEngine = QueryEvaluation.newEngine(queryEnv);
56+
final EvaluationResult evalResult = evaluationEngine.eval(build, variables);
57+
58+
final BasicDiagnostic diagnostic = new BasicDiagnostic();
59+
if (Diagnostic.OK != build.getDiagnostic().getSeverity()) {
60+
diagnostic.merge(build.getDiagnostic());
61+
}
62+
if (Diagnostic.OK != evalResult.getDiagnostic().getSeverity()) {
63+
diagnostic.merge(evalResult.getDiagnostic());
64+
}
65+
66+
return new IEvaluationResult() {
67+
68+
@Override
69+
public Object getValue() {
70+
return evalResult.getResult();
71+
}
72+
73+
@Override
74+
public Diagnostic getDiagnostic() {
75+
List<Diagnostic> children = diagnostic.getChildren();
76+
if (children.size() == 1) {
77+
return children.get(0);
78+
} else {
79+
return diagnostic;
80+
}
81+
}
82+
};
83+
}
84+
return new IEvaluationResult() {
85+
@Override
86+
public Object getValue() {
87+
return null;
88+
}
89+
90+
@Override
91+
public Diagnostic getDiagnostic() {
92+
return new BasicDiagnostic();
93+
}
94+
95+
};
96+
97+
98+
}
99+
100+
101+
public Object evaluate(EObject target, String fullExpression) throws EvaluationException {
102+
IEvaluationResult evaluationResult = this.evaluateExpression(target, fullExpression);
103+
// We fire the exception to keep the old behavior
104+
Diagnostic diagnostic = evaluationResult.getDiagnostic();
105+
if (diagnostic.getSeverity() == Diagnostic.ERROR) {
106+
String uri = EcoreUtil.getURI(target).toString();
107+
String message = MessageFormat.format(Messages.ALEInterpreter_errorWithExpression, fullExpression, diagnostic.toString(), uri, target);
108+
throw new EvaluationException(message, diagnostic.getException());
109+
}
110+
return evaluationResult.getValue();
111+
}
112+
113+
/**
114+
* {@inheritDoc}
115+
*/
116+
public Collection<EObject> evaluateCollection(EObject context, String expression) throws EvaluationException {
117+
Object raw = evaluate(context, expression);
118+
final Collection<EObject> result;
119+
if (raw instanceof Collection<?>) {
120+
result = Lists.newArrayList(Iterables.filter((Collection<?>) raw, EObject.class));
121+
} else if (raw instanceof EObject) {
122+
result = Collections.singleton((EObject) raw);
123+
} else if (raw != null && raw.getClass().isArray()) {
124+
result = Lists.newArrayList(Iterables.filter(Lists.newArrayList((Object[]) raw), EObject.class));
125+
} else {
126+
result = Collections.emptySet();
127+
}
128+
return result;
129+
}
130+
131+
/**
132+
* {@inheritDoc}
133+
*/
134+
public boolean evaluateBoolean(EObject context, String expression) throws EvaluationException {
135+
Object raw = evaluate(context, expression);
136+
final boolean result;
137+
if (raw == null) {
138+
result = false;
139+
} else if (raw instanceof Boolean) {
140+
result = ((Boolean) raw).booleanValue();
141+
} else {
142+
String toString = raw.toString();
143+
if ("true".equalsIgnoreCase(toString)) { //$NON-NLS-1$
144+
result = true;
145+
} else if ("false".equalsIgnoreCase(toString)) { //$NON-NLS-1$
146+
result = false;
147+
} else {
148+
/*
149+
* raw is != null and its toString is neither true or false,
150+
* this happens when the user expect the condition to check that
151+
* a value is existing, then we consider any non null value
152+
* returns true and null returns false.
153+
*/
154+
result = true;
155+
}
156+
}
157+
return result;
158+
}
159+
160+
/**
161+
* {@inheritDoc}
162+
*/
163+
public EObject evaluateEObject(EObject context, String expression) throws EvaluationException {
164+
Object raw = evaluate(context, expression);
165+
if (raw instanceof EObject) {
166+
return (EObject) raw;
167+
} else {
168+
return null;
169+
}
170+
}
171+
172+
/**
173+
* {@inheritDoc}
174+
*/
175+
public String evaluateString(EObject context, String expression) throws EvaluationException {
176+
Object raw = evaluate(context, expression);
177+
if (raw != null) {
178+
return String.valueOf(raw);
179+
} else {
180+
return ""; //$NON-NLS-1$
181+
}
182+
}
183+
184+
/**
185+
* {@inheritDoc}
186+
*/
187+
public Integer evaluateInteger(EObject context, String expression) throws EvaluationException {
188+
Object raw = evaluate(context, expression);
189+
try {
190+
return Integer.parseInt(String.valueOf(raw));
191+
} catch (NumberFormatException e) {
192+
return Integer.valueOf(0);
193+
}
194+
}
195+
196+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2015 Obeo.
3+
* This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License 2.0
5+
* which accompanies this distribution, and is available at
6+
* https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* Obeo - initial API and implementation
12+
*******************************************************************************/
13+
package org.eclipse.emf.ecoretools.ale;
14+
15+
import org.eclipse.osgi.util.NLS;
16+
17+
/**
18+
* Helper class to obtains translated strings.
19+
*
20+
*/
21+
22+
23+
public final class Messages extends NLS {
24+
private static final String BASE_NAME = "org.eclipse.emf.ecoretools.ale.messages"; //$NON-NLS-1$
25+
26+
static {
27+
// load message values from bundle file
28+
NLS.initializeMessages(BASE_NAME, Messages.class);
29+
}
30+
31+
public static String ALEInterpreter_errorWithExpression;
32+
33+
34+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
###############################################################################
2+
# Copyright (c) 2019 Inria and others.
3+
# All rights reserved. This program and the accompanying materials
4+
# are made available under the terms of the Eclipse Public License v1.0
5+
# which accompanies this distribution, and is available at
6+
# http://www.eclipse.org/legal/epl-v10.html
7+
#
8+
# Contributors:
9+
# Inria - initial API and implementation
10+
###############################################################################
11+
12+
### Supplied messageses
13+
ALEInterpreter_errorWithExpression= An error has appeared during the evaluation of an expression:\n\
14+
Expression: {0}\n\
15+
Diagnostic: {1}\n\
16+
Target URI Fragment: {2}\n\
17+
Target: {3}\n\

0 commit comments

Comments
 (0)