Search in sources :

Example 16 with DMNModelInstrumentedBase

use of org.kie.dmn.model.api.DMNModelInstrumentedBase in project drools by kiegroup.

the class DMNValidatorImpl method validateModel.

private List<DMNMessage> validateModel(DMNResource mainModel, List<DMNResource> otherModels) {
    Definitions mainDefinitions = mainModel.getDefinitions();
    StatelessKieSession kieSession = mainDefinitions instanceof org.kie.dmn.model.v1_1.KieDMNModelInstrumentedBase ? kb11.newStatelessKieSession() : kb12.newStatelessKieSession();
    MessageReporter reporter = new MessageReporter(mainModel);
    kieSession.setGlobal("reporter", reporter);
    // exclude dynamicDecisionService for validation
    List<DMNModelInstrumentedBase> dmnModelElements = allChildren(mainDefinitions).filter(d -> !(d instanceof DecisionService && Boolean.parseBoolean(d.getAdditionalAttributes().get(new QName("http://www.trisotech.com/2015/triso/modeling", "dynamicDecisionService"))))).collect(toList());
    List<Definitions> otherModelsDefinitions = otherModels.stream().map(DMNResource::getDefinitions).collect(Collectors.toList());
    BatchExecutionCommand batch = CommandFactory.newBatchExecution(Arrays.asList(CommandFactory.newInsertElements(dmnModelElements, "DEFAULT", false, "DEFAULT"), CommandFactory.newInsertElements(otherModelsDefinitions, "DMNImports", false, "DMNImports")));
    kieSession.execute(batch);
    return reporter.getMessages().getMessages();
}
Also used : Arrays(java.util.Arrays) DMNModelInstrumentedBase(org.kie.dmn.model.api.DMNModelInstrumentedBase) DMNMessage(org.kie.dmn.api.core.DMNMessage) URL(java.net.URL) LoggerFactory(org.slf4j.LoggerFactory) DMNCompilerImpl(org.kie.dmn.core.compiler.DMNCompilerImpl) FileSystemResource(org.drools.core.io.impl.FileSystemResource) CommandFactory(org.kie.internal.command.CommandFactory) XStreamMarshaller(org.kie.dmn.backend.marshalling.v1x.XStreamMarshaller) DMNModel(org.kie.dmn.api.core.DMNModel) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) ResourceWithConfigurationImpl(org.kie.internal.io.ResourceWithConfigurationImpl) DMNAssemblerService(org.kie.dmn.core.assembler.DMNAssemblerService) DMNCompilerConfiguration(org.kie.dmn.api.core.DMNCompilerConfiguration) DMNProfile(org.kie.dmn.core.compiler.DMNProfile) EnumSet(java.util.EnumSet) DMNFactory(org.kie.dmn.core.api.DMNFactory) DMNCompilerConfigurationImpl(org.kie.dmn.core.compiler.DMNCompilerConfigurationImpl) DefaultDMNMessagesManager(org.kie.dmn.core.util.DefaultDMNMessagesManager) DMN_VERSION(org.kie.dmn.backend.marshalling.v1x.XStreamMarshaller.DMN_VERSION) Collection(java.util.Collection) Set(java.util.Set) Reader(java.io.Reader) ChainedProperties(org.kie.internal.utils.ChainedProperties) Collectors(java.util.stream.Collectors) BatchExecutionCommand(org.kie.api.command.BatchExecutionCommand) Resource(org.kie.api.io.Resource) List(java.util.List) Stream(java.util.stream.Stream) VALIDATE_SCHEMA(org.kie.dmn.validation.DMNValidator.Validation.VALIDATE_SCHEMA) SAXException(org.xml.sax.SAXException) InternalDMNDTAnalyserFactory(org.kie.dmn.validation.dtanalysis.InternalDMNDTAnalyserFactory) ANALYZE_DECISION_TABLE(org.kie.dmn.validation.DMNValidator.Validation.ANALYZE_DECISION_TABLE) DTAnalysis(org.kie.dmn.validation.dtanalysis.model.DTAnalysis) QName(javax.xml.namespace.QName) MsgUtil(org.kie.dmn.core.util.MsgUtil) BaseResource(org.drools.core.io.impl.BaseResource) DMNMessageImpl(org.kie.dmn.core.impl.DMNMessageImpl) VALIDATE_COMPILATION(org.kie.dmn.validation.DMNValidator.Validation.VALIDATE_COMPILATION) StreamSource(javax.xml.transform.stream.StreamSource) ReaderResource(org.drools.core.io.impl.ReaderResource) ClassLoaderUtil(org.kie.dmn.feel.util.ClassLoaderUtil) Source(javax.xml.transform.Source) Function(java.util.function.Function) Schema(javax.xml.validation.Schema) Definitions(org.kie.dmn.model.api.Definitions) ArrayList(java.util.ArrayList) DMNMarshallerFactory(org.kie.dmn.backend.marshalling.v1x.DMNMarshallerFactory) XMLConstants(javax.xml.XMLConstants) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) InternalDMNDTAnalyser(org.kie.dmn.validation.dtanalysis.InternalDMNDTAnalyser) Properties(java.util.Properties) SchemaFactory(javax.xml.validation.SchemaFactory) Logger(org.slf4j.Logger) DMNMessageManager(org.kie.dmn.core.api.DMNMessageManager) IOException(java.io.IOException) Validator(javax.xml.validation.Validator) File(java.io.File) KieBaseBuilder(org.drools.modelcompiler.builder.KieBaseBuilder) Collectors.toList(java.util.stream.Collectors.toList) StringReader(java.io.StringReader) DMNResourceDependenciesSorter(org.kie.dmn.core.assembler.DMNResourceDependenciesSorter) DMNResource(org.kie.dmn.core.assembler.DMNResource) Msg(org.kie.dmn.core.util.Msg) VALIDATE_MODEL(org.kie.dmn.validation.DMNValidator.Validation.VALIDATE_MODEL) Collections(java.util.Collections) DecisionService(org.kie.dmn.model.api.DecisionService) InputStream(java.io.InputStream) QName(javax.xml.namespace.QName) Definitions(org.kie.dmn.model.api.Definitions) DMNModelInstrumentedBase(org.kie.dmn.model.api.DMNModelInstrumentedBase) DecisionService(org.kie.dmn.model.api.DecisionService) BatchExecutionCommand(org.kie.api.command.BatchExecutionCommand) StatelessKieSession(org.kie.api.runtime.StatelessKieSession)

