Search in sources :

Example 1 with CompositeType

use of org.kie.dmn.feel.lang.CompositeType 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.resolveType(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 2 with CompositeType

use of org.kie.dmn.feel.lang.CompositeType 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);
        if (resolved != null && resolved.getType() instanceof CompositeType) {
            pushName(name);
            pushScope(resolved.getType());
            CompositeType type = (CompositeType) resolved.getType();
            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 && resolved.getType() instanceof BuiltInType) {
            BuiltInType resolvedBIType = (BuiltInType) resolved.getType();
            pushName(name);
            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));
                    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.NUMBER));
                    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));
                    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.NUMBER));
                    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;
                // table 53 applies only to type(e) is a date/time/duration
                case UNKNOWN:
                    enableDynamicResolution();
                    break;
                default:
                    break;
            }
        } else {
            pushScope();
        }
    }
}
Also used : CompositeType(org.kie.dmn.feel.lang.CompositeType) Type(org.kie.dmn.feel.lang.Type) JavaBackedType(org.kie.dmn.feel.lang.impl.JavaBackedType) BuiltInType(org.kie.dmn.feel.lang.types.BuiltInType) Scope(org.kie.dmn.feel.lang.Scope) Symbol(org.kie.dmn.feel.lang.Symbol) VariableSymbol(org.kie.dmn.feel.lang.types.VariableSymbol) BuiltInType(org.kie.dmn.feel.lang.types.BuiltInType) Map(java.util.Map) VariableSymbol(org.kie.dmn.feel.lang.types.VariableSymbol) CompositeType(org.kie.dmn.feel.lang.CompositeType)

Example 3 with CompositeType

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

the class JavaBackedTypeTest method testMyPojoNoMethodAnn.

@Test
public void testMyPojoNoMethodAnn() {
    CompositeType personType = (CompositeType) JavaBackedType.of(MyPojoNoMethodAnn.class);
    Set<String> personProperties = personType.getFields().keySet();
    assertThat(personProperties, hasItem("a"));
    assertThat(personProperties, hasItem("b"));
    // should not include private methods
    assertThat(personProperties, not(hasItem("bPrivate")));
    // should not include methods which are actually Object methods.
    assertThat(personProperties, not(hasItem("equals")));
    assertThat(personProperties, not(hasItem("wait")));
}
Also used : CompositeType(org.kie.dmn.feel.lang.CompositeType) Test(org.junit.Test)

Example 4 with CompositeType

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

the class JavaBackedTypeTest method testPerson.

@Test
public void testPerson() {
    CompositeType personType = (CompositeType) JavaBackedType.of(Person.class);
    Set<String> personProperties = personType.getFields().keySet();
    assertThat(personProperties, hasItem("home address"));
    assertThat(personProperties, hasItem("address"));
    // for consistency and to fix possible hierarchy resolution problem, we add the property also as per standard JavaBean specs.
    assertThat(personProperties, hasItem("homeAddress"));
}
Also used : Person(org.kie.dmn.feel.model.Person) CompositeType(org.kie.dmn.feel.lang.CompositeType) Test(org.junit.Test)

Aggregations

CompositeType (org.kie.dmn.feel.lang.CompositeType)4 Test (org.junit.Test)2 Type (org.kie.dmn.feel.lang.Type)2 BuiltInType (org.kie.dmn.feel.lang.types.BuiltInType)2 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Scope (org.kie.dmn.feel.lang.Scope)1 Symbol (org.kie.dmn.feel.lang.Symbol)1 NameRefNode (org.kie.dmn.feel.lang.ast.NameRefNode)1 JavaBackedType (org.kie.dmn.feel.lang.impl.JavaBackedType)1 VariableSymbol (org.kie.dmn.feel.lang.types.VariableSymbol)1 Person (org.kie.dmn.feel.model.Person)1