Search in sources :

Example 1 with BuiltInType

use of org.kie.dmn.feel.lang.types.BuiltInType 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 2 with BuiltInType

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

the class DMNAllTypesIndex method convertBuiltin.

private String convertBuiltin(DMNType expectedFEELType) {
    BuiltInType builtin = DMNTypeUtils.getFEELBuiltInType(expectedFEELType);
    Class<?> convertedClass = Object.class;
    if (builtin == BuiltInType.DURATION) {
        convertedClass = convertDurationToJavaClass(expectedFEELType);
    } else {
        convertedClass = convertBuiltInToJavaClass(builtin);
    }
    return convertedClass.getCanonicalName();
}
Also used : BuiltInType(org.kie.dmn.feel.lang.types.BuiltInType)

Example 3 with BuiltInType

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

the class DMNRuntimeTest method testYearsAndMonthsDuration.

@Test
public void testYearsAndMonthsDuration() {
    final DMNRuntime runtime = DMNRuntimeUtil.createRuntime("yearMonthDuration.dmn", this.getClass());
    final DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/dmn/definitions/_6eda1490-21ca-441e-8a26-ab3ca800e43c", "Drawing 1");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    final BuiltInType feelType = (BuiltInType) BuiltInType.determineTypeFromName("yearMonthDuration");
    final ChronoPeriod period = (ChronoPeriod) feelType.fromString("P2Y1M");
    final DMNContext context = runtime.newContext();
    context.set("iDuration", period);
    final DMNResult dmnResult = runtime.evaluateAll(dmnModel, context);
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult.getMessages()), dmnResult.hasErrors(), is(false));
    final DMNContext result = dmnResult.getContext();
    assertThat(result.get("How long"), is("Longer than a year"));
}
Also used : ChronoPeriod(java.time.chrono.ChronoPeriod) DMNResult(org.kie.dmn.api.core.DMNResult) DMNContext(org.kie.dmn.api.core.DMNContext) BuiltInType(org.kie.dmn.feel.lang.types.BuiltInType) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DMNModel(org.kie.dmn.api.core.DMNModel) Test(org.junit.Test)

Example 4 with BuiltInType

use of org.kie.dmn.feel.lang.types.BuiltInType in project tck by dmn-tck.

the class DroolsTCKTest method parseType.

private Object parseType(ValueType value, DMNType dmnType) {
    if (value.getList() != null && !value.getList().isNil()) {
        List<Object> result = new ArrayList<>();
        ValueType.List list = value.getList().getValue();
        for (ValueType vt : list.getItem()) {
            result.add(parseType(vt, dmnType));
        }
        return result;
    } else if (!dmnType.isComposite()) {
        String text = null;
        Object val = value.getValue();
        if (val != null && !value.getValue().isNil() && val instanceof JAXBElement<?> && ((JAXBElement<?>) val).getValue() instanceof Node && !isDateTimeOrDuration(((JAXBElement<?>) val).getValue())) {
            Node nodeVal = (Node) ((JAXBElement<?>) val).getValue();
            if (nodeVal.getFirstChild() != null) {
                text = nodeVal.getFirstChild().getTextContent();
            }
            return text != null ? ((BuiltInType) ((BaseDMNTypeImpl) dmnType).getFeelType()).fromString(text) : null;
        } else if (val instanceof JAXBElement<?> && !(((JAXBElement<?>) val).getValue() instanceof Node) && !isDateTimeOrDuration(((JAXBElement<?>) val).getValue())) {
            return ((JAXBElement<?>) val).getValue();
        } else {
            try {
                Object dateTimeOrDurationValue = (val != null) ? ((JAXBElement<?>) val).getValue() : null;
                if (dateTimeOrDurationValue instanceof Duration || dateTimeOrDurationValue instanceof XMLGregorianCalendar) {
                    // need to convert to java.time.* equivalent
                    text = dateTimeOrDurationValue.toString();
                    return text != null ? ((BuiltInType) ((BaseDMNTypeImpl) dmnType).getFeelType()).fromString(text) : null;
                }
            } catch (Exception e) {
                logger.error("Error trying to coerce JAXB type " + val.getClass().getName() + " with value '" + val.toString() + "': " + e.getMessage());
            }
            return val;
        }
    } else {
        Map<String, Object> result = new HashMap<>();
        for (ValueType.Component component : value.getComponent()) {
            if (!dmnType.getFields().containsKey(component.getName())) {
                throw new RuntimeException("Error parsing input: unknown field '" + component.getName() + "' for type '" + dmnType.getName() + "'");
            }
            DMNType fieldType = dmnType.getFields().get(component.getName());
            if (fieldType == null) {
                throw new RuntimeException("Error parsing input: unknown type for field '" + component.getName() + "' on type " + dmnType.getName() + "'");
            }
            Object fieldValue = parseType(component, fieldType);
            result.put(component.getName(), fieldValue);
        }
        return result;
    }
}
Also used : ValueType(org.omg.dmn.tck.marshaller._20160719.ValueType) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) Node(org.w3c.dom.Node) InputDataNode(org.kie.dmn.api.core.ast.InputDataNode) ArrayList(java.util.ArrayList) Duration(javax.xml.datatype.Duration) JAXBElement(javax.xml.bind.JAXBElement) BuiltInType(org.kie.dmn.feel.lang.types.BuiltInType) BaseDMNTypeImpl(org.kie.dmn.core.impl.BaseDMNTypeImpl) MalformedURLException(java.net.MalformedURLException) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) HashMap(java.util.HashMap) Map(java.util.Map) DMNType(org.kie.dmn.api.core.DMNType)

Example 5 with BuiltInType

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

Aggregations

BuiltInType (org.kie.dmn.feel.lang.types.BuiltInType)11 DMNType (org.kie.dmn.api.core.DMNType)6 Type (org.kie.dmn.feel.lang.Type)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ArrayList (java.util.ArrayList)3 DMNModel (org.kie.dmn.api.core.DMNModel)3 CompositeTypeImpl (org.kie.dmn.core.impl.CompositeTypeImpl)3 SimpleTypeImpl (org.kie.dmn.core.impl.SimpleTypeImpl)3 InputStream (java.io.InputStream)2 ChronoPeriod (java.time.chrono.ChronoPeriod)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 OutputField (org.dmg.pmml.OutputField)2 DecisionNode (org.kie.dmn.api.core.ast.DecisionNode)2 InputDataNode (org.kie.dmn.api.core.ast.InputDataNode)2 BaseDMNTypeImpl (org.kie.dmn.core.impl.BaseDMNTypeImpl)2 DMNModelImpl (org.kie.dmn.core.impl.DMNModelImpl)2