Example 17 with DMNModelInstrumentedBase

use of org.kie.dmn.model.api.DMNModelInstrumentedBase in project drools by kiegroup.

the class DecisionServiceConverter method parseElements.

@Override
protected void parseElements(HierarchicalStreamReader reader, UnmarshallingContext context, Object parent) {
    while (reader.hasMoreChildren()) {
        reader.moveDown();
        Object object = null;
        String nodeName = reader.getNodeName();
        if (nodeName.equals(INPUT_DATA)) {
            // Patch because the tag name inputData is used in both decision services and as a DRG Element
            DMNElementReference ref = new TDMNElementReference();
            ref.setHref(reader.getAttribute("href"));
            object = ref;
        } else {
            // Default behaviour
            object = readItem(reader, context, null);
        }
        if (object instanceof DMNModelInstrumentedBase) {
            ((KieDMNModelInstrumentedBase) object).setParent((KieDMNModelInstrumentedBase) parent);
            ((KieDMNModelInstrumentedBase) parent).addChildren((KieDMNModelInstrumentedBase) object);
        }
        reader.moveUp();
        assignChildElement(parent, nodeName, object);
    }
}
Also used : DMNElementReference(org.kie.dmn.model.api.DMNElementReference) TDMNElementReference(org.kie.dmn.model.v1_1.TDMNElementReference) DMNModelInstrumentedBase(org.kie.dmn.model.api.DMNModelInstrumentedBase) KieDMNModelInstrumentedBase(org.kie.dmn.model.v1_1.KieDMNModelInstrumentedBase) TDMNElementReference(org.kie.dmn.model.v1_1.TDMNElementReference) KieDMNModelInstrumentedBase(org.kie.dmn.model.v1_1.KieDMNModelInstrumentedBase)

Example 18 with DMNModelInstrumentedBase

use of org.kie.dmn.model.api.DMNModelInstrumentedBase in project drools by kiegroup.

the class XStreamMarshaller method marshal.

