Search in sources :

Example 6 with JSITImport

use of org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITImport in project kie-wb-common by kiegroup.

the class DMNMarshallerImportsClientHelper method getImportDefinitionsAsync.

public Promise<Map<JSITImport, JSITDefinitions>> getImportDefinitionsAsync(final Metadata metadata, final List<JSITImport> imports) {
    if (!imports.isEmpty()) {
        return loadDMNDefinitions().then(otherDefinitions -> {
            final Map<JSITImport, JSITDefinitions> importDefinitions = new HashMap<>();
            for (final Map.Entry<String, JSITDefinitions> entry : otherDefinitions.entrySet()) {
                final JSITDefinitions def = Js.uncheckedCast(entry.getValue());
                findImportByDefinitions(def, imports).ifPresent(anImport -> {
                    final JSITImport foundImported = Js.uncheckedCast(anImport);
                    importDefinitions.put(foundImported, def);
                });
            }
            return promises.resolve(importDefinitions);
        });
    }
    return promises.resolve(Collections.emptyMap());
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) JSITImport(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITImport) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) JSITDefinitions(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITDefinitions)

Example 7 with JSITImport

use of org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITImport in project kie-wb-common by kiegroup.

the class DMNMarshallerImportsClientHelper method getDrgElementsWithNamespace.

private List<JSITDRGElement> getDrgElementsWithNamespace(final JSITDefinitions definitions, final JSITImport anImport) {
    final List<JSITDRGElement> result = new ArrayList<>();
    final List<JSITDRGElement> drgElements = definitions.getDrgElement();
    for (int i = 0; i < drgElements.size(); i++) {
        final JSITDRGElement drgElement = Js.uncheckedCast(drgElements.get(i));
        final JSITDRGElement element = Js.uncheckedCast(drgElementWithNamespace(drgElement, anImport));
        result.add(element);
    }
    return result;
}
Also used : JSITDRGElement(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITDRGElement) ArrayList(java.util.ArrayList)

Example 8 with JSITImport

use of org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITImport in project kie-wb-common by kiegroup.

the class DMNMarshallerImportsClientHelperTest method getPMMLDocumentsAsync.

@Test
public void getPMMLDocumentsAsync() {
    final PMMLDocumentMetadata pmmlDocumentMetadata = new PMMLDocumentMetadata(PMML_PATH, PMML_FILE, PMML.getDefaultNamespace(), Collections.emptyList());
    when(dmnImportsContentService.getModelsPMMLFilesURIs()).thenReturn(promises.resolve(new String[] { PMML_PATH }));
    when(dmnImportsContentService.loadFile(PMML_PATH)).thenReturn(promises.resolve(PMML_CONTENT));
    doReturn(promises.resolve(pmmlDocumentMetadata)).when(dmnImportsContentService).getPMMLDocumentMetadata(PMML_PATH);
    final List<JSITImport> imports = new ArrayList<>();
    final JSITImport jsImportMock = mock(JSITImport.class);
    when(jsImportMock.getLocationURI()).thenReturn(PMML_FILE);
    imports.add(jsImportMock);
    final Promise<Map<JSITImport, PMMLDocumentMetadata>> returnPromise = importsHelper.getPMMLDocumentsAsync(metadataMock, imports);
    returnPromise.then(def -> {
        assertEquals(1, def.size());
        assertEquals(PMML_PATH, def.get(jsImportMock).getPath());
        assertEquals(PMML_FILE, def.get(jsImportMock).getName());
        assertEquals(PMML.getDefaultNamespace(), def.get(jsImportMock).getImportType());
        assertEquals(0, def.get(jsImportMock).getModels().size());
        return promises.resolve();
    }).catch_(i -> {
        fail("Promise should've been resolved!");
        return promises.resolve();
    });
}
Also used : GwtMockitoTestRunner(com.google.gwtmockito.GwtMockitoTestRunner) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Arrays(java.util.Arrays) PMMLDocumentMetadata(org.kie.workbench.common.dmn.api.editors.included.PMMLDocumentMetadata) Mock(org.mockito.Mock) Promises(org.uberfire.client.promise.Promises) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) RunWith(org.junit.runner.RunWith) Promise(elemental2.promise.Promise) Captor(org.mockito.Captor) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) PMML(org.kie.workbench.common.dmn.api.editors.included.DMNImportTypes.PMML) ArgumentCaptor(org.mockito.ArgumentCaptor) Map(java.util.Map) Assert.fail(org.junit.Assert.fail) SyncPromises(org.uberfire.promise.SyncPromises) Mockito.doReturn(org.mockito.Mockito.doReturn) ServiceCallback(org.kie.workbench.common.stunner.core.client.service.ServiceCallback) Before(org.junit.Before) PMMLIncludedModel(org.kie.workbench.common.dmn.api.editors.included.PMMLIncludedModel) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Metadata(org.kie.workbench.common.stunner.core.diagram.Metadata) Mockito.verify(org.mockito.Mockito.verify) Mockito(org.mockito.Mockito) List(java.util.List) Mockito.never(org.mockito.Mockito.never) JSITImport(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITImport) IncludedModel(org.kie.workbench.common.dmn.api.editors.included.IncludedModel) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) PMMLDocumentMetadata(org.kie.workbench.common.dmn.api.editors.included.PMMLDocumentMetadata) ArrayList(java.util.ArrayList) JSITImport(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITImport) Map(java.util.Map) Test(org.junit.Test)

