Skip to content

Commit

Permalink
[269] Handle start action in AFV
Browse files Browse the repository at this point in the history
Bug: eclipse-syson#269
Signed-off-by: Jerome Gout <jerome.gout@obeosoft.com>
  • Loading branch information
jerome-obeo committed May 29, 2024
1 parent b7c5684 commit 66ed5d4
Show file tree
Hide file tree
Showing 22 changed files with 577 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,22 +204,22 @@ protected void createView(Element element, IEditingContext editingContext, IDiag

protected void createView(Element element, IEditingContext editingContext, IDiagramContext diagramContext, Object selectedNode,
Map<org.eclipse.sirius.components.view.diagram.NodeDescription, NodeDescription> convertedNodes, NodeContainmentKind nodeKind) {
var parentElementId = this.getParentElementId(element, diagramContext, selectedNode, convertedNodes);
var descriptionId = this.getDescriptionId(element, editingContext, diagramContext, selectedNode, convertedNodes);

if (descriptionId.isPresent()) {
var request = ViewCreationRequest.newViewCreationRequest()
.containmentKind(nodeKind)
.descriptionId(descriptionId.get())
.parentElementId(parentElementId)
.targetObjectId(this.objectService.getId(element))
.build();
diagramContext.getViewCreationRequests().add(request);
}
var parentElementId = this.getParentElementId(selectedNode, diagramContext);
this.getDescriptionId(element, editingContext, diagramContext, selectedNode, convertedNodes)
.ifPresent(descriptionId -> this.createView(element, parentElementId, descriptionId, diagramContext, nodeKind));
}

protected String getParentElementId(Element element, IDiagramContext diagramContext, Object selectedNode,
Map<org.eclipse.sirius.components.view.diagram.NodeDescription, NodeDescription> convertedNodes) {
protected void createView(Element element, String parentElementId, String descriptionId, IDiagramContext diagramContext, NodeContainmentKind nodeKind) {
var request = ViewCreationRequest.newViewCreationRequest()
.containmentKind(nodeKind)
.descriptionId(descriptionId)
.parentElementId(parentElementId)
.targetObjectId(this.objectService.getId(element))
.build();
diagramContext.getViewCreationRequests().add(request);
}

protected String getParentElementId(Object selectedNode, IDiagramContext diagramContext) {
if (selectedNode instanceof Node node) {
return node.getId();
}
Expand Down Expand Up @@ -260,7 +260,7 @@ protected void moveElement(Element droppedElement, Node droppedNode, Element tar
} else {
return;
}
this.createView(droppedElement, editingContext, diagramContext, targetNode, convertedNodes, NodeContainmentKind.CHILD_NODE);
this.createView(droppedElement, editingContext, diagramContext, targetNode, convertedNodes);
diagramContext.getViewDeletionRequests().add(ViewDeletionRequest.newViewDeletionRequest().elementId(droppedNode.getId()).build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,21 @@
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.syson.sysml.AcceptActionUsage;
import org.eclipse.syson.sysml.ActionUsage;
import org.eclipse.syson.sysml.Definition;
import org.eclipse.syson.sysml.Element;
import org.eclipse.syson.sysml.Membership;
import org.eclipse.syson.sysml.Namespace;
import org.eclipse.syson.sysml.Package;
import org.eclipse.syson.sysml.PartUsage;
import org.eclipse.syson.sysml.PortUsage;
import org.eclipse.syson.sysml.Relationship;
import org.eclipse.syson.sysml.TransitionFeatureKind;
import org.eclipse.syson.sysml.TransitionFeatureMembership;
import org.eclipse.syson.sysml.TransitionUsage;
import org.eclipse.syson.sysml.Type;
import org.eclipse.syson.sysml.Usage;
import org.eclipse.syson.sysml.helper.NameHelper;
import org.eclipse.syson.util.SysMLMetamodelHelper;
import org.eclipse.syson.util.SysONEContentAdapter;

Expand Down Expand Up @@ -142,6 +146,33 @@ public List<EObject> getAllReachable(EObject eObject, EClass eClass) {
return allReachable;
}

/**
* Find an {@link Element} that match the given name in the ResourceSet of the given element.
*
* @param object
* the object for which to find a corresponding type.
* @param elementName
* the element name to match.
* @return the found element or <code>null</code>.
*/
@SuppressWarnings("unchecked")
public <T extends Element> T findByName(EObject object, String elementName) {
T result = null;
Namespace namespace = null;
if (object instanceof Element element) {
namespace = element.getOwningNamespace();
} else if (object instanceof Relationship relationship && relationship.getOwner() != null) {
namespace = relationship.getOwner().getOwningNamespace();
}
if (namespace != null) {
var membership = namespace.resolve(elementName);
if (membership != null) {
result = (T) membership.getMemberElement();
}
}
return result;
}

/**
* Find an {@link Element} that match the given name and type in the ResourceSet of the given element.
*
Expand Down Expand Up @@ -320,12 +351,8 @@ private boolean nameMatches(Element element, String name) {
* @return <code>true</code> if the given element name is a qualified name, <code>false</code> otherwise.
*/
public boolean isQualifiedName(String elementName) {
boolean isQualifiedName = false;
if (elementName != null) {
String[] splitElementName = elementName.split("::");
isQualifiedName = splitElementName.length > 1;
}
return isQualifiedName;
List<String> segments = NameHelper.parseQualifiedName(elementName);
return segments.size() > 1;
}

/**
Expand All @@ -344,4 +371,34 @@ public void removeTransitionFeaturesOfSpecificKind(TransitionUsage transition, T
.toList();
EcoreUtil.removeAll(elementsToDelete);
}

/**
* Retrieve the start action defined inside the standard library <code>Actions</code>.
*
* @param eObject
* an object to access to the library resources.
*
* @return the standard start ActionUsage defined in the <code>Actions</code> library.
*/
public ActionUsage retrieveStandardStartAction(Element eObject) {
return this.findByName(eObject, "Actions::Action::start");
}

/**
* Check if the given element is actually the standard start action defined by <code>Actions::Action::start</code>.
*
* @param element
* an element that could be the standard start action.
* @return <code>true</code> if the given element is the standard start action and <code>false</code> otherwise.
*/
public boolean isStandardStartAction(Element element) {
var elt = element;
if (element instanceof Membership membership) {
elt = membership.getMemberElement();
}
if (elt instanceof ActionUsage au) {
return "9a0d2905-0f9c-5bb4-af74-9780d6db1817".equals(au.getElementId());
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@
*/
public class AQLUtils {

/**
* Return an AQL string from the given string.
*
* @param string
* the string to transform to an AQL string
* @return the AQL string built upon the given string.
*/
public static String aqlString(String string) {
return '\'' + string + '\'';
}

/**
* Returns the AQL expression for calling a service without any parameter using <code>self</code> as the
* instance.<br>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.eclipse.syson.diagram.common.view.diagram.AbstractDiagramDescriptionProvider;
import org.eclipse.syson.diagram.common.view.nodes.ActionFlowCompartmentNodeDescriptionProvider;
import org.eclipse.syson.diagram.common.view.nodes.CompartmentItemNodeDescriptionProvider;
import org.eclipse.syson.diagram.common.view.nodes.StartActionNodeDescriptionProvider;
import org.eclipse.syson.diagram.common.view.tools.ToolSectionDescription;
import org.eclipse.syson.sysml.SysmlPackage;
import org.eclipse.syson.util.IDescriptionNameGenerator;
Expand Down Expand Up @@ -116,6 +117,7 @@ public RepresentationDescription create(IColorProvider colorProvider) {
diagramElementDescriptionProviders.add(new FakeNodeDescriptionProvider(colorProvider));
diagramElementDescriptionProviders.add(new ActionFlowViewEmptyDiagramNodeDescriptionProvider(colorProvider));
diagramElementDescriptionProviders.add(new PackageNodeDescriptionProvider(colorProvider));
diagramElementDescriptionProviders.add(new StartActionNodeDescriptionProvider(colorProvider, this.getNameGenerator()));

DEFINITIONS.forEach(definition -> {
diagramElementDescriptionProviders.add(new DefinitionNodeDescriptionProvider(definition, colorProvider));
Expand Down Expand Up @@ -173,7 +175,7 @@ private NodeTool[] createElementsOfToolSection(IViewDiagramElementFinder cache,
var nodeTools = new ArrayList<NodeTool>();

elements.forEach(definition -> {
nodeTools.add(this.createNodeToolFromDiagramBackground(cache.getNodeDescription(this.descriptionNameGenerator.getNodeName(definition)).get(), definition));
nodeTools.add(this.createNodeToolFromDiagramBackground(cache.getNodeDescription(this.getNameGenerator().getNodeName(definition)).get(), definition));
});

nodeTools.sort((nt1, nt2) -> nt1.getName().compareTo(nt2.getName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.eclipse.syson.diagram.actionflow.view.AFVDescriptionNameGenerator;
import org.eclipse.syson.diagram.actionflow.view.ActionFlowViewDiagramDescriptionProvider;
import org.eclipse.syson.diagram.common.view.edges.AbstractSuccessionEdgeDescriptionProvider;
import org.eclipse.syson.diagram.common.view.nodes.StartActionNodeDescriptionProvider;

/**
* Used to create a Succession Edge provider in Action Flow View diagram.
Expand All @@ -33,7 +34,7 @@ public SuccessionEdgeDescriptionProvider(IColorProvider colorProvider) {
super(colorProvider, new AFVDescriptionNameGenerator());
}

private List<NodeDescription> getSourceAndTargetNodes(IViewDiagramElementFinder cache) {
private List<NodeDescription> getAllUsages(IViewDiagramElementFinder cache) {
var sourcesAndTargets = new ArrayList<NodeDescription>();

ActionFlowViewDiagramDescriptionProvider.USAGES.forEach(usage -> {
Expand All @@ -45,11 +46,14 @@ private List<NodeDescription> getSourceAndTargetNodes(IViewDiagramElementFinder

@Override
protected List<NodeDescription> getSourceNodes(IViewDiagramElementFinder cache) {
return this.getSourceAndTargetNodes(cache);
var sources = this.getAllUsages(cache);
// the start node can be the source of a succession
cache.getNodeDescription(this.nameGenerator.getNodeName(StartActionNodeDescriptionProvider.START_ACTION_NAME)).ifPresent(sources::add);
return sources;
}

@Override
protected List<NodeDescription> getTargetNodes(IViewDiagramElementFinder cache) {
return this.getSourceAndTargetNodes(cache);
return this.getAllUsages(cache);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected List<NodeDescription> getAllNodeDescriptions(IViewDiagramElementFinder

@Override
protected List<NodeToolSection> getToolSections(NodeDescription nodeDescription, IViewDiagramElementFinder cache) {
ActionFlowViewNodeToolSectionSwitch toolSectionSwitch = new ActionFlowViewNodeToolSectionSwitch(this.getAllNodeDescriptions(cache));
ActionFlowViewNodeToolSectionSwitch toolSectionSwitch = new ActionFlowViewNodeToolSectionSwitch(cache, this.getAllNodeDescriptions(cache));
return toolSectionSwitch.doSwitch(this.eClass);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected List<NodeDescription> getAllNodeDescriptions(IViewDiagramElementFinder

@Override
protected List<NodeToolSection> getToolSections(NodeDescription nodeDescription, IViewDiagramElementFinder cache) {
ActionFlowViewNodeToolSectionSwitch toolSectionSwitch = new ActionFlowViewNodeToolSectionSwitch(this.getAllNodeDescriptions(cache));
ActionFlowViewNodeToolSectionSwitch toolSectionSwitch = new ActionFlowViewNodeToolSectionSwitch(cache, this.getAllNodeDescriptions(cache));
return toolSectionSwitch.doSwitch(this.eClass);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.sirius.components.view.builder.IViewDiagramElementFinder;
import org.eclipse.sirius.components.view.diagram.NodeDescription;
import org.eclipse.sirius.components.view.diagram.NodeTool;
import org.eclipse.sirius.components.view.diagram.NodeToolSection;
Expand All @@ -27,6 +28,7 @@
import org.eclipse.syson.diagram.common.view.tools.AcceptActionPortUsageReceiverToolNodeProvider;
import org.eclipse.syson.diagram.common.view.tools.ActionFlowCompartmentNodeToolProvider;
import org.eclipse.syson.diagram.common.view.tools.CompartmentNodeToolProvider;
import org.eclipse.syson.diagram.common.view.tools.StartActionNodeToolProvider;
import org.eclipse.syson.sysml.AcceptActionUsage;
import org.eclipse.syson.sysml.ActionDefinition;
import org.eclipse.syson.sysml.ActionUsage;
Expand All @@ -44,8 +46,11 @@ public class ActionFlowViewNodeToolSectionSwitch extends AbstractViewNodeToolSec

private final List<NodeDescription> allNodeDescriptions;

public ActionFlowViewNodeToolSectionSwitch(List<NodeDescription> allNodeDescriptions) {
private final IViewDiagramElementFinder cache;

public ActionFlowViewNodeToolSectionSwitch(IViewDiagramElementFinder cache, List<NodeDescription> allNodeDescriptions) {
super(new AFVDescriptionNameGenerator());
this.cache = Objects.requireNonNull(cache);
this.allNodeDescriptions = Objects.requireNonNull(allNodeDescriptions);
}

Expand Down Expand Up @@ -75,16 +80,16 @@ public List<NodeToolSection> caseAcceptActionUsage(AcceptActionUsage object) {

@Override
public List<NodeToolSection> caseActionUsage(ActionUsage object) {
var createSection = this.buildCreateSection();
createSection.getNodeTools().add(new CompartmentNodeToolProvider(SysmlPackage.eINSTANCE.getUsage_NestedItem(), this.descriptionNameGenerator).create(null));
createSection.getNodeTools().add(new ActionFlowCompartmentNodeToolProvider(SysmlPackage.eINSTANCE.getActionUsage(), this.descriptionNameGenerator).create(null));
var createSection = this.buildCreateSection(new StartActionNodeToolProvider(SysmlPackage.eINSTANCE.getActionUsage(), this.descriptionNameGenerator).create(this.cache));
createSection.getNodeTools().add(new CompartmentNodeToolProvider(SysmlPackage.eINSTANCE.getUsage_NestedItem(), this.descriptionNameGenerator).create(this.cache));
createSection.getNodeTools().add(new ActionFlowCompartmentNodeToolProvider(SysmlPackage.eINSTANCE.getActionUsage(), this.descriptionNameGenerator).create(this.cache));
return List.of(createSection, this.addElementsToolSection());
}

@Override
public List<NodeToolSection> caseActionDefinition(ActionDefinition object) {
var createSection = this.buildCreateSection();
createSection.getNodeTools().add(new ActionFlowCompartmentNodeToolProvider(SysmlPackage.eINSTANCE.getActionDefinition(), this.descriptionNameGenerator).create(null));
var createSection = this.buildCreateSection(new StartActionNodeToolProvider(SysmlPackage.eINSTANCE.getActionDefinition(), this.descriptionNameGenerator).create(this.cache));
createSection.getNodeTools().add(new ActionFlowCompartmentNodeToolProvider(SysmlPackage.eINSTANCE.getActionDefinition(), this.descriptionNameGenerator).create(this.cache));
return List.of(createSection, this.addElementsToolSection());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
import java.util.Objects;

import org.eclipse.emf.common.util.EList;
import org.eclipse.sirius.components.view.UserColor;
import org.eclipse.sirius.components.view.builder.providers.IColorProvider;
import org.eclipse.sirius.components.view.builder.providers.INodeDescriptionProvider;
import org.eclipse.sirius.components.view.diagram.ImageNodeStyleDescription;
import org.eclipse.sirius.components.view.diagram.NodeTool;
import org.eclipse.sirius.components.view.diagram.NodeToolSection;
import org.eclipse.sirius.components.view.diagram.provider.DefaultToolsFactory;
Expand Down Expand Up @@ -56,4 +58,17 @@ protected void orderToolSectionsTools(List<NodeToolSection> toolSections) {
toolSection.getNodeTools().addAll(sortedNodeTools);
});
}

protected ImageNodeStyleDescription createImageNodeStyleDescription(String shapeId) {
return this.createImageNodeStyleDescription(shapeId, this.colorProvider.getColor("transparent"));
}

protected ImageNodeStyleDescription createImageNodeStyleDescription(String imagePath, UserColor borderColor) {
return this.diagramBuilderHelper.newImageNodeStyleDescription()
.borderColor(borderColor)
.borderRadius(0)
.positionDependentRotation(true)
.shape(imagePath)
.build();
}
}
Loading

0 comments on commit 66ed5d4

Please sign in to comment.