Search in sources :

Example 1 with CompilerContext

use of org.kie.dmn.feel.lang.CompilerContext in project drools by kiegroup.

the class CompileEvaluateTest method test2.

@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());
}
Also used : HashMap(java.util.HashMap) CompilerContext(org.kie.dmn.feel.lang.CompilerContext) MapBackedType(org.kie.dmn.feel.lang.impl.MapBackedType) CompiledExpression(org.kie.dmn.feel.lang.CompiledExpression) Test(org.junit.Test)

Example 2 with CompilerContext

use of org.kie.dmn.feel.lang.CompilerContext in project drools by kiegroup.

the class CompileEvaluateTest method testHyphenInPropertyOfCollectionForAccessor.

@Test
public void testHyphenInPropertyOfCollectionForAccessor() {
    MapBackedType compositeType = new MapBackedType().addField("Primary-Key", BuiltInType.STRING).addField("Value", BuiltInType.STRING);
    CompilerContext ctx = feel.newCompilerContext();
    ctx.addInputVariableType("my input", new GenListType(compositeType));
    CompiledExpression compiledExpression = feel.compile("my input[1].Primary-Key", ctx);
    assertTrue(errors.isEmpty());
    Map<String, Object> inputs = new HashMap<>();
    inputs.put("my input", Arrays.asList(prototype(entry("Primary-Key", "k987"))));
    Object result = feel.evaluate(compiledExpression, inputs);
    assertThat(result, is("k987"));
    assertTrue(errors.isEmpty());
}
Also used : HashMap(java.util.HashMap) GenListType(org.kie.dmn.feel.lang.types.GenListType) CompilerContext(org.kie.dmn.feel.lang.CompilerContext) MapBackedType(org.kie.dmn.feel.lang.impl.MapBackedType) CompiledExpression(org.kie.dmn.feel.lang.CompiledExpression) Test(org.junit.Test)

Example 3 with CompilerContext

use of org.kie.dmn.feel.lang.CompilerContext in project drools by kiegroup.

the class CompileEvaluateTest method test_isDynamicResolution.

@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"));
}
Also used : HashMap(java.util.HashMap) CompilerContext(org.kie.dmn.feel.lang.CompilerContext) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) CompiledExpression(org.kie.dmn.feel.lang.CompiledExpression) Test(org.junit.Test)

Example 4 with CompilerContext

use of org.kie.dmn.feel.lang.CompilerContext in project drools by kiegroup.

the class CompileEvaluateTest method testHyphenInPropertyOfCollectionForProjection.

@Test
public void testHyphenInPropertyOfCollectionForProjection() {
    MapBackedType compositeType = new MapBackedType().addField("Primary-Key", BuiltInType.STRING).addField("Value", BuiltInType.STRING);
    CompilerContext ctx = feel.newCompilerContext();
    ctx.addInputVariableType("input", new GenListType(compositeType));
    CompiledExpression compiledExpression = feel.compile("input.Primary-Key", ctx);
    assertTrue(errors.isEmpty());
    Map<String, Object> inputs = new HashMap<>();
    inputs.put("input", Arrays.asList(prototype(entry("Primary-Key", "k987"))));
    Object result = feel.evaluate(compiledExpression, inputs);
    assertThat(result, is(Arrays.asList("k987")));
    assertTrue(errors.isEmpty());
}
Also used : HashMap(java.util.HashMap) GenListType(org.kie.dmn.feel.lang.types.GenListType) CompilerContext(org.kie.dmn.feel.lang.CompilerContext) MapBackedType(org.kie.dmn.feel.lang.impl.MapBackedType) CompiledExpression(org.kie.dmn.feel.lang.CompiledExpression) Test(org.junit.Test)

Example 5 with CompilerContext

use of org.kie.dmn.feel.lang.CompilerContext in project drools by kiegroup.

the class TemporalConstantFoldingParserTest method testKie.

@Test
public void testKie() {
    CompilerContext ctx = FEEL_KIE.newCompilerContext();
    CompiledExpression compile = FEEL_KIE.compile(expression, ctx);
    ASTNode ast = extractAST(compile);
    assertThat(ast).isInstanceOf(FunctionInvocationNode.class);
    assertThat(((FunctionInvocationNode) ast).getTcFolded()).isNotNull();
}
Also used : CompilerContext(org.kie.dmn.feel.lang.CompilerContext) ASTNode(org.kie.dmn.feel.lang.ast.ASTNode) CompiledExpression(org.kie.dmn.feel.lang.CompiledExpression) FunctionInvocationNode(org.kie.dmn.feel.lang.ast.FunctionInvocationNode) Test(org.junit.Test)

Aggregations

CompilerContext (org.kie.dmn.feel.lang.CompilerContext)21 Test (org.junit.Test)13 CompiledExpression (org.kie.dmn.feel.lang.CompiledExpression)12 HashMap (java.util.HashMap)10 FEELEvent (org.kie.dmn.api.feel.runtime.events.FEELEvent)6 FEELEventListener (org.kie.dmn.api.feel.runtime.events.FEELEventListener)6 FEEL (org.kie.dmn.feel.FEEL)5 MapBackedType (org.kie.dmn.feel.lang.impl.MapBackedType)5 Map (java.util.Map)4 ArrayList (java.util.ArrayList)2 List (java.util.List)2 DMNType (org.kie.dmn.api.core.DMNType)2 Type (org.kie.dmn.feel.lang.Type)2 ASTNode (org.kie.dmn.feel.lang.ast.ASTNode)2 FunctionInvocationNode (org.kie.dmn.feel.lang.ast.FunctionInvocationNode)2 GenListType (org.kie.dmn.feel.lang.types.GenListType)2 CompiledNetwork (org.drools.ancompiler.CompiledNetwork)1 CompiledNetworkSources (org.drools.ancompiler.CompiledNetworkSources)1 ObjectTypeNodeCompiler (org.drools.ancompiler.ObjectTypeNodeCompiler)1 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)1