use of org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.kie.JSITComponentsWidthsExtension in project kie-wb-common by kiegroup.
the class DMNMarshaller method marshall.
public JSITDefinitions marshall() {
final Map<String, JSITDRGElement> nodes = new HashMap<>();
final Map<String, JSITTextAnnotation> textAnnotations = new HashMap<>();
final Node<View<DMNDiagram>, ?> dmnDiagramRoot = (Node<View<DMNDiagram>, ?>) DMNGraphUtils.findDMNDiagramRoot(dmnDiagramsSession.getDRGDiagram().getGraph());
final Definitions definitionsStunnerPojo = ((DMNDiagram) getElementDefinition(dmnDiagramRoot)).getDefinitions();
final List<String> dmnDiagramElementIds = new ArrayList<>();
final JSITDefinitions definitions = DefinitionsConverter.dmnFromWB(definitionsStunnerPojo, true);
if (Objects.isNull(definitions.getExtensionElements())) {
JSITDMNElement.JSIExtensionElements jsiExtensionElements = new JSITDMNElement.JSIExtensionElements();
definitions.setExtensionElements(jsiExtensionElements);
}
final JsArrayLike<JSIDMNDiagram> dmnDiagrams = definitions.getDMNDI().getNativeDMNDiagram();
for (int i = 0; i < dmnDiagrams.getLength(); i++) {
JSIDMNDiagram diagram = Js.uncheckedCast(dmnDiagrams.getAt(i));
final String elementDiagramId = diagram.getId();
final List<JSIDMNEdge> dmnEdges = new ArrayList<>();
final List<Node> diagramNodes = getNodeStream(dmnDiagramsSession.getDiagram(elementDiagramId));
// Setup callback for marshalling ComponentWidths
if (Objects.isNull(diagram.getExtension())) {
diagram.setExtension(new JSIDiagramElement.JSIExtension());
}
final JSITComponentsWidthsExtension componentsWidthsExtension = new JSITComponentsWidthsExtension();
final JSIDiagramElement.JSIExtension extension = diagram.getExtension();
JSITComponentsWidthsExtension wrappedComponentsWidthsExtension = WrapperUtils.getWrappedJSITComponentsWidthsExtension(componentsWidthsExtension);
extension.addAny(wrappedComponentsWidthsExtension);
final Consumer<JSITComponentWidths> componentWidthsConsumer = (cw) -> componentsWidthsExtension.addComponentWidths(cw);
// Convert relative positioning to absolute
for (final Node<?, ?> node : diagramNodes) {
PointUtils.convertToAbsoluteBounds(node);
}
// Iterate Graph processing nodes..
for (final Node<?, ?> node : diagramNodes) {
if (!(node.getContent() instanceof View<?>)) {
continue;
}
final View<?> view = (View<?>) node.getContent();
final Object viewDefinition = view.getDefinition();
if (!(viewDefinition instanceof HasContentDefinitionId)) {
continue;
}
final HasContentDefinitionId hasContentDefinitionId = (HasContentDefinitionId) viewDefinition;
final String nodeDiagramId = hasContentDefinitionId.getDiagramId();
if (!Objects.equals(nodeDiagramId, elementDiagramId)) {
continue;
}
if (viewDefinition instanceof DRGElement) {
final DRGElement drgElement = (DRGElement) viewDefinition;
if (!drgElement.isAllowOnlyVisualChange()) {
if (nodes.containsKey(drgElement.getId().getValue())) {
final JSITDRGElement currentValue = nodes.get(drgElement.getId().getValue());
mergeNodeRequirements(stunnerToDMN(withIncludedModels(node, definitionsStunnerPojo), componentWidthsConsumer), currentValue);
} else {
nodes.put(drgElement.getId().getValue(), stunnerToDMN(withIncludedModels(node, definitionsStunnerPojo), componentWidthsConsumer));
}
}
final String namespaceURI = definitionsStunnerPojo.getDefaultNamespace();
diagram.addDMNDiagramElement(WrapperUtils.getWrappedJSIDMNShape(diagram, dmnDiagramElementIds, definitionsStunnerPojo, (View<? extends DMNElement>) view, namespaceURI));
}
if (viewDefinition instanceof TextAnnotation) {
final TextAnnotation textAnnotation = (TextAnnotation) viewDefinition;
if (!textAnnotation.isAllowOnlyVisualChange()) {
textAnnotations.put(textAnnotation.getId().getValue(), textAnnotationConverter.dmnFromNode((Node<View<TextAnnotation>, ?>) node, componentWidthsConsumer));
}
final String namespaceURI = definitionsStunnerPojo.getDefaultNamespace();
diagram.addDMNDiagramElement(WrapperUtils.getWrappedJSIDMNShape(diagram, dmnDiagramElementIds, definitionsStunnerPojo, (View<? extends DMNElement>) view, namespaceURI));
final List<JSITAssociation> associations = AssociationConverter.dmnFromWB((Node<View<TextAnnotation>, ?>) node);
forEach(associations, association -> {
final JSITAssociation wrappedJSITAssociation = WrapperUtils.getWrappedJSITAssociation(Js.uncheckedCast(association));
definitions.addArtifact(wrappedJSITAssociation);
});
}
connect(diagram, dmnDiagramElementIds, definitionsStunnerPojo, dmnEdges, node, view);
}
nodes.values().forEach(node -> {
addNodeToDefinitionsIfNotPresent(node, definitions);
});
textAnnotations.values().forEach(text -> {
final boolean exists = anyMatch(definitions.getArtifact(), artifact -> Objects.equals(artifact.getId(), text.getId()));
if (!exists) {
definitions.addArtifact(WrapperUtils.getWrappedJSITTextAnnotation(text));
}
});
forEach(dmnEdges, dmnEdge -> {
final boolean exists = anyMatch(diagram.getDMNDiagramElement(), diagramElement -> {
if (JSIDMNEdge.instanceOf(diagramElement)) {
final JSIDMNEdge jsidmnEdge = Js.uncheckedCast(diagramElement);
return Objects.equals(jsidmnEdge.getDmnElementRef(), dmnEdge.getDmnElementRef());
}
return false;
});
if (!exists) {
diagram.addDMNDiagramElement(WrapperUtils.getWrappedJSIDMNEdge(Js.uncheckedCast(dmnEdge)));
}
});
// Convert absolute positioning to relative
for (final Node<?, ?> node : diagramNodes) {
PointUtils.convertToRelativeBounds(node);
}
}
;
return definitions;
}
use of org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.kie.JSITComponentsWidthsExtension in project kie-wb-common by kiegroup.
the class DMNUnmarshaller method findComponentsWidthsExtensions.
private List<JSITComponentsWidthsExtension> findComponentsWidthsExtensions(final List<JSIDMNDiagram> dmnDDDiagrams) {
final List<JSITComponentsWidthsExtension> componentsWidthsExtensions = new ArrayList<>();
for (int index = 0, dmnDiagram1Size = dmnDDDiagrams.size(); index < dmnDiagram1Size; index++) {
final JSIDMNDiagram jsiDiagram = Js.uncheckedCast(dmnDDDiagrams.get(index));
final JSIDiagramElement.JSIExtension dmnDDExtensions = Js.uncheckedCast(jsiDiagram.getExtension());
if (Objects.isNull(dmnDDExtensions)) {
break;
}
if (Objects.isNull(dmnDDExtensions.getAny())) {
break;
}
final List<Object> extensions = dmnDDExtensions.getAny();
if (!Objects.isNull(extensions)) {
for (int i = 0; i < extensions.size(); i++) {
final Object wrapped = extensions.get(i);
final Object extension = JsUtils.getUnwrappedElement(wrapped);
if (JSITComponentsWidthsExtension.instanceOf(extension)) {
final JSITComponentsWidthsExtension jsiExtension = Js.uncheckedCast(extension);
componentsWidthsExtensions.add(jsiExtension);
}
}
}
}
return componentsWidthsExtensions;
}
use of org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.kie.JSITComponentsWidthsExtension 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);
}
use of org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.kie.JSITComponentsWidthsExtension in project kie-wb-common by kiegroup.
the class WrapperUtils method getWrappedJSITComponentsWidthsExtension.
public static JSITComponentsWidthsExtension getWrappedJSITComponentsWidthsExtension(final JSITComponentsWidthsExtension componentsWidthsExtension) {
final JSITComponentsWidthsExtension toReturn = Js.uncheckedCast(JsUtils.getWrappedElement(componentsWidthsExtension));
final JSIName jsiName = JSITComponentsWidthsExtension.getJSIName();
updateJSIName(jsiName, "kie", "ComponentsWidthsExtension");
JsUtils.setNameOnWrapped(toReturn, jsiName);
return toReturn;
}
Aggregations