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