Search in sources :

Example 21 with Type

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

the class InstanceOfNode method evaluate.

@Override
public Object evaluate(EvaluationContext ctx) {
    Object value = expression.evaluate(ctx);
    Type t = type.evaluate(ctx);
    if (t != BuiltInType.DURATION) {
        return t.isInstanceOf(value);
    } else {
        switch(type.getText()) {
            case SimpleType.YEARS_AND_MONTHS_DURATION:
                return value instanceof ChronoPeriod;
            case SimpleType.DAYS_AND_TIME_DURATION:
                return value instanceof Duration;
            default:
                return t.isInstanceOf(value);
        }
    }
}
Also used : ChronoPeriod(java.time.chrono.ChronoPeriod) BuiltInType(org.kie.dmn.feel.lang.types.BuiltInType) SimpleType(org.kie.dmn.feel.lang.SimpleType) Type(org.kie.dmn.feel.lang.Type) Duration(java.time.Duration)

Example 22 with Type

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

the class RangeNode method evaluate.

@Override
public Range evaluate(EvaluationContext ctx) {
    Object s = start.evaluate(ctx);
    Object e = end.evaluate(ctx);
    Type sType = BuiltInType.determineTypeFromInstance(s);
    Type eType = BuiltInType.determineTypeFromInstance(e);
    if (s != null && e != null && sType != eType && !s.getClass().isAssignableFrom(e.getClass())) {
        ctx.notifyEvt(astEvent(Severity.ERROR, Msg.createMessage(Msg.X_TYPE_INCOMPATIBLE_WITH_Y_TYPE, "Start", "End")));
        return null;
    }
    Comparable start = convertToComparable(ctx, s);
    Comparable end = convertToComparable(ctx, e);
    return new RangeImpl(lowerBound == IntervalBoundary.OPEN ? Range.RangeBoundary.OPEN : Range.RangeBoundary.CLOSED, start, end, upperBound == IntervalBoundary.OPEN ? Range.RangeBoundary.OPEN : Range.RangeBoundary.CLOSED);
}
Also used : BuiltInType(org.kie.dmn.feel.lang.types.BuiltInType) Type(org.kie.dmn.feel.lang.Type) RangeImpl(org.kie.dmn.feel.runtime.impl.RangeImpl)

Example 23 with Type

use of org.kie.dmn.feel.lang.Type in project kie-wb-common by kiegroup.

the class FEELLanguageServiceTest method assertGetType.

private void assertGetType(final String expression, final BuiltInType expected) {
    final String cursor = "|";
    final int line = expression.substring(0, expression.indexOf(cursor)).split("\n").length;
    final int column = expression.split("\n")[line - 1].indexOf(cursor);
    final Position position = new Position(line, column);
    final String expressionToParse = expression.replace(cursor, "");
    final BaseNode astNode = getASTNode(expressionToParse);
    final Type actual = service.getType(astNode, position);
    assertEquals(expected, actual);
}
Also used : BuiltInType(org.kie.dmn.feel.lang.types.BuiltInType) Type(org.kie.dmn.feel.lang.Type) Position(org.kie.workbench.common.dmn.client.widgets.codecompletion.feel.FEELLanguageService.Position) BaseNode(org.kie.dmn.feel.lang.ast.BaseNode)

Example 24 with Type

use of org.kie.dmn.feel.lang.Type in project kie-wb-common by kiegroup.

the class TypeStackUtils method getTypeStack.

private List<Type> getTypeStack(final ASTNode currentNode, final ASTNode nextNode, final Position position, final boolean isListOfParameters, final boolean isParentEligible) {
    final List<Type> typeStack = new ArrayList<>();
    if (currentNode == null) {
        return typeStack;
    }
    final Type type = getType(currentNode);
    final boolean isNextListOfParameters = currentNode instanceof FunctionInvocationNode;
    final boolean isEligibleType = isEligibleType(currentNode, nextNode, position, isParentEligible) && !isListOfParameters;
    if (isEligibleType) {
        typeStack.add(type);
    }
    try {
        forEach(getChildren(currentNode), (current, next) -> {
            final boolean isParentEligibleType = isListOfParameters ? isParentEligible : isEligibleType;
            typeStack.addAll(getTypeStack(current, next, position, isNextListOfParameters, isParentEligibleType));
        });
    } catch (final Exception e) {
    // Ignore errors during node inspection.
    }
    return typeStack;
}
Also used : BuiltInType(org.kie.dmn.feel.lang.types.BuiltInType) Type(org.kie.dmn.feel.lang.Type) ArrayList(java.util.ArrayList) FunctionInvocationNode(org.kie.dmn.feel.lang.ast.FunctionInvocationNode)

Example 25 with Type

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

the class DMNUtilsTest method getRootType.

@Test
public void getRootType() {
    SimpleTypeImpl simpleTypeAny = new SimpleTypeImpl(null, "tSimple", null, false, Collections.emptyList(), null, BuiltInType.UNKNOWN);
    assertEquals(BuiltInType.UNKNOWN, DMNUtils.getRootType(simpleTypeAny));
    Type aliasFeelType = new AliasFEELType("alias", BuiltInType.UNKNOWN);
    CompositeTypeImpl aliasType = new CompositeTypeImpl(null, "tSimple", null, false, Collections.emptyMap(), simpleTypeAny, aliasFeelType);
    assertEquals(BuiltInType.UNKNOWN, DMNUtils.getRootType(aliasType));
    Type notBuiltInType = new AliasFEELType("notBuiltIn", BuiltInType.UNKNOWN);
    SimpleTypeImpl notBuiltIn = new SimpleTypeImpl(null, "tSimple", null, false, Collections.emptyList(), null, notBuiltInType);
    assertEquals(notBuiltInType, DMNUtils.getRootType(notBuiltIn));
}
Also used : BuiltInType(org.kie.dmn.feel.lang.types.BuiltInType) Type(org.kie.dmn.feel.lang.Type) AliasFEELType(org.kie.dmn.feel.lang.types.AliasFEELType) SimpleTypeImpl(org.kie.dmn.core.impl.SimpleTypeImpl) AliasFEELType(org.kie.dmn.feel.lang.types.AliasFEELType) CompositeTypeImpl(org.kie.dmn.core.impl.CompositeTypeImpl) Test(org.junit.Test)

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