@Test public void unknownVariable() { final FEEL feel = FEEL.newInstance(); final FEELEventListener fel = Mockito.mock(FEELEventListener.class ); feel.addListener( fel ); final CompilerContext ctx = feel.newCompilerContext(); feel.compile( "a variable name", ctx ); final ArgumentCaptor<FEELEvent> captor = ArgumentCaptor.forClass(FEELEvent.class ); verify( fel, times(2) ).onEvent( captor.capture() ); Assert.assertThat( captor.getAllValues().size(), is( 2 ) ); Assert.assertThat( captor.getAllValues().get(1), is( instanceOf( UnknownVariableErrorEvent.class ) ) ); Assert.assertThat( ((UnknownVariableErrorEvent)captor.getAllValues().get(1)).getOffendingSymbol(), is( "a variable name" ) ); }
protected void assertResult(final String expression, final Map<String, Type> inputTypes, final Map<String, Object> inputValues, final Object result) { final CompilerContext ctx = feel.newCompilerContext(); inputTypes.forEach(ctx::addInputVariableType); final CompiledExpression compiledExpression = feel.compile(expression, ctx ); if( result == null ) { assertThat( "Evaluating: '" + expression + "'", feel.evaluate( compiledExpression, inputValues ), is( nullValue() ) ); } else if( result instanceof Class<?> ) { assertThat( "Evaluating: '" + expression + "'", feel.evaluate( compiledExpression, inputValues ), is( instanceOf( (Class<?>) result ) ) ); } else { assertThat( "Evaluating: '"+expression+"'", feel.evaluate( compiledExpression, inputValues ), is( result ) ); } }
/** * Convert row to DTDecisionRule * @param mainCtx the main context is used to identify the hosted FEELEventManager * @param embeddedFEEL a possibly cached embedded FEEL to compile the output expression, error will be reported up to the mainCtx * @param index * @param rule * @param inputSize * @return */ private static DTDecisionRule toDecisionRule(EvaluationContext mainCtx, FEEL embeddedFEEL, int index, List<?> rule, int inputSize) { // TODO should be check indeed block of inputSize n inputs, followed by block of outputs. DTDecisionRule dr = new DTDecisionRule( index ); for ( int i = 0; i < rule.size(); i++ ) { Object o = rule.get( i ); if ( i < inputSize ) { dr.getInputEntry().add( toUnaryTest( mainCtx, o ) ); } else { FEELEventListener ruleListener = event -> mainCtx.notifyEvt( () -> new FEELEventBase(event.getSeverity(), Msg.createMessage(Msg.ERROR_COMPILE_EXPR_DT_FUNCTION_RULE_IDX, index+1, event.getMessage()), event.getSourceException())); embeddedFEEL.addListener(ruleListener); CompiledExpression compiledExpression = embeddedFEEL.compile((String) o, embeddedFEEL.newCompilerContext()); dr.getOutputEntry().add( compiledExpression ); embeddedFEEL.removeListener(ruleListener); } } return dr; }
@Test public void test2() { CompilerContext ctx = feel.newCompilerContext(); ctx.addInputVariableType( "MyPerson", new MapBackedType().addField( "FullName", BuiltInType.STRING ) ); CompiledExpression compiledExpression = feel.compile( "MyPerson.fullName", ctx ); assertThat(errors.toString(), errors.size(), is(1) ); Map<String, Object> inputs = new HashMap<>(); inputs.put( "MyPerson", prototype(entry("FullName", "John Doe")) ); Object result = feel.evaluate(compiledExpression, inputs); assertThat(result, nullValue()); }
@Test public void test_isDynamicResolution() { CompilerContext ctx = feel.newCompilerContext(); ctx.addInputVariableType( "Person List", BuiltInType.LIST); CompiledExpression compiledExpression = feel.compile( "Person List[My Variable 1 = \"A\"]", ctx ); assertThat(errors.toString(), errors.size(), is(0) ); Map<String, Object> inputs = new HashMap<>(); List<Map<String, ?>> pList = new ArrayList<>(); inputs.put("Person List", pList); pList.add(prototype(entry("Full Name", "Edson Tirelli"), entry("My Variable 1","A")) ); pList.add(prototype(entry("Full Name", "Matteo Mortari"), entry("My Variable 1","B")) ); Object result = feel.evaluate(compiledExpression, inputs); assertThat( result, instanceOf( List.class ) ); assertThat( (List<?>) result, hasSize(1) ); assertThat( ((Map<?, ?>) ((List<?>) result).get(0)).get("Full Name"), is("Edson Tirelli") ); }
@Test public void test2OK() { CompilerContext ctx = feel.newCompilerContext(); ctx.addInputVariableType( "MyPerson", new MapBackedType().addField( "FullName", BuiltInType.STRING ) ); CompiledExpression compiledExpression = feel.compile( "MyPerson.FullName", ctx ); Map<String, Object> inputs = new HashMap<>(); inputs.put( "MyPerson", prototype(entry("FullName", "John Doe")) ); Object result = feel.evaluate(compiledExpression, inputs); assertThat(result, is("John Doe")); } }