use of org.kie.dmn.model.api.Definitions in project drools by kiegroup.
the class ValidatorTest method utilDefinitions.
private Definitions utilDefinitions(String filename, String modelName) {
// List<DMNMessage> validateXML;
// try {
// validateXML = validator.validate( new File(this.getClass().getResource(filename).toURI()), DMNValidator.Validation.VALIDATE_SCHEMA );
// assertThat( "using unit test method utilDefinitions must received a XML valid DMN file", validateXML, IsEmptyCollection.empty() );
// } catch (URISyntaxException e) {
// e.printStackTrace();
// fail("Unable for the test suite to locate the file for XML validation.");
// }
DMNMarshaller marshaller = DMNMarshallerFactory.newDefaultMarshaller();
try (InputStreamReader isr = new InputStreamReader(getClass().getResourceAsStream(filename))) {
Definitions definitions = marshaller.unmarshal(isr);
assertThat(definitions, notNullValue());
return definitions;
} catch (IOException e) {
e.printStackTrace();
fail("Unable for the test suite to locate the file for validation.");
}
return null;
}
use of org.kie.dmn.model.api.Definitions in project drools by kiegroup.
the class ValidatorTest method testMACDInputDefinitions.
@Test
public void testMACDInputDefinitions() {
DMNRuntime runtime = DMNRuntimeUtil.createRuntime("MACD-enhanced_iteration.dmn", DMNInputRuntimeTest.class);
DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/definitions/_6cfe7d88-6741-45d1-968c-b61a597d0964", "MACD-enhanced iteration");
assertThat(dmnModel, notNullValue());
Definitions definitions = dmnModel.getDefinitions();
assertThat(definitions, notNullValue());
List<DMNMessage> messages = DMNValidatorFactory.newValidator().validate(definitions, VALIDATE_MODEL, VALIDATE_COMPILATION);
assertThat(messages.toString(), messages.size(), is(0));
}
use of org.kie.dmn.model.api.Definitions in project drools by kiegroup.
the class ValidatorImportTest method testWrongImportBaseModelFromReaderInput.
@Test
public void testWrongImportBaseModelFromReaderInput() throws IOException {
try (final Reader reader0 = getReader("import/Base-model.dmn");
final Reader reader1 = getReader("import/Wrong-Import-base-model.dmn")) {
final List<DMNMessage> messages = validator.validateUsing(Validation.VALIDATE_MODEL).theseModels(reader0, reader1);
assertThat(ValidatorUtil.formatMessages(messages), messages.size(), is(1));
assertTrue(messages.stream().anyMatch(p -> p.getMessageType().equals(DMNMessageType.REQ_NOT_FOUND) && p.getSourceReference() instanceof DMNElementReference && ((DMNElementReference) p.getSourceReference()).getHref().equals("http://www.trisotech.com/definitions/_70df1ad5-2a33-4ede-b8b2-869988ac1d30#_1d52934e-aa4e-47c9-a011-fc989d795664")));
}
}
use of org.kie.dmn.model.api.Definitions in project drools by kiegroup.
the class OverlapHitPolicyTest method testOverlapHitPolicy.
@Test
public void testOverlapHitPolicy() {
Definitions definitions = getDefinitions("OverlapHitPolicy.dmn", "https://github.com/kiegroup/drools/kie-dmn/_3010653A-DD3F-4C88-89DA-3FDD845F6604", "OverlapHitPolicy");
// mutates XML file in the Hit Policy, accordingly to this test parameter.
((DecisionTable) ((Decision) definitions.getDrgElement().get(0)).getExpression()).setHitPolicy(hp);
List<DMNMessage> validate = validator.validate(definitions, VALIDATE_COMPILATION, ANALYZE_DECISION_TABLE);
checkAnalysis(validate);
if (hp == HitPolicy.UNIQUE) {
assertTrue("It should contain at least 1 DMNMessage for the type", validate.stream().anyMatch(p -> p.getMessageType().equals(DMNMessageType.DECISION_TABLE_OVERLAP_HITPOLICY_UNIQUE)));
} else if (hp == HitPolicy.ANY) {
assertTrue("It should contain at least 1 DMNMessage for the type", validate.stream().anyMatch(p -> p.getMessageType().equals(DMNMessageType.DECISION_TABLE_OVERLAP_HITPOLICY_ANY)));
} else {
LOG.debug("Testing for {} I am expecting there is NOT DMNMessage pertaining to Overlaps", hp);
assertTrue(validate.stream().noneMatch(p -> p.getMessageType().equals(DMNMessageType.DECISION_TABLE_OVERLAP_HITPOLICY_UNIQUE)) && validate.stream().noneMatch(p -> p.getMessageType().equals(DMNMessageType.DECISION_TABLE_OVERLAP_HITPOLICY_ANY)) && validate.stream().noneMatch(p -> p.getMessageType().equals(DMNMessageType.DECISION_TABLE_OVERLAP)));
}
}
use of org.kie.dmn.model.api.Definitions 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