@Override
public String marshal(Object o) {
    try (Writer writer = new StringWriter();
        CustomStaxWriter hsWriter = (CustomStaxWriter) staxDriver.createWriter(writer)) {
        XStream xStream = newXStream();
        if (o instanceof DMNModelInstrumentedBase) {
            KieDMNModelInstrumentedBase base = (KieDMNModelInstrumentedBase) o;
            String dmnPrefix = base.getNsContext().entrySet().stream().filter(kv -> KieDMNModelInstrumentedBase.URI_DMN.equals(kv.getValue())).findFirst().map(Map.Entry::getKey).orElse("");
            hsWriter.getQNameMap().setDefaultPrefix(dmnPrefix);
        }
        extensionRegisters.forEach(r -> r.beforeMarshal(o, hsWriter.getQNameMap()));
        xStream.marshal(o, hsWriter);
        hsWriter.flush();
        return writer.toString();
    } catch (Exception e) {
        logger.error("Error marshalling DMN model to XML.", e);
    }
    return null;
}
Also used : XStream(com.thoughtworks.xstream.XStream) DMNModelInstrumentedBase(org.kie.dmn.model.api.DMNModelInstrumentedBase) TDecisionService(org.kie.dmn.model.v1_3.TDecisionService) TInputClause(org.kie.dmn.model.v1_3.TInputClause) LoggerFactory(org.slf4j.LoggerFactory) TInformationItem(org.kie.dmn.model.v1_3.TInformationItem) TPerformanceIndicator(org.kie.dmn.model.v1_3.TPerformanceIndicator) DMNDecisionServiceDividerLine(org.kie.dmn.model.v1_3.dmndi.DMNDecisionServiceDividerLine) TypeHierarchyPermission(com.thoughtworks.xstream.security.TypeHierarchyPermission) TDefinitions(org.kie.dmn.model.v1_3.TDefinitions) TElementCollection(org.kie.dmn.model.v1_3.TElementCollection) TAuthorityRequirement(org.kie.dmn.model.v1_3.TAuthorityRequirement) XMLStreamReader(javax.xml.stream.XMLStreamReader) Bounds(org.kie.dmn.model.v1_3.dmndi.Bounds) XStreamUtils(org.kie.utll.xml.XStreamUtils) Map(java.util.Map) XMLStreamException(javax.xml.stream.XMLStreamException) TImport(org.kie.dmn.model.v1_3.TImport) TContext(org.kie.dmn.model.v1_3.TContext) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) TBusinessKnowledgeModel(org.kie.dmn.model.v1_3.TBusinessKnowledgeModel) TAssociation(org.kie.dmn.model.v1_3.TAssociation) Point(org.kie.dmn.model.v1_3.dmndi.Point) DMNXStream(org.kie.dmn.backend.marshalling.v1x.DMNXStream) TRuleAnnotation(org.kie.dmn.model.v1_3.TRuleAnnotation) Color(org.kie.dmn.model.v1_3.dmndi.Color) TDMNElementReference(org.kie.dmn.model.v1_3.TDMNElementReference) TKnowledgeRequirement(org.kie.dmn.model.v1_3.TKnowledgeRequirement) DMNEdge(org.kie.dmn.model.v1_3.dmndi.DMNEdge) Reader(java.io.Reader) TArtifact(org.kie.dmn.model.v1_3.TArtifact) TBusinessContextElement(org.kie.dmn.model.v1_3.TBusinessContextElement) AbstractPullReader(com.thoughtworks.xstream.io.xml.AbstractPullReader) TContextEntry(org.kie.dmn.model.v1_3.TContextEntry) List(java.util.List) DMNDI(org.kie.dmn.model.v1_3.dmndi.DMNDI) TDecision(org.kie.dmn.model.v1_3.TDecision) StaxWriter(com.thoughtworks.xstream.io.xml.StaxWriter) QNameMap(com.thoughtworks.xstream.io.xml.QNameMap) TImportedValues(org.kie.dmn.model.v1_3.TImportedValues) Writer(java.io.Writer) DiagramElement(org.kie.dmn.model.v1_3.dmndi.DiagramElement) QName(javax.xml.namespace.QName) TRuleAnnotationClause(org.kie.dmn.model.v1_3.TRuleAnnotationClause) TDecisionTable(org.kie.dmn.model.v1_3.TDecisionTable) DMNMarshaller(org.kie.dmn.api.marshalling.DMNMarshaller) KieDMNModelInstrumentedBase(org.kie.dmn.model.v1_3.KieDMNModelInstrumentedBase) CustomStaxReader(org.kie.dmn.backend.marshalling.CustomStaxReader) Dimension(org.kie.dmn.model.v1_3.dmndi.Dimension) TLiteralExpression(org.kie.dmn.model.v1_3.TLiteralExpression) TUnaryTests(org.kie.dmn.model.v1_3.TUnaryTests) DMNShape(org.kie.dmn.model.v1_3.dmndi.DMNShape) TExpression(org.kie.dmn.model.v1_3.TExpression) TInformationRequirement(org.kie.dmn.model.v1_3.TInformationRequirement) Definitions(org.kie.dmn.model.api.Definitions) ArrayList(java.util.ArrayList) CustomStaxWriter(org.kie.dmn.backend.marshalling.CustomStaxWriter) TFunctionDefinition(org.kie.dmn.model.v1_3.TFunctionDefinition) TInputData(org.kie.dmn.model.v1_3.TInputData) DMNStyle(org.kie.dmn.model.v1_3.dmndi.DMNStyle) Logger(org.slf4j.Logger) TFunctionItem(org.kie.dmn.model.v1_3.TFunctionItem) DMNDiagram(org.kie.dmn.model.v1_3.dmndi.DMNDiagram) TGroup(org.kie.dmn.model.v1_3.TGroup) StringWriter(java.io.StringWriter) TBinding(org.kie.dmn.model.v1_3.TBinding) StaxDriver(com.thoughtworks.xstream.io.xml.StaxDriver) DMNExtensionRegister(org.kie.dmn.api.marshalling.DMNExtensionRegister) TTextAnnotation(org.kie.dmn.model.v1_3.TTextAnnotation) TDMNElement(org.kie.dmn.model.v1_3.TDMNElement) TDecisionRule(org.kie.dmn.model.v1_3.TDecisionRule) TInvocation(org.kie.dmn.model.v1_3.TInvocation) TOutputClause(org.kie.dmn.model.v1_3.TOutputClause) TRelation(org.kie.dmn.model.v1_3.TRelation) TItemDefinition(org.kie.dmn.model.v1_3.TItemDefinition) DMNLabel(org.kie.dmn.model.v1_3.dmndi.DMNLabel) StringReader(java.io.StringReader) TOrganizationUnit(org.kie.dmn.model.v1_3.TOrganizationUnit) TKnowledgeSource(org.kie.dmn.model.v1_3.TKnowledgeSource) TNamedElement(org.kie.dmn.model.v1_3.TNamedElement) StringWriter(java.io.StringWriter) CustomStaxWriter(org.kie.dmn.backend.marshalling.CustomStaxWriter) XStream(com.thoughtworks.xstream.XStream) DMNXStream(org.kie.dmn.backend.marshalling.v1x.DMNXStream) DMNModelInstrumentedBase(org.kie.dmn.model.api.DMNModelInstrumentedBase) KieDMNModelInstrumentedBase(org.kie.dmn.model.v1_3.KieDMNModelInstrumentedBase) KieDMNModelInstrumentedBase(org.kie.dmn.model.v1_3.KieDMNModelInstrumentedBase) Map(java.util.Map) QNameMap(com.thoughtworks.xstream.io.xml.QNameMap) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) StaxWriter(com.thoughtworks.xstream.io.xml.StaxWriter) Writer(java.io.Writer) CustomStaxWriter(org.kie.dmn.backend.marshalling.CustomStaxWriter) StringWriter(java.io.StringWriter) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 19 with DMNModelInstrumentedBase

