Search in sources :

Example 6 with BuiltInType

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

the class DMNScenarioValidation method isConstraintAdded.

/**
 * To define if a constraint (allowed values) were added in a DMNType fieldType given a typeName, the following conditions
 * are requires:
 * - typeName MUST BE a BuiltInType (eg. STRING, NUMERIC ..)
 * - DMNType fieldType MUST have at least one defined Allowed Values
 * - DMNType fieldType MUST have a Base Type. It's name MUST be equals to given typeName.
 * @param typeName TypeName present in the scesim file
 * @param fieldType DMNType of field under analysis
 * @return
 */
private boolean isConstraintAdded(String typeName, DMNType fieldType) {
    boolean isTypeNameBuiltInType = !Objects.equals(UNKNOWN, BuiltInType.determineTypeFromName(typeName));
    boolean hasFieldTypeAllowedValues = fieldType.getAllowedValues() != null && !fieldType.getAllowedValues().isEmpty();
    boolean hasFieldTypeBaseType = Objects.nonNull(fieldType.getBaseType());
    if (isTypeNameBuiltInType && hasFieldTypeBaseType && hasFieldTypeAllowedValues) {
        Type baseType = getRootType((BaseDMNTypeImpl) fieldType.getBaseType());
        return Objects.equals(typeName, baseType.getName());
    }
    return false;
}
Also used : DMNType(org.kie.dmn.api.core.DMNType) BuiltInType(org.kie.dmn.feel.lang.types.BuiltInType) DMNUtils.getRootType(org.drools.workbench.screens.scenariosimulation.backend.server.util.DMNUtils.getRootType) DMNUtils.navigateDMNType(org.drools.workbench.screens.scenariosimulation.backend.server.util.DMNUtils.navigateDMNType) Type(org.kie.dmn.feel.lang.Type)

Example 7 with BuiltInType

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

the class DMNCompilerImpl method buildTypeDef.

/**
 * @param topLevel null if it is a top level ItemDefinition
 */
