Search in sources :

Example 21 with DecisionTable

use of org.kie.dmn.model.api.DecisionTable 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)

Example 22 with DecisionTable

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

the class DecisionTableConverter method writeChildren.

@Override
protected void writeChildren(HierarchicalStreamWriter writer, MarshallingContext context, Object parent) {
    super.writeChildren(writer, context, parent);
    DecisionTable dt = (DecisionTable) parent;
    for (InputClause i : dt.getInput()) {
        writeChildrenNode(writer, context, i, INPUT);
    }
    for (OutputClause o : dt.getOutput()) {
        writeChildrenNode(writer, context, o, OUTPUT);
    }
    for (RuleAnnotationClause a : dt.getAnnotation()) {
        writeChildrenNode(writer, context, a, ANNOTATION);
    }
    for (DecisionRule r : dt.getRule()) {
        writeChildrenNode(writer, context, r, RULE);
    }
}
Also used : OutputClause(org.kie.dmn.model.api.OutputClause) DecisionTable(org.kie.dmn.model.api.DecisionTable) TDecisionTable(org.kie.dmn.model.v1_3.TDecisionTable) RuleAnnotationClause(org.kie.dmn.model.api.RuleAnnotationClause) InputClause(org.kie.dmn.model.api.InputClause) DecisionRule(org.kie.dmn.model.api.DecisionRule)

Example 23 with DecisionTable

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

the class DecisionTableConverter method writeChildren.

@Override
protected void writeChildren(HierarchicalStreamWriter writer, MarshallingContext context, Object parent) {
    super.writeChildren(writer, context, parent);
    DecisionTable dt = (DecisionTable) parent;
    for (InputClause i : dt.getInput()) {
        writeChildrenNode(writer, context, i, INPUT);
    }
    for (OutputClause o : dt.getOutput()) {
        writeChildrenNode(writer, context, o, OUTPUT);
    }
    for (RuleAnnotationClause a : dt.getAnnotation()) {
        writeChildrenNode(writer, context, a, ANNOTATION);
    }
    for (DecisionRule r : dt.getRule()) {
        writeChildrenNode(writer, context, r, RULE);
    }
}
Also used : OutputClause(org.kie.dmn.model.api.OutputClause) DecisionTable(org.kie.dmn.model.api.DecisionTable) TDecisionTable(org.kie.dmn.model.v1_4.TDecisionTable) RuleAnnotationClause(org.kie.dmn.model.api.RuleAnnotationClause) InputClause(org.kie.dmn.model.api.InputClause) DecisionRule(org.kie.dmn.model.api.DecisionRule)

Example 24 with DecisionTable

use of org.kie.dmn.model.api.DecisionTable in project kie-wb-common by kiegroup.

the class DMNMarshallerStandaloneTest method test_DecisionTableInputOutputClausesWhenEmpty.

@Test
public void test_DecisionTableInputOutputClausesWhenEmpty() throws IOException {
    DMNRuntime runtime = roundTripUnmarshalMarshalThenUnmarshalDMN(this.getClass().getResourceAsStream("/qGslQdo2.dmn"));
    Assert.assertNotNull(runtime);
    DMNModel model = runtime.getModel("https://github.com/kiegroup/drools/kie-dmn/_A2C75C01-7EAD-46B8-A499-D85D6C07D273", "_5FE8CBFD-821B-41F6-A6C7-42BE3FC45F2F");
    Assert.assertNotNull(model);
    assertThat(model.hasErrors(), is(false));
    DMNContext dmnContext = runtime.newContext();
    dmnContext.set("my number", -99);
    DMNResult dmnResult = runtime.evaluateAll(model, dmnContext);
    LOG.debug("{}", dmnResult);
    assertThat(dmnResult.hasErrors(), is(false));
    assertThat(dmnResult.getDecisionResultByName("my decision").getEvaluationStatus(), is(DecisionEvaluationStatus.SUCCEEDED));
    assertThat(dmnResult.getDecisionResultByName("my decision").getResult(), is("negative"));
    org.kie.dmn.model.api.DecisionTable dmnDT = (DecisionTable) model.getDecisionByName("my decision").getDecision().getExpression();
    // DROOLS-3262
    assertThat(dmnDT.getInput().get(0).getInputValues(), nullValue());
    // DROOLS-3262
    assertThat(dmnDT.getOutput().get(0).getOutputValues(), nullValue());
    // DROOLS-3262
    assertThat(dmnDT.getOutput().get(0).getDefaultOutputEntry(), nullValue());
    // DROOLS-3281
    assertThat(dmnDT.getOutput().get(0).getName(), nullValue());
}
Also used : DecisionTable(org.kie.dmn.model.api.DecisionTable) DMNResult(org.kie.dmn.api.core.DMNResult) DecisionTable(org.kie.dmn.model.api.DecisionTable) DMNContext(org.kie.dmn.api.core.DMNContext) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DMNModel(org.kie.dmn.api.core.DMNModel) Test(org.junit.Test)

Aggregations

DecisionTable (org.kie.dmn.model.api.DecisionTable)24 OutputClause (org.kie.dmn.model.api.OutputClause)7 Definitions (org.kie.dmn.model.api.Definitions)6 InputClause (org.kie.dmn.model.api.InputClause)6 DecisionRule (org.kie.dmn.model.api.DecisionRule)5 TDecisionTable (org.kie.dmn.model.v1_2.TDecisionTable)5 DTAnalysis (org.kie.dmn.validation.dtanalysis.model.DTAnalysis)5 DMNMessage (org.kie.dmn.api.core.DMNMessage)4 Decision (org.kie.dmn.model.api.Decision)4 TDecisionTable (org.kie.dmn.model.v1_3.TDecisionTable)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 QName (javax.xml.namespace.QName)3 Test (org.junit.Test)3 BigDecimal (java.math.BigDecimal)2 Collectors (java.util.stream.Collectors)2 DMNContext (org.kie.dmn.api.core.DMNContext)2 DMNModel (org.kie.dmn.api.core.DMNModel)2 DMNResult (org.kie.dmn.api.core.DMNResult)2 DMNRuntime (org.kie.dmn.api.core.DMNRuntime)2