-
Notifications
You must be signed in to change notification settings - Fork 3
/
EvaluationServices.java
512 lines (479 loc) · 17.3 KB
/
EvaluationServices.java
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
/*******************************************************************************
* Copyright (c) 2015, 2024 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.acceleo.query.runtime.impl;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.acceleo.query.ast.Call;
import org.eclipse.acceleo.query.ast.EClassifierTypeLiteral;
import org.eclipse.acceleo.query.ast.EnumLiteral;
import org.eclipse.acceleo.query.ast.Lambda;
import org.eclipse.acceleo.query.parser.AstBuilderListener;
import org.eclipse.acceleo.query.runtime.AcceleoQueryEvaluationException;
import org.eclipse.acceleo.query.runtime.IReadOnlyQueryEnvironment;
import org.eclipse.acceleo.query.runtime.IService;
import org.eclipse.acceleo.query.runtime.namespace.IQualifiedNameLookupEngine;
import org.eclipse.acceleo.query.validation.type.ClassType;
import org.eclipse.acceleo.query.validation.type.EClassifierType;
import org.eclipse.acceleo.query.validation.type.IType;
import org.eclipse.emf.common.util.BasicDiagnostic;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.DiagnosticChain;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EEnumLiteral;
import org.eclipse.emf.ecore.EObject;
/**
* Implementation of the elementary language services like variable access and service call.
*
* @author <a href="mailto:romain.guider@obeo.fr">Romain Guider</a>
*/
public class EvaluationServices extends AbstractLanguageServices {
/**
* Log message used when an internal evaluation error is encountered.
*/
private static final String INTERNAL_ERROR_MSG = "An internal error occured during evaluation of a query";
/**
* Creates a new {@link EvaluationServices} instance given a lookupEngine and logging flag.
*
* @param queryEnv
* the {@link IReadOnlyQueryEnvironment} to use during evaluation
*/
public EvaluationServices(IReadOnlyQueryEnvironment queryEnv) {
super(queryEnv);
}
/**
* Returns the value of the specified variable in the specified map. Returns NOTHING when the variable is
* not found.
*
* @param variableDefinitions
* the set of variable definition in which to lookup the specified variable.
* @param variableName
* the name of the variable to lookup in the specified map.
* @param diagnostic
* The status to update in case of warnings or errors during this call.
* @return Returns the value of the specified variable in the specified map or nothing.
*/
public Object getVariableValue(Map<String, Object> variableDefinitions, String variableName,
Diagnostic diagnostic) {
try {
Object result = variableDefinitions.get(variableName);
if (result == null && !variableDefinitions.containsKey(variableName)) {
Nothing placeHolder = nothing(VARIABLE_NOT_FOUND, variableName);
addDiagnosticFor(diagnostic, Diagnostic.ERROR, placeHolder);
result = placeHolder;
}
return result;
} catch (NullPointerException e) {
throw new AcceleoQueryEvaluationException(INTERNAL_ERROR_MSG, e);
}
}
/**
* returns the nothing value and logs the specified error message.
*
* @param message
* the message to log.
* @param msgArgs
* the object arguments used to format the log message.
* @return an instance of nothing initialized with the formatted error message.
*/
private Nothing nothing(String message, Object... msgArgs) {
String formatedMessage = String.format(message, msgArgs);
return new Nothing(formatedMessage);
}
/**
* Do a service call.
*
* @param call
* the initial {@link Call}
* @param service
* the specification of the service to call.
* @param arguments
* the arguments of the service call.
* @param argumentTypes
* the array of {@link IType}
* @param diagnostic
* The status to update in case of warnings or errors during this call.
* @return the value produced by the service execution.
*/
private Object internalCallService(Call call, IService<?> service, Object[] arguments,
IType[] argumentTypes, Diagnostic diagnostic) {
try {
final Object result;
final Object value = service.invoke(arguments);
if (value instanceof Nothing) {
addDiagnosticFor(diagnostic, Diagnostic.WARNING, (Nothing)value);
result = value;
} else if (value == null && !isLambda(call)) {
final List<IType> types = Arrays.asList(argumentTypes);
final Set<IType> serviceTypes = service.getType(call, new ValidationServices(
queryEnvironment), null, queryEnvironment, types);
result = new NullValue(serviceTypes);
} else {
result = value;
}
return result;
} catch (AcceleoQueryEvaluationException e) {
Nothing placeHolder = new Nothing(e.getMessage(), e);
if (e.getCause() instanceof AcceleoQueryEvaluationException) {
addDiagnosticFor(diagnostic, Diagnostic.WARNING, placeHolder);
} else {
addDiagnosticFor(diagnostic, Diagnostic.ERROR, placeHolder);
}
return placeHolder;
}
}
/**
* @param call
* @return
*/
private boolean isLambda(Call call) {
return call.getArguments().size() == 2 && call.getArguments().get(1) instanceof Lambda;
}
/**
* Evaluate a service call.
*
* @param call
* the {@link Call}
* @param arguments
* the arguments to pass to the service evaluation.
* @param diagnostic
* The status to update in case of warnings or errors during this call.
* @return the value resulting from evaluating the service given the specified arguments.
*/
public Object call(Call call, Object[] arguments, Diagnostic diagnostic) {
final Object result;
switch (call.getType()) {
case CALLSERVICE:
result = internalCall(call, arguments, diagnostic);
break;
case CALLORAPPLY:
result = internalCallOrApply(call, arguments, diagnostic);
break;
case COLLECTIONCALL:
result = internalCollectionServiceCall(call, arguments, diagnostic);
break;
default:
throw new UnsupportedOperationException("should never happen");
}
return result;
}
/**
* Evaluate a service call.
*
* @param call
* the {@link Call}
* @param arguments
* the arguments to pass to the service evaluation.
* @param diagnostic
* The status to update in case of warnings or errors during this call.
* @return the value resulting from evaluating the service given the specified arguments.
*/
private Object internalCall(Call call, Object[] arguments, Diagnostic diagnostic) {
final Object result;
final String serviceName = call.getServiceName();
if (arguments.length == 0) {
throw new AcceleoQueryEvaluationException(
"An internal error occured during evaluation of a query : at least one argument must be specified for service "
+ serviceName + ".");
}
try {
final IType[] argumentTypes = new IType[arguments.length];
final Object[] localArguments = new Object[arguments.length];
for (int i = 0; i < arguments.length; i++) {
if (arguments[i] == null) {
argumentTypes[i] = new ClassType(queryEnvironment, null);
localArguments[i] = null;
} else if (arguments[i].getClass() == NullValue.class) {
argumentTypes[i] = ((NullValue)arguments[i]).getType();
localArguments[i] = null;
} else if (arguments[i] instanceof EObject) {
argumentTypes[i] = new EClassifierType(queryEnvironment, ((EObject)arguments[i])
.eClass());
localArguments[i] = arguments[i];
} else {
argumentTypes[i] = new ClassType(queryEnvironment, arguments[i].getClass());
localArguments[i] = arguments[i];
}
}
IService<?> service;
if (call.isSuperCall()) {
service = ((IQualifiedNameLookupEngine)queryEnvironment.getLookupEngine()).superServiceLookup(
serviceName, argumentTypes);
} else {
service = queryEnvironment.getLookupEngine().lookup(serviceName, argumentTypes);
}
if (service == null) {
Nothing placeHolder = nothing(SERVICE_NOT_FOUND, serviceSignature(serviceName,
argumentTypes));
addDiagnosticFor(diagnostic, Diagnostic.WARNING, placeHolder);
result = placeHolder;
} else {
result = internalCallService(call, service, localArguments, argumentTypes, diagnostic);
}
// CHECKSTYLE:OFF
} catch (Exception e) {
// CHECKSTYLE:ON
throw new AcceleoQueryEvaluationException(INTERNAL_ERROR_MSG, e);
}
return result;
}
/**
* The callOrApply method evaluates an expression of the form "<exp>.<service name>(<exp>*)" The first
* argument in the arguments array is considered the receiver of the call. If the receiver is a collection
* then callOrApply is applied recursively to all elements of the collection thus returning a collection
* of the result of this application(nothing values not being added).
*
* @param call
* the {@link Call}
* @param arguments
* the arguments to pass to the called service.
* @param diagnostic
* The status to update in case of warnings or errors during this call.
* @return the result of evaluating the specified service on the specified arguments.
*/
@SuppressWarnings("unchecked")
private Object internalCallOrApply(Call call, Object[] arguments, Diagnostic diagnostic) {
try {
Object result;
if (arguments[0] instanceof List) {
List<Object> list = (List<Object>)arguments[0];
result = applyCallOnSequence(call, list, arguments, diagnostic);
} else if (arguments[0] instanceof Set) {
Set<Object> set = (Set<Object>)arguments[0];
result = applyCallOnSet(call, set, arguments, diagnostic);
} else {
result = internalCall(call, arguments, diagnostic);
}
return result;
// CHECKSTYLE:OFF
} catch (Exception e) {
// CHECKSTYLE:ON
throw new AcceleoQueryEvaluationException(INTERNAL_ERROR_MSG, e);
}
}
/**
* Calls a collection's service.
*
* @param call
* the {@link Call}
* @param arguments
* the service's arguments.
* @param diagnostic
* The status to update in case of warnings or errors during this call.
* @return the result of evaluating the specified service on the specified arguments.
*/
private Object internalCollectionServiceCall(Call call, Object[] arguments, Diagnostic diagnostic) {
Object[] newArguments;
try {
Object receiver = arguments[0];
if (!(receiver instanceof Collection) && !(receiver instanceof Nothing)) {
// implicit set conversion.
Set<Object> newReceiver = new LinkedHashSet<Object>();
// treat "null" as a non existing value. The auto-boxed set will then be empty.
if (receiver != null) {
newReceiver.add(receiver);
}
receiver = newReceiver;
newArguments = arguments.clone();
newArguments[0] = newReceiver;
} else {
newArguments = arguments;
}
return internalCall(call, newArguments, diagnostic);
// CHECKSTYLE:OFF
} catch (Exception e) {
// CHECKSTYLE:ON
throw new AcceleoQueryEvaluationException(INTERNAL_ERROR_MSG, e);
}
}
/**
* Applies a service call on a sequence of objects.
*
* @param call
* the {@link Call}
* @param origin
* the sequence on which elements to apply the service
* @param arguments
* the arguments to pass to the service.
* @param diagnostic
* The status to update in case of warnings or errors during this call.
* @return a sequence made of the result of calling the specified service on the specified arguments on
* the origin list elements.
*/
private Object applyCallOnSequence(Call call, List<Object> origin, Object[] arguments,
Diagnostic diagnostic) {
try {
List<Object> result = new ArrayList<Object>(origin.size());
Object[] innerArguments = arguments.clone();
for (Object obj : origin) {
innerArguments[0] = obj;
final Object newResult = internalCallOrApply(call, innerArguments, diagnostic);
flattenList(result, newResult);
}
return result;
// CHECKSTYLE:OFF
} catch (Exception e) {
// CHECKSTYLE:ON
throw new AcceleoQueryEvaluationException("empty argument array passed to callOrApply " + call
.getServiceName(), e);
}
}
/**
* Flatten the given {@link Object} into the given {@link List}.
*
* @param list
* the {@link List}
* @param object
* the {@link Object}
*/
protected void flattenList(List<Object> list, Object object) {
if (!(object instanceof Nothing)) {
if (object instanceof Collection) {
list.addAll((Collection<?>)object);
} else if (object != null && object.getClass() != NullValue.class) {
list.add(object);
}
}
}
/**
* Applies a service call on a set of objects.
*
* @param call
* the {@link Call}
* @param origin
* the sequence on which elements to apply the service
* @param arguments
* the arguments to pass to the service.
* @param diagnostic
* The status to update in case of warnings or errors during this call.
* @return a sequence made of the result of calling the specified service on the specified arguments on
* the origin set elements.
*/
private Object applyCallOnSet(Call call, Set<Object> origin, Object[] arguments, Diagnostic diagnostic) {
try {
Set<Object> result = new LinkedHashSet<Object>(origin.size());
Object[] innerArguments = arguments.clone();
for (Object obj : origin) {
innerArguments[0] = obj;
final Object newResult = internalCallOrApply(call, innerArguments, diagnostic);
flattenSet(result, newResult);
}
return result;
// CHECKSTYLE:OFF
} catch (Exception e) {
// CHECKSTYLE:ON
throw new AcceleoQueryEvaluationException(INTERNAL_ERROR_MSG, e);
}
}
/**
* Flatten the given {@link Object} into the given {@link Set}.
*
* @param set
* the {@link Set}
* @param object
* the {@link Object}
*/
protected void flattenSet(Set<Object> set, Object object) {
if (!(object instanceof Nothing)) {
if (object instanceof Collection) {
set.addAll((Collection<?>)object);
} else if (object != null && object.getClass() != NullValue.class) {
set.add(object);
}
}
}
/**
* Build up the specified service's signature for reporting. Only use this when the service is not
* available directly, otherwise use {@link IService#getShortSignature() short signature}.
*
* @param serviceName
* the name of the service
* @param argumentTypes
* the service's call argument types
* @return the specified service's signature.
*/
protected String serviceSignature(String serviceName, IType[] argumentTypes) {
StringBuilder builder = new StringBuilder();
builder.append(serviceName).append('(');
boolean first = true;
for (IType argType : argumentTypes) {
if (!first) {
builder.append(',');
} else {
first = false;
}
if (argType == null) {
builder.append("Object=null");
} else {
builder.append(argType.toString());
}
}
return builder.append(')').toString();
}
/**
* Adds a child to the given diagnostic chain with the information from that Nothing instance.
*
* @param chain
* The parent chain.
* @param severity
* Severity to give that issue.
* @param nothing
* The nothing we've obtained as a result of an evaluation failure.
*/
private void addDiagnosticFor(Diagnostic chain, int severity, Nothing nothing) {
if (chain instanceof DiagnosticChain) {
Diagnostic child = new BasicDiagnostic(severity, AstBuilderListener.PLUGIN_ID, 0, nothing
.getMessage(), new Object[] {nothing.getCause(), });
((DiagnosticChain)chain).add(child);
}
}
/**
* Gets the {@link EClassifier} for the given {@link EClassifierTypeLiteral}.
*
* @param eClassifierTypeLiteral
* the {@link EClassifierTypeLiteral}
* @return the {@link EClassifier} for the given {@link EClassifierTypeLiteral} if any, <code>null</code>
* otherwise
*/
public EClassifier getEClassifier(EClassifierTypeLiteral eClassifierTypeLiteral) {
final EClassifier result;
final Collection<EClassifier> eClassifiers = queryEnvironment.getEPackageProvider().getTypes(
eClassifierTypeLiteral.getEPackageName(), eClassifierTypeLiteral.getEClassifierName());
if (eClassifiers.isEmpty()) {
result = null;
} else {
result = eClassifiers.iterator().next();
}
return result;
}
/**
* Gets the {@link EEnumLiteral} for the given {@link EnumLiteral}.
*
* @param enumLiteral
* the {@link EnumLiteral}
* @return the {@link EEnumLiteral} for the given {@link EnumLiteral} if any, <code>null</code> otherwise
*/
public EEnumLiteral getEEnumLiteral(EnumLiteral enumLiteral) {
final EEnumLiteral result;
final Collection<EEnumLiteral> literals = queryEnvironment.getEPackageProvider().getEnumLiterals(
enumLiteral.getEPackageName(), enumLiteral.getEEnumName(), enumLiteral.getEEnumLiteralName());
if (literals.isEmpty()) {
result = null;
} else {
result = literals.iterator().next();
}
return result;
}
}