Search in sources :

Example 6 with MapBackedType

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

the class DirectCompilerTest method testQualifiedName.

@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);
    // Please notice input variable in context is a Map containing and entry value for int 47.
    Object resultPersonAge = personAgeExpression.apply(context);
    LOG.debug("{}", resultPersonAge);
    assertThat(resultPersonAge, is(BigDecimal.valueOf(47)));
}
Also used : MapBackedType(org.kie.dmn.feel.lang.impl.MapBackedType) Type(org.kie.dmn.feel.lang.Type) JavaBackedType(org.kie.dmn.feel.lang.impl.JavaBackedType) BuiltInType(org.kie.dmn.feel.lang.types.BuiltInType) MapBackedType(org.kie.dmn.feel.lang.impl.MapBackedType) EvaluationContext(org.kie.dmn.feel.lang.EvaluationContext) FEELConditionsAndLoopsTest(org.kie.dmn.feel.runtime.FEELConditionsAndLoopsTest) FEELTernaryLogicTest(org.kie.dmn.feel.runtime.FEELTernaryLogicTest) Test(org.junit.Test) FEELParserTest(org.kie.dmn.feel.parser.feel11.FEELParserTest)

Example 7 with MapBackedType

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

the class CompositeTypeImpl method addField.

public void addField(String name, DMNType type) {
    this.fields.put(name, type);
    MapBackedType mbType = !isCollection() ? (MapBackedType) getFeelType() : (MapBackedType) ((GenListType) getFeelType()).getGen();
    mbType.addField(name, ((BaseDMNTypeImpl) type).getFeelType());
}
Also used : GenListType(org.kie.dmn.feel.lang.types.GenListType) MapBackedType(org.kie.dmn.feel.lang.impl.MapBackedType)

Example 8 with MapBackedType

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

the class CompileEvaluateTest method test2OK.

@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"));
}
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 9 with MapBackedType

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

the class CompileEvaluateTest method testHyphenInProperty.

@Test
public void testHyphenInProperty() {
    CompilerContext ctx = feel.newCompilerContext();
    ctx.addInputVariableType("input", new MapBackedType().addField("Primary-Key", BuiltInType.STRING).addField("Value", BuiltInType.STRING));
    CompiledExpression compiledExpression = feel.compile("input.Primary-Key", ctx);
    assertTrue(errors.isEmpty());
    Map<String, Object> inputs = new HashMap<>();
    inputs.put("input", prototype(entry("Primary-Key", "k987")));
    Object result = feel.evaluate(compiledExpression, inputs);
    assertThat(result, is("k987"));
    assertTrue(errors.isEmpty());
}
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)

Aggregations

MapBackedType (org.kie.dmn.feel.lang.impl.MapBackedType)9 Test (org.junit.Test)7 HashMap (java.util.HashMap)6 CompiledExpression (org.kie.dmn.feel.lang.CompiledExpression)5 CompilerContext (org.kie.dmn.feel.lang.CompilerContext)5 GenListType (org.kie.dmn.feel.lang.types.GenListType)3 Type (org.kie.dmn.feel.lang.Type)2 BaseNode (org.kie.dmn.feel.lang.ast.BaseNode)2 NameRefNode (org.kie.dmn.feel.lang.ast.NameRefNode)2 QualifiedNameNode (org.kie.dmn.feel.lang.ast.QualifiedNameNode)2 BuiltInType (org.kie.dmn.feel.lang.types.BuiltInType)2 StaticJavaParser (com.github.javaparser.StaticJavaParser)1 FieldDeclaration (com.github.javaparser.ast.body.FieldDeclaration)1 Parameter (com.github.javaparser.ast.body.Parameter)1 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)1 BooleanLiteralExpr (com.github.javaparser.ast.expr.BooleanLiteralExpr)1 ConditionalExpr (com.github.javaparser.ast.expr.ConditionalExpr)1 EnclosedExpr (com.github.javaparser.ast.expr.EnclosedExpr)1 Expression (com.github.javaparser.ast.expr.Expression)1 FieldAccessExpr (com.github.javaparser.ast.expr.FieldAccessExpr)1