|
| 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 | +} |
0 commit comments