private DMNType buildTypeDef(DMNCompilerContext ctx, DMNModelImpl dmnModel, DMNNode node, ItemDefinition itemDef, DMNType topLevel) {
    BaseDMNTypeImpl type = null;
    if (itemDef.getTypeRef() != null) {
        // this is a reference to an existing type, so resolve the reference
        type = (BaseDMNTypeImpl) resolveTypeRef(dmnModel, itemDef, itemDef, itemDef.getTypeRef());
        if (type != null) {
            UnaryTests allowedValuesStr = itemDef.getAllowedValues();
            // or if it changes the metadata for the base type
            if (topLevel == null || allowedValuesStr != null || itemDef.isIsCollection() != type.isCollection()) {
                // we have to clone this type definition into a new one
                String name = itemDef.getName();
                String namespace = dmnModel.getNamespace();
                String id = itemDef.getId();
                BaseDMNTypeImpl baseType = type;
                Type baseFEELType = type.getFeelType();
                if (baseFEELType instanceof BuiltInType) {
                    // Then it is an ItemDefinition in place for "aliasing" a base FEEL type, for having type(itemDefname) I need to define its SimpleType.
                    baseFEELType = new AliasFEELType(itemDef.getName(), (BuiltInType) baseFEELType);
                }
                List<UnaryTest> av = null;
                if (allowedValuesStr != null) {
                    av = ctx.getFeelHelper().evaluateUnaryTests(ctx, allowedValuesStr.getText(), dmnModel, itemDef, Msg.ERR_COMPILING_ALLOWED_VALUES_LIST_ON_ITEM_DEF, allowedValuesStr.getText(), node.getName());
                }
                boolean isCollection = itemDef.isIsCollection();
                if (isCollection) {
                    baseFEELType = new GenListType(baseFEELType);
                }
                if (type instanceof CompositeTypeImpl) {
                    CompositeTypeImpl compositeTypeImpl = (CompositeTypeImpl) type;
                    type = new CompositeTypeImpl(namespace, name, id, isCollection, compositeTypeImpl.getFields(), baseType, baseFEELType);
                } else if (type instanceof SimpleTypeImpl) {
                    type = new SimpleTypeImpl(namespace, name, id, isCollection, av, baseType, baseFEELType);
                }
                if (topLevel != null) {
                    ((BaseDMNTypeImpl) type).setBelongingType(topLevel);
                }
            }
            if (topLevel == null) {
                DMNType registered = dmnModel.getTypeRegistry().registerType(type);
                if (registered != type) {
                    MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, itemDef, dmnModel, null, null, Msg.DUPLICATED_ITEM_DEFINITION, itemDef.getName());
                }
            }
        }
    } else if (itemDef.getItemComponent() != null && itemDef.getItemComponent().size() > 0) {
        // first, locate preregistered or create anonymous inner composite
        if (topLevel == null) {
            type = (CompositeTypeImpl) dmnModel.getTypeRegistry().resolveType(dmnModel.getNamespace(), itemDef.getName());
        } else {
            DMNCompilerHelper.checkVariableName(dmnModel, itemDef, itemDef.getName());
            type = new CompositeTypeImpl(dmnModel.getNamespace(), itemDef.getName(), itemDef.getId(), itemDef.isIsCollection());
            ((BaseDMNTypeImpl) type).setBelongingType(topLevel);
        }
        // second, add fields to located composite
        for (ItemDefinition fieldDef : itemDef.getItemComponent()) {
            DMNCompilerHelper.checkVariableName(dmnModel, fieldDef, fieldDef.getName());
            DMNType fieldType = buildTypeDef(ctx, dmnModel, node, fieldDef, type);
            fieldType = fieldType != null ? fieldType : dmnModel.getTypeRegistry().unknown();
            ((CompositeTypeImpl) type).addField(fieldDef.getName(), fieldType);
        }
    } else if (isFunctionItem(itemDef)) {
        FunctionItem fi = itemDef.getFunctionItem();
        String name = itemDef.getName();
        String namespace = dmnModel.getNamespace();
        String id = itemDef.getId();
        Map<String, DMNType> params = new HashMap<>();
        for (InformationItem p : fi.getParameters()) {
            DMNType resolveTypeRef = resolveTypeRef(dmnModel, itemDef, itemDef, p.getTypeRef());
            params.put(p.getName(), resolveTypeRef);
        }
        DMNType returnType = resolveTypeRef(dmnModel, itemDef, itemDef, fi.getOutputTypeRef());
        List<Type> feelPs = fi.getParameters().stream().map(InformationItem::getName).map(n -> ((BaseDMNTypeImpl) params.get(n)).getFeelType()).collect(Collectors.toList());
        GenFnType feeltype = new GenFnType(feelPs, ((BaseDMNTypeImpl) returnType).getFeelType());
        type = new SimpleFnTypeImpl(namespace, name, id, feeltype, params, returnType, fi);
        DMNType registered = dmnModel.getTypeRegistry().registerType(type);
        if (registered != type) {
            MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, itemDef, dmnModel, null, null, Msg.DUPLICATED_ITEM_DEFINITION, itemDef.getName());
        }
    } else {
        DMNType unknown = (BaseDMNTypeImpl) resolveTypeRef(dmnModel, itemDef, itemDef, null);
        type = new SimpleTypeImpl(dmnModel.getNamespace(), itemDef.getName(), itemDef.getId(), itemDef.isIsCollection(), null, unknown, ((BaseDMNTypeImpl) unknown).getFeelType());
        if (topLevel == null) {
            DMNType registered = dmnModel.getTypeRegistry().registerType(type);
            if (registered != type) {
                MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, itemDef, dmnModel, null, null, Msg.DUPLICATED_ITEM_DEFINITION, itemDef.getName());
            }
        } else {
            ((BaseDMNTypeImpl) type).setBelongingType(topLevel);
        }
    }
    return type;
}
Also used : DMNModelInstrumentedBase(org.kie.dmn.model.api.DMNModelInstrumentedBase) DecisionTable(org.kie.dmn.model.api.DecisionTable) DMNElementReference(org.kie.dmn.model.api.DMNElementReference) DMNMessage(org.kie.dmn.api.core.DMNMessage) ListIterator(java.util.ListIterator) URL(java.net.URL) SimpleFnTypeImpl(org.kie.dmn.core.impl.SimpleFnTypeImpl) FEELProfile(org.kie.dmn.feel.lang.FEELProfile) URISyntaxException(java.net.URISyntaxException) ResourceFactory(org.kie.internal.io.ResourceFactory) LoggerFactory(org.slf4j.LoggerFactory) ItemDefNodeImpl(org.kie.dmn.core.ast.ItemDefNodeImpl) Either(org.kie.dmn.feel.util.Either) FileSystemResource(org.drools.core.io.impl.FileSystemResource) SimpleTypeImpl(org.kie.dmn.core.impl.SimpleTypeImpl) DMNCompiler(org.kie.dmn.api.core.DMNCompiler) TInformationItem(org.kie.dmn.model.v1_1.TInformationItem) Matcher(java.util.regex.Matcher) DMNModel(org.kie.dmn.api.core.DMNModel) Map(java.util.Map) URI(java.net.URI) DMNCompilerConfiguration(org.kie.dmn.api.core.DMNCompilerConfiguration) DMNFactory(org.kie.dmn.core.api.DMNFactory) UnaryTest(org.kie.dmn.feel.runtime.UnaryTest) ImportType(org.kie.dmn.core.compiler.ImportDMNResolverUtil.ImportType) DecisionServices(org.kie.dmn.model.v1_1.extensions.DecisionServices) DMNNode(org.kie.dmn.api.core.ast.DMNNode) BusinessKnowledgeModelNodeImpl(org.kie.dmn.core.ast.BusinessKnowledgeModelNodeImpl) Collection(java.util.Collection) BaseDMNTypeImpl(org.kie.dmn.core.impl.BaseDMNTypeImpl) BuiltInType(org.kie.dmn.feel.lang.types.BuiltInType) OutputClause(org.kie.dmn.model.api.OutputClause) Set(java.util.Set) DMNModelImpl(org.kie.dmn.core.impl.DMNModelImpl) Import(org.kie.dmn.model.api.Import) ItemDefNode(org.kie.dmn.api.core.ast.ItemDefNode) Reader(java.io.Reader) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) BusinessKnowledgeModelNode(org.kie.dmn.api.core.ast.BusinessKnowledgeModelNode) FunctionItem(org.kie.dmn.model.api.FunctionItem) Resource(org.kie.api.io.Resource) List(java.util.List) NamedElement(org.kie.dmn.model.api.NamedElement) Entry(java.util.Map.Entry) ItemDefinition(org.kie.dmn.model.api.ItemDefinition) Optional(java.util.Optional) InputDataNode(org.kie.dmn.api.core.ast.InputDataNode) Pattern(java.util.regex.Pattern) QName(javax.xml.namespace.QName) InformationItem(org.kie.dmn.model.api.InformationItem) DMNMarshaller(org.kie.dmn.api.marshalling.DMNMarshaller) AliasFEELType(org.kie.dmn.feel.lang.types.AliasFEELType) GenListType(org.kie.dmn.feel.lang.types.GenListType) MsgUtil(org.kie.dmn.core.util.MsgUtil) DMNType(org.kie.dmn.api.core.DMNType) GenFnType(org.kie.dmn.feel.lang.types.GenFnType) HashMap(java.util.HashMap) Deque(java.util.Deque) Function(java.util.function.Function) Definitions(org.kie.dmn.model.api.Definitions) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) DecisionServiceNodeImpl(org.kie.dmn.core.ast.DecisionServiceNodeImpl) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) DMNMarshallerFactory(org.kie.dmn.backend.marshalling.v1x.DMNMarshallerFactory) DMNBaseNode(org.kie.dmn.core.ast.DMNBaseNode) Decision(org.kie.dmn.model.api.Decision) LinkedList(java.util.LinkedList) XMLConstants(javax.xml.XMLConstants) Type(org.kie.dmn.feel.lang.Type) Logger(org.slf4j.Logger) InformationRequirement(org.kie.dmn.model.api.InformationRequirement) KnowledgeRequirement(org.kie.dmn.model.api.KnowledgeRequirement) IOException(java.io.IOException) DMNExtensionRegister(org.kie.dmn.api.marshalling.DMNExtensionRegister) Consumer(java.util.function.Consumer) DMNImportPMMLInfo(org.kie.dmn.core.pmml.DMNImportPMMLInfo) UnaryTests(org.kie.dmn.model.api.UnaryTests) DecisionServiceNode(org.kie.dmn.api.core.ast.DecisionServiceNode) CompositeTypeImpl(org.kie.dmn.core.impl.CompositeTypeImpl) DRGElement(org.kie.dmn.model.api.DRGElement) Msg(org.kie.dmn.core.util.Msg) Collections(java.util.Collections) DecisionService(org.kie.dmn.model.api.DecisionService) InputStream(java.io.InputStream) DecisionNodeImpl(org.kie.dmn.core.ast.DecisionNodeImpl) HashMap(java.util.HashMap) ItemDefinition(org.kie.dmn.model.api.ItemDefinition) TInformationItem(org.kie.dmn.model.v1_1.TInformationItem) InformationItem(org.kie.dmn.model.api.InformationItem) BaseDMNTypeImpl(org.kie.dmn.core.impl.BaseDMNTypeImpl) BuiltInType(org.kie.dmn.feel.lang.types.BuiltInType) SimpleFnTypeImpl(org.kie.dmn.core.impl.SimpleFnTypeImpl) UnaryTest(org.kie.dmn.feel.runtime.UnaryTest) ImportType(org.kie.dmn.core.compiler.ImportDMNResolverUtil.ImportType) BuiltInType(org.kie.dmn.feel.lang.types.BuiltInType) AliasFEELType(org.kie.dmn.feel.lang.types.AliasFEELType) GenListType(org.kie.dmn.feel.lang.types.GenListType) DMNType(org.kie.dmn.api.core.DMNType) GenFnType(org.kie.dmn.feel.lang.types.GenFnType) Type(org.kie.dmn.feel.lang.Type) SimpleTypeImpl(org.kie.dmn.core.impl.SimpleTypeImpl) GenListType(org.kie.dmn.feel.lang.types.GenListType) GenFnType(org.kie.dmn.feel.lang.types.GenFnType) AliasFEELType(org.kie.dmn.feel.lang.types.AliasFEELType) FunctionItem(org.kie.dmn.model.api.FunctionItem) UnaryTests(org.kie.dmn.model.api.UnaryTests) CompositeTypeImpl(org.kie.dmn.core.impl.CompositeTypeImpl) DMNType(org.kie.dmn.api.core.DMNType)

