Search in sources :

Example 6 with TDefinitions

use of org.kie.dmn.model.v1_1.TDefinitions in project kie-wb-common by kiegroup.

the class DMNDIExtensionsRegister method beforeMarshal.

@Override
public void beforeMarshal(final Object o, final QNameMap qmap) {
    if (o instanceof TDefinitions) {
        final TDefinitions tDefinitions = (TDefinitions) o;
        final String prefix = tDefinitions.getPrefixForNamespaceURI(KieDMNModelInstrumentedBase.URI_KIE).orElse(KIE.getPrefix());
        qmap.registerMapping(new QName(KIE.getUri(), COMPONENTS_WIDTHS_EXTENSION_ALIAS, prefix), COMPONENTS_WIDTHS_EXTENSION_ALIAS);
        qmap.registerMapping(new QName(KIE.getUri(), COMPONENT_WIDTHS_ALIAS, prefix), COMPONENT_WIDTHS_ALIAS);
        qmap.registerMapping(new QName(KIE.getUri(), COMPONENT_WIDTH_ALIAS, prefix), COMPONENT_WIDTH_ALIAS);
        qmap.registerMapping(new QName(KIE.getUri(), EXTERNAL_LINK_ALIAS, prefix), EXTERNAL_LINK_ALIAS);
    }
}
Also used : QName(javax.xml.namespace.QName) TDefinitions(org.kie.dmn.model.v1_2.TDefinitions)

Example 7 with TDefinitions

use of org.kie.dmn.model.v1_1.TDefinitions in project drools by kiegroup.

the class DMNModelInstrumentedBaseConverter method writeAttributes.

@Override
protected void writeAttributes(HierarchicalStreamWriter writer, Object parent) {
    KieDMNModelInstrumentedBase mib = (KieDMNModelInstrumentedBase) parent;
    CustomStaxWriter staxWriter = ((CustomStaxWriter) writer.underlyingWriter());
    for (Entry<String, String> kv : mib.getNsContext().entrySet()) {
        try {
            if (KieDMNModelInstrumentedBase.URI_DMN.equals(kv.getValue())) {
            // skip as that is the default namespace xmlns<:prefix>=DMN is handled by the stax driver.
            } else {
                staxWriter.writeNamespace(kv.getKey(), kv.getValue());
            }
        } catch (Exception e) {
            LOG.warn("The XML driver writer failed to manage writing namespace, namespaces prefixes could be wrong in the resulting file.", e);
        }
    }
    for (Entry<QName, String> kv : mib.getAdditionalAttributes().entrySet()) {
        staxWriter.addAttribute(kv.getKey().getPrefix() + ":" + kv.getKey().getLocalPart(), kv.getValue());
    }
    if (parent instanceof TDefinitions) {
        TDefinitions tDefinitions = (TDefinitions) parent;
        String dmndiPrefix = tDefinitions.getPrefixForNamespaceURI(KieDMNModelInstrumentedBase.URI_DMNDI).orElse("dmndi");
        String diPrefix = tDefinitions.getPrefixForNamespaceURI(KieDMNModelInstrumentedBase.URI_DI).orElse("di");
        String dcPrefix = tDefinitions.getPrefixForNamespaceURI(KieDMNModelInstrumentedBase.URI_DC).orElse("dc");
        staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DMNDI, "DMNDI", dmndiPrefix), "DMNDI");
        staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DMNDI, "DMNDiagram", dmndiPrefix), "DMNDiagram");
        staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DMNDI, "DMNStyle", dmndiPrefix), "style");
        staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DMNDI, "DMNStyle", dmndiPrefix), "DMNStyle");
        staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DMNDI, "DMNShape", dmndiPrefix), "DMNShape");
        staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DMNDI, "DMNEdge", dmndiPrefix), "DMNEdge");
        staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DMNDI, "DMNDecisionServiceDividerLine", dmndiPrefix), "DMNDecisionServiceDividerLine");
        staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DMNDI, "DMNLabel", dmndiPrefix), "DMNLabel");
        staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DMNDI, DMNLabelConverter.TEXT, dmndiPrefix), DMNLabelConverter.TEXT);
        staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DMNDI, "Size", dmndiPrefix), "Size");
        staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DMNDI, "FillColor", dmndiPrefix), "FillColor");
        staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DMNDI, "StrokeColor", dmndiPrefix), "StrokeColor");
        staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DMNDI, "FontColor", dmndiPrefix), "FontColor");
        staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DI, "waypoint", diPrefix), "waypoint");
        staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DI, "extension", diPrefix), "extension");
        staxWriter.getQNameMap().registerMapping(new QName(KieDMNModelInstrumentedBase.URI_DC, "Bounds", dcPrefix), "Bounds");
    }
}
Also used : CustomStaxWriter(org.kie.dmn.backend.marshalling.CustomStaxWriter) QName(javax.xml.namespace.QName) KieDMNModelInstrumentedBase(org.kie.dmn.model.v1_2.KieDMNModelInstrumentedBase) TDefinitions(org.kie.dmn.model.v1_2.TDefinitions)

