Search in sources :

Example 11 with DRGElement

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

the class DMNDecisionTableRuntimeTest method checkDTCollectOperatorsMultipleOutputs.

private void checkDTCollectOperatorsMultipleOutputs(BuiltinAggregator aggregator, int level, int a, int b) {
    // DROOLS-6590 DMN composite output on DT Collect with operators - this is beyond the spec.
    final KieServices ks = KieServices.Factory.get();
    DMNMarshaller marshaller = DMNMarshallerFactory.newDefaultMarshaller();
    final Definitions definitions = marshaller.unmarshal(new InputStreamReader(this.getClass().getResourceAsStream("multipleOutputsCollectDT.dmn")));
    DRGElement drgElement1 = definitions.getDrgElement().get(1);
    assertThat(drgElement1).describedAs("xml of the test changed, unable to locate Decision Node").isInstanceOf(Decision.class);
    ((DecisionTable) ((Decision) drgElement1).getExpression()).setAggregation(aggregator);
    final String dmnXml = marshaller.marshal(definitions);
    final ReleaseId kjarReleaseId = ks.newReleaseId("org.kie.dmn.core", "DMNDecisionTableRuntimeTest", UUID.randomUUID().toString());
    final KieFileSystem kfs = ks.newKieFileSystem();
    kfs.write("src/main/resources/multipleOutputsCollectDT.dmn", dmnXml);
    kfs.generateAndWritePomXML(kjarReleaseId);
    final KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
    assertTrue(kieBuilder.getResults().getMessages().toString(), kieBuilder.getResults().getMessages().isEmpty());
    final KieContainer container = ks.newKieContainer(kjarReleaseId);
    final DMNRuntime runtime = KieRuntimeFactory.of(container.getKieBase()).get(DMNRuntime.class);
    final DMNModel dmnModel = runtime.getModel("https://kiegroup.org/dmn/_943A3581-5FD1-4BCF-9A52-AC7242CC451C", "multipleOutputsCollectDT");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    final DMNContext context = DMNFactory.newContext();
    context.set("level", level);
    final DMNResult dmnResult = runtime.evaluateAll(dmnModel, context);
    LOG.debug("{}", dmnResult);
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult.getMessages()), dmnResult.hasErrors(), is(false));
    Object decision1 = dmnResult.getDecisionResultByName("Decision-1").getResult();
    assertThat(decision1).hasFieldOrPropertyWithValue("a", new BigDecimal(a));
    assertThat(decision1).hasFieldOrPropertyWithValue("b", new BigDecimal(b));
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) DMNMarshaller(org.kie.dmn.api.marshalling.DMNMarshaller) InputStreamReader(java.io.InputStreamReader) KieFileSystem(org.kie.api.builder.KieFileSystem) Definitions(org.kie.dmn.model.api.Definitions) DMNContext(org.kie.dmn.api.core.DMNContext) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) BigDecimal(java.math.BigDecimal) DecisionTable(org.kie.dmn.model.api.DecisionTable) KieBuilder(org.kie.api.builder.KieBuilder) DMNModel(org.kie.dmn.api.core.DMNModel) DRGElement(org.kie.dmn.model.api.DRGElement) KieContainer(org.kie.api.runtime.KieContainer)

Example 12 with DRGElement

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

the class DefinitionsConverter method writeChildren.