Example 8 with BuiltInType

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

the class DMNImportPMMLInfo method registerOutputFieldType.

private static void registerOutputFieldType(Model pmmlModel, DMNModelImpl dmnModel, Import i) {
    String modelName = pmmlModel.getModelName();
    List<OutputField> outputFields = pmmlModel.getOutput() == null ? Collections.emptyList() : pmmlModel.getOutput().getOutputFields();
    if (outputFields.size() > 1) {
        if (modelName != null && !modelName.isEmpty()) {
            // In case of multiple output fields,
            // register <import name>.<pmml MODEL name>, being a composite type of the different model outputs fields
            Map<String, DMNType> typeMap = new HashMap<>();
            outputFields.stream().forEach(field -> {
                String fieldName = field.getName().getValue();
                BuiltInType ft = getBuiltInTypeByDataType(field.getDataType());
                DMNType type = new SimpleTypeImpl(i.getNamespace(), fieldName, null, false, null, dmnModel.getTypeRegistry().resolveType(dmnModel.getDefinitions().getURIFEEL(), ft.getName()), ft);
                typeMap.put(fieldName, type);
            });
            DMNType compositeType = new CompositeTypeImpl(i.getNamespace(), modelName, null, false, typeMap, null, null);
            dmnModel.getTypeRegistry().registerType(compositeType);
            return;
        } else {
            // Case of multiple/complex output AND model without name, raise a Warning from the compilation/engine side (for the editor to use FEEL Any as the typeRef in the BKM)
            LOG.warn("PMML modelName is not provided, while output is a composite / multiple fields. Unable to synthesize CompositeType for DMN side.");
        }
    }
}
Also used : SimpleTypeImpl(org.kie.dmn.core.impl.SimpleTypeImpl) HashMap(java.util.HashMap) OutputField(org.dmg.pmml.OutputField) BuiltInType(org.kie.dmn.feel.lang.types.BuiltInType) CompositeTypeImpl(org.kie.dmn.core.impl.CompositeTypeImpl) DMNType(org.kie.dmn.api.core.DMNType)

