【问题标题】:Generate some child in my ecore model在我的生态模型中生成一些孩子
【发布时间】:2018-09-26 08:29:16
【问题描述】:

为了简化我的问题,我制作了我的problem 的一个小模型。

在这个模型中,我在 Simulation 中有一个 Plane。我想用一小段代码生成其他一些具有相同子类的 PlaneMotorTypeOtherClass1OtherClass2 ) 和相同的值,除了 MotorType 中的数值随每次迭代递增。

对于 example,我有一个由名为“plane1”的 Plane 组成的模拟,MotorType=TypeB 的值为 10,还有一个 其他类1.

我想生成 10 个新飞机,OtherClass1 具有相同的值和相同的 MotorType,但“值”增加了 10。

如何在我的模拟中生成一些新的平面子代,它是现有平面的副本,但具有参数增量?
是否可以通过右键单击我的飞机复制来使用 Sirius 执行此操作?

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

【问题讨论】:

  • 优秀的第一个问题哥们。干得好!
  • 非常感谢! @Rann-Lifshitz

标签: eclipse emf ecore eclipse-sirius


【解决方案1】:

您可能希望在原始模拟上使用EcoreUtil.copy(EObject) 来创建您的副本。

然后,使用 Java EMF API,您可以在副本中导航并随意更改它。

如果您希望每个模拟都在自己的文件中,则必须创建适当的 EMF 资源并将新创建的模拟添加到其内容中,然后再保存。

在您实现了执行上述所有操作的 Java 方法后,您可以使用 Java service 从 Sirius 图中调用它

【讨论】:

  • 有可能在天狼星的树上做到这一点吗?我已经创建了我的 java 服务,在 plugin.xml 的扩展中添加 org.eclipse.sirius.externalJavaAction,添加 java 动作来引用我的 java 类,并创建一个 External Java Action 在弹出菜单中,但我在菜单中看不到我的操作...也许我错过了什么,或者在树描述中是不可能的?
  • 好朋友!
  • 使用我在回答中引用的 Java 服务,而不是 Java 操作。它也应该在树上工作。
【解决方案2】:

您应该通过扩展 EcoreUtil.Copier 来定义自己的 EMF 复印机。

这样您可以覆盖默认的 Copier 行为,并使用一些自定义行为处理感兴趣的 EStructuralFeature。

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

并在循环中使用它:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-17
    • 2013-11-04
    • 2015-06-23
    • 2012-12-13
    • 2018-03-08
    • 2011-02-02
    • 2018-08-30
    • 1970-01-01
    相关资源
    最近更新 更多