Search in sources :

Example 6 with Type

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

the class ASTBuilderVisitor method visitQualifiedName.

@Override
public BaseNode visitQualifiedName(FEEL_1_1Parser.QualifiedNameContext ctx) {
    ArrayList<NameRefNode> parts = new ArrayList<>();
    Type typeCursor = null;
    for (FEEL_1_1Parser.NameRefContext t : ctx.nameRef()) {
        String originalText = ParserHelper.getOriginalText(t);
        if (typeCursor == null) {
            typeCursor = scopeHelper.resolve(originalText).orElse(BuiltInType.UNKNOWN);
        } else if (typeCursor instanceof CompositeType) {
            typeCursor = ((CompositeType) typeCursor).getFields().get(originalText);
        } else {
            // TODO throw error here?
            typeCursor = BuiltInType.UNKNOWN;
        }
        parts.add(ASTBuilderFactory.newNameRefNode(t, typeCursor));
    }
    return parts.size() > 1 ? ASTBuilderFactory.newQualifiedNameNode(ctx, parts, typeCursor) : parts.get(0);
}
Also used : CompositeType(org.kie.dmn.feel.lang.CompositeType) Type(org.kie.dmn.feel.lang.Type) BuiltInType(org.kie.dmn.feel.lang.types.BuiltInType) ArrayList(java.util.ArrayList) NameRefNode(org.kie.dmn.feel.lang.ast.NameRefNode) CompositeType(org.kie.dmn.feel.lang.CompositeType)

Example 7 with Type

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

the class ParserHelper method recoverScope.

public void recoverScope(String name) {
    LOG.trace("[{}] recoverScope( name: {}) with currentScope: {}", this.currentScope.getName(), name, currentScope);
    Scope s = this.currentScope.getChildScopes().get(name);
    if (s != null) {
        currentScope = s;
        if (currentScope.getType() != null && currentScope.getType().equals(BuiltInType.UNKNOWN)) {
            enableDynamicResolution();
        }
    } else {
        Symbol resolved = this.currentScope.resolve(name);
        Type scopeType = resolved != null ? resolved.getType() : null;
        if (scopeType instanceof GenListType) {
            scopeType = ((GenListType) scopeType).getGen();
        }
        if (resolved != null && scopeType instanceof CompositeType) {
            pushScope(scopeType);
            CompositeType type = (CompositeType) scopeType;
            for (Map.Entry<String, Type> f : type.getFields().entrySet()) {
                this.currentScope.define(new VariableSymbol(f.getKey(), f.getValue()));
            }
            LOG.trace(".. PUSHED, scope name {} with symbols {}", this.currentName.peek(), this.currentScope.getSymbols());
        } else if (resolved != null && scopeType instanceof SimpleType) {
            BuiltInType resolvedBIType = null;
            if (scopeType instanceof BuiltInType) {
                resolvedBIType = (BuiltInType) scopeType;
            } else if (scopeType instanceof AliasFEELType) {
                resolvedBIType = ((AliasFEELType) scopeType).getBuiltInType();
            } else {
                throw new UnsupportedOperationException("Unsupported BIType " + scopeType + "!");
            }
            pushScope(resolvedBIType);
            switch(resolvedBIType) {
                // FEEL spec table 53
                case DATE:
                    this.currentScope.define(new VariableSymbol("year", BuiltInType.NUMBER));
                    this.currentScope.define(new VariableSymbol("month", BuiltInType.NUMBER));
                    this.currentScope.define(new VariableSymbol("day", BuiltInType.NUMBER));
                    if (isFeatDMN12weekday()) {
                        // Table 60 spec DMN v1.2
                        this.currentScope.define(new VariableSymbol("weekday", BuiltInType.NUMBER));
                    }
                    break;
                case TIME:
                    this.currentScope.define(new VariableSymbol("hour", BuiltInType.NUMBER));
                    this.currentScope.define(new VariableSymbol("minute", BuiltInType.NUMBER));
                    this.currentScope.define(new VariableSymbol("second", BuiltInType.NUMBER));
                    this.currentScope.define(new VariableSymbol("time offset", BuiltInType.DURATION));
                    this.currentScope.define(new VariableSymbol("timezone", BuiltInType.NUMBER));
                    break;
                case DATE_TIME:
                    this.currentScope.define(new VariableSymbol("year", BuiltInType.NUMBER));
                    this.currentScope.define(new VariableSymbol("month", BuiltInType.NUMBER));
                    this.currentScope.define(new VariableSymbol("day", BuiltInType.NUMBER));
                    if (isFeatDMN12weekday()) {
                        // Table 60 spec DMN v1.2
                        this.currentScope.define(new VariableSymbol("weekday", BuiltInType.NUMBER));
                    }
                    this.currentScope.define(new VariableSymbol("hour", BuiltInType.NUMBER));
                    this.currentScope.define(new VariableSymbol("minute", BuiltInType.NUMBER));
                    this.currentScope.define(new VariableSymbol("second", BuiltInType.NUMBER));
                    this.currentScope.define(new VariableSymbol("time offset", BuiltInType.DURATION));
                    this.currentScope.define(new VariableSymbol("timezone", BuiltInType.NUMBER));
                    break;
                case DURATION:
                    // TODO might need to distinguish between `years and months duration` and `days and time duration`
                    this.currentScope.define(new VariableSymbol("years", BuiltInType.NUMBER));
                    this.currentScope.define(new VariableSymbol("months", BuiltInType.NUMBER));
                    this.currentScope.define(new VariableSymbol("days", BuiltInType.NUMBER));
                    this.currentScope.define(new VariableSymbol("hours", BuiltInType.NUMBER));
                    this.currentScope.define(new VariableSymbol("minutes", BuiltInType.NUMBER));
                    this.currentScope.define(new VariableSymbol("seconds", BuiltInType.NUMBER));
                    break;
                case RANGE:
                    this.currentScope.define(new VariableSymbol("start included", BuiltInType.BOOLEAN));
                    this.currentScope.define(new VariableSymbol("start", BuiltInType.UNKNOWN));
                    this.currentScope.define(new VariableSymbol("end", BuiltInType.UNKNOWN));
                    this.currentScope.define(new VariableSymbol("end included", BuiltInType.BOOLEAN));
                    break;
                // table 53 applies only to type(e) is a date/time/duration
                case UNKNOWN:
                    enableDynamicResolution();
                    break;
                default:
                    break;
            }
        } else {
            pushScope();
        }
    }
}
Also used : Symbol(org.kie.dmn.feel.lang.Symbol) VariableSymbol(org.kie.dmn.feel.lang.types.VariableSymbol) BuiltInType(org.kie.dmn.feel.lang.types.BuiltInType) SimpleType(org.kie.dmn.feel.lang.SimpleType) SimpleType(org.kie.dmn.feel.lang.SimpleType) CompositeType(org.kie.dmn.feel.lang.CompositeType) Type(org.kie.dmn.feel.lang.Type) BuiltInType(org.kie.dmn.feel.lang.types.BuiltInType) AliasFEELType(org.kie.dmn.feel.lang.types.AliasFEELType) GenListType(org.kie.dmn.feel.lang.types.GenListType) Scope(org.kie.dmn.feel.lang.Scope) GenListType(org.kie.dmn.feel.lang.types.GenListType) AliasFEELType(org.kie.dmn.feel.lang.types.AliasFEELType) Map(java.util.Map) VariableSymbol(org.kie.dmn.feel.lang.types.VariableSymbol) CompositeType(org.kie.dmn.feel.lang.CompositeType)