Example 9 with JSITImport

use of org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITImport in project kie-wb-common by kiegroup.

the class DMNUnmarshaller method unmarshall.

private Promise<Graph> unmarshall(final Metadata metadata, final JSITDefinitions dmnDefinitions, final Map<JSITImport, JSITDefinitions> importDefinitions, final Map<JSITImport, PMMLDocumentMetadata> pmmlDocuments) {
    final Map<String, HasComponentWidths> hasComponentWidthsMap = new HashMap<>();
    final BiConsumer<String, HasComponentWidths> hasComponentWidthsConsumer = (uuid, hcw) -> {
        if (Objects.nonNull(uuid)) {
            hasComponentWidthsMap.put(uuid, hcw);
        }
    };
    // Check before the DRG creation ('ensureDRGElementExists').
    final boolean isDMNDIPresent = Optional.ofNullable(dmnDefinitions.getDMNDI()).isPresent();
    ensureDRGElementExists(dmnDefinitions);
    final Definitions wbDefinitions = DefinitionsConverter.wbFromDMN(dmnDefinitions, importDefinitions, pmmlDocuments);
    final List<NodeEntry> nodeEntries = modelToStunnerConverter.makeNodes(dmnDefinitions, importDefinitions, isDMNDIPresent, hasComponentWidthsConsumer);
    final List<JSITDecisionService> dmnDecisionServices = getDecisionServices(nodeEntries);
    // Ensure all locations are updated to relative for Stunner
    nodeEntries.forEach(e -> PointUtils.convertToRelativeBounds(e.getNode()));
    final Map<String, Diagram> stunnerDiagramsById = new HashMap<>();
    final Map<String, DMNDiagramElement> dmnDiagramsById = new HashMap<>();
    for (final DMNDiagramElement dmnDiagramElement : wbDefinitions.getDiagramElements()) {
        final String dmnDiagramId = dmnDiagramElement.getId().getValue();
        final Diagram value = factoryManager.newDiagram(dmnDiagramId, BindableAdapterUtils.getDefinitionSetId(DMNDefinitionSet.class), metadata);
        stunnerDiagramsById.put(dmnDiagramId, value);
        dmnDiagramsById.put(dmnDiagramId, dmnDiagramElement);
    }
    final DMNDiagramsSessionState state = dmnDiagramsSession.setState(metadata, stunnerDiagramsById, dmnDiagramsById);
    nodeEntries.forEach(nodeEntry -> {
        final String diagramId = nodeEntry.getDiagramId();
        final Graph graph = stunnerDiagramsById.get(diagramId).getGraph();
        graph.addNode(nodeEntry.getNode());
    });
    final Graph drgGraph = state.getDRGDiagram().getGraph();
    loadImportedItemDefinitions(wbDefinitions, importDefinitions);
    for (final Diagram value : stunnerDiagramsById.values()) {
        final Node<?, ?> dmnDiagramRoot = DMNGraphUtils.findDMNDiagramRoot(value.getGraph());
        ((View<DMNDiagram>) dmnDiagramRoot.getContent()).getDefinition().setDefinitions(wbDefinitions);
        nodeEntries.forEach(nodeEntry -> {
            if (Objects.equals(stunnerDiagramsById.get(nodeEntry.getDiagramId()), value)) {
                connectRootWithChild(dmnDiagramRoot, nodeEntry.getNode());
            }
        });
    }
    // Only connect Nodes to the Diagram that are not referenced by DecisionServices
    final List<String> references = new ArrayList<>();
    final List<JSITDecisionService> lstDecisionServices = new ArrayList<>(dmnDecisionServices);
    for (int iDS = 0; iDS < lstDecisionServices.size(); iDS++) {
        final JSITDecisionService jsiDecisionService = Js.uncheckedCast(lstDecisionServices.get(iDS));
        final List<JSITDMNElementReference> jsiEncapsulatedDecisions = jsiDecisionService.getEncapsulatedDecision();
        if (Objects.nonNull(jsiEncapsulatedDecisions)) {
            for (int i = 0; i < jsiEncapsulatedDecisions.size(); i++) {
                final JSITDMNElementReference jsiEncapsulatedDecision = Js.uncheckedCast(jsiEncapsulatedDecisions.get(i));
                references.add(jsiEncapsulatedDecision.getHref());
            }
        }
        final List<JSITDMNElementReference> jsiOutputDecisions = jsiDecisionService.getOutputDecision();
        if (Objects.nonNull(jsiOutputDecisions)) {
            for (int i = 0; i < jsiOutputDecisions.size(); i++) {
                final JSITDMNElementReference jsiOutputDecision = Js.uncheckedCast(jsiOutputDecisions.get(i));
                references.add(jsiOutputDecision.getHref());
            }
        }
    }
    // Copy ComponentWidths information
    final List<JSITComponentsWidthsExtension> extensions = findComponentsWidthsExtensions(dmnDefinitions.getDMNDI().getDMNDiagram());
    extensions.forEach(componentsWidthsExtension -> {
        // can be imported from another diagram but the extension is not imported or present in this diagram.
        if (Objects.nonNull(componentsWidthsExtension.getComponentWidths())) {
            hasComponentWidthsMap.entrySet().forEach(es -> {
                final List<JSITComponentWidths> jsiComponentWidths = componentsWidthsExtension.getComponentWidths();
                for (int i = 0; i < jsiComponentWidths.size(); i++) {
                    final JSITComponentWidths jsiWidths = Js.uncheckedCast(jsiComponentWidths.get(i));
                    if (Objects.equals(jsiWidths.getDmnElementRef(), es.getKey())) {
                        final List<Double> widths = es.getValue().getComponentWidths();
                        if (Objects.nonNull(jsiWidths.getWidth())) {
                            widths.clear();
                            for (int w = 0; w < jsiWidths.getWidth().size(); w++) {
                                final double width = jsiWidths.getWidth().get(w).doubleValue();
                                widths.add(width);
                            }
                        }
                    }
                }
            });
        }
    });
    return promises.resolve(drgGraph);
}
Also used : JSIDMNDiagram(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmndi12.JSIDMNDiagram) Definitions(org.kie.workbench.common.dmn.api.definition.model.Definitions) Edge(org.kie.workbench.common.stunner.core.graph.Edge) UUID(org.kie.workbench.common.stunner.core.util.UUID) EdgeImpl(org.kie.workbench.common.stunner.core.graph.impl.EdgeImpl) Map(java.util.Map) DMNDiagramElementsUtils(org.kie.workbench.common.dmn.client.marshaller.common.DMNDiagramElementsUtils) JSITDMNElementReference(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITDMNElementReference) FactoryManager(org.kie.workbench.common.stunner.core.api.FactoryManager) Diagram(org.kie.workbench.common.stunner.core.diagram.Diagram) HasComponentWidths(org.kie.workbench.common.dmn.api.definition.HasComponentWidths) Child(org.kie.workbench.common.stunner.core.graph.content.relationship.Child) ItemDefinition(org.kie.workbench.common.dmn.api.definition.model.ItemDefinition) DMNDiagram(org.kie.workbench.common.dmn.api.definition.model.DMNDiagram) Collectors(java.util.stream.Collectors) JSITComponentWidths(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.kie.JSITComponentWidths) Metadata(org.kie.workbench.common.stunner.core.diagram.Metadata) Objects(java.util.Objects) JSITDecisionService(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITDecisionService) List(java.util.List) DMNDiagramsSession(org.kie.workbench.common.dmn.client.docks.navigator.drds.DMNDiagramsSession) DMNGraphUtils(org.kie.workbench.common.dmn.client.marshaller.common.DMNGraphUtils) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) ApplicationScoped(javax.enterprise.context.ApplicationScoped) DMNDiagramElement(org.kie.workbench.common.dmn.api.definition.model.DMNDiagramElement) Node(org.kie.workbench.common.stunner.core.graph.Node) PMMLDocumentMetadata(org.kie.workbench.common.dmn.api.editors.included.PMMLDocumentMetadata) JSIDiagramElement(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.di.JSIDiagramElement) DMNDefinitionSet(org.kie.workbench.common.dmn.api.DMNDefinitionSet) Promises(org.uberfire.client.promise.Promises) HashMap(java.util.HashMap) Promise(elemental2.promise.Promise) View(org.kie.workbench.common.stunner.core.graph.content.view.View) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Js(jsinterop.base.Js) NodeEntry(org.kie.workbench.common.dmn.client.marshaller.unmarshall.nodes.NodeEntry) DMNMarshallerImportsClientHelper(org.kie.workbench.common.dmn.client.marshaller.included.DMNMarshallerImportsClientHelper) BiConsumer(java.util.function.BiConsumer) NodeEntriesFactory(org.kie.workbench.common.dmn.client.marshaller.unmarshall.nodes.NodeEntriesFactory) JSITDefinitions(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITDefinitions) BindableAdapterUtils(org.kie.workbench.common.stunner.core.definition.adapter.binding.BindableAdapterUtils) MainJs(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.MainJs) DMNDiagramsSessionState(org.kie.workbench.common.dmn.client.docks.navigator.drds.DMNDiagramsSessionState) JSITItemDefinition(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITItemDefinition) DefinitionsConverter(org.kie.workbench.common.dmn.client.marshaller.converters.DefinitionsConverter) PointUtils(org.kie.workbench.common.dmn.client.marshaller.converters.dd.PointUtils) DMNModelInstrumentedBase(org.kie.workbench.common.dmn.api.definition.model.DMNModelInstrumentedBase) Graph(org.kie.workbench.common.stunner.core.graph.Graph) JsUtils(org.kie.workbench.common.dmn.webapp.kogito.marshaller.mapper.JsUtils) ItemDefinitionPropertyConverter(org.kie.workbench.common.dmn.client.marshaller.converters.ItemDefinitionPropertyConverter) JSITImport(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITImport) JSITComponentsWidthsExtension(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.kie.JSITComponentsWidthsExtension) JSITComponentsWidthsExtension(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.kie.JSITComponentsWidthsExtension) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HasComponentWidths(org.kie.workbench.common.dmn.api.definition.HasComponentWidths) JSITDecisionService(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITDecisionService) DMNDiagramElement(org.kie.workbench.common.dmn.api.definition.model.DMNDiagramElement) DMNDiagramsSessionState(org.kie.workbench.common.dmn.client.docks.navigator.drds.DMNDiagramsSessionState) JSITComponentWidths(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.kie.JSITComponentWidths) JSIDMNDiagram(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmndi12.JSIDMNDiagram) DMNDiagram(org.kie.workbench.common.dmn.api.definition.model.DMNDiagram) Definitions(org.kie.workbench.common.dmn.api.definition.model.Definitions) JSITDefinitions(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITDefinitions) JSITDMNElementReference(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITDMNElementReference) DMNDefinitionSet(org.kie.workbench.common.dmn.api.DMNDefinitionSet) JSIDMNDiagram(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmndi12.JSIDMNDiagram) Diagram(org.kie.workbench.common.stunner.core.diagram.Diagram) DMNDiagram(org.kie.workbench.common.dmn.api.definition.model.DMNDiagram) Graph(org.kie.workbench.common.stunner.core.graph.Graph) NodeEntry(org.kie.workbench.common.dmn.client.marshaller.unmarshall.nodes.NodeEntry)