Example 8 with TDefinitions

use of org.kie.dmn.model.v1_1.TDefinitions in project drools by kiegroup.

the class DMNRuntimeTest method buildSimplifiedDefinitions.

private Definitions buildSimplifiedDefinitions(final String namespace, final String... decisions) {
    final Definitions def = new TDefinitions();
    def.setNamespace(namespace);
    for (final String d : decisions) {
        final Decision dec = new TDecision();
        dec.setName(d);
        def.getDrgElement().add(dec);
        def.addChildren(dec);
        dec.setParent(def);
    }
    return def;
}
Also used : TDefinitions(org.kie.dmn.model.v1_1.TDefinitions) Definitions(org.kie.dmn.model.api.Definitions) TDecision(org.kie.dmn.model.v1_1.TDecision) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) TDefinitions(org.kie.dmn.model.v1_1.TDefinitions) TDecision(org.kie.dmn.model.v1_1.TDecision) Decision(org.kie.dmn.model.api.Decision)

Example 9 with TDefinitions

use of org.kie.dmn.model.v1_1.TDefinitions in project drools by kiegroup.

the class DMNRuntimeTest method buildSimplifiedDefinitions.

private Definitions buildSimplifiedDefinitions(final String namespace, final String... decisions) {
    final Definitions def = new TDefinitions();
    def.setNamespace(namespace);
    for (final String d : decisions) {
        final Decision dec = new TDecision();
        dec.setName(d);
        def.getDrgElement().add(dec);
        def.addChildren(dec);
        dec.setParent(def);
    }
    return def;
}
Also used : TDefinitions(org.kie.dmn.model.v1_1.TDefinitions) Definitions(org.kie.dmn.model.api.Definitions) TDecision(org.kie.dmn.model.v1_1.TDecision) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) TDefinitions(org.kie.dmn.model.v1_1.TDefinitions) TDecision(org.kie.dmn.model.v1_1.TDecision) Decision(org.kie.dmn.model.api.Decision)

Example 10 with TDefinitions

use of org.kie.dmn.model.v1_1.TDefinitions 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

TDefinitions (org.kie.dmn.model.v1_1.TDefinitions)5 QName (javax.xml.namespace.QName)4 Test (org.junit.Test)3 CustomStaxWriter (org.kie.dmn.backend.marshalling.CustomStaxWriter)3 Definitions (org.kie.dmn.model.api.Definitions)3 TDefinitions (org.kie.dmn.model.v1_2.TDefinitions)3 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 Decision (org.kie.dmn.model.api.Decision)2 TDecision (org.kie.dmn.model.v1_1.TDecision)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Cell (org.apache.poi.ss.usermodel.Cell)1 DataFormatter (org.apache.poi.ss.usermodel.DataFormatter)1 Row (org.apache.poi.ss.usermodel.Row)1 Sheet (org.apache.poi.ss.usermodel.Sheet)1 ExcelParser (org.drools.decisiontable.parser.xls.ExcelParser)1 DMNMarshaller (org.kie.dmn.api.marshalling.DMNMarshaller)1 DMNModelInstrumentedBase (org.kie.dmn.model.api.DMNModelInstrumentedBase)1