FEEL feel = FEEL.newInstance(runtime.getRootClassLoader(), profiles); Object value = feel.evaluate(expr, variables);
/** * Unmarshalls the string into a FEEL value by executing it. * * @param feelType this parameter is ignored by this marshaller and can be set to null * @param value the FEEL code to execute for unmarshalling * * @return the value resulting from executing the code */ @Override public Object unmarshall(Type feelType, String value) { return feel.evaluate( value ); } }
private Object[] resolveActualInputs(EvaluationContext ctx, FEEL feel) { Map<String, Object> variables = ctx.getAllValues(); Object[] actualInputs = new Object[ inputs.size() ]; for( int i = 0; i < inputs.size(); i++ ) { CompiledExpression compiledInput = inputs.get( i ).getCompiledInput(); if( compiledInput != null ) { actualInputs[i] = feel.evaluate( compiledInput, variables ); } else { actualInputs[i] = feel.evaluate( inputs.get( i ).getInputExpression(), variables ); } } return actualInputs; }
@BeforeClass public static void setupTest() { String expression = loadExpression( "example_10_6_1.feel" ); feel = FEEL.newInstance(); context = (Map) feel.evaluate( expression ); }
@Test public void testJavaCall() { String expression = loadExpression( "javacall.feel" ); Map context = (Map) feel.evaluate( expression ); System.out.println( printContext( context ) ); }
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 ) ); } }
@Test public void testLoadExample_10_6_7() { String expression = loadExpression( "example_10_6_7.feel" ); Boolean bankrupcy = (Boolean) feel.evaluate( expression, context ); System.out.println( "Is there bankrupcy event? " + bankrupcy ); assertThat( bankrupcy, is( Boolean.FALSE ) ); }
@Test public void testExpression() { feel = (testFEELTarget == FEEL_TARGET.JAVA_TRANSLATED) ? FEEL.newInstance(Collections.singletonList(new DoCompileFEELProfile())) : FEEL.newInstance(); assertResult( expression, inputTypes, inputValues, result ); }
@Before public void setup() { testVariable = null; feel = FEEL.newInstance(); feel.addListener(event -> testVariable = LISTENER_OUTPUT); feel.addListener(System.out::println); feel.addListener( (evt) -> { if (evt.getSeverity() == Severity.ERROR) System.err.println(evt); } ); }
@After public void after() { feel.removeListener( errorsCountingListener ); }
@Before public void before() { errors = new ArrayList<>(); errorsCountingListener = evt -> { if ( evt.getSeverity() == Severity.ERROR ) { errors.add(evt); } }; feel.addListener( errorsCountingListener ); }
@Override public Object getValueForGiven(String className, Object raw) { if (!(raw instanceof String)) { return raw; } EvaluationContext evaluationContext = newEvaluationContext(); return feel.evaluate((String) raw, evaluationContext); }
@Test public void testAdhocExpression() { String expression = loadExpression( "custom.feel" ); Object result = feel.evaluate( expression ); if ( result instanceof Map ) { System.out.println( printContext( (Map) result ) ); } else { System.out.println( "Result: " + result ); } }
@Test public void testLoadExample_10_6_3() { String expression = loadExpression( "example_10_6_3.feel" ); String maritalStatus = (String) feel.evaluate( expression, context ); System.out.println( "Marital status = " + maritalStatus ); assertThat( maritalStatus, is( "valid" ) ); }
@SafeVarargs private final Object evaluate(final String expression, final Map.Entry<String, ?>... vars) { final HashMap<String, Object> inputVariables = new HashMap<>(); for (final Map.Entry<String, ?> v : vars) { inputVariables.put(v.getKey(), v.getValue()); } return feel.evaluate(expression, inputVariables); }
private Object evaluateInfix(final Object x, final InfixOperator op, final Object y) { final Map<String, Object> inputVariables = new HashMap<>(); inputVariables.put("x", x); inputVariables.put("y", y); final String expression = "x " + op.symbol + " y"; System.out.println(expression); return feel.evaluate(expression, inputVariables); }
protected void assertResult(final String expression, final Object result ) { if( result == null ) { assertThat( "Evaluating: '" + expression + "'", feel.evaluate( expression ), is( nullValue() ) ); } else if( result instanceof Class<?> ) { assertThat( "Evaluating: '" + expression + "'", feel.evaluate( expression ), is( instanceOf( (Class<?>) result ) ) ); } else { assertThat( "Evaluating: '"+expression+"'", feel.evaluate( expression ), is( result ) ); } }
@Test public void testParserError() { feel.evaluate( "10 + / 5" ); Assert.assertThat(testVariable, is(LISTENER_OUTPUT)); }
@Test public void testLoadExample_10_6_4() { Number totalExpenses = (Number) feel.evaluate( "sum( monthly outgoings )", context ); System.out.println( "Monthly total expenses = " + totalExpenses ); assertThat( totalExpenses, is( new BigDecimal( "5500.00" ) ) ); }
@Test public void testSomeBuiltinFunctions() { System.out.println( feel.evaluate("append( null, 1, 2 )") ); Assert.assertThat(testVariable, is(LISTENER_OUTPUT)); } }