Thibault Santonja
Thibault Santonja

Reputation: 25

Generate some child in my ecore model

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

Upvotes: 1

Views: 226

Answers (2)

Romain Bernard
Romain Bernard

Reputation: 96

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);
}

Upvotes: 0

Banoffee
Banoffee

Reputation: 862

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

Upvotes: 2

Related Questions