use of org.kie.dmn.model.api.DMNModelInstrumentedBase in project drools by kiegroup.

the class DecisionServiceConverter method parseElements.

@Override
protected void parseElements(HierarchicalStreamReader reader, UnmarshallingContext context, Object parent) {
    while (reader.hasMoreChildren()) {
        reader.moveDown();
        Object object = null;
        String nodeName = reader.getNodeName();
        if (nodeName.equals(INPUT_DATA)) {
            // Patch because the tag name inputData is used in both decision services and as a DRG Element
            DMNElementReference ref = new TDMNElementReference();
            ref.setHref(reader.getAttribute("href"));
            object = ref;
        } else {
            // Default behaviour
            object = readItem(reader, context, null);
        }
        if (object instanceof DMNModelInstrumentedBase) {
            ((KieDMNModelInstrumentedBase) object).setParent((KieDMNModelInstrumentedBase) parent);
            ((KieDMNModelInstrumentedBase) parent).addChildren((KieDMNModelInstrumentedBase) object);
        }
        reader.moveUp();
        assignChildElement(parent, nodeName, object);
    }
}
Also used : DMNElementReference(org.kie.dmn.model.api.DMNElementReference) TDMNElementReference(org.kie.dmn.model.v1_3.TDMNElementReference) DMNModelInstrumentedBase(org.kie.dmn.model.api.DMNModelInstrumentedBase) KieDMNModelInstrumentedBase(org.kie.dmn.model.v1_3.KieDMNModelInstrumentedBase) TDMNElementReference(org.kie.dmn.model.v1_3.TDMNElementReference) KieDMNModelInstrumentedBase(org.kie.dmn.model.v1_3.KieDMNModelInstrumentedBase)