@Override
protected void writeChildren(HierarchicalStreamWriter writer, MarshallingContext context, Object parent) {
    super.writeChildren(writer, context, parent);
    Definitions def = (Definitions) parent;
    for (Import i : def.getImport()) {
        writeChildrenNode(writer, context, i, IMPORT);
    }
    for (ItemDefinition id : def.getItemDefinition()) {
        writeChildrenNode(writer, context, id, ITEM_DEFINITION);
    }
    for (DRGElement e : def.getDrgElement()) {
        String nodeName = DRG_ELEMENT;
        if (e instanceof BusinessKnowledgeModel) {
            nodeName = "businessKnowledgeModel";
        } else if (e instanceof Decision) {
            nodeName = "decision";
        } else if (e instanceof InputData) {
            nodeName = "inputData";
        } else if (e instanceof KnowledgeSource) {
            nodeName = "knowledgeSource";
        } else if (e instanceof DecisionService) {
            nodeName = "decisionService";
        }
        writeChildrenNode(writer, context, e, nodeName);
    }
    for (Artifact a : def.getArtifact()) {
        String nodeName = ARTIFACT;
        if (a instanceof Association) {
            nodeName = "association";
        } else if (a instanceof TextAnnotation) {
            nodeName = "textAnnotation";
        }
        writeChildrenNode(writer, context, a, nodeName);
    }
    for (ElementCollection ec : def.getElementCollection()) {
        writeChildrenNode(writer, context, ec, ELEMENT_COLLECTION);
    }
    for (BusinessContextElement bce : def.getBusinessContextElement()) {
        String nodeName = BUSINESS_CONTEXT_ELEMENT;
        if (bce instanceof OrganizationUnit) {
            nodeName = "organizationUnit";
        } else if (bce instanceof PerformanceIndicator) {
            nodeName = "performanceIndicator";
        }
        writeChildrenNode(writer, context, bce, nodeName);
    }
    if (def.getDMNDI() != null) {
        writeChildrenNode(writer, context, def.getDMNDI(), "DMNDI");
    }
}
Also used : Import(org.kie.dmn.model.api.Import) PerformanceIndicator(org.kie.dmn.model.api.PerformanceIndicator) TDefinitions(org.kie.dmn.model.v1_2.TDefinitions) Definitions(org.kie.dmn.model.api.Definitions) ItemDefinition(org.kie.dmn.model.api.ItemDefinition) BusinessContextElement(org.kie.dmn.model.api.BusinessContextElement) Decision(org.kie.dmn.model.api.Decision) Artifact(org.kie.dmn.model.api.Artifact) DecisionService(org.kie.dmn.model.api.DecisionService) Association(org.kie.dmn.model.api.Association) KnowledgeSource(org.kie.dmn.model.api.KnowledgeSource) OrganizationUnit(org.kie.dmn.model.api.OrganizationUnit) BusinessKnowledgeModel(org.kie.dmn.model.api.BusinessKnowledgeModel) ElementCollection(org.kie.dmn.model.api.ElementCollection) InputData(org.kie.dmn.model.api.InputData) TextAnnotation(org.kie.dmn.model.api.TextAnnotation) DRGElement(org.kie.dmn.model.api.DRGElement)

Example 13 with DRGElement

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

the class DefinitionsConverter method writeChildren.

@Override
protected void writeChildren(HierarchicalStreamWriter writer, MarshallingContext context, Object parent) {
    super.writeChildren(writer, context, parent);
    Definitions def = (Definitions) parent;
    for (Import i : def.getImport()) {
        writeChildrenNode(writer, context, i, IMPORT);
    }
    for (ItemDefinition id : def.getItemDefinition()) {
        writeChildrenNode(writer, context, id, ITEM_DEFINITION);
    }
    for (DRGElement e : def.getDrgElement()) {
        String nodeName = DRG_ELEMENT;
        if (e instanceof BusinessKnowledgeModel) {
            nodeName = "businessKnowledgeModel";
        } else if (e instanceof Decision) {
            nodeName = "decision";
        } else if (e instanceof InputData) {
            nodeName = "inputData";
        } else if (e instanceof KnowledgeSource) {
            nodeName = "knowledgeSource";
        }
        writeChildrenNode(writer, context, e, nodeName);
    }
    for (Artifact a : def.getArtifact()) {
        String nodeName = ARTIFACT;
        if (a instanceof Association) {
            nodeName = "association";
        } else if (a instanceof TextAnnotation) {
            nodeName = "textAnnotation";
        }
        writeChildrenNode(writer, context, a, nodeName);
    }
    for (ElementCollection ec : def.getElementCollection()) {
        writeChildrenNode(writer, context, ec, ELEMENT_COLLECTION);
    }
    for (BusinessContextElement bce : def.getBusinessContextElement()) {
        String nodeName = BUSINESS_CONTEXT_ELEMENT;
        if (bce instanceof OrganizationUnit) {
            nodeName = "organizationUnit";
        } else if (bce instanceof PerformanceIndicator) {
            nodeName = "performanceIndicator";
        }
        writeChildrenNode(writer, context, bce, nodeName);
    }
}
Also used : Import(org.kie.dmn.model.api.Import) PerformanceIndicator(org.kie.dmn.model.api.PerformanceIndicator) TDefinitions(org.kie.dmn.model.v1_1.TDefinitions) Definitions(org.kie.dmn.model.api.Definitions) ItemDefinition(org.kie.dmn.model.api.ItemDefinition) BusinessContextElement(org.kie.dmn.model.api.BusinessContextElement) Decision(org.kie.dmn.model.api.Decision) Artifact(org.kie.dmn.model.api.Artifact) Association(org.kie.dmn.model.api.Association) KnowledgeSource(org.kie.dmn.model.api.KnowledgeSource) OrganizationUnit(org.kie.dmn.model.api.OrganizationUnit) BusinessKnowledgeModel(org.kie.dmn.model.api.BusinessKnowledgeModel) ElementCollection(org.kie.dmn.model.api.ElementCollection) InputData(org.kie.dmn.model.api.InputData) TextAnnotation(org.kie.dmn.model.api.TextAnnotation) DRGElement(org.kie.dmn.model.api.DRGElement)

