From 716f9ecf19ad9241e548f16ab6ddc0ff944062b9 Mon Sep 17 00:00:00 2001 From: Frederic Bats Date: Fri, 23 Mar 2018 14:08:42 +0100 Subject: [PATCH] [REA] #111 SysML Activity diagram Implement tools to apply/unapply the following SysMl stereotypes: - Optional (on ActivityParameterNode) - Continuous/Discrete (on ActivityParameter node, ActivityEdge) - Probability (on ActivityEdge) - ControlOperator (on activity, CallBehaviorAction, CallOperationAction) - NoBuffer/Overwrite (ObjectNodes) --- .../description/sysml.odesign | 181 ++++++++- .../plugin.xml | 6 + .../api/services/ActivityDiagramServices.java | 374 ++++++++++++++++++ .../BlockDefinitionDiagramServices.java | 12 - .../services/ReusedDescriptionServices.java | 9 +- .../SysmlAbstractDiagramServices.java | 48 ++- .../listeners/ActivityEdgeListener.java | 80 ++++ .../SysMLSessionManagerListener.java | 46 +++ .../internal/services/DisplayLabelSwitch.java | 153 +++++++ .../internal/services/LabelServices.java | 14 +- .../services/SysmlElementServices.java | 18 + 11 files changed, 919 insertions(+), 22 deletions(-) create mode 100644 plugins/org.obeonetwork.dsl.sysml.design/src/org/obeonetwork/dsl/sysml/design/api/services/ActivityDiagramServices.java create mode 100644 plugins/org.obeonetwork.dsl.sysml.design/src/org/obeonetwork/dsl/sysml/design/internal/listeners/ActivityEdgeListener.java create mode 100644 plugins/org.obeonetwork.dsl.sysml.design/src/org/obeonetwork/dsl/sysml/design/internal/listeners/SysMLSessionManagerListener.java create mode 100644 plugins/org.obeonetwork.dsl.sysml.design/src/org/obeonetwork/dsl/sysml/design/internal/services/DisplayLabelSwitch.java diff --git a/plugins/org.obeonetwork.dsl.sysml.design/description/sysml.odesign b/plugins/org.obeonetwork.dsl.sysml.design/description/sysml.odesign index 23a1b5c..b1f038f 100644 --- a/plugins/org.obeonetwork.dsl.sysml.design/description/sysml.odesign +++ b/plugins/org.obeonetwork.dsl.sysml.design/description/sysml.odesign @@ -2016,7 +2016,185 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -2031,6 +2209,7 @@ + diff --git a/plugins/org.obeonetwork.dsl.sysml.design/plugin.xml b/plugins/org.obeonetwork.dsl.sysml.design/plugin.xml index 24abe7c..31a845e 100644 --- a/plugins/org.obeonetwork.dsl.sysml.design/plugin.xml +++ b/plugins/org.obeonetwork.dsl.sysml.design/plugin.xml @@ -71,4 +71,10 @@ Contributors: top="/org.obeonetwork.dsl.sysml.design/images/InputPortUp.svg"> + + + + diff --git a/plugins/org.obeonetwork.dsl.sysml.design/src/org/obeonetwork/dsl/sysml/design/api/services/ActivityDiagramServices.java b/plugins/org.obeonetwork.dsl.sysml.design/src/org/obeonetwork/dsl/sysml/design/api/services/ActivityDiagramServices.java new file mode 100644 index 0000000..69504c9 --- /dev/null +++ b/plugins/org.obeonetwork.dsl.sysml.design/src/org/obeonetwork/dsl/sysml/design/api/services/ActivityDiagramServices.java @@ -0,0 +1,374 @@ +/******************************************************************************* + * Copyright (c) 2018 Obeo. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.obeonetwork.dsl.sysml.design.api.services; + +import org.eclipse.emf.common.util.EList; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.papyrus.sysml14.activities.Probability; +import org.eclipse.papyrus.sysml14.activities.Rate; +import org.eclipse.uml2.uml.ActivityEdge; +import org.eclipse.uml2.uml.ActivityNode; +import org.eclipse.uml2.uml.ActivityParameterNode; +import org.eclipse.uml2.uml.DecisionNode; +import org.eclipse.uml2.uml.Element; +import org.eclipse.uml2.uml.Expression; +import org.eclipse.uml2.uml.InstanceSpecification; +import org.eclipse.uml2.uml.ObjectNode; +import org.eclipse.uml2.uml.Package; +import org.eclipse.uml2.uml.Parameter; +import org.eclipse.uml2.uml.ParameterSet; +import org.eclipse.uml2.uml.UMLFactory; +import org.eclipse.uml2.uml.ValueSpecification; +import org.eclipse.uml2.uml.util.UMLUtil; +import org.obeonetwork.dsl.sysml.design.internal.services.SysmlElementServices; + +/** + * Services For Activity Diagram. + * + * @author Frederic Bats frederic.bats@obeo.fr + */ +public class ActivityDiagramServices { + private final String activitiesProfile = "SysML::Activities"; //$NON-NLS-1$ + + private final String continuesStereotype = "Continuous"; //$NON-NLS-1$ + + private final String discreteStereotype = "Discrete"; //$NON-NLS-1$ + + private final String probabilityStereotype = "Probability"; //$NON-NLS-1$ + + private void applyProbability(Element element) { + if (!canApplyProbability(element)) { + return; + } + final Package parentPkg = element.getNearestPackage(); + if (parentPkg != null) { + // Apply continuous or discrete stereotype + SysmlElementServices.INSTANCE.createAssociatedStereotype(element, activitiesProfile, + probabilityStereotype); + // Create a new probability ValueSpecification (expression) + final Expression probability = UMLFactory.eINSTANCE.createExpression(); + probability.setName("Probability"); //$NON-NLS-1$ + parentPkg.getPackagedElements().add(probability); + setProbabilityStereotypeValue(element, probability); + } + } + + /** + * Apply generic Rate Stereotype (continuous or discrete); + * + * @param element + * selected element (activity edge or ActivityParameterNode) + * @param stereotypeName + * name of the stereotype to apply + */ + private boolean applyRateStereotype(Element element, String stereotypeName) { + if (!(element instanceof Parameter || element instanceof ActivityEdge)) { + return false; + } + if (!(continuesStereotype.equals(stereotypeName) || discreteStereotype.equals(stereotypeName))) { + return false; + } + final Package parentPkg = element.getNearestPackage(); + if (parentPkg != null) { + // Apply continuous or discrete stereotype + SysmlElementServices.INSTANCE.createAssociatedStereotype(element, activitiesProfile, + stereotypeName); + // Create a new rate instanceSpecification + final InstanceSpecification rate = UMLFactory.eINSTANCE.createInstanceSpecification(); + rate.setName("Rate"); //$NON-NLS-1$ + parentPkg.getPackagedElements().add(rate); + setRateStereotypeValue(element, rate); + return true; + } + return false; + } + + /** + * Can be applied stereotype Probability. + * + * @param element + * selected element + * @return true if ActivityEdge is a descisionNode or an ObjectNode + */ + public boolean canApplyProbability(Element element) { + /* + * Probability can be applied to ActivityEdge if source is a Descision node or an ObjectNode + * Probability can be applied if source is output parameterSet + */ + boolean result = false; + if (element instanceof ActivityEdge) { + final ActivityNode source = ((ActivityEdge)element).getSource(); + if (source instanceof DecisionNode || source instanceof ObjectNode) { + result = true; + } + } else if (element instanceof ParameterSet) { + // widget ParameterSet are not implemented + result = false; + } + return result; + } + + /** + * Delete rate stereotype value + * + * @param element + * selected element + */ + public void deleteRateStereotypeValue(Element element) { + final Rate sterApp = UMLUtil.getStereotypeApplication(element, Rate.class); + if (sterApp != null) { + final InstanceSpecification rate = sterApp.getRate(); + if (rate != null) { + final EList references = rate.eCrossReferences(); + if (references.size() == 2) { + // 2 references expected the package and the stereotype + rate.destroy(); + } + } + } + } + + /** + * Is the Continuous Stereotype applied. + * + * @param element + * selected Activity Edge + * @return true if stereotype is applied + */ + public boolean hasContinuousStereotypeApplied(ActivityEdge element) { + return SysmlElementServices.INSTANCE.hasStereotype(element, continuesStereotype); + } + + /** + * Is the Continuous Stereotype applied. + * + * @param element + * selected Activity Parameter Node + * @return true if stereotype is applied + */ + public boolean hasContinuousStereotypeApplied(ActivityParameterNode element) { + boolean result = true; + final Parameter parameter = element.getParameter(); + if (parameter != null) { + result = SysmlElementServices.INSTANCE.hasStereotype(parameter, continuesStereotype); + } + return result; + } + + /** + * Is the Discrete Stereotype applied. + * + * @param element + * selected Activity Edge + * @return true if stereotype is applied + */ + public boolean hasDiscreteStereotypeApplied(ActivityEdge element) { + return SysmlElementServices.INSTANCE.hasStereotype(element, discreteStereotype); + } + + /** + * Is the Discrete Stereotype applied. + * + * @param element + * selected Activity Parameter Node + * @return true if stereotype is applied + */ + public boolean hasDiscreteStereotypeApplied(ActivityParameterNode element) { + boolean result = true; + final Parameter parameter = element.getParameter(); + if (parameter != null) { + result = SysmlElementServices.INSTANCE.hasStereotype(parameter, discreteStereotype); + } + return result; + } + + /** + * Apply Continuous stereotype. + * + * @param element + * selected element + */ + public void setContinuousStereotype(Element element) { + setRateStereotype(element, continuesStereotype); + } + + /** + * Apply Discrete stereotype. + * + * @param element + * selected element + */ + public void setDiscreteStereotype(Element element) { + setRateStereotype(element, discreteStereotype); + } + + /** + * Apply Probability stereotype to an Activity Edge. + * + * @param edge + * selected edge + */ + public void setProbabilityStereotype(ActivityEdge edge) { + // get all edges which have the same source as the selected edge + final ActivityNode source = edge.getSource(); + source.getOutgoings(); + + for (final ActivityEdge outgoingEdge : source.getOutgoings()) { + if (!SysmlElementServices.INSTANCE.hasStereotype(outgoingEdge, probabilityStereotype)) { + applyProbability(outgoingEdge); + } + } + } + + /** + * Set the Probability expression to a value. + * + * @param element + * selected Element + * @param probability + * new value + */ + private void setProbabilityStereotypeValue(Element element, Expression probability) { + final Probability sterApp = UMLUtil.getStereotypeApplication(element, Probability.class); + if (sterApp != null) { + sterApp.setProbability(probability); + } + } + + /** + * Apply Rate Stereotype (continuous or discrete); + * + * @param element + * selected element (activity edge or ActivityParameterNode) + * @param stereotypeName + * name of the stereotype to apply + */ + private void setRateStereotype(Element element, String stereotypeName) { + if (element instanceof ActivityEdge) { + applyRateStereotype(element, stereotypeName); + } else if (element instanceof ActivityParameterNode) { + final ActivityParameterNode paramNode = (ActivityParameterNode)element; + final Parameter parameter = paramNode.getParameter(); + if (parameter == null) { + return; + } + if (applyRateStereotype(parameter, stereotypeName)) { + // parameter must be set to streaming + parameter.setIsStream(true); + } + } + } + + /** + * Set the Rate value for Rate Stereotype attribute. + * + * @param element + * element stereotyped as Rate (continuous or discrete) + * @param newValue + * new Rate value + */ + public void setRateStereotypeValue(Element element, InstanceSpecification newValue) { + final Rate sterApp = UMLUtil.getStereotypeApplication(element, Rate.class); + if (sterApp != null) { + sterApp.setRate(newValue); + } + } + + /** + * Unapply Probability stereotype. + * + * @param element + * selected element + */ + private void unapplyProbabilityStereotype(Element element) { + // Delete Probability expression associated to the element + final Probability sterApp = UMLUtil.getStereotypeApplication(element, Probability.class); + if (sterApp != null) { + final ValueSpecification probability = sterApp.getProbability(); + if (probability != null) { + final EList references = probability.eCrossReferences(); + if (references.size() == 2) { + // 2 references expected the package and the stereotype + probability.destroy(); + } + } + } + // unapply the Probability Stereotype + final String qualifiedStereotypeName = activitiesProfile + "::" + probabilityStereotype; //$NON-NLS-1$ + SysmlElementServices.INSTANCE.deleteAssociatedStereotype(element, qualifiedStereotypeName); + } + + /** + * Unapply Continuous/Discrete stereotype. + * + * @param element + * selected element + */ + private void unapplyRateStereotype(Element element, String stereotypeName) { + if (element instanceof ActivityEdge) { + unsetRateStereotype(element, stereotypeName); + } else if (element instanceof ActivityParameterNode) { + final ActivityParameterNode paramNode = (ActivityParameterNode)element; + final Parameter parameter = paramNode.getParameter(); + if (parameter == null) { + return; + } + unsetRateStereotype(parameter, stereotypeName); + } + } + + /** + * Unapply Continuous Stereotype. + * + * @param element + * Selected element + */ + public void unsetContinuousStereotype(Element element) { + unapplyRateStereotype(element, continuesStereotype); + } + + /** + * Unapply Discrete Stereotype. + * + * @param element + * Selected element + */ + public void unsetDiscreteStereotype(Element element) { + unapplyRateStereotype(element, discreteStereotype); + } + + /** + * Unapply Probability Stereotype. + * + * @param element + * Selected element + */ + public void unsetProbabilityStereotype(ActivityEdge edge) { + final ActivityNode source = edge.getSource(); + for (final ActivityEdge outgoingEdge : source.getOutgoings()) { + if (SysmlElementServices.INSTANCE.hasStereotype(outgoingEdge, probabilityStereotype)) { + unapplyProbabilityStereotype(outgoingEdge); + } + } + } + + /** + * Unapply Continuous/Discrete stereotype. + * + * @param element + * selected element + */ + private void unsetRateStereotype(Element element, String stereotypeName) { + deleteRateStereotypeValue(element); + final String qualifiedStereotypeName = activitiesProfile + "::" + stereotypeName; //$NON-NLS-1$ + SysmlElementServices.INSTANCE.deleteAssociatedStereotype(element, qualifiedStereotypeName); + } +} diff --git a/plugins/org.obeonetwork.dsl.sysml.design/src/org/obeonetwork/dsl/sysml/design/api/services/BlockDefinitionDiagramServices.java b/plugins/org.obeonetwork.dsl.sysml.design/src/org/obeonetwork/dsl/sysml/design/api/services/BlockDefinitionDiagramServices.java index 84e273f..ab2e757 100644 --- a/plugins/org.obeonetwork.dsl.sysml.design/src/org/obeonetwork/dsl/sysml/design/api/services/BlockDefinitionDiagramServices.java +++ b/plugins/org.obeonetwork.dsl.sysml.design/src/org/obeonetwork/dsl/sysml/design/api/services/BlockDefinitionDiagramServices.java @@ -73,18 +73,6 @@ public String computeAssociationEndLabel(Association association) { return LabelServices.INSTANCE.computeAssociationEndLabel(association); } - /** - * Delete the Associated Stereotype with the given element. - * - * @param e - * : the given element for which you want to delete the stereotype. - * @param steQualified - * : the qualified name of the stereotype you want to delete (ex. : SysML::Blocks::Block). - */ - public void deleteAssociatedStereotype(Element e, String steQualified) { - SysmlElementServices.INSTANCE.deleteAssociatedStereotype(e, steQualified); - } - /** * Get all actors. * diff --git a/plugins/org.obeonetwork.dsl.sysml.design/src/org/obeonetwork/dsl/sysml/design/api/services/ReusedDescriptionServices.java b/plugins/org.obeonetwork.dsl.sysml.design/src/org/obeonetwork/dsl/sysml/design/api/services/ReusedDescriptionServices.java index d7df0a7..1328c78 100644 --- a/plugins/org.obeonetwork.dsl.sysml.design/src/org/obeonetwork/dsl/sysml/design/api/services/ReusedDescriptionServices.java +++ b/plugins/org.obeonetwork.dsl.sysml.design/src/org/obeonetwork/dsl/sysml/design/api/services/ReusedDescriptionServices.java @@ -271,13 +271,8 @@ public List getValidsForSysmlDiagram(final Element element, * Stereotype name to check * @return True if the UML element has the given stereotype */ - protected boolean hasStereotype(Element element, String stereotypeName) { - for (final EObject stereotype : element.getStereotypeApplications()) { - if (stereotypeName.equals(stereotype.eClass().getName())) { - return true; - } - } - return false; + public boolean hasStereotype(Element element, String stereotypeName) { + return SysmlElementServices.INSTANCE.hasStereotype(element, stereotypeName); } private boolean isLayerActive(DDiagram diagram, String layerName) { diff --git a/plugins/org.obeonetwork.dsl.sysml.design/src/org/obeonetwork/dsl/sysml/design/api/services/SysmlAbstractDiagramServices.java b/plugins/org.obeonetwork.dsl.sysml.design/src/org/obeonetwork/dsl/sysml/design/api/services/SysmlAbstractDiagramServices.java index de6860e..5c4a806 100644 --- a/plugins/org.obeonetwork.dsl.sysml.design/src/org/obeonetwork/dsl/sysml/design/api/services/SysmlAbstractDiagramServices.java +++ b/plugins/org.obeonetwork.dsl.sysml.design/src/org/obeonetwork/dsl/sysml/design/api/services/SysmlAbstractDiagramServices.java @@ -13,8 +13,15 @@ import org.eclipse.emf.ecore.EObject; import org.eclipse.sirius.diagram.DDiagram; import org.eclipse.sirius.viewpoint.DSemanticDecorator; +import org.eclipse.uml2.uml.Activity; +import org.eclipse.uml2.uml.ActivityEdge; +import org.eclipse.uml2.uml.ActivityParameterNode; +import org.eclipse.uml2.uml.CallBehaviorAction; +import org.eclipse.uml2.uml.CallOperationAction; +import org.eclipse.uml2.uml.Element; import org.eclipse.uml2.uml.NamedElement; import org.obeonetwork.dsl.sysml.design.internal.services.LabelServices; +import org.obeonetwork.dsl.sysml.design.internal.services.SysmlElementServices; import org.obeonetwork.dsl.uml2.core.api.services.AbstractDiagramServices; /** @@ -24,7 +31,18 @@ */ public abstract class SysmlAbstractDiagramServices extends AbstractDiagramServices { /** - * Compute the label of the given element. + * Compute displayed label of the given element + * + * @param element + * the {@link Element} for which to retrieve a label. + * @return the computed label. + */ + public String computeSysmlDisplayLabel(NamedElement element) { + return LabelServices.INSTANCE.computeSysmlDisplayLabel(element); + } + + /** + * Compute the default label of the given element. * * @param element * the {@link NamedElement} for which to retrieve a label. @@ -34,6 +52,34 @@ public String computeSysmlLabel(NamedElement element) { return LabelServices.INSTANCE.computeSysmlLabel(element); } + /** + * Delete the Associated Stereotype with the given element. + * + * @param e + * : the given element for which you want to delete the stereotype. + * @param steQualified + * : the qualified name of the stereotype you want to delete (ex. : SysML::Blocks::Block). + */ + public void deleteAssociatedStereotype(Element e, String steQualified) { + SysmlElementServices.INSTANCE.deleteAssociatedStereotype(e, steQualified); + } + + /** + * Is it a specific SysML label. + * + * @param element + * uml element + * @return true if the label is redefined in SysML Specification + */ + public boolean isRedefinedLabel(EObject element) { + if (element instanceof CallBehaviorAction || element instanceof CallOperationAction + || element instanceof Activity || element instanceof ActivityParameterNode + || element instanceof ActivityEdge) { + return true; + } + return false; + } + /** * Open the select existing element dialog. * diff --git a/plugins/org.obeonetwork.dsl.sysml.design/src/org/obeonetwork/dsl/sysml/design/internal/listeners/ActivityEdgeListener.java b/plugins/org.obeonetwork.dsl.sysml.design/src/org/obeonetwork/dsl/sysml/design/internal/listeners/ActivityEdgeListener.java new file mode 100644 index 0000000..5497988 --- /dev/null +++ b/plugins/org.obeonetwork.dsl.sysml.design/src/org/obeonetwork/dsl/sysml/design/internal/listeners/ActivityEdgeListener.java @@ -0,0 +1,80 @@ +/******************************************************************************* + * Copyright (c) 2014 Obeo. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.obeonetwork.dsl.sysml.design.internal.listeners; + +import java.util.Iterator; + +import org.eclipse.emf.common.command.Command; +import org.eclipse.emf.common.notify.Notification; +import org.eclipse.emf.transaction.RecordingCommand; +import org.eclipse.emf.transaction.ResourceSetChangeEvent; +import org.eclipse.emf.transaction.ResourceSetListenerImpl; +import org.eclipse.emf.transaction.RollbackException; +import org.eclipse.emf.transaction.TransactionalEditingDomain; +import org.eclipse.sirius.business.api.session.Session; +import org.eclipse.sirius.business.api.session.SessionManager; +import org.eclipse.uml2.uml.ActivityEdge; +import org.eclipse.uml2.uml.ActivityNode; +import org.obeonetwork.dsl.sysml.design.api.services.ActivityDiagramServices; +import org.obeonetwork.dsl.sysml.design.internal.services.SysmlElementServices; + +/** + * To listen ActivityEdge changes. In SysML if an activityEdgge is created on aDecision node or parametersSet + * if an edge outgoing from this node is stereotyped as Probability all edge have to be stereotyped as + * Probability. + * + * @author Frederic Bats frederic.bats@obeo.fr + */ +public class ActivityEdgeListener extends ResourceSetListenerImpl { + + @Override + public boolean isPrecommitOnly() { + return true; + } + + @Override + public Command transactionAboutToCommit(ResourceSetChangeEvent event) throws RollbackException { + + for (final Iterator iter = event.getNotifications().iterator(); iter.hasNext();) { + final Notification notification = (Notification)iter.next(); + final Object notifier = notification.getNotifier(); + + // An activityEdge is created + if (notifier instanceof ActivityEdge && Notification.SET == notification.getEventType()) { + final ActivityEdge activityEdge = (ActivityEdge)notifier; + final ActivityNode source = activityEdge.getSource(); + + boolean isProbability = false; + if (source != null) { + for (final ActivityEdge edge : source.getOutgoings()) { + if (SysmlElementServices.INSTANCE.hasStereotype(edge, "Probability")) { //$NON-NLS-1$ + isProbability = true; + break; + } + } + } + if (isProbability) { + final Session session = SessionManager.INSTANCE.getSession(activityEdge); + final TransactionalEditingDomain editingDomain = session.getTransactionalEditingDomain(); + return new RecordingCommand(editingDomain) { + + @Override + protected void doExecute() { + final ActivityDiagramServices actService = new ActivityDiagramServices(); + actService.setProbabilityStereotype(activityEdge); + } + }; + } + } + } + return null; + } +} diff --git a/plugins/org.obeonetwork.dsl.sysml.design/src/org/obeonetwork/dsl/sysml/design/internal/listeners/SysMLSessionManagerListener.java b/plugins/org.obeonetwork.dsl.sysml.design/src/org/obeonetwork/dsl/sysml/design/internal/listeners/SysMLSessionManagerListener.java new file mode 100644 index 0000000..1bcfbf0 --- /dev/null +++ b/plugins/org.obeonetwork.dsl.sysml.design/src/org/obeonetwork/dsl/sysml/design/internal/listeners/SysMLSessionManagerListener.java @@ -0,0 +1,46 @@ +/******************************************************************************* + * Copyright (c) 2014 Obeo. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.obeonetwork.dsl.sysml.design.internal.listeners; + +import org.eclipse.sirius.business.api.session.Session; +import org.eclipse.sirius.business.api.session.SessionManagerListener; +import org.eclipse.sirius.viewpoint.description.Viewpoint; + +/** + * Session listener. + * + * @author Frederic Bats frederic.bats@obeo.fr + */ +public class SysMLSessionManagerListener implements SessionManagerListener { + + private final ActivityEdgeListener activityEdgeListener = new ActivityEdgeListener(); + + public void notify(Session updated, int notification) { + // Nothing + } + + public void notifyAddSession(Session newSession) { + newSession.getTransactionalEditingDomain().addResourceSetListener(activityEdgeListener); + + } + + public void notifyRemoveSession(Session removedSession) { + removedSession.getTransactionalEditingDomain().removeResourceSetListener(activityEdgeListener); + } + + public void viewpointDeselected(Viewpoint deselectedSirius) { + // Nothing + } + + public void viewpointSelected(Viewpoint selectedSirius) { + // Nothing + } +} diff --git a/plugins/org.obeonetwork.dsl.sysml.design/src/org/obeonetwork/dsl/sysml/design/internal/services/DisplayLabelSwitch.java b/plugins/org.obeonetwork.dsl.sysml.design/src/org/obeonetwork/dsl/sysml/design/internal/services/DisplayLabelSwitch.java new file mode 100644 index 0000000..af68015 --- /dev/null +++ b/plugins/org.obeonetwork.dsl.sysml.design/src/org/obeonetwork/dsl/sysml/design/internal/services/DisplayLabelSwitch.java @@ -0,0 +1,153 @@ +/******************************************************************************* + * Copyright (c) 2014 Obeo. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.obeonetwork.dsl.sysml.design.internal.services; + +import org.eclipse.emf.ecore.EObject; +import org.eclipse.papyrus.sysml14.activities.Probability; +import org.eclipse.uml2.uml.Activity; +import org.eclipse.uml2.uml.ActivityEdge; +import org.eclipse.uml2.uml.ActivityParameterNode; +import org.eclipse.uml2.uml.Behavior; +import org.eclipse.uml2.uml.CallBehaviorAction; +import org.eclipse.uml2.uml.CallOperationAction; +import org.eclipse.uml2.uml.ControlFlow; +import org.eclipse.uml2.uml.Expression; +import org.eclipse.uml2.uml.ObjectFlow; +import org.eclipse.uml2.uml.Operation; +import org.eclipse.uml2.uml.Parameter; +import org.eclipse.uml2.uml.ValueSpecification; +import org.eclipse.uml2.uml.util.UMLUtil; + +/** + * Specific SysML labels for Sysml. + * + * @author Frederic Bats frederic.bats@obeo.fr + */ +public class DisplayLabelSwitch extends org.obeonetwork.dsl.uml2.core.internal.services.DisplayLabelSwitch { + + private final static String CONTROL_OPERATOR = "ControlOperator"; //$NON-NLS-1$ + + private final static String SQUARE_BRACKET_OPEN = "["; //$NON-NLS-1$ + + private final static String SQUARE_BRACKET_CLOSE = "]"; //$NON-NLS-1$ + + private final static String BRACKET_OPEN = "{"; //$NON-NLS-1$ + + private final static String BRACKET_CLOSE = "}"; //$NON-NLS-1$ + + private final String continuousStereotype = "Continuous"; //$NON-NLS-1$ + + private final String discreteStereotype = "Discrete"; //$NON-NLS-1$ + + /** + * {@inheritDoc} + */ + @Override + public String caseActivity(Activity object) { + String label = defaultCase(object); + if (SysmlElementServices.INSTANCE.hasStereotype(object, CONTROL_OPERATOR)) { + label = label + SPACE + SQUARE_BRACKET_OPEN + CONTROL_OPERATOR + SQUARE_BRACKET_CLOSE; + } + return label; + } + + /** + * {@inheritDoc} + */ + @Override + public String caseActivityEdge(ActivityEdge object) { + String label = defaultCase(object); + if (SysmlElementServices.INSTANCE.hasStereotype(object, "Probability")) { //$NON-NLS-1$ + final Probability sterApp = UMLUtil.getStereotypeApplication(object, Probability.class); + final ValueSpecification valSpec = sterApp.getProbability(); + if (valSpec instanceof Expression) { + valSpec.getName(); + label = SPACE + BRACKET_OPEN + "probability=" + valSpec.getName() + BRACKET_CLOSE; //$NON-NLS-1$ + } + } + return label; + } + + /** + * {@inheritDoc} + */ + @Override + public String caseActivityParameterNode(ActivityParameterNode object) { + final String label = defaultCase(object); + final Parameter param = object.getParameter(); + String postfix = ""; //$NON-NLS-1$ + String streamLabel = ""; //$NON-NLS-1$ + if (param != null) { + final String stereotypes = computeStereotypes(param); + if (stereotypes != null && !stereotypes.isEmpty()) { + postfix = System.lineSeparator() + stereotypes; + } + if (param.isStream() && !(SysmlElementServices.INSTANCE.hasStereotype(param, continuousStereotype) + || SysmlElementServices.INSTANCE.hasStereotype(param, discreteStereotype))) { + + streamLabel = BRACKET_OPEN + "stream" + BRACKET_CLOSE; //$NON-NLS-1$ + } + } + return streamLabel + label + postfix; + } + + /** + * {@inheritDoc} + */ + @Override + public String caseCallBehaviorAction(CallBehaviorAction object) { + final Behavior behavior = object.getBehavior(); + String prefix = ""; //$NON-NLS-1$ + if (behavior != null) { + final String stereotypes = computeStereotypes(behavior); + if (stereotypes != null && !stereotypes.isEmpty()) { + prefix = stereotypes + System.lineSeparator(); + } + } + return prefix + defaultCase(object); + } + + /** + * {@inheritDoc} + */ + @Override + public String caseCallOperationAction(CallOperationAction object) { + final Operation operation = object.getOperation(); + String prefix = ""; //$NON-NLS-1$ + if (operation != null) { + final String stereotypes = computeStereotypes(operation); + if (stereotypes != null && !stereotypes.isEmpty()) { + prefix = stereotypes + System.lineSeparator(); + } + } + return prefix + defaultCase(object); + } + + @Override + public String caseControlFlow(ControlFlow object) { + return null; + } + + @Override + public String caseObjectFlow(ObjectFlow object) { + return null; + } + + /** + * {@inheritDoc} + */ + @Override + public String defaultCase(EObject object) { + final org.obeonetwork.dsl.uml2.core.internal.services.DisplayLabelSwitch labelSwitch = new org.obeonetwork.dsl.uml2.core.internal.services.DisplayLabelSwitch(); + final String label = labelSwitch.doSwitch(object); + return label; + } +} diff --git a/plugins/org.obeonetwork.dsl.sysml.design/src/org/obeonetwork/dsl/sysml/design/internal/services/LabelServices.java b/plugins/org.obeonetwork.dsl.sysml.design/src/org/obeonetwork/dsl/sysml/design/internal/services/LabelServices.java index 0b85f7b..9d7b47d 100644 --- a/plugins/org.obeonetwork.dsl.sysml.design/src/org/obeonetwork/dsl/sysml/design/internal/services/LabelServices.java +++ b/plugins/org.obeonetwork.dsl.sysml.design/src/org/obeonetwork/dsl/sysml/design/internal/services/LabelServices.java @@ -32,7 +32,19 @@ private LabelServices() { } /** - * Compute the label of the given element. + * Compute displayed label of the given element + * + * @param element + * the {@link Element} for which to retrieve a label. + * @return the computed label. + */ + public String computeSysmlDisplayLabel(NamedElement element) { + final DisplayLabelSwitch genLabel = new DisplayLabelSwitch(); + return genLabel.doSwitch(element); + } + + /** + * Compute the default label of the given element. * * @param element * the {@link Element} for which to retrieve a label. diff --git a/plugins/org.obeonetwork.dsl.sysml.design/src/org/obeonetwork/dsl/sysml/design/internal/services/SysmlElementServices.java b/plugins/org.obeonetwork.dsl.sysml.design/src/org/obeonetwork/dsl/sysml/design/internal/services/SysmlElementServices.java index 57e5724..c899f81 100644 --- a/plugins/org.obeonetwork.dsl.sysml.design/src/org/obeonetwork/dsl/sysml/design/internal/services/SysmlElementServices.java +++ b/plugins/org.obeonetwork.dsl.sysml.design/src/org/obeonetwork/dsl/sysml/design/internal/services/SysmlElementServices.java @@ -182,6 +182,24 @@ public EObject getRootContainer(EObject eObject) { return EcoreUtil.getRootContainer(eObject); } + /** + * Check if an UML element has a stereotype defined as parameter. + * + * @param element + * UML element + * @param stereotype + * Stereotype name to check + * @return True if the UML element has the given stereotype + */ + public boolean hasStereotype(Element element, String stereotypeName) { + for (final EObject stereotype : element.getStereotypeApplications()) { + if (stereotypeName.equals(stereotype.eClass().getName())) { + return true; + } + } + return false; + } + /** * Checks if element has the stereotype block. *