Search in sources :

Example 21 with DMNMarshaller

use of org.kie.dmn.api.marshalling.DMNMarshaller in project drools by kiegroup.

the class DMNXMLLoaderTest method test0004_multiple_extensions.

@Test
public void test0004_multiple_extensions() throws Exception {
    DMNMarshaller marshaller = DMNMarshallerFactory.newMarshallerWithExtensions(Arrays.asList(new DecisionServicesExtensionRegister()));
    final InputStream is = this.getClass().getResourceAsStream("0004-decision-services_multiple_extensions.dmn");
    final InputStreamReader isr = new InputStreamReader(is);
    final Definitions def = marshaller.unmarshal(isr);
    assertThat(def.getExtensionElements().getAny().size(), is(1));
// if arrived here, means it did not fail with exception while trying to unmarshall unknown rss extension element, hence it just skipped it.
}
Also used : DMNMarshaller(org.kie.dmn.api.marshalling.DMNMarshaller) InputStreamReader(java.io.InputStreamReader) DecisionServicesExtensionRegister(org.kie.dmn.backend.marshalling.v1_1.xstream.extensions.DecisionServicesExtensionRegister) InputStream(java.io.InputStream) Definitions(org.kie.dmn.model.api.Definitions) Test(org.junit.Test)

Example 22 with DMNMarshaller

use of org.kie.dmn.api.marshalling.DMNMarshaller in project drools by kiegroup.

the class UnmarshalMarshalTest method test0004_ns_other_location.

@Test
public void test0004_ns_other_location() throws Exception {
    DMNMarshaller marshaller = DMNMarshallerFactory.newMarshallerWithExtensions(Arrays.asList(new DecisionServicesExtensionRegister()));
    testRoundTrip("org/kie/dmn/backend/marshalling/v1_1/", "0004-decision-services_ns_other_location.dmn", marshaller);
}
Also used : DMNMarshaller(org.kie.dmn.api.marshalling.DMNMarshaller) DecisionServicesExtensionRegister(org.kie.dmn.backend.marshalling.v1_1.xstream.extensions.DecisionServicesExtensionRegister) Test(org.junit.Test)

Example 23 with DMNMarshaller

use of org.kie.dmn.api.marshalling.DMNMarshaller 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 24 with DMNMarshaller

use of org.kie.dmn.api.marshalling.DMNMarshaller 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 25 with DMNMarshaller

use of org.kie.dmn.api.marshalling.DMNMarshaller in project kie-wb-common by kiegroup.

the class DMNMarshallerProducerTest method testGet.

@Test
public void testGet() {
    final DMNMarshallerProducer producer = new DMNMarshallerProducer();
    final DMNMarshaller marshaller = producer.get();
    assertNotNull(marshaller);
    assertSame(marshaller, producer.get());
    assertSame(marshaller, producer.get());
}
Also used : DMNMarshaller(org.kie.dmn.api.marshalling.DMNMarshaller) Test(org.junit.Test)

Aggregations

DMNMarshaller (org.kie.dmn.api.marshalling.DMNMarshaller)25 Test (org.junit.Test)19 Definitions (org.kie.dmn.model.api.Definitions)16 InputStreamReader (java.io.InputStreamReader)14 InputStream (java.io.InputStream)6 DecisionServicesExtensionRegister (org.kie.dmn.backend.marshalling.v1_1.xstream.extensions.DecisionServicesExtensionRegister)6 File (java.io.File)5 FileInputStream (java.io.FileInputStream)5 FileOutputStream (java.io.FileOutputStream)5 FileWriter (java.io.FileWriter)5 Files (java.nio.file.Files)5 Arrays (java.util.Arrays)5 HashSet (java.util.HashSet)5 Set (java.util.Set)5 QName (javax.xml.namespace.QName)5 Source (javax.xml.transform.Source)5 StreamSource (javax.xml.transform.stream.StreamSource)5 Assert.assertFalse (org.junit.Assert.assertFalse)5 Assert.assertTrue (org.junit.Assert.assertTrue)5 Logger (org.slf4j.Logger)5