Search in sources :

Example 96 with Definitions

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;
}
Also used : DMNMarshaller(org.kie.dmn.api.marshalling.DMNMarshaller) InputStreamReader(java.io.InputStreamReader) Definitions(org.kie.dmn.model.api.Definitions) IOException(java.io.IOException)

Example 97 with Definitions

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));
}
Also used : DMNMessage(org.kie.dmn.api.core.DMNMessage) Definitions(org.kie.dmn.model.api.Definitions) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DMNModel(org.kie.dmn.api.core.DMNModel) DMNRuntimeTest(org.kie.dmn.core.DMNRuntimeTest) DMN13specificTest(org.kie.dmn.core.v1_3.DMN13specificTest) Test(org.junit.Test) DMNInputRuntimeTest(org.kie.dmn.core.DMNInputRuntimeTest) DMNDecisionServicesTest(org.kie.dmn.core.decisionservices.DMNDecisionServicesTest) ImportsTest(org.kie.dmn.core.imports.ImportsTest)

Example 98 with Definitions

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")));
    }
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Arrays(java.util.Arrays) DMNElementReference(org.kie.dmn.model.api.DMNElementReference) DMNMessage(org.kie.dmn.api.core.DMNMessage) DMNResult(org.kie.dmn.api.core.DMNResult) DMNMessageType(org.kie.dmn.api.core.DMNMessageType) LoggerFactory(org.slf4j.LoggerFactory) DynamicTypeUtils.entry(org.kie.dmn.core.util.DynamicTypeUtils.entry) ValidatorImportReaderResolver(org.kie.dmn.validation.DMNValidator.ValidatorBuilder.ValidatorImportReaderResolver) Definitions(org.kie.dmn.model.api.Definitions) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) BigDecimal(java.math.BigDecimal) DMNModel(org.kie.dmn.api.core.DMNModel) Validation(org.kie.dmn.validation.DMNValidator.Validation) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) DMNFactory(org.kie.dmn.core.api.DMNFactory) Logger(org.slf4j.Logger) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) Reader(java.io.Reader) DMNRuntimeUtil(org.kie.dmn.core.util.DMNRuntimeUtil) DMNDecisionServicesTest(org.kie.dmn.core.decisionservices.DMNDecisionServicesTest) DynamicTypeUtils.mapOf(org.kie.dmn.core.util.DynamicTypeUtils.mapOf) List(java.util.List) Ignore(org.junit.Ignore) DMNContext(org.kie.dmn.api.core.DMNContext) DMNMessage(org.kie.dmn.api.core.DMNMessage) DMNElementReference(org.kie.dmn.model.api.DMNElementReference) Reader(java.io.Reader) Test(org.junit.Test) DMNDecisionServicesTest(org.kie.dmn.core.decisionservices.DMNDecisionServicesTest)

Example 99 with Definitions

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)));
    }
}
Also used : Arrays(java.util.Arrays) RangeBoundary(org.kie.dmn.feel.runtime.Range.RangeBoundary) DecisionTable(org.kie.dmn.model.api.DecisionTable) DMNMessage(org.kie.dmn.api.core.DMNMessage) Bound(org.kie.dmn.validation.dtanalysis.model.Bound) DMNMessageType(org.kie.dmn.api.core.DMNMessageType) VALIDATE_COMPILATION(org.kie.dmn.validation.DMNValidator.Validation.VALIDATE_COMPILATION) RunWith(org.junit.runner.RunWith) Definitions(org.kie.dmn.model.api.Definitions) BigDecimal(java.math.BigDecimal) Hyperrectangle(org.kie.dmn.validation.dtanalysis.model.Hyperrectangle) Decision(org.kie.dmn.model.api.Decision) Matchers.hasSize(org.hamcrest.Matchers.hasSize) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Parameterized(org.junit.runners.Parameterized) Interval(org.kie.dmn.validation.dtanalysis.model.Interval) Collection(java.util.Collection) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Collectors(java.util.stream.Collectors) HitPolicy(org.kie.dmn.model.api.HitPolicy) List(java.util.List) Matchers.contains(org.hamcrest.Matchers.contains) DTAnalysis(org.kie.dmn.validation.dtanalysis.model.DTAnalysis) Overlap(org.kie.dmn.validation.dtanalysis.model.Overlap) ANALYZE_DECISION_TABLE(org.kie.dmn.validation.DMNValidator.Validation.ANALYZE_DECISION_TABLE) DecisionTable(org.kie.dmn.model.api.DecisionTable) DMNMessage(org.kie.dmn.api.core.DMNMessage) Definitions(org.kie.dmn.model.api.Definitions) Test(org.junit.Test)

Example 100 with Definitions

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

Definitions (org.kie.dmn.model.api.Definitions)114 Test (org.junit.Test)71 DMNRuntime (org.kie.dmn.api.core.DMNRuntime)39 DMNMessage (org.kie.dmn.api.core.DMNMessage)37 DMNModel (org.kie.dmn.api.core.DMNModel)34 DMNResult (org.kie.dmn.api.core.DMNResult)32 TDefinitions (org.kie.dmn.model.v1_1.TDefinitions)31 Arrays (java.util.Arrays)28 Assert.assertTrue (org.junit.Assert.assertTrue)28 List (java.util.List)26 ItemDefinition (org.kie.dmn.model.api.ItemDefinition)26 Logger (org.slf4j.Logger)26 LoggerFactory (org.slf4j.LoggerFactory)26 HashMap (java.util.HashMap)25 Decision (org.kie.dmn.model.api.Decision)25 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)24 Assert.assertFalse (org.junit.Assert.assertFalse)24 DecisionNodeImpl (org.kie.dmn.core.ast.DecisionNodeImpl)24 DMNModelImpl (org.kie.dmn.core.impl.DMNModelImpl)24 ArrayList (java.util.ArrayList)23