Example 20 with DMNModelInstrumentedBase

use of org.kie.dmn.model.api.DMNModelInstrumentedBase in project drools by kiegroup.

the class ExtensionElementsConverter method unmarshal.

@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
    DMNModelInstrumentedBase obj = createModelObject();
    assignAttributes(reader, obj);
    if (extensionRegisters.size() == 0) {
        while (reader.hasMoreChildren()) {
            reader.moveDown();
            String nodeName = reader.getNodeName();
            // skipping nodeName
            reader.moveUp();
        }
    } else {
        // do as default behavior, but in case cannot unmarshall an extension element child, just skip it.
        while (reader.hasMoreChildren()) {
            reader.moveDown();
            String nodeName = reader.getNodeName();
            try {
                Object object = readItem(reader, context, null);
                if (object instanceof DMNModelInstrumentedBase) {
                    ((KieDMNModelInstrumentedBase) object).setParent(obj);
                    ((KieDMNModelInstrumentedBase) obj).addChildren((KieDMNModelInstrumentedBase) object);
                }
                assignChildElement(obj, nodeName, object);
            } catch (CannotResolveClassException e) {
                // do nothing; I tried to convert the extension element child with the converters, but no converter is registered for this child.
                LOG.debug("Tried to convert the extension element child {}, but no converter is registered for this child.", nodeName);
            }
            reader.moveUp();
        }
    }
    return obj;
}
Also used : DMNModelInstrumentedBase(org.kie.dmn.model.api.DMNModelInstrumentedBase) KieDMNModelInstrumentedBase(org.kie.dmn.model.v1_3.KieDMNModelInstrumentedBase) KieDMNModelInstrumentedBase(org.kie.dmn.model.v1_3.KieDMNModelInstrumentedBase) CannotResolveClassException(com.thoughtworks.xstream.mapper.CannotResolveClassException)

Aggregations

DMNModelInstrumentedBase (org.kie.dmn.model.api.DMNModelInstrumentedBase)22 CannotResolveClassException (com.thoughtworks.xstream.mapper.CannotResolveClassException)6 ArrayList (java.util.ArrayList)6 QName (javax.xml.namespace.QName)6 Reader (java.io.Reader)5 StringReader (java.io.StringReader)5 List (java.util.List)5 Definitions (org.kie.dmn.model.api.Definitions)5 KieDMNModelInstrumentedBase (org.kie.dmn.model.v1_2.KieDMNModelInstrumentedBase)5 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5 XStream (com.thoughtworks.xstream.XStream)4 AbstractPullReader (com.thoughtworks.xstream.io.xml.AbstractPullReader)4 QNameMap (com.thoughtworks.xstream.io.xml.QNameMap)4 StaxDriver (com.thoughtworks.xstream.io.xml.StaxDriver)4 StaxWriter (com.thoughtworks.xstream.io.xml.StaxWriter)4 TypeHierarchyPermission (com.thoughtworks.xstream.security.TypeHierarchyPermission)4 StringWriter (java.io.StringWriter)4 Writer (java.io.Writer)4 Map (java.util.Map)4