use of org.kie.workbench.common.dmn.api.definition.model.TextAnnotation in project kie-wb-common by kiegroup.
the class DMNMarshallerStandalone method marshall.
@Override
@SuppressWarnings("unchecked")
public String marshall(final Diagram<Graph, Metadata> diagram) {
final Graph<?, Node<View, ?>> g = diagram.getGraph();
final Map<String, org.kie.dmn.model.api.DRGElement> nodes = new HashMap<>();
final Map<String, org.kie.dmn.model.api.TextAnnotation> textAnnotations = new HashMap<>();
final Node<View<DMNDiagram>, ?> dmnDiagramRoot = (Node<View<DMNDiagram>, ?>) findDMNDiagramRoot(g);
final Definitions definitionsStunnerPojo = ((DMNDiagram) DefinitionUtils.getElementDefinition(dmnDiagramRoot)).getDefinitions();
cleanImportedItemDefinitions(definitionsStunnerPojo);
final org.kie.dmn.model.api.Definitions definitions = DefinitionsConverter.dmnFromWB(definitionsStunnerPojo);
if (definitions.getExtensionElements() == null) {
if (definitions instanceof org.kie.dmn.model.v1_1.KieDMNModelInstrumentedBase) {
definitions.setExtensionElements(new org.kie.dmn.model.v1_1.TDMNElement.TExtensionElements());
} else if (definitions instanceof org.kie.dmn.model.v1_2.KieDMNModelInstrumentedBase) {
definitions.setExtensionElements(new org.kie.dmn.model.v1_2.TDMNElement.TExtensionElements());
} else {
definitions.setExtensionElements(new org.kie.dmn.model.v1_2.TDMNElement.TExtensionElements());
}
}
if (definitions.getDMNDI() == null) {
definitions.setDMNDI(new DMNDI());
}
final org.kie.dmn.model.api.dmndi.DMNDiagram dmnDDDMNDiagram = new org.kie.dmn.model.v1_2.dmndi.DMNDiagram();
definitions.getDMNDI().getDMNDiagram().add(dmnDDDMNDiagram);
final List<DMNEdge> dmnEdges = new ArrayList<>();
// Convert relative positioning to absolute
for (Node<?, ?> node : g.nodes()) {
PointUtils.convertToAbsoluteBounds(node);
}
// Setup callback for marshalling ComponentWidths
if (dmnDDDMNDiagram.getExtension() == null) {
dmnDDDMNDiagram.setExtension(new DiagramElement.Extension());
}
final ComponentsWidthsExtension componentsWidthsExtension = new ComponentsWidthsExtension();
dmnDDDMNDiagram.getExtension().getAny().add(componentsWidthsExtension);
final Consumer<ComponentWidths> componentWidthsConsumer = cw -> componentsWidthsExtension.getComponentsWidths().add(cw);
// Iterate Graph processing nodes..
for (Node<?, ?> node : g.nodes()) {
if (node.getContent() instanceof View<?>) {
final View<?> view = (View<?>) node.getContent();
if (view.getDefinition() instanceof DRGElement) {
final DRGElement drgElement = (org.kie.workbench.common.dmn.api.definition.model.DRGElement) view.getDefinition();
if (!drgElement.isAllowOnlyVisualChange()) {
nodes.put(drgElement.getId().getValue(), stunnerToDMN(node, componentWidthsConsumer));
}
dmnDDDMNDiagram.getDMNDiagramElement().add(stunnerToDDExt(definitionsStunnerPojo, (View<? extends DMNElement>) view));
} else if (view.getDefinition() instanceof TextAnnotation) {
final TextAnnotation textAnnotation = (TextAnnotation) view.getDefinition();
textAnnotations.put(textAnnotation.getId().getValue(), textAnnotationConverter.dmnFromNode((Node<View<TextAnnotation>, ?>) node, componentWidthsConsumer));
dmnDDDMNDiagram.getDMNDiagramElement().add(stunnerToDDExt(definitionsStunnerPojo, (View<? extends DMNElement>) view));
final List<org.kie.dmn.model.api.Association> associations = AssociationConverter.dmnFromWB((Node<View<TextAnnotation>, ?>) node);
definitions.getArtifact().addAll(associations);
}
// 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) {
Point2D sourcePoint = sourceConnection.getLocation();
Point2D targetPoint = targetConnection.getLocation();
final Node<?, ?> sourceNode = e.getSourceNode();
final View<?> sourceView = (View<?>) sourceNode.getContent();
double xSource = xOfBound(upperLeftBound(sourceView));
double ySource = yOfBound(upperLeftBound(sourceView));
double xTarget = xOfBound(upperLeftBound(view));
double yTarget = yOfBound(upperLeftBound(view));
if (sourcePoint == null) {
// 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());
}
if (targetPoint == null) {
// 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 DMNEdge dmnEdge = new org.kie.dmn.model.v1_2.dmndi.DMNEdge();
// DMNDI edge elementRef is uuid of Stunner edge,
// with the only exception when edge contains as content a DMN Association (Association is an edge)
String uuid = e.getUUID();
if (e.getContent() instanceof View<?>) {
final View<?> edgeView = (View<?>) e.getContent();
if (edgeView.getDefinition() instanceof Association) {
uuid = ((Association) edgeView.getDefinition()).getId().getValue();
}
}
String autoConnectionId = "";
if (sourceConnection.isAuto()) {
autoConnectionId += AUTO_SOURCE_CONNECTION;
}
if (targetConnection.isAuto()) {
autoConnectionId += AUTO_TARGET_CONNECTION;
}
dmnEdge.setId("dmnedge-" + uuid + autoConnectionId);
dmnEdge.setDmnElementRef(new QName(uuid));
dmnEdge.getWaypoint().add(PointUtils.point2dToDMNDIPoint(sourcePoint));
for (ControlPoint cp : connectionContent.getControlPoints()) {
dmnEdge.getWaypoint().add(PointUtils.point2dToDMNDIPoint(cp.getLocation()));
}
dmnEdge.getWaypoint().add(PointUtils.point2dToDMNDIPoint(targetPoint));
dmnEdges.add(dmnEdge);
}
}
}
}
}
nodes.values().forEach(n -> {
n.setParent(definitions);
definitions.getDrgElement().add(n);
});
textAnnotations.values().forEach(definitions.getArtifact()::add);
// add DMNEdge last.
dmnDDDMNDiagram.getDMNDiagramElement().addAll(dmnEdges);
return marshaller.marshal(definitions);
}
use of org.kie.workbench.common.dmn.api.definition.model.TextAnnotation in project kie-wb-common by kiegroup.
the class DMNMarshallerStandalone method stunnerToDDExt.
@SuppressWarnings("unchecked")
private static DMNShape stunnerToDDExt(final Definitions definitions, final View<? extends DMNElement> v) {
final DMNShape result = new org.kie.dmn.model.v1_2.dmndi.DMNShape();
result.setId("dmnshape-" + v.getDefinition().getId().getValue());
result.setDmnElementRef(getDmnElementRef(definitions, v));
final Bounds bounds = new org.kie.dmn.model.v1_2.dmndi.Bounds();
result.setBounds(bounds);
bounds.setX(xOfBound(upperLeftBound(v)));
bounds.setY(yOfBound(upperLeftBound(v)));
result.setStyle(new org.kie.dmn.model.v1_2.dmndi.DMNStyle());
result.setDMNLabel(new org.kie.dmn.model.v1_2.dmndi.DMNLabel());
if (v.getDefinition() instanceof Decision) {
final Decision d = (Decision) v.getDefinition();
applyBounds(d.getDimensionsSet(), bounds);
applyBackgroundStyles(d.getStylingSet(), result);
applyFontStyle(d.getStylingSet(), result);
} else if (v.getDefinition() instanceof InputData) {
final InputData d = (InputData) v.getDefinition();
applyBounds(d.getDimensionsSet(), bounds);
applyBackgroundStyles(d.getStylingSet(), result);
applyFontStyle(d.getStylingSet(), result);
} else if (v.getDefinition() instanceof BusinessKnowledgeModel) {
final BusinessKnowledgeModel d = (BusinessKnowledgeModel) v.getDefinition();
applyBounds(d.getDimensionsSet(), bounds);
applyBackgroundStyles(d.getStylingSet(), result);
applyFontStyle(d.getStylingSet(), result);
} else if (v.getDefinition() instanceof KnowledgeSource) {
final KnowledgeSource d = (KnowledgeSource) v.getDefinition();
applyBounds(d.getDimensionsSet(), bounds);
applyBackgroundStyles(d.getStylingSet(), result);
applyFontStyle(d.getStylingSet(), result);
} else if (v.getDefinition() instanceof TextAnnotation) {
final TextAnnotation d = (TextAnnotation) v.getDefinition();
applyBounds(d.getDimensionsSet(), bounds);
applyBackgroundStyles(d.getStylingSet(), result);
applyFontStyle(d.getStylingSet(), result);
} else if (v.getDefinition() instanceof DecisionService) {
final DecisionService d = (DecisionService) v.getDefinition();
applyBounds(d.getDimensionsSet(), bounds);
applyBackgroundStyles(d.getStylingSet(), result);
applyFontStyle(d.getStylingSet(), result);
final DMNDecisionServiceDividerLine dl = new org.kie.dmn.model.v1_2.dmndi.DMNDecisionServiceDividerLine();
final org.kie.dmn.model.api.dmndi.Point leftPoint = new org.kie.dmn.model.v1_2.dmndi.Point();
leftPoint.setX(v.getBounds().getUpperLeft().getX());
final double dlY = v.getBounds().getUpperLeft().getY() + d.getDividerLineY().getValue();
leftPoint.setY(dlY);
dl.getWaypoint().add(leftPoint);
final org.kie.dmn.model.api.dmndi.Point rightPoint = new org.kie.dmn.model.v1_2.dmndi.Point();
rightPoint.setX(v.getBounds().getLowerRight().getX());
rightPoint.setY(dlY);
dl.getWaypoint().add(rightPoint);
result.setDMNDecisionServiceDividerLine(dl);
}
return result;
}
use of org.kie.workbench.common.dmn.api.definition.model.TextAnnotation in project kie-wb-common by kiegroup.
the class AssociationConverter method dmnFromWB.
public static List<org.kie.dmn.model.api.Association> dmnFromWB(final Node<View<TextAnnotation>, ?> node) {
final TextAnnotation ta = (TextAnnotation) DefinitionUtils.getElementDefinition(node);
final org.kie.dmn.model.api.DMNElementReference ta_elementReference = new org.kie.dmn.model.v1_2.TDMNElementReference();
ta_elementReference.setHref(new StringBuilder("#").append(ta.getId().getValue()).toString());
final List<org.kie.dmn.model.api.Association> result = new ArrayList<>();
final List<Edge<?, ?>> inEdges = (List<Edge<?, ?>>) node.getInEdges();
for (Edge<?, ?> e : inEdges) {
Node<?, ?> sourceNode = e.getSourceNode();
if (sourceNode.getContent() instanceof View<?>) {
final View<?> view = (View<?>) sourceNode.getContent();
if (view.getDefinition() instanceof DRGElement) {
final DRGElement drgElement = (DRGElement) view.getDefinition();
final org.kie.dmn.model.api.DMNElementReference sourceRef = new org.kie.dmn.model.v1_2.TDMNElementReference();
sourceRef.setHref(getHref(drgElement));
final org.kie.dmn.model.api.Association adding = new org.kie.dmn.model.v1_2.TAssociation();
adding.setId(((View<Association>) e.getContent()).getDefinition().getId().getValue());
adding.setDescription(DescriptionPropertyConverter.dmnFromWB(((View<Association>) e.getContent()).getDefinition().getDescription()));
adding.setSourceRef(sourceRef);
adding.setTargetRef(ta_elementReference);
result.add(adding);
}
}
}
final List<Edge<?, ?>> outEdges = (List<Edge<?, ?>>) node.getOutEdges();
for (Edge<?, ?> e : outEdges) {
final Node<?, ?> targetNode = e.getTargetNode();
if (targetNode.getContent() instanceof View<?>) {
final View<?> view = (View<?>) targetNode.getContent();
if (view.getDefinition() instanceof DRGElement) {
final DRGElement drgElement = (DRGElement) view.getDefinition();
final org.kie.dmn.model.api.DMNElementReference targetRef = new org.kie.dmn.model.v1_2.TDMNElementReference();
targetRef.setHref(getHref(drgElement));
final org.kie.dmn.model.api.Association adding = new org.kie.dmn.model.v1_2.TAssociation();
adding.setId(((View<Association>) e.getContent()).getDefinition().getId().getValue());
adding.setDescription(DescriptionPropertyConverter.dmnFromWB(((View<Association>) e.getContent()).getDefinition().getDescription()));
adding.setSourceRef(ta_elementReference);
adding.setTargetRef(targetRef);
result.add(adding);
}
}
}
return result;
}
use of org.kie.workbench.common.dmn.api.definition.model.TextAnnotation in project kie-wb-common by kiegroup.
the class TextAnnotationConverter method nodeFromDMN.
@Override
public Node<View<TextAnnotation>, ?> nodeFromDMN(final NodeEntry nodeEntry) {
final JSITTextAnnotation dmn = Js.uncheckedCast(nodeEntry.getDmnElement());
@SuppressWarnings("unchecked") final Node<View<TextAnnotation>, ?> node = (Node<View<TextAnnotation>, ?>) factoryManager.newElement(nodeEntry.getId(), getDefinitionId(TextAnnotation.class)).asNode();
final Id id = IdPropertyConverter.wbFromDMN(dmn.getId());
final Description description = DescriptionPropertyConverter.wbFromDMN(dmn.getDescription());
final Text text = new Text(dmn.getText());
final TextFormat textFormat = new TextFormat(dmn.getTextFormat());
final TextAnnotation textAnnotation = new TextAnnotation(id, description, text, textFormat, new StylingSet(), new GeneralRectangleDimensionsSet());
textAnnotation.setDiagramId(nodeEntry.getDiagramId());
node.getContent().setDefinition(textAnnotation);
return node;
}
use of org.kie.workbench.common.dmn.api.definition.model.TextAnnotation in project kie-wb-common by kiegroup.
the class DRGElementTextPropertyProviderImplTest method checkSupportsDRGElement.
@Test
@SuppressWarnings("unchecked")
public void checkSupportsDRGElement() {
assertTrue(provider.supports(element));
final Element other = mock(Element.class);
final Definition otherContent = mock(Definition.class);
final TextAnnotation otherDefinition = mock(TextAnnotation.class);
when(other.getContent()).thenReturn(otherContent);
when(otherContent.getDefinition()).thenReturn(otherDefinition);
assertFalse(provider.supports(other));
}
Aggregations