blob: 84752092ff40440cbdebf0832e87505b185e8044 [file] [log] [blame]
/**
* Copyright (c)2020 CEA LIST, Committer Name, and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* CEA LIST - Initial API and implementation
* Gabriel Pedroza (CEA LIST) gabriel.pedroza@cea.fr
*
*/
package org.eclipse.papyrus.pdp4eng.designer.controller.internal;
import java.util.ArrayList;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.papyrus.pdp4eng.designer.utils.ModelPackageNames;
import org.eclipse.uml2.uml.Action;
import org.eclipse.uml2.uml.Activity;
import org.eclipse.uml2.uml.ActivityParameterNode;
import org.eclipse.uml2.uml.Behavior;
import org.eclipse.uml2.uml.CallBehaviorAction;
import org.eclipse.uml2.uml.InputPin;
import org.eclipse.uml2.uml.OutputPin;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.Pin;
import org.eclipse.uml2.uml.UMLFactory;
import org.eclipse.uml2.uml.UMLPackage;
/**
* Creates an abstraction from Data.
*
*/
public class ActionCallProcessToProcessCommand extends RecordingCommand {
protected Action actionCallProcess;
public ActionCallProcessToProcessCommand(TransactionalEditingDomain domain, Action actionCallProcess) {
super(domain, "Transform a call process (OpaqueAction or CallBehaviorAction) to a process (Activity)");
this.actionCallProcess = actionCallProcess;
}
@Override
protected void doExecute() {
// Get DFD_L1 Package. If it is not found create the package.
Package DFDL1_Pkg = this.actionCallProcess.getModel().getNestedPackage(ModelPackageNames.DFD_L1_PackageName);
if (DFDL1_Pkg==null) {
actionCallProcess.getModel().createPackagedElement(ModelPackageNames.DFD_L1_PackageName, UMLPackage.eINSTANCE.getPackage());
DFDL1_Pkg = actionCallProcess.getModel().getNestedPackage(ModelPackageNames.DFD_L1_PackageName);
}
// If the Action is an OpaqueAction then create a new CallBehaviorAction and set it as behavior of the new Activity
if (actionCallProcess instanceof org.eclipse.uml2.uml.OpaqueAction) {
Activity newProcess= UMLFactory.eINSTANCE.createActivity();
newProcess.setName(actionCallProcess.getName());
DFDL1_Pkg.getPackagedElements().add(newProcess);
CallBehaviorAction callBehaviorAction=UMLFactory.eINSTANCE.createCallBehaviorAction();
actionCallProcess.getActivity().getOwnedNodes().add(callBehaviorAction);
callBehaviorAction.setName(actionCallProcess.getName());
callBehaviorAction.setBehavior(newProcess);
// For each Input pin of the Action create a new ActivityParameterNode
// Each ActivityParameterNode is added to the new Activity
ArrayList<Pin> list=new ArrayList<Pin>();
for (InputPin input : actionCallProcess.getInputs()) {
ActivityParameterNode parameter=UMLFactory.eINSTANCE.createActivityParameterNode();
parameter.setName(input.getName());
parameter.setType(input.getType());
newProcess.getOwnedNodes().add(parameter);
list.add(input);
}
// The Input pins in the OpaqueAction are added to the CallBehaviorAction
for (Pin pin : list) {
callBehaviorAction.getArguments().add((InputPin)pin);
}
list.clear();
// For each Output pin of the OpaqueAction create a new ActivityParameterNode
// Each ActivityParameterNode is added to the new Activity
for (OutputPin output : actionCallProcess.getOutputs()) {
ActivityParameterNode parameter=UMLFactory.eINSTANCE.createActivityParameterNode();
parameter.setName(output.getName());
parameter.setType(output.getType());
newProcess.getOwnedNodes().add(parameter);
list.add(output);
}
// The Output pins in the OpaqueAction are added to the CallBehaviorAction
for (Pin pin : list) {
callBehaviorAction.getResults().add((OutputPin)pin);
}
// The OpaqueAction can be replaced by the CallBehaviorAction
// The code that follows is intended to remove the OpaqueAction (Code to be reviewed!)
//actionCallProcess.getActivity().getOwnedNodes().remove(actionCallProcess);
//actionCallProcess.destroy();
EcoreUtil.delete(actionCallProcess);
} else {
// If the Action is already a CallBehaviorAction then create the ActivityParameterNodes
// A new Behavior (Activity) will be created if the CallBehaviorAction is not associated to a Behavior
// Or if the associated Behavior is not named as the CallBehaviorAction
if (actionCallProcess instanceof org.eclipse.uml2.uml.CallBehaviorAction && (
(((CallBehaviorAction) actionCallProcess).getBehavior()==null) || (
(((CallBehaviorAction) actionCallProcess).getBehavior()!=null) &&
!((CallBehaviorAction) actionCallProcess).getBehavior().getName().equals(actionCallProcess.getName())))) {
Activity newProcess= UMLFactory.eINSTANCE.createActivity();
newProcess.setName(actionCallProcess.getName());
DFDL1_Pkg.getPackagedElements().add(newProcess);
// The CallBehaviorAction and the associated Behavior (Activity) should have the same name
// Otherwise the Behavior is renamed as a candidate to be deleted (assistance to clean model)
if ((((CallBehaviorAction) actionCallProcess).getBehavior()!=null) &&
!((CallBehaviorAction) actionCallProcess).getBehavior().getName().equals(actionCallProcess.getName())) {
Behavior behavior =((CallBehaviorAction) actionCallProcess).getBehavior();
behavior.setName("ToDelete_"+behavior.getName());
}
((CallBehaviorAction) actionCallProcess).setBehavior(newProcess);
// For each Input pin of the Action create a new ActivityParameterNode
// Each ActivityParameterNode is added to the new Activity
for (InputPin input : actionCallProcess.getInputs()) {
ActivityParameterNode parameter=UMLFactory.eINSTANCE.createActivityParameterNode();
parameter.setName(input.getName());
parameter.setType(input.getType());
newProcess.getOwnedNodes().add(parameter);
}
// For each Output pin of the OpaqueAction create a new ActivityParameterNode
// Each ActivityParameterNode is added to the new Activity
for (OutputPin output : actionCallProcess.getOutputs()) {
ActivityParameterNode parameter=UMLFactory.eINSTANCE.createActivityParameterNode();
parameter.setName(output.getName());
parameter.setType(output.getType());
newProcess.getOwnedNodes().add(parameter);
}
}
}
}
}