Example 14 with DRGElement

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

the class DefinitionsConverter method writeChildren.

@Override
protected void writeChildren(HierarchicalStreamWriter writer, MarshallingContext context, Object parent) {
    super.writeChildren(writer, context, parent);
    Definitions def = (Definitions) parent;
    for (Import i : def.getImport()) {
        writeChildrenNode(writer, context, i, IMPORT);
    }
    for (ItemDefinition id : def.getItemDefinition()) {
        writeChildrenNode(writer, context, id, ITEM_DEFINITION);
    }
    for (DRGElement e : def.getDrgElement()) {
        String nodeName = DRG_ELEMENT;
        if (e instanceof BusinessKnowledgeModel) {
            nodeName = "businessKnowledgeModel";
        } else if (e instanceof Decision) {
            nodeName = "decision";
        } else if (e instanceof InputData) {
            nodeName = "inputData";
        } else if (e instanceof KnowledgeSource) {
            nodeName = "knowledgeSource";
        } else if (e instanceof DecisionService) {
            nodeName = "decisionService";
        }
        writeChildrenNode(writer, context, e, nodeName);
    }
    for (Artifact a : def.getArtifact()) {
        String nodeName = ARTIFACT;
        if (a instanceof Association) {
            nodeName = "association";
        } else if (a instanceof TextAnnotation) {
            nodeName = "textAnnotation";
        } else if (a instanceof Group) {
            nodeName = "group";
        }
        writeChildrenNode(writer, context, a, nodeName);
    }
    for (ElementCollection ec : def.getElementCollection()) {
        writeChildrenNode(writer, context, ec, ELEMENT_COLLECTION);
    }
    for (BusinessContextElement bce : def.getBusinessContextElement()) {
        String nodeName = BUSINESS_CONTEXT_ELEMENT;
        if (bce instanceof OrganizationUnit) {
            nodeName = "organizationUnit";
        } else if (bce instanceof PerformanceIndicator) {
            nodeName = "performanceIndicator";
        }
        writeChildrenNode(writer, context, bce, nodeName);
    }
    if (def.getDMNDI() != null) {
        writeChildrenNode(writer, context, def.getDMNDI(), "DMNDI");
    }
}
Also used : Group(org.kie.dmn.model.api.Group) Import(org.kie.dmn.model.api.Import) PerformanceIndicator(org.kie.dmn.model.api.PerformanceIndicator) TDefinitions(org.kie.dmn.model.v1_4.TDefinitions) Definitions(org.kie.dmn.model.api.Definitions) ItemDefinition(org.kie.dmn.model.api.ItemDefinition) BusinessContextElement(org.kie.dmn.model.api.BusinessContextElement) Decision(org.kie.dmn.model.api.Decision) Artifact(org.kie.dmn.model.api.Artifact) DecisionService(org.kie.dmn.model.api.DecisionService) Association(org.kie.dmn.model.api.Association) KnowledgeSource(org.kie.dmn.model.api.KnowledgeSource) OrganizationUnit(org.kie.dmn.model.api.OrganizationUnit) BusinessKnowledgeModel(org.kie.dmn.model.api.BusinessKnowledgeModel) ElementCollection(org.kie.dmn.model.api.ElementCollection) InputData(org.kie.dmn.model.api.InputData) TextAnnotation(org.kie.dmn.model.api.TextAnnotation) DRGElement(org.kie.dmn.model.api.DRGElement)

Example 15 with DRGElement

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

the class XLS2DMNParser method parseWorkbook.

