protected void initializeClosureParameters(IEvaluationContext context, Object[] args) { if (args.length != closure.getFormalParameters().size()) throw new IllegalStateException("Number of arguments did not match. Expected: " + closure.getFormalParameters().size() + " but was: " + args.length); int i = 0; for(JvmFormalParameter param: closure.getFormalParameters()) { context.newValue(QualifiedName.create(param.getName()), args[i]); i++; } }
@Override public void assignValue(QualifiedName qualifiedName, Object value) { if (values == null || !values.containsKey(qualifiedName)) parent.assignValue(qualifiedName, value); else values.put(qualifiedName, value); }
@Override public Object getValue(QualifiedName qualifiedName) { if (values != null && values.containsKey(qualifiedName)) return values.get(qualifiedName); return parent.getValue(qualifiedName); }
protected Object _doEvaluate(XForLoopExpression forLoop, IEvaluationContext context, CancelIndicator indicator) { Object iterableOrIterator = internalEvaluate(forLoop.getForExpression(), context, indicator); if (iterableOrIterator == null) return throwNullPointerException(forLoop.getForExpression(), "iterable evaluated to 'null'"); Iterator<?> iter = null; if (iterableOrIterator instanceof Iterable<?>) { iter = ((Iterable<?>) iterableOrIterator).iterator(); } else if (iterableOrIterator.getClass().isArray()) { iter = ((Iterable<?>) Conversions.doWrapArray(iterableOrIterator)).iterator(); } else { return throwClassCastException(forLoop.getForExpression(), iterableOrIterator, java.lang.Iterable.class); } IEvaluationContext forkedContext = context.fork(); QualifiedName paramName = QualifiedName.create(forLoop.getDeclaredParam().getName()); forkedContext.newValue(paramName, null); while (iter.hasNext()) { Object next = iter.next(); forkedContext.assignValue(paramName, next); internalEvaluate(forLoop.getEachExpression(), forkedContext, indicator); } return null; }
protected Object _doEvaluate(XSwitchExpression switchExpression, IEvaluationContext context, CancelIndicator indicator) { IEvaluationContext forkedContext = context.fork(); Object conditionResult = internalEvaluate(switchExpression.getSwitch(), forkedContext, indicator); String simpleName = featureNameProvider.getSimpleName(switchExpression.getDeclaredParam()); if (simpleName != null) { forkedContext.newValue(QualifiedName.create(simpleName), conditionResult); } for (XCasePart casePart : switchExpression.getCases()) { JvmTypeReference typeGuard = casePart.getTypeGuard(); if (typeGuard != null && switchExpression.getSwitch() == null) throw new IllegalStateException("Switch without expression or implicit 'this' may not use type guards"); if (typeGuard == null || isInstanceoOf(conditionResult, typeGuard)) { if (casePart.getCase() != null) { Object casePartResult = internalEvaluate(casePart.getCase(), forkedContext, indicator); if (Boolean.TRUE.equals(casePartResult) || eq(conditionResult, casePartResult)) { XExpression then = switchExpressions.getThen(casePart, switchExpression); return internalEvaluate(then, forkedContext, indicator); } } else { XExpression then = switchExpressions.getThen(casePart, switchExpression); return internalEvaluate(then, forkedContext, indicator); } } } if (switchExpression.getDefault() != null) { Object defaultResult = internalEvaluate(switchExpression.getDefault(), forkedContext, indicator); return defaultResult; } return getDefaultObjectValue(typeResolver.resolveTypes(switchExpression).getActualType(switchExpression)); }
protected Object _doEvaluate(XBlockExpression literal, IEvaluationContext context, CancelIndicator indicator) { List<XExpression> expressions = literal.getExpressions(); Object result = null; IEvaluationContext forkedContext = context.fork(); for (int i = 0; i < expressions.size(); i++) { result = internalEvaluate(expressions.get(i), forkedContext, indicator); } return result; }
protected Object _doEvaluate(XTryCatchFinallyExpression tryCatchFinally, IEvaluationContext context, CancelIndicator indicator) { Object result = null; try { result = internalEvaluate(tryCatchFinally.getExpression(), context, indicator); } catch (EvaluationException evaluationException) { Throwable cause = evaluationException.getCause(); for (XCatchClause catchClause : tryCatchFinally.getCatchClauses()) { JvmFormalParameter exception = catchClause.getDeclaredParam(); JvmTypeReference catchParameterType = exception.getParameterType(); if (!isInstanceoOf(cause, catchParameterType)) { continue; } IEvaluationContext forked = context.fork(); forked.newValue(QualifiedName.create(exception.getName()), cause); result = internalEvaluate(catchClause.getExpression(), forked, indicator); break; } } if (tryCatchFinally.getFinallyExpression() != null) { try { internalEvaluate(tryCatchFinally.getFinallyExpression(), context, indicator); } catch (EvaluationException e) { throw new EvaluationException(new FinallyDidNotCompleteException(e)); } } return result; }
protected Object _doEvaluate(XBasicForLoopExpression forLoop, IEvaluationContext context, CancelIndicator indicator) { IEvaluationContext forkedContext = context.fork(); for (XExpression initExpression : forLoop.getInitExpressions()) { internalEvaluate(initExpression, forkedContext, indicator); } XExpression expression = forLoop.getExpression(); Object condition = expression == null ? Boolean.TRUE : internalEvaluate(expression, forkedContext, indicator); while (Boolean.TRUE.equals(condition)) { internalEvaluate(forLoop.getEachExpression(), forkedContext, indicator); for (XExpression updateExpression : forLoop.getUpdateExpressions()) { internalEvaluate(updateExpression, forkedContext, indicator); } condition = expression == null ? Boolean.TRUE : internalEvaluate(expression, forkedContext, indicator); } return null; }
@Override public Object evaluateExpression(IValueProvider provider) throws Exception { IEvaluationContext context = contextProvider.get(); for (String name : getInputParameterNames()) { context.newValue(nameConverter.toQualifiedName(name), provider.getValue(name)); } IEvaluationResult result = interpreter.evaluate(xExpression, context, CancelIndicator.NullImpl); if (result == null) throw new ViatraQueryException(String.format( "XBase expression interpreter returned no result while evaluating expression %s in pattern %s.", xExpression, pattern), "XBase expression interpreter returned no result."); Throwable throwable = result.getException(); if (throwable instanceof Error) { throw (Error) throwable; } else if (throwable instanceof Exception) { throw (Exception) throwable; } else if (throwable != null) { throw new ViatraQueryException(String.format("Strange throwable (%s) encountered: %s", throwable.getClass() .getCanonicalName(), throwable.getMessage()), "Strange throwable encountered", throwable); } return result.getResult(); }
@Override public void assignValue(QualifiedName qualifiedName, Object value) { try { super.assignValue(qualifiedName, value); } catch (IllegalStateException e) { if (globalContext != null) { globalContext.assignValue(qualifiedName, value); } else { throw e; } } }
@Override public Object getValue(QualifiedName qualifiedName) { Object value = super.getValue(qualifiedName); if (value == null && this.globalContext != null) { value = globalContext.getValue(qualifiedName); } return value; }
@Override protected Object doInvoke(Method method, Object[] args) throws Throwable { IEvaluationContext forkedContext = context.fork(); if (args != null) { initializeClosureParameters(forkedContext, args); } IEvaluationResult result = interpreter.evaluate(closure.getExpression(), forkedContext, indicator); if (indicator.isCanceled()) throw new InterpreterCanceledException(); if (result.getException() != null) throw result.getException(); return result.getResult(); }
@Override public Object evaluateExpression(IValueProvider provider) throws Exception { IEvaluationContext context = contextProvider.get(); for (String name : getInputParameterNames()) { context.newValue(nameConverter.toQualifiedName(name), provider.getValue(name)); } IEvaluationResult result = interpreter.evaluate(xExpression, context, CancelIndicator.NullImpl); if (result == null) throw new IncQueryException(String.format( "XBase expression interpreter returned no result while evaluating expression %s in pattern %s.", xExpression, pattern), "XBase expression interpreter returned no result."); Throwable throwable = result.getException(); if (throwable instanceof Error) { throw (Error) throwable; } else if (throwable instanceof Exception) { throw (Exception) throwable; } else if (throwable != null) { throw new IncQueryException(String.format("Strange throwable (%s) encountered: %s", throwable.getClass() .getCanonicalName(), throwable.getMessage()), "Strange throwable encountered", throwable); } return result.getResult(); }
/** * @param assignment unused in this context but required for dispatching * @param indicator unused in this context but required for dispatching */ protected Object _assignValueTo(XVariableDeclaration variable, XAbstractFeatureCall assignment, Object value, IEvaluationContext context, CancelIndicator indicator) { if (variable.getType() != null) { JvmTypeReference type = variable.getType(); Object coerced = coerceArgumentType(value, type); context.assignValue(QualifiedName.create(variable.getName()), coerced); } else { context.assignValue(QualifiedName.create(variable.getName()), value); } return value; }
/** * @param featureCall unused in this context but required for dispatching * @param indicator unused in this context but required for dispatching */ protected Object _invokeFeature(JvmIdentifiableElement identifiable, XAbstractFeatureCall featureCall, Object receiver, IEvaluationContext context, CancelIndicator indicator) { if (receiver != null) throw new IllegalStateException("feature was simple feature call but got receiver instead of null. Receiver: " + receiver); String localVarName = featureNameProvider.getSimpleName(identifiable); Object value = context.getValue(QualifiedName.create(localVarName)); return value; }
context.newValue(QualifiedName.create(variableDecl.getName()), initialValue); return null;
@Override public Object _assignValueTo(final JvmField jvmField, final XAbstractFeatureCall assignment, final Object value, final IEvaluationContext context, final CancelIndicator indicator) { Object _xblockexpression = null; { final EObject sourceElement = IterableExtensions.<EObject>head(this._iJvmModelAssociations.getSourceElements(jvmField)); Object _xifexpression = null; boolean _notEquals = (!Objects.equal(sourceElement, null)); if (_notEquals) { Object _xblockexpression_1 = null; { context.assignValue(QualifiedName.create(jvmField.getSimpleName()), value); _xblockexpression_1 = value; } _xifexpression = _xblockexpression_1; } else { _xifexpression = super._assignValueTo(jvmField, assignment, value, context, indicator); } _xblockexpression = _xifexpression; } return _xblockexpression; }
Object _xblockexpression_1 = null; final Object value = context.getValue(QualifiedName.create(jvmField.getSimpleName())); Object _elvis = null; if (value != null) {
Object initialValue = var.getRight() == null ? null : scriptEngine.newScriptFromXExpression(var.getRight()).execute(); evaluationContext.newValue(QualifiedName.create(var.getName()), initialValue); } catch (ScriptExecutionException e) { logger.warn("Variable '{}' on rule file '{}' cannot be initialized with value '{}': {}",