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 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(); }
@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(); }
context.newValue(QualifiedName.create(variableDecl.getName()), initialValue); return null;
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; }
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 '{}': {}",
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(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; }