1

To simplify my question I've made a little model of my problem.

In this model, I have a Plane in a Simulation. I would like to generate with a little piece of code, some other Plane with the same children classes (MotorType, OtherClass1, OtherClass2) and the same values except the numeric value in MotorType that is incremented with each iteration.

For example, I have a Simulation comprised of a Plane named "plane1", with a MotorType=TypeB with the value 10, and an OtherClass1.

I would like to generate 10 new planes, with OtherClass1 with the same values and the same MotorType, but with the "value" incremented by 10.

How can I generate some new plane child of my simulation that is a copy of an existing plane but with an incrementation of a parameter ?
Is it possible to do this with Sirius by a right click on my plane to copy ?

Example of my model class diagram
Example of a creation of a simulation

Rann Lifshitz
  • 4,040
  • 4
  • 22
  • 42

2 Answers2

2

You probably want to use EcoreUtil.copy(EObject) on your original Simulation to create your copy.

Then, using the Java EMF APIs you can navigate inside your copy and alter it however you wish to.

If you want each Simulation to be in its own file, you will have to create the appropriate EMF Resource and add your newly-created Simulation to its contents before saving it.

After you have implemented the Java method that does all the above, you can call it from a Sirius diagram using a Java service

user1292456
  • 778
  • 4
  • 12
  • Is it possible to do this in a Sirius's Tree ? I have created my java service, add _org.eclipse.sirius.externalJavaAction_ in Extentions of plugin.xml, add the java action to refer to my java class, and create an _External Java Action_ in a popup menu, but I can't see my operation in the menu... Maybe I've missed something, or maybe it was impossible in a tree description ? – Thibault Santonja Apr 17 '18 at 08:47
  • Good answer buddy! – Rann Lifshitz Apr 17 '18 at 09:13
  • Use the Java services I referenced in my answer, not java actions. It should work in trees too. – user1292456 Apr 17 '18 at 09:20
0

You should define your own EMF copier by extending EcoreUtil.Copier.

This way you can override the default Copier behaviour, and process the EStructuralFeature of interest with some custom behaviour.

class PlaneCopier extends Copier {

    int motorType;

    public EObject copy(EObject eObject, int motorType) {
        this.motorType = motorType
        return super.copy(eObject);
    }

    @Override
    protected void copyAttribute(EAttribute eAttribute, EObject eObject, EObject copyEObject) {
        if (eAttribute.equals(YouEMFPackage.Literals.PLANE__MOTOR_TYPE)) {
            copyEObject.eSet(YouEMFPackage.Literals.PLANE__MOTOR_TYPE, motorType);
        } else {
            super.copyAttribute(eAttribute, eObject, copyEObject);
        }
    }
}

And use it in a loop :

PlaneCopier copier = new PlaneCopier();
Plane templatePlane = ...
int motorType = 0;
for (var i=0; i<nbPlanes; i++) {
    motorType += 10;
    newPlane = copier.copy(templatePlane, motorType);
}