Example 9 with BuiltInType

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

the class DMNImportPMMLInfo method from.

public static Either<Exception, DMNImportPMMLInfo> from(InputStream is, DMNCompilerConfigurationImpl cc, DMNModelImpl model, Import i) {
    try {
        final PMML pmml = org.jpmml.model.PMMLUtil.unmarshal(is);
        PMMLHeaderInfo h = PMMLInfo.pmmlToHeaderInfo(pmml, pmml.getHeader());
        for (DataField df : pmml.getDataDictionary().getDataFields()) {
            String dfName = df.getName().getValue();
            BuiltInType ft = getBuiltInTypeByDataType(df.getDataType());
            List<FEELProfile> helperFEELProfiles = cc.getFeelProfiles();
            DMNFEELHelper feel = new DMNFEELHelper(cc.getRootClassLoader(), helperFEELProfiles);
            List<UnaryTest> av = new ArrayList<>();
            if (df.getValues() != null && !df.getValues().isEmpty() && ft != BuiltInType.UNKNOWN) {
                final BuiltInType feelType = ft;
                String lov = df.getValues().stream().map(Value::getValue).map(o -> feelType == BuiltInType.STRING ? "\"" + o.toString() + "\"" : o.toString()).collect(Collectors.joining(","));
                av = feel.evaluateUnaryTests(lov, Collections.emptyMap());
            } else if (df.getIntervals() != null && !df.getIntervals().isEmpty() && ft != BuiltInType.UNKNOWN) {
                for (Interval interval : df.getIntervals()) {
                    String utString = null;
                    switch(interval.getClosure()) {
                        case CLOSED_CLOSED:
                            utString = new StringBuilder("[").append(interval.getLeftMargin()).append("..").append(interval.getRightMargin()).append("]").toString();
                            break;
                        case CLOSED_OPEN:
                            utString = new StringBuilder("[").append(interval.getLeftMargin()).append("..").append(interval.getRightMargin()).append(")").toString();
                            break;
                        case OPEN_CLOSED:
                            utString = new StringBuilder("(").append(interval.getLeftMargin()).append("..").append(interval.getRightMargin()).append("]").toString();
                            break;
                        case OPEN_OPEN:
                            utString = new StringBuilder("(").append(interval.getLeftMargin()).append("..").append(interval.getRightMargin()).append(")").toString();
                            break;
                    }
                    List<UnaryTest> ut = feel.evaluateUnaryTests(utString, Collections.emptyMap());
                    av.addAll(ut);
                }
            }
            DMNType type = new SimpleTypeImpl(i.getNamespace(), dfName, null, false, av, model.getTypeRegistry().resolveType(model.getDefinitions().getURIFEEL(), ft.getName()), ft);
            model.getTypeRegistry().registerType(type);
        }
        pmml.getModels().stream().forEach(m -> registerOutputFieldType(m, model, i));
        List<DMNPMMLModelInfo> models = pmml.getModels().stream().map(m -> PMMLInfo.pmmlToModelInfo(m)).map(proto -> DMNPMMLModelInfo.from(proto, model, i)).collect(Collectors.toList());
        DMNImportPMMLInfo info = new DMNImportPMMLInfo(i, models, h);
        return Either.ofRight(info);
    } catch (Throwable e) {
        return Either.ofLeft(new Exception("Unable to process DMNImportPMMLInfo", e));
    }
}
Also used : DMNType(org.kie.dmn.api.core.DMNType) Interval(org.dmg.pmml.Interval) Model(org.dmg.pmml.Model) OutputField(org.dmg.pmml.OutputField) FEELProfile(org.kie.dmn.feel.lang.FEELProfile) LoggerFactory(org.slf4j.LoggerFactory) Either(org.kie.dmn.feel.util.Either) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SimpleTypeImpl(org.kie.dmn.core.impl.SimpleTypeImpl) Value(org.dmg.pmml.Value) Map(java.util.Map) UnaryTest(org.kie.dmn.feel.runtime.UnaryTest) PMML(org.dmg.pmml.PMML) DMNCompilerConfigurationImpl(org.kie.dmn.core.compiler.DMNCompilerConfigurationImpl) Logger(org.slf4j.Logger) Collection(java.util.Collection) DataType(org.dmg.pmml.DataType) BuiltInType(org.kie.dmn.feel.lang.types.BuiltInType) DMNModelImpl(org.kie.dmn.core.impl.DMNModelImpl) Import(org.kie.dmn.model.api.Import) Collectors(java.util.stream.Collectors) DataField(org.dmg.pmml.DataField) List(java.util.List) DMNFEELHelper(org.kie.dmn.core.compiler.DMNFEELHelper) CompositeTypeImpl(org.kie.dmn.core.impl.CompositeTypeImpl) Collections(java.util.Collections) InputStream(java.io.InputStream) DMNFEELHelper(org.kie.dmn.core.compiler.DMNFEELHelper) FEELProfile(org.kie.dmn.feel.lang.FEELProfile) ArrayList(java.util.ArrayList) BuiltInType(org.kie.dmn.feel.lang.types.BuiltInType) UnaryTest(org.kie.dmn.feel.runtime.UnaryTest) DataField(org.dmg.pmml.DataField) SimpleTypeImpl(org.kie.dmn.core.impl.SimpleTypeImpl) Value(org.dmg.pmml.Value) PMML(org.dmg.pmml.PMML) ArrayList(java.util.ArrayList) List(java.util.List) Interval(org.dmg.pmml.Interval) DMNType(org.kie.dmn.api.core.DMNType)

Example 10 with BuiltInType

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

the class DMNTypeUtils method getFEELBuiltInType.

public static BuiltInType getFEELBuiltInType(DMNType dmnType) {
    Type feelType = DMNTypeUtils.getFEELType(dmnType);
    BuiltInType builtin;
    try {
        builtin = (BuiltInType) feelType;
    } catch (ClassCastException e) {
        throw new IllegalArgumentException();
    }
    return builtin;
}
Also used : DMNType(org.kie.dmn.api.core.DMNType) BuiltInType(org.kie.dmn.feel.lang.types.BuiltInType) Type(org.kie.dmn.feel.lang.Type) BuiltInType(org.kie.dmn.feel.lang.types.BuiltInType)

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