gpt4 book ai didi

java - 添加到电动势模型

转载 作者:太空宇宙 更新时间:2023-11-04 14:01:23 25 4
gpt4 key购买 nike

我有一个基于电动势的模型。在模型中,我有一个要素类,并且我可以向要素添加约束。例如功能 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;

}

编辑

当我删除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));
}

上面的代码向功能添加了约束,但缺少上下文。有什么想法

谢谢

最佳答案

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

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

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

关于java - 添加到电动势模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29274496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com