use of org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmndi12.JSIDMNDiagram 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.dmndi12.JSIDMNDiagram in project kie-wb-common by kiegroup.
the class DMNMarshaller method connect.
protected void connect(final JSIDMNDiagram diagram, final List<String> dmnDiagramElementIds, final Definitions definitionsStunnerPojo, final List<JSIDMNEdge> dmnEdges, final Node<?, ?> node, final View<?> view) {
// DMNDI Edge management.
final List<Edge<?, ?>> inEdges = (List<Edge<?, ?>>) node.getInEdges();
for (Edge<?, ?> e : inEdges) {
if (e.getContent() instanceof ViewConnector) {
final ViewConnector connectionContent = (ViewConnector) e.getContent();
DiscreteConnection sourceConnection = (DiscreteConnection) connectionContent.getSourceConnection().orElse(null);
DiscreteConnection targetConnection = (DiscreteConnection) connectionContent.getTargetConnection().orElse(null);
if (sourceConnection != null && targetConnection != null) {
final Node<?, ?> sourceNode = e.getSourceNode();
final View<?> sourceView = (View<?>) sourceNode.getContent();
Point2D sourcePoint = sourceConnection.getLocation();
double xSource = xOfBound(upperLeftBound(sourceView));
double ySource = yOfBound(upperLeftBound(sourceView));
if (Objects.isNull(sourcePoint)) {
// If the "connection source/target location is null" assume it's the centre of the shape.
if (sourceView.getDefinition() instanceof DMNViewDefinition) {
DMNViewDefinition dmnViewDefinition = (DMNViewDefinition) sourceView.getDefinition();
xSource += dmnViewDefinition.getDimensionsSet().getWidth().getValue() / 2;
ySource += dmnViewDefinition.getDimensionsSet().getHeight().getValue() / 2;
}
sourcePoint = Point2D.create(xSource, ySource);
} else {
// If it is non-null it is relative to the source/target shape location.
sourcePoint = Point2D.create(xSource + sourcePoint.getX(), ySource + sourcePoint.getY());
}
Point2D targetPoint = targetConnection.getLocation();
double xTarget = xOfBound(upperLeftBound(view));
double yTarget = yOfBound(upperLeftBound(view));
if (Objects.isNull(targetPoint)) {
// If the "connection source/target location is null" assume it's the centre of the shape.
if (view.getDefinition() instanceof DMNViewDefinition) {
DMNViewDefinition dmnViewDefinition = (DMNViewDefinition) view.getDefinition();
xTarget += dmnViewDefinition.getDimensionsSet().getWidth().getValue() / 2;
yTarget += dmnViewDefinition.getDimensionsSet().getHeight().getValue() / 2;
}
targetPoint = Point2D.create(xTarget, yTarget);
} else {
// If it is non-null it is relative to the source/target shape location.
targetPoint = Point2D.create(xTarget + targetPoint.getX(), yTarget + targetPoint.getY());
}
final JSIDMNEdge dmnEdge = new JSIDMNEdge();
// DMNDI edge elementRef is uuid of Stunner edge,
// with the only exception when edge contains as content a DMN Association (Association is an edge)
final String uuid = getRawId(getUUID(e));
final String edgeId = getEdgeId(diagram, dmnDiagramElementIds, uuid, sourceConnection.isAuto(), targetConnection.isAuto());
dmnEdge.setId(edgeId);
final String namespaceURI = definitionsStunnerPojo.getDefaultNamespace();
dmnEdge.setDmnElementRef(new QName(namespaceURI, uuid, XMLConstants.DEFAULT_NS_PREFIX));
dmnEdge.addWaypoint(PointUtils.point2dToDMNDIPoint(sourcePoint));
for (ControlPoint cp : connectionContent.getControlPoints()) {
dmnEdge.addWaypoint(PointUtils.point2dToDMNDIPoint(cp.getLocation()));
}
dmnEdge.addWaypoint(PointUtils.point2dToDMNDIPoint(targetPoint));
dmnEdges.add(dmnEdge);
}
}
}
}
use of org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmndi12.JSIDMNDiagram in project kie-wb-common by kiegroup.
the class NodeConnector method connect.
void connect(final JSIDMNDiagram dmnDiagram, final List<JSIDMNEdge> edges, final List<JSITAssociation> associations, final List<NodeEntry> nodeEntries, final boolean isDMNDIPresent) {
final Map<String, List<NodeEntry>> entriesById = makeNodeIndex(nodeEntries);
final String diagramId = dmnDiagram.getId();
for (final NodeEntry nodeEntry : nodeEntries) {
final JSITDMNElement element = nodeEntry.getDmnElement();
final Node node = nodeEntry.getNode();
// For imported nodes, we don't have its connections
if (nodeEntry.isIncluded()) {
continue;
}
// DMN spec table 2: Requirements
if (JSITDecision.instanceOf(element)) {
final JSITDecision decision = Js.uncheckedCast(element);
final List<JSITInformationRequirement> jsiInformationRequirements = decision.getInformationRequirement();
for (int i = 0; i < jsiInformationRequirements.size(); i++) {
final JSITInformationRequirement ir = Js.uncheckedCast(jsiInformationRequirements.get(i));
connectEdgeToNodes(INFO_REQ_ID, ir, ir.getRequiredInput(), entriesById, diagramId, edges, isDMNDIPresent, node);
connectEdgeToNodes(INFO_REQ_ID, ir, ir.getRequiredDecision(), entriesById, diagramId, edges, isDMNDIPresent, node);
}
final List<JSITKnowledgeRequirement> jsiKnowledgeRequirements = decision.getKnowledgeRequirement();
for (int i = 0; i < jsiKnowledgeRequirements.size(); i++) {
final JSITKnowledgeRequirement kr = Js.uncheckedCast(jsiKnowledgeRequirements.get(i));
connectEdgeToNodes(KNOWLEDGE_REQ_ID, kr, kr.getRequiredKnowledge(), entriesById, diagramId, edges, isDMNDIPresent, node);
}
final List<JSITAuthorityRequirement> jsiAuthorityRequirements = decision.getAuthorityRequirement();
for (int i = 0; i < jsiAuthorityRequirements.size(); i++) {
final JSITAuthorityRequirement ar = Js.uncheckedCast(jsiAuthorityRequirements.get(i));
connectEdgeToNodes(AUTH_REQ_ID, ar, ar.getRequiredAuthority(), entriesById, diagramId, edges, isDMNDIPresent, node);
}
continue;
}
if (JSITBusinessKnowledgeModel.instanceOf(element)) {
final JSITBusinessKnowledgeModel bkm = Js.uncheckedCast(element);
final List<JSITKnowledgeRequirement> jsiKnowledgeRequirements = bkm.getKnowledgeRequirement();
for (int i = 0; i < jsiKnowledgeRequirements.size(); i++) {
final JSITKnowledgeRequirement kr = Js.uncheckedCast(jsiKnowledgeRequirements.get(i));
connectEdgeToNodes(KNOWLEDGE_REQ_ID, kr, kr.getRequiredKnowledge(), entriesById, diagramId, edges, isDMNDIPresent, node);
}
final List<JSITAuthorityRequirement> jsiAuthorityRequirements = bkm.getAuthorityRequirement();
for (int i = 0; i < jsiAuthorityRequirements.size(); i++) {
final JSITAuthorityRequirement ar = Js.uncheckedCast(jsiAuthorityRequirements.get(i));
connectEdgeToNodes(AUTH_REQ_ID, ar, ar.getRequiredAuthority(), entriesById, diagramId, edges, isDMNDIPresent, node);
}
continue;
}
if (JSITKnowledgeSource.instanceOf(element)) {
final JSITKnowledgeSource ks = Js.uncheckedCast(element);
final List<JSITAuthorityRequirement> jsiAuthorityRequirements = ks.getAuthorityRequirement();
for (int i = 0; i < jsiAuthorityRequirements.size(); i++) {
final JSITAuthorityRequirement ar = Js.uncheckedCast(jsiAuthorityRequirements.get(i));
connectEdgeToNodes(AUTH_REQ_ID, ar, ar.getRequiredInput(), entriesById, diagramId, edges, isDMNDIPresent, node);
connectEdgeToNodes(AUTH_REQ_ID, ar, ar.getRequiredDecision(), entriesById, diagramId, edges, isDMNDIPresent, node);
connectEdgeToNodes(AUTH_REQ_ID, ar, ar.getRequiredAuthority(), entriesById, diagramId, edges, isDMNDIPresent, node);
}
continue;
}
if (JSITDecisionService.instanceOf(element)) {
final JSITDecisionService ds = Js.uncheckedCast(element);
final List<JSITDMNElementReference> encapsulatedDecisions = ds.getEncapsulatedDecision();
forEach(encapsulatedDecisions, er -> {
final String reqInputID = getId(er);
getNode(nodeEntry, reqInputID, entriesById).ifPresent(requiredNode -> {
connectDSChildEdge(node, requiredNode);
});
});
final List<JSITDMNElementReference> outputDecisions = ds.getOutputDecision();
forEach(outputDecisions, er -> {
final String reqInputID = getId(er);
getNode(nodeEntry, reqInputID, entriesById).ifPresent(requiredNode -> {
connectDSChildEdge(node, requiredNode);
});
});
}
}
forEach(associations, association -> {
final String sourceId = getId(association.getSourceRef());
final String targetId = getId(association.getTargetRef());
final List<NodeEntry> source = entriesById.get(sourceId);
final List<NodeEntry> target = entriesById.get(targetId);
final boolean sourcePresent = source != null && source.size() > 0;
final boolean targetPresent = target != null && target.size() > 0;
if (sourcePresent && targetPresent) {
final NodeEntry sourceEntry = source.get(0);
final NodeEntry targetEntry = target.get(0);
final Node sourceNode = sourceEntry.getNode();
final Node targetNode = targetEntry.getNode();
@SuppressWarnings("unchecked") final Edge<View<Association>, ?> myEdge = (Edge<View<Association>, ?>) factoryManager.newElement(diagramId + "#" + association.getId(), ASSOCIATION_ID).asEdge();
final ViewConnector connectionContent = (ViewConnector) myEdge.getContent();
final Id id = new Id(association.getId());
final Description description = new Description(association.getDescription());
final Association definition = new Association(id, description);
connectEdge(myEdge, sourceNode, targetNode);
connectionContent.setDefinition(definition);
connectionContent.setTargetConnection(MagnetConnection.Builder.atCenter(targetNode));
connectionContent.setSourceConnection(MagnetConnection.Builder.atCenter(sourceNode));
}
});
}
use of org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmndi12.JSIDMNDiagram in project kie-wb-common by kiegroup.
the class NodeEntriesFactory method getEdges.
private List<JSIDMNEdge> getEdges(final JSIDMNDiagram dmnDiagram) {
final List<JSIDMNEdge> edges = new ArrayList<>();
final List<JSIDiagramElement> dmnDiagramElements = dmnDiagram.getDMNDiagramElement();
forEach(dmnDiagramElements, dmnDiagramElement -> {
if (JSIDMNEdge.instanceOf(dmnDiagramElement)) {
final JSIDMNEdge jsiEdge = Js.uncheckedCast(dmnDiagramElement);
edges.add(jsiEdge);
}
});
return edges;
}
use of org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmndi12.JSIDMNDiagram in project kie-wb-common by kiegroup.
the class DMNMarshallerTest method testConnect.
@Test
public void testConnect() {
final DMNMarshaller dmnMarshaller = new DMNMarshaller();
final JSIDMNDiagram diagram = mock(JSIDMNDiagram.class);
final List<String> dmnDiagramElementIds = mock(List.class);
final Definitions definitionsStunnerPojo = mock(Definitions.class);
final List<JSIDMNEdge> dmnEdges = new ArrayList<>();
final Node<?, ?> node = mock(Node.class);
final List inEdges = new ArrayList<>();
final Edge edge = mock(Edge.class);
final Node sourceNode = mock(Node.class);
final View sourceView = mock(View.class);
final ViewConnector viewConnector = mock(ViewConnector.class);
final DiscreteConnection sourceConnection = mock(DiscreteConnection.class);
final DiscreteConnection targetConnection = mock(DiscreteConnection.class);
final View<?> view = mock(View.class);
inEdges.add(edge);
when(edge.getSourceNode()).thenReturn(sourceNode);
when(sourceNode.getContent()).thenReturn(sourceView);
when(node.getInEdges()).thenReturn(inEdges);
when(edge.getContent()).thenReturn(viewConnector);
when(viewConnector.getControlPoints()).thenReturn(new ControlPoint[] {});
when(sourceConnection.isAuto()).thenReturn(true);
when(targetConnection.isAuto()).thenReturn(true);
when(diagram.getName()).thenReturn("dmnEdge");
when(definitionsStunnerPojo.getDefaultNamespace()).thenReturn("org.edge");
when(viewConnector.getSourceConnection()).thenReturn(Optional.of(sourceConnection));
when(viewConnector.getTargetConnection()).thenReturn(Optional.of(targetConnection));
dmnMarshaller.connect(diagram, dmnDiagramElementIds, definitionsStunnerPojo, dmnEdges, node, view);
when(viewConnector.getSourceConnection()).thenReturn(Optional.empty());
when(viewConnector.getTargetConnection()).thenReturn(Optional.empty());
dmnMarshaller.connect(diagram, dmnDiagramElementIds, definitionsStunnerPojo, dmnEdges, node, view);
when(viewConnector.getSourceConnection()).thenReturn(Optional.of(sourceConnection));
when(viewConnector.getTargetConnection()).thenReturn(Optional.empty());
dmnMarshaller.connect(diagram, dmnDiagramElementIds, definitionsStunnerPojo, dmnEdges, node, view);
verify(sourceConnection).isAuto();
verify(targetConnection).isAuto();
}
Aggregations