@Override public void setValue(String name, Object value) { wrapped.setValue(name, value); }
public ContextBuilder setEntry(String key, Object value) { resultContext.put(key, value); evaluationContext.setValue(key, value); return this; }
public static void setValueIntoContext(EvaluationContext ctx, ForIteration forIteration) { ctx.setValue( forIteration.getName(), forIteration.getNextValue() ); }
public static void setValueIntoContext(EvaluationContext ctx, QEIteration qeIteration) { ctx.setValue( qeIteration.getName(), qeIteration.getNextValue() ); }
public Object rreturn(Function<EvaluationContext, Object> expression) { try { ctx.enterFrame(); List results = new ArrayList(); ctx.setValue("partial", results); ForIteration[] ictx = initializeContexts(ctx, iterationContexts); while (ForExpressionNode.nextIteration(ctx, ictx)) { Object result = expression.apply(ctx); results.add(result); } return results; } catch (EndpointOfRangeNotOfNumberException e) { // ast error already reported return null; } finally { ctx.exitFrame(); } }
/** * Checks if the parameters match a single rule * @param ctx * @param params * @param rule * @return */ private boolean matches(EvaluationContext ctx, Object[] params, DTDecisionRule rule) { for( int i = 0; i < params.length; i++ ) { CompiledExpression compiledInput = inputs.get(i).getCompiledInput(); if ( compiledInput instanceof CompiledFEELExpression) { ctx.setValue("?", ((CompiledFEELExpression) compiledInput).apply(ctx)); } if( ! satisfies( ctx, params[i], rule.getInputEntry().get( i ) ) ) { return false; } } return true; }
public FEELFnResult<Object> invoke(EvaluationContext ctx, Object[] params) { FEELEvent capturedException = null; try { ctx.enterFrame(); for( int i = 0; i < params.length; i++ ) { ctx.setValue( dt.getParameterNames().get( i ), params[i] ); } return dt.evaluate( ctx, params ); } catch ( Exception e ) { String message = "Error invoking decision table '" + getName() + "': " + e.getClass().getSimpleName(); capturedException = new FEELEventBase( Severity.ERROR, message, e); logger.error( message, e ); } finally { ctx.exitFrame(); } return FEELFnResult.ofError( capturedException ); }
@Override public Object evaluate(EvaluationContext ctx) { try { ctx.enterFrame(); List results = new ArrayList( ); ctx.setValue("partial", results); ForIteration[] ictx = initializeContexts( ctx, iterationContexts); while ( nextIteration( ctx, ictx ) ) { Object result = expression.evaluate( ctx ); results.add( result ); } return results; } catch (EndpointOfRangeNotOfNumberException e) { // ast error already reported return null; } finally { ctx.exitFrame(); } }
@Override public FEELFnResult<Object> invoke(EvaluationContext ctx, Object[] params ) { if( params.length != parameters.size() ) { return FEELFnResult.ofError(new InvalidInputEvent(Severity.ERROR, "Illegal invocation of function", getName(), getName() + "( " + Arrays.asList(params)+" )", getSignature())); } FEELEvent capturedException = null; try { ctx.enterFrame(); for ( int i = 0; i < parameters.size(); i++ ) { ctx.setValue( parameters.get( i ), params[i] ); } Object result = this.body.apply(ctx); return FEELFnResult.ofResult( result ); } catch( Exception e ) { capturedException = new FEELEventBase(Severity.ERROR, "Error invoking function", new RuntimeException("Error invoking function " + getSignature() + ".", e)); } finally { ctx.exitFrame(); } return FEELFnResult.ofError( capturedException ); }
public FEELFnResult<Object> invoke(EvaluationContext ctx, Object[] params ) { if( params.length != parameters.size() ) { return FEELFnResult.ofError(new InvalidInputEvent(Severity.ERROR, "Illegal invocation of function", getName(), getName() + "( " + Arrays.asList(params)+" )", getSignature())); } FEELEvent capturedException = null; try { ctx.enterFrame(); for ( int i = 0; i < parameters.size(); i++ ) { ctx.setValue( parameters.get( i ), params[i] ); } Object result = this.body.evaluate( ctx ); return FEELFnResult.ofResult( result ); } catch( Exception e ) { capturedException = new FEELEventBase(Severity.ERROR, "Error invoking function", new RuntimeException("Error invoking function " + getSignature() + ".", e)); } finally { ctx.exitFrame(); } return FEELFnResult.ofError( capturedException ); }
public FEELFnResult<Object> invoke(EvaluationContext ctx, Object[] params) { if( params.length != parameters.size() ) { return FEELFnResult.ofError(new InvalidInputEvent(Severity.ERROR, "Illegal invocation of function", getName(), getName() + "( " + Arrays.asList(params)+" )", getSignature())); } FEELEvent capturedException = null; try { ctx.enterFrame(); for ( int i = 0; i < parameters.size(); i++ ) { ctx.setValue( parameters.get( i ), params[i] ); } Object[] actualParams = prepareParams( params ); Object result = method.invoke( clazz, actualParams ); return FEELFnResult.ofResult( result ); } catch ( InvocationTargetException e ) { String message = e.getTargetException().getMessage(); capturedException = new FEELEventBase(Severity.ERROR, "Error invoking "+toString()+": "+message, new RuntimeException("Error invoking function " + getSignature() + ".", e)); } catch ( IllegalAccessException e ) { String message = e.getCause().getMessage(); capturedException = new FEELEventBase(Severity.ERROR, "Error invoking "+toString()+": "+message, new RuntimeException("Error invoking function " + getSignature() + ".", e)); } finally { ctx.exitFrame(); } return FEELFnResult.ofError( capturedException ); }
ctx.setValue("item", v);
private void evaluateExpressionInContext(EvaluationContext ctx, List results, Object v) { try { ctx.enterFrame(); // handle it as a predicate // Have the "item" variable set first, so to respect the DMN spec: The expression in square brackets can reference a list // element using the name item, unless the list element is a context that contains the key "item". ctx.setValue( "item", v ); // using Root object logic to avoid having to eagerly inspect all attributes. ctx.setRootObject(v); // a filter would always return a list with all the elements for which the filter is true. // In case any element fails in there or the filter expression returns null, it will only exclude the element, but will continue to process the list. // In case all elements fail, the result will be an empty list. Object r = this.filter.evaluate(new SilentWrappingEvaluationContextImpl(ctx)); // evaluate filter, ignoring errors if( r instanceof Boolean && ((Boolean)r) == Boolean.TRUE ) { results.add( v ); } } finally { ctx.exitFrame(); } }
@Test public void test_filterPath_tricky2() { CompiledFEELExpression nameRef = parse("[ {x:1, y:2}, {x:2, y:3} ][x]"); LOG.debug("{}", nameRef); EvaluationContext context = CodegenTestUtil.newEmptyEvaluationContext(); context.setValue("x", false); Object result = nameRef.apply(context); LOG.debug("{}", result); assertThat(result, is(Collections.emptyList())); }
@Test public void testQualifiedName3() { String inputExpression = "a date.year"; Type dateType = BuiltInType.DATE; CompiledFEELExpression qualRef = parse(inputExpression, mapOf(entry("a date", dateType))); LOG.debug("{}", qualRef); EvaluationContext context = CodegenTestUtil.newEmptyEvaluationContext(); context.setValue("a date", LocalDate.of(2016, 8, 2)); Object result = qualRef.apply(context); LOG.debug("{}", result); assertThat(result, is(BigDecimal.valueOf(2016))); }
@Override public Object evaluate(EvaluationContext ctx) { try { ctx.enterFrame(); Map<String, Object> c = new LinkedHashMap<>(); for( ContextEntryNode cen : entries ) { String name = EvalHelper.normalizeVariableName( cen.evaluateName( ctx ) ); Object value = cen.evaluate( ctx ); if( value instanceof CustomFEELFunction ) { // helpful for debugging ((CustomFEELFunction) value).setName( name ); } else if( value instanceof JavaFunction ) { ((JavaFunction) value).setName( name ); } else if ( value instanceof DTInvokerFunction ) { ((DTInvokerFunction) value).setName(name); } ctx.setValue( name, value ); c.put( name, value ); } return c; } finally { ctx.exitFrame(); } }
@Test public void testNameReference() { String inputExpression = "someSimpleName"; CompiledFEELExpression nameRef = parse( inputExpression, mapOf( entry("someSimpleName", BuiltInType.STRING) ) ); LOG.debug("{}", nameRef); EvaluationContext context = CodegenTestUtil.newEmptyEvaluationContext(); context.setValue("someSimpleName", 123L); Object result = nameRef.apply(context); LOG.debug("{}", result); assertThat(result, is( BigDecimal.valueOf(123) )); }
@Test public void test_filterPath_tricky1() { CompiledFEELExpression nameRef = parse( "[ {x:1, y:2}, {x:2, y:3} ][x]"); LOG.debug("{}", nameRef); EvaluationContext context = CodegenTestUtil.newEmptyEvaluationContext(); context.setValue("x", 2); Object result = nameRef.apply(context); LOG.debug("{}", result); assertThat(result, is(mapOf(entry("x", new BigDecimal(2)), entry("y", new BigDecimal(3))))); }
@Test public void testQualifiedName2() { String inputExpression = "My Person.Full Name"; Type personType = JavaBackedType.of(MyPerson.class); CompiledFEELExpression qualRef = parse( inputExpression, mapOf( entry("My Person", personType) ) ); LOG.debug("{}", qualRef); EvaluationContext context = CodegenTestUtil.newEmptyEvaluationContext(); context.setValue("My Person", new MyPerson()); Object result = qualRef.apply(context); LOG.debug("{}", result); assertThat(result, is( "John Doe" )); }
@Test public void testQualifiedName() { String inputExpression = "My Person.Full Name"; Type personType = new MapBackedType("Person", mapOf( entry("Full Name", BuiltInType.STRING), entry("Age", BuiltInType.NUMBER) ) ); CompiledFEELExpression qualRef = parse( inputExpression, mapOf( entry("My Person", personType) ) ); LOG.debug("{}", qualRef); EvaluationContext context = CodegenTestUtil.newEmptyEvaluationContext(); context.setValue("My Person", mapOf( entry("Full Name", "John Doe"), entry("Age", 47) )); Object result = qualRef.apply(context); LOG.debug("{}", result); assertThat(result, is( "John Doe" )); // check number coercion for qualified name CompiledFEELExpression personAgeExpression = parse("My Person.Age", mapOf(entry("My Person", personType))); LOG.debug("{}", personAgeExpression); Object resultPersonAge = personAgeExpression.apply(context); // Please notice input variable in context is a Map containing and entry value for int 47. LOG.debug("{}", resultPersonAge); assertThat(resultPersonAge, is(BigDecimal.valueOf(47))); }