Search in sources :

Example 1 with MapBackedType

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

the class ASTCompilerVisitor method visit.

@Override
public DirectCompilerResult visit(ContextNode n) {
    if (n.getEntries().isEmpty()) {
        return DirectCompilerResult.of(FeelCtx.emptyContext(), BuiltInType.CONTEXT);
    }
    scopeHelper.pushScope();
    // openContext(feelCtx)
    MapBackedType resultType = new MapBackedType();
    DirectCompilerResult openContext = DirectCompilerResult.of(FeelCtx.openContext(), resultType);
    // .setEntry( k,v )
    // .setEntry( k,v )
    // ...
    DirectCompilerResult entries = n.getEntries().stream().map(e -> {
        DirectCompilerResult r = e.accept(this);
        scopeHelper.addInScope(e.getName().getText(), r.resultType);
        return r;
    }).reduce(openContext, (l, r) -> DirectCompilerResult.of(r.getExpression().asMethodCallExpr().setScope(l.getExpression()), r.resultType, DirectCompilerResult.mergeFDs(l, r)));
    scopeHelper.popScope();
    // .closeContext()
    return DirectCompilerResult.of(FeelCtx.closeContext(entries), resultType, entries.getFieldDeclarations());
}
Also used : QuantifiedExpressionNode(org.kie.dmn.feel.lang.ast.QuantifiedExpressionNode) SimpleType(org.kie.dmn.feel.lang.SimpleType) Visitor(org.kie.dmn.feel.lang.ast.Visitor) ListNode(org.kie.dmn.feel.lang.ast.ListNode) ContextNode(org.kie.dmn.feel.lang.ast.ContextNode) FunctionInvocationNode(org.kie.dmn.feel.lang.ast.FunctionInvocationNode) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) InNode(org.kie.dmn.feel.lang.ast.InNode) BaseNode(org.kie.dmn.feel.lang.ast.BaseNode) Duration(java.time.Duration) Map(java.util.Map) Msg(org.kie.dmn.feel.util.Msg) NumberNode(org.kie.dmn.feel.lang.ast.NumberNode) Expression(com.github.javaparser.ast.expr.Expression) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) ForExpressionNode(org.kie.dmn.feel.lang.ast.ForExpressionNode) IfExpressionNode(org.kie.dmn.feel.lang.ast.IfExpressionNode) UnknownType(com.github.javaparser.ast.type.UnknownType) DashNode(org.kie.dmn.feel.lang.ast.DashNode) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) BuiltInType(org.kie.dmn.feel.lang.types.BuiltInType) TypeNode(org.kie.dmn.feel.lang.ast.TypeNode) ASTNode(org.kie.dmn.feel.lang.ast.ASTNode) Collectors(java.util.stream.Collectors) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) List(java.util.List) StaticJavaParser(com.github.javaparser.StaticJavaParser) InfixOpNode(org.kie.dmn.feel.lang.ast.InfixOpNode) QualifiedNameNode(org.kie.dmn.feel.lang.ast.QualifiedNameNode) Entry(java.util.Map.Entry) NullNode(org.kie.dmn.feel.lang.ast.NullNode) CTypeNode(org.kie.dmn.feel.lang.ast.CTypeNode) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) FunctionTypeNode(org.kie.dmn.feel.lang.ast.FunctionTypeNode) ChronoPeriod(java.time.chrono.ChronoPeriod) FunctionDefNode(org.kie.dmn.feel.lang.ast.FunctionDefNode) EvalHelper(org.kie.dmn.feel.util.EvalHelper) ScopeHelper(org.kie.dmn.feel.parser.feel11.ScopeHelper) ContextEntryNode(org.kie.dmn.feel.lang.ast.ContextEntryNode) NamedParameterNode(org.kie.dmn.feel.lang.ast.NamedParameterNode) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) NameRefNode(org.kie.dmn.feel.lang.ast.NameRefNode) MapBackedType(org.kie.dmn.feel.lang.impl.MapBackedType) ConditionalExpr(com.github.javaparser.ast.expr.ConditionalExpr) UnaryTestNode(org.kie.dmn.feel.lang.ast.UnaryTestNode) NameDefNode(org.kie.dmn.feel.lang.ast.NameDefNode) Parameter(com.github.javaparser.ast.body.Parameter) HashMap(java.util.HashMap) IterationContextNode(org.kie.dmn.feel.lang.ast.IterationContextNode) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) CompositeType(org.kie.dmn.feel.lang.CompositeType) TemporalConstantNode(org.kie.dmn.feel.lang.ast.TemporalConstantNode) BooleanNode(org.kie.dmn.feel.lang.ast.BooleanNode) Type(org.kie.dmn.feel.lang.Type) AtLiteralNode(org.kie.dmn.feel.lang.ast.AtLiteralNode) ListTypeNode(org.kie.dmn.feel.lang.ast.ListTypeNode) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) UnaryTestListNode(org.kie.dmn.feel.lang.ast.UnaryTestListNode) RangeNode(org.kie.dmn.feel.lang.ast.RangeNode) FormalParameterNode(org.kie.dmn.feel.lang.ast.FormalParameterNode) NameExpr(com.github.javaparser.ast.expr.NameExpr) PathExpressionNode(org.kie.dmn.feel.lang.ast.PathExpressionNode) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) DirectCompilerResult.mergeFDs(org.kie.dmn.feel.codegen.feel11.DirectCompilerResult.mergeFDs) SignedUnaryNode(org.kie.dmn.feel.lang.ast.SignedUnaryNode) TypeAndFn(org.kie.dmn.feel.lang.ast.AtLiteralNode.TypeAndFn) BetweenNode(org.kie.dmn.feel.lang.ast.BetweenNode) ContextTypeNode(org.kie.dmn.feel.lang.ast.ContextTypeNode) FilterExpressionNode(org.kie.dmn.feel.lang.ast.FilterExpressionNode) IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) InstanceOfNode(org.kie.dmn.feel.lang.ast.InstanceOfNode) StringNode(org.kie.dmn.feel.lang.ast.StringNode) MapBackedType(org.kie.dmn.feel.lang.impl.MapBackedType)

Example 2 with MapBackedType

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

the class FEELParserTest method testQualifiedName.

@Test
public void testQualifiedName() {
    String inputExpression = "My Person.Full Name";
    MapBackedType personType = new MapBackedType("Person", mapOf(entry("Full Name", BuiltInType.STRING), entry("Age", BuiltInType.NUMBER)));
    BaseNode qualRef = parse(inputExpression, mapOf(entry("My Person", personType)));
    assertThat(qualRef, is(instanceOf(QualifiedNameNode.class)));
    assertThat(qualRef.getResultType(), is(BuiltInType.STRING));
    List<NameRefNode> parts = ((QualifiedNameNode) qualRef).getParts();
    // `My Person` ...
    assertThat(parts.get(0), is(instanceOf(NameRefNode.class)));
    assertThat(parts.get(0).getResultType(), is(personType));
    // ... `.Full Name`
    assertThat(parts.get(1), is(instanceOf(NameRefNode.class)));
    assertThat(parts.get(1).getResultType(), is(BuiltInType.STRING));
    assertLocation(inputExpression, qualRef);
}
Also used : BaseNode(org.kie.dmn.feel.lang.ast.BaseNode) MapBackedType(org.kie.dmn.feel.lang.impl.MapBackedType) QualifiedNameNode(org.kie.dmn.feel.lang.ast.QualifiedNameNode) NameRefNode(org.kie.dmn.feel.lang.ast.NameRefNode) Test(org.junit.Test)

Example 3 with MapBackedType

use of org.kie.dmn.feel.lang.impl.MapBackedType 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 4 with MapBackedType

use of org.kie.dmn.feel.lang.impl.MapBackedType 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 5 with MapBackedType

use of org.kie.dmn.feel.lang.impl.MapBackedType 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)

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