use of org.kie.workbench.common.dmn.api.definition.model.DMNElement in project kie-wb-common by kiegroup.
the class WrapperUtils method getImportPrefix.
private static Optional<String> getImportPrefix(final Definitions definitions, final DMNElement dmnElement) {
if (!(dmnElement instanceof NamedElement)) {
return Optional.empty();
}
final NamedElement namedElement = (NamedElement) dmnElement;
final Optional<String> name = Optional.ofNullable(namedElement.getName().getValue());
return definitions.getImport().stream().filter(anImport -> {
final String importName = anImport.getName().getValue();
return name.map(n -> n.startsWith(importName + ".")).orElse(false);
}).map(anImport -> {
final String importNamespace = anImport.getNamespace();
return getNsContextsByNamespace(definitions, importNamespace);
}).findFirst();
}
use of org.kie.workbench.common.dmn.api.definition.model.DMNElement in project kie-wb-common by kiegroup.
the class WrapperUtils method getDmnElementRef.
static QName getDmnElementRef(final Definitions definitions, final View<? extends DMNElement> v, final String namespaceURI) {
final DMNElement dmnElement = v.getDefinition();
final String dmnElementId = dmnElement.getId().getValue();
return getImportPrefix(definitions, dmnElement).map(prefix -> new QName(namespaceURI, prefix + ":" + dmnElementId, XMLConstants.DEFAULT_NS_PREFIX)).orElse(new QName(namespaceURI, dmnElementId, XMLConstants.DEFAULT_NS_PREFIX));
}
use of org.kie.workbench.common.dmn.api.definition.model.DMNElement in project kie-wb-common by kiegroup.
the class ObserverBuilderControlTest method testUpdateIdFromDefinition.
@Test
public void testUpdateIdFromDefinition() {
final String expectedId = "happyId";
final Element element = mock(Element.class);
final View elementContent = mock(View.class);
final DMNElement newDefinition = mock(DMNElement.class);
final Id newDefinitionId = mock(Id.class);
final DMNElement definition = mock(DMNElement.class);
final Id definitionId = mock(Id.class);
when(element.getContent()).thenReturn(elementContent);
when(elementContent.getDefinition()).thenReturn(newDefinition);
when(newDefinition.getId()).thenReturn(newDefinitionId);
when(definition.getId()).thenReturn(definitionId);
when(definitionId.getValue()).thenReturn(expectedId);
observerBuilderControl.updateElementFromDefinition(element, definition);
verify(newDefinitionId).setValue(expectedId);
}
use of org.kie.workbench.common.dmn.api.definition.model.DMNElement in project kie-wb-common by kiegroup.
the class ObserverBuilderControl method updateElementFromDefinition.
@Override
@SuppressWarnings("unchecked")
protected void updateElementFromDefinition(final Element element, final Object definition) {
final Object content = element.getContent();
if (!(content instanceof View)) {
return;
}
final Object newDefinition = ((View) content).getDefinition();
if (newDefinition instanceof HasName && definition instanceof HasName) {
((HasName) newDefinition).getName().setValue(((HasName) definition).getName().getValue());
}
if (newDefinition instanceof DynamicReadOnly && definition instanceof DynamicReadOnly) {
((DynamicReadOnly) newDefinition).setAllowOnlyVisualChange(((DynamicReadOnly) definition).isAllowOnlyVisualChange());
}
if (newDefinition instanceof HasVariable && definition instanceof HasVariable) {
((HasVariable) newDefinition).setVariable(((HasVariable) definition).getVariable());
}
if (newDefinition instanceof BusinessKnowledgeModel && definition instanceof BusinessKnowledgeModel) {
((BusinessKnowledgeModel) newDefinition).setEncapsulatedLogic(((BusinessKnowledgeModel) definition).getEncapsulatedLogic());
}
if (newDefinition instanceof HasExpression && definition instanceof HasExpression) {
((HasExpression) newDefinition).setExpression(((HasExpression) definition).getExpression());
}
if (newDefinition instanceof DMNElement && definition instanceof DMNElement) {
final DMNElement dmnElement = (DMNElement) definition;
if (!StringUtils.isEmpty(dmnElement.getId().getValue())) {
((DMNElement) newDefinition).getId().setValue(dmnElement.getId().getValue());
}
}
final Optional<DMNDiagramElement> currentDMNDiagramElement = getDMNDiagramsSession().getCurrentDMNDiagramElement();
if (currentDMNDiagramElement.isPresent() && newDefinition instanceof HasContentDefinitionId) {
((HasContentDefinitionId) newDefinition).setDiagramId(currentDMNDiagramElement.get().getId().getValue());
}
}
use of org.kie.workbench.common.dmn.api.definition.model.DMNElement 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;
}
Aggregations