blob: 542e9d4895e6366afb7f16ddc98d50c45d58dfd4 (
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
|
/*******************************************************************************
* Copyright (c) 2016 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.tests.internal;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import org.eclipse.acceleo.query.runtime.EvaluationResult;
import org.eclipse.acceleo.query.runtime.IQueryBuilderEngine;
import org.eclipse.acceleo.query.runtime.IQueryBuilderEngine.AstResult;
import org.eclipse.acceleo.query.runtime.IQueryEnvironment;
import org.eclipse.acceleo.query.runtime.IQueryEvaluationEngine;
import org.eclipse.acceleo.query.runtime.Query;
import org.eclipse.acceleo.query.runtime.QueryEvaluation;
import org.eclipse.acceleo.query.runtime.QueryParsing;
import org.eclipse.acceleo.query.runtime.ServiceUtils;
import org.eclipse.emf.common.util.BasicDiagnostic;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.ecore.impl.EStringToStringMapEntryImpl;
import org.eclipse.sirius.common.interpreter.api.IEvaluationResult;
import org.eclipse.sirius.common.interpreter.api.IInterpreter;
/**
* An AQL interpreter used for tests.
*
* @author sbegaudeau
*/
public class AQLInterpreter implements IInterpreter {
/**
* The prefix used by AQL expressions.
*/
private static final String AQL_PREFIX = "aql:"; //$NON-NLS-1$
/**
* The cache of the expressions parsed.
*/
private LoadingCache<String, AstResult> parsedExpressions;
/**
* The query environment.
*/
private IQueryEnvironment queryEnvironment;
/**
* The constructor.
*/
public AQLInterpreter() {
this.queryEnvironment = Query.newEnvironmentWithDefaultServices(null);
this.queryEnvironment.registerEPackage(EcorePackage.eINSTANCE);
ServiceUtils.registerServices(this.queryEnvironment, ServiceUtils.getServices(this.queryEnvironment, EEFTestServices.class));
this.queryEnvironment.registerCustomClassMapping(EcorePackage.eINSTANCE.getEStringToStringMapEntry(), EStringToStringMapEntryImpl.class);
initExpressionsCache();
}
/**
* Initializes the cache of the expressions.
*/
private void initExpressionsCache() {
final IQueryBuilderEngine builder = QueryParsing.newBuilder(queryEnvironment);
final int maxCacheSize = 500;
this.parsedExpressions = CacheBuilder.newBuilder().maximumSize(maxCacheSize).build(new CacheLoader<String, AstResult>() {
@Override
public AstResult load(String key) throws Exception {
return builder.build(key);
}
});
}
/**
* {@inheritDoc}
*
* @see org.eclipse.sirius.common.interpreter.api.IInterpreter#evaluateExpression(java.util.Map, java.lang.String)
*/
@Override
public IEvaluationResult evaluateExpression(Map<String, Object> variables, String expressionBody) {
String expression = expressionBody;
if (expression.startsWith(AQL_PREFIX)) {
expression = expression.substring(AQL_PREFIX.length());
}
try {
AstResult build = parsedExpressions.get(expression);
IQueryEvaluationEngine evaluationEngine = QueryEvaluation.newEngine(queryEnvironment);
final EvaluationResult evalResult = evaluationEngine.eval(build, variables);
final BasicDiagnostic diagnostic = new BasicDiagnostic();
if (Diagnostic.OK != build.getDiagnostic().getSeverity()) {
diagnostic.merge(build.getDiagnostic());
}
if (Diagnostic.OK != evalResult.getDiagnostic().getSeverity()) {
diagnostic.merge(evalResult.getDiagnostic());
}
return org.eclipse.sirius.common.interpreter.api.EvaluationResult.of(evalResult.getResult(), diagnostic);
} catch (ExecutionException e) {
throw new IllegalStateException(e);
}
}
}
|