public void parseWorkbook(String dmnModelName, Workbook workbook) {
    Map<String, List<String>> overview = new HashMap<>();
    DataFormatter formatter = new DataFormatter();
    for (int s = 0; s < workbook.getNumberOfSheets(); s++) {
        Sheet sheet = workbook.getSheetAt(s);
        int maxRows = sheet.getLastRowNum();
        for (int i = 0; i <= maxRows; i++) {
            Row row = sheet.getRow(i);
            int lastCellNum = row != null ? row.getLastCellNum() : 0;
            if (lastCellNum == 0) {
                // skip empty row.
                continue;
            }
            List<String> header = new ArrayList<>();
            for (Cell c : row) {
                String text = formatter.formatCellValue(c);
                header.add(text);
            }
            overview.put(sheet.getSheetName(), header);
            // header found.
            break;
        }
    }
    overview.entrySet().forEach(e -> LOG.debug("{}", e));
    Map<String, DTHeaderInfo> headerInfos = generateDTHeaderInfo(overview);
    LOG.info("Sheets have been indexed as:");
    headerInfos.entrySet().forEach(e -> LOG.info("{}", e));
    Definitions definitions = new TDefinitions();
    setDefaultNSContext(definitions);
    definitions.setId("dmnid_" + dmnModelName);
    definitions.setName(dmnModelName);
    String namespace = "xls2dmn_" + UUID.randomUUID();
    definitions.setNamespace(namespace);
    definitions.getNsContext().put(XMLConstants.DEFAULT_NS_PREFIX, namespace);
    definitions.setExporter("kie-dmn-xls2dmn");
    appendInputData(definitions, headerInfos);
    appendDecisionDT(definitions, headerInfos);
    final Map<String, List<DataListener>> sheetListeners = new HashMap<>();
    for (DTHeaderInfo hi : headerInfos.values()) {
        String sheetName = hi.getSheetName();
        DRGElement drgElem = definitions.getDrgElement().stream().filter(e -> e.getName().equals(sheetName)).findFirst().orElseThrow(() -> new XLS2DMNException("Unable to locate DRG element for sheet: " + sheetName));
        DecisionTable dt = (DecisionTable) ((Decision) drgElem).getExpression();
        DTSheetListener listener = new DTSheetListener(dt, hi);
        sheetListeners.put(sheetName, Arrays.asList(listener));
    }
    new ExcelParser(sheetListeners).parseWorkbook(workbook);
    DMNMarshaller dmnMarshaller = DMNMarshallerFactory.newDefaultMarshaller();
    String xml = dmnMarshaller.marshal(definitions);
    try {
        Files.write(outFile.toPath(), xml.getBytes());
    } catch (IOException e) {
        LOG.error("Unable to write to outputfile.", e);
        throw new XLS2DMNException("Unable to write to outputfile", e);
    }
    LOG.debug("output XML can be displayed at trace level", xml);
    LOG.trace("output XML:\n{}", xml);
}
Also used : DMNMarshaller(org.kie.dmn.api.marshalling.DMNMarshaller) HashMap(java.util.HashMap) TDefinitions(org.kie.dmn.model.v1_2.TDefinitions) Definitions(org.kie.dmn.model.api.Definitions) ArrayList(java.util.ArrayList) IOException(java.io.IOException) DecisionTable(org.kie.dmn.model.api.DecisionTable) TDecisionTable(org.kie.dmn.model.v1_2.TDecisionTable) ExcelParser(org.drools.decisiontable.parser.xls.ExcelParser) List(java.util.List) ArrayList(java.util.ArrayList) Row(org.apache.poi.ss.usermodel.Row) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) TDefinitions(org.kie.dmn.model.v1_2.TDefinitions) DataFormatter(org.apache.poi.ss.usermodel.DataFormatter) DRGElement(org.kie.dmn.model.api.DRGElement)

Aggregations

DRGElement (org.kie.dmn.model.api.DRGElement)25 Definitions (org.kie.dmn.model.api.Definitions)15 Test (org.junit.Test)10 ArrayList (java.util.ArrayList)8 ItemDefinition (org.kie.dmn.model.api.ItemDefinition)8 Artifact (org.kie.dmn.model.api.Artifact)7 BusinessContextElement (org.kie.dmn.model.api.BusinessContextElement)7 Decision (org.kie.dmn.model.api.Decision)7 Import (org.kie.dmn.model.api.Import)7 BusinessKnowledgeModel (org.kie.dmn.model.api.BusinessKnowledgeModel)6 InputData (org.kie.dmn.model.api.InputData)6 DMNModel (org.kie.dmn.api.core.DMNModel)4 Association (org.kie.dmn.model.api.Association)4 DecisionService (org.kie.dmn.model.api.DecisionService)4 ElementCollection (org.kie.dmn.model.api.ElementCollection)4 KnowledgeSource (org.kie.dmn.model.api.KnowledgeSource)4 OrganizationUnit (org.kie.dmn.model.api.OrganizationUnit)4 PerformanceIndicator (org.kie.dmn.model.api.PerformanceIndicator)4 TextAnnotation (org.kie.dmn.model.api.TextAnnotation)4 DMNShape (org.kie.dmn.model.api.dmndi.DMNShape)4