Example 10 with JSITImport

use of org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITImport in project kie-wb-common by kiegroup.

the class DMNUnmarshaller method getWbImportedItemDefinitions.

private List<ItemDefinition> getWbImportedItemDefinitions(final Map<JSITImport, JSITDefinitions> importDefinitions) {
    final List<ItemDefinition> definitions = new ArrayList<>();
    final List<JSITItemDefinition> importedDefinitions = dmnMarshallerImportsHelper.getImportedItemDefinitions(importDefinitions);
    for (int i = 0; i < importedDefinitions.size(); i++) {
        final JSITItemDefinition definition = Js.uncheckedCast(importedDefinitions.get(i));
        final ItemDefinition converted = ItemDefinitionPropertyConverter.wbFromDMN(definition);
        if (converted != null) {
            converted.setAllowOnlyVisualChange(true);
            definitions.add(converted);
        }
    }
    return definitions;
}
Also used : ItemDefinition(org.kie.workbench.common.dmn.api.definition.model.ItemDefinition) JSITItemDefinition(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITItemDefinition) ArrayList(java.util.ArrayList) JSITItemDefinition(org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITItemDefinition)

Aggregations

JSITImport (org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITImport)11 HashMap (java.util.HashMap)9 Map (java.util.Map)9 ArrayList (java.util.ArrayList)7 JSITDefinitions (org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITDefinitions)7 JSITItemDefinition (org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITItemDefinition)6 PMMLDocumentMetadata (org.kie.workbench.common.dmn.api.editors.included.PMMLDocumentMetadata)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 Import (org.kie.workbench.common.dmn.api.definition.model.Import)4 ItemDefinition (org.kie.workbench.common.dmn.api.definition.model.ItemDefinition)4 Name (org.kie.workbench.common.dmn.api.property.dmn.Name)3 QName (org.kie.workbench.common.dmn.api.property.dmn.QName)3 Promise (elemental2.promise.Promise)2 List (java.util.List)2 Objects (java.util.Objects)2 Optional (java.util.Optional)2 QName (javax.xml.namespace.QName)2 DMNDiagramElement (org.kie.workbench.common.dmn.api.definition.model.DMNDiagramElement)2 DMNModelInstrumentedBase (org.kie.workbench.common.dmn.api.definition.model.DMNModelInstrumentedBase)2 Definitions (org.kie.workbench.common.dmn.api.definition.model.Definitions)2