Example 8 with Type

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

the class FunctionTypeNode method evaluate.

@Override
public Type evaluate(EvaluationContext ctx) {
    List<Type> args = argTypes.stream().map(t -> t.evaluate(ctx)).collect(Collectors.toList());
    Type ret = retType.evaluate(ctx);
    return new GenFnType(args, ret);
}
Also used : List(java.util.List) GenFnType(org.kie.dmn.feel.lang.types.GenFnType) EvaluationContext(org.kie.dmn.feel.lang.EvaluationContext) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) Type(org.kie.dmn.feel.lang.Type) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) GenFnType(org.kie.dmn.feel.lang.types.GenFnType) Type(org.kie.dmn.feel.lang.Type) GenFnType(org.kie.dmn.feel.lang.types.GenFnType)

Example 9 with Type

use of org.kie.dmn.feel.lang.Type 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 10 with Type

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

the class DirectCompilerTest method testQualifiedName2.

@Test
public void testQualifiedName2() {
    String inputExpression = "My Person.Full Name";
    Type personType = JavaBackedType.of(MyPerson.class);
    CompiledFEELExpression qualRef = parse(inputExpression, mapOf(entry("My Person", personType)));
    LOG.debug("{}", qualRef);
    EvaluationContext context = CodegenTestUtil.newEmptyEvaluationContext();
    context.setValue("My Person", new MyPerson());
    Object result = qualRef.apply(context);
    LOG.debug("{}", result);
    assertThat(result, is("John Doe"));
}
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) 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)

Aggregations

Type (org.kie.dmn.feel.lang.Type)27 BuiltInType (org.kie.dmn.feel.lang.types.BuiltInType)21 ArrayList (java.util.ArrayList)8 DMNType (org.kie.dmn.api.core.DMNType)7 SimpleType (org.kie.dmn.feel.lang.SimpleType)7 MapBackedType (org.kie.dmn.feel.lang.impl.MapBackedType)7 HashMap (java.util.HashMap)6 List (java.util.List)6 CompositeType (org.kie.dmn.feel.lang.CompositeType)6 Map (java.util.Map)5 UnknownType (com.github.javaparser.ast.type.UnknownType)4 Collectors (java.util.stream.Collectors)4 Test (org.junit.Test)4 EvaluationContext (org.kie.dmn.feel.lang.EvaluationContext)4 BaseNode (org.kie.dmn.feel.lang.ast.BaseNode)4 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)3 CompositeTypeImpl (org.kie.dmn.core.impl.CompositeTypeImpl)3 SimpleTypeImpl (org.kie.dmn.core.impl.SimpleTypeImpl)3 JavaBackedType (org.kie.dmn.feel.lang.impl.JavaBackedType)3 AliasFEELType (org.kie.dmn.feel.lang.types.AliasFEELType)3