【问题标题】:Adding to an emf model添加到 emf 模型
【发布时间】:2015-05-30 05:38:57
【问题描述】:

我有一个基于 emf 的模型。在模型中,我有一个要素类,并且可以向这些要素添加约束。例如功能 A “IMPLIES”功能 B。我正在尝试使用 emf 命令堆栈向功能添加约束。它将约束添加到特征但缺少属性。我的代码如下

 public static Object doExecute(Feature contextFeature, FeatureModel featureModel, ComposedAdapterFactory adapterFactory) {


    CreateConstraintDialog dlg = new CreateConstraintDialog(Display.getCurrent().getActiveShell(), contextFeature, featureModel, adapterFactory);
    dlg.open();

    // check if dialog was cancelled:
    if (dlg.getReturnCode() == Window.CANCEL)
        return null;

    Feature selectedFeature = dlg.getSelectedFeature();
    if (selectedFeature == null)
        return null;

    ConstraintType selectedConstraintType = dlg.getSelectedConstraintType();

    Constraint constraint = FmFactory.eINSTANCE.createConstraint();
    constraint.setType(selectedConstraintType);
    constraint.setConstrainedFeature(selectedFeature);
    constraint.setContext(contextFeature);

    EditingDomain editingDomain = AdapterFactoryEditingDomain.getEditingDomainFor(contextFeature);
    Command cmd = AddCommand.create(editingDomain, contextFeature, FmPackage.FEATURE__CONSTRAINTS, constraint);
    editingDomain.getCommandStack().execute(cmd);
    return null;

}

编辑

当我删除 constraint.setContext(contextFeature);从上面的代码中,编辑器会收到有关更改的通知(即向功能添加了一个新约束),但由于未设置上下文属性而丢失。

setContext方法如下

/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
public void setContext(Feature newContext) {
    if (newContext != eInternalContainer() || (eContainerFeatureID() != FmPackage.CONSTRAINT__CONTEXT && newContext != null)) {
        if (EcoreUtil.isAncestor(this, newContext))
            throw new IllegalArgumentException("Recursive containment not allowed for " + toString());
        NotificationChain msgs = null;
        if (eInternalContainer() != null)
            msgs = eBasicRemoveFromContainer(msgs);
        if (newContext != null)
            msgs = ((InternalEObject)newContext).eInverseAdd(this, FmPackage.FEATURE__CONSTRAINTS, Feature.class, msgs);
        msgs = basicSetContext(newContext, msgs);
        if (msgs != null) msgs.dispatch();
    }
    else if (eNotificationRequired())
        eNotify(new ENotificationImpl(this, Notification.SET, FmPackage.CONSTRAINT__CONTEXT, newContext, newContext));
}

上面的代码为特征添加了约束,但是上下文丢失了。任何想法

谢谢

【问题讨论】:

    标签: java eclipse-plugin eclipse-emf emf


    【解决方案1】:

    由于您刚刚创建了 constraint 实例,因此无需使用命令来设置其属性,因为它尚未附加到 EMF 模型。您可以只调用 setter 方法。您使用命令的地方只是将constraint 添加到您现有的功能中。

    不相关,但您也应该始终在执行命令之前调用canExecute 方法:

    CompoundCommand cmd = ....;
    if (cmd.canExecute()) {
        editingDomain.getCommandStack().execute( cmd );
    }
    

    【讨论】:

    • 当我只是设置约束的设置器之后我需要执行命令吗?检查更新的代码
    • 这段代码的问题是它没有检测到约束的添加,即模型中的变化,因此我的编辑器不会进入脏状态,因为在保存时我将模型对象写入 xmi跨度>
    • 更新后的代码看起来一切都以正确的顺序进行。 AddCommand 将在功能级别生成通知,而不是在约束级别。您的编辑器如何设置以听取更改?
    • 我不知道编辑器是如何设置监听变化的,你能给我一些指点吗?由于此功能很旧并由其他人实现
    • 不能确定,但​​可能有一个CommandStackListener 在某处监听ResourceSetChangeEvent 事件。该事件将包含Notification 项目(在本例中为Notification.ADD 类型),它们包含更改内容的详细信息。也许您的事件侦听器正在过滤这些事件?
    猜你喜欢
    • 1970-01-01
    • 2014-03-13
    • 1970-01-01
    • 2021-09-26
    • 2010-11-16
    • 2012-02-28
    • 2013-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多