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);
}
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);
}
}
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);
}
}
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());
}
Aggregations