Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 添加到emf模型_Java_Eclipse Plugin_Eclipse Emf_Emf - Fatal编程技术网

Java 添加到emf模型

Java 添加到emf模型,java,eclipse-plugin,eclipse-emf,emf,Java,Eclipse Plugin,Eclipse Emf,Emf,我有一个基于emf的模型。在模型中,我有一个要素类,可以向要素添加约束。例如,功能A“暗示”功能B。我正在尝试使用emf命令堆栈向功能添加约束。它将约束添加到特征,但缺少属性。我的代码如下 public static Object doExecute(Feature contextFeature, FeatureModel featureModel, ComposedAdapterFactory adapterFactory) { CreateConstraintDialog dl

我有一个基于emf的模型。在模型中,我有一个要素类,可以向要素添加约束。例如,功能A“暗示”功能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;

}
/**
 * <!-- 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));
}
编辑

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

setContext方法如下所示

 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;

}
/**
 * <!-- 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));
}
/**
* 
* 
*@生成
*/
公共void setContext(功能newContext){
if(newContext!=eInternalContainer()| |(eContainerFeatureID()!=FmPackage.CONSTRAINT_uucontext&&newContext!=null)){
if(EcoreUtil.isAncestor(this,newContext))
抛出新的IllegalArgumentException(“不允许对“+toString()使用递归包含”);
NotificationChain msgs=null;
如果(eInternalContainer()!=null)
msgs=eBasicRemoveFromContainer(msgs);
if(newContext!=null)
msgs=((InternalEObject)newContext).eInverseAdd(这个,FmPackage.FEATURE\u约束,FEATURE.class,msgs);
msgs=basicSetContext(newContext,msgs);
如果(msgs!=null)msgs.dispatch();
}
else if(eNotificationRequired())
eNotify(新的EnotifyImpl(this,Notification.SET,FmPackage.CONSTRAINT\uuu CONTEXT,newContext,newContext));
}
上述代码向功能添加了约束,但缺少上下文。有什么想法吗


谢谢

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

不相关,但在执行命令之前,还应始终调用命令上的
canExecute
方法:

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

当我刚刚设置了约束的setter之后,我需要执行这个命令吗?检查更新的代码此代码的问题在于,它没有检测到模型中添加的约束i-e更改,因此我的编辑器没有进入脏状态,因为在保存时,我将模型对象写入XMITH。更新的代码看起来好像一切都按正确的顺序进行。AddCommand将在功能级别而不是约束级别生成通知。你的编辑器是如何设置来监听更改的?我不知道编辑器是如何设置来监听更改的。你能给我一些建议吗?由于此功能很旧,并且是由其他人实现的,因此无法确定,但可能有一个
CommandStackListener
在某处侦听
ResourceSetChangeEvent
事件。该事件将包含
通知
项(在本例中为
通知.添加
)以及更改内容的详细信息。可能您的事件侦听器正在筛选这些事件?