使用String Literal设置EMF EStructuralFeature

时间:2015-07-04 14:31:24

标签: java eclipse emf

是否可以使用字符串文字在EMF中设置EStructuralFeature的值?

示例:

someObject.set(intFeature, "1") 
someObject.set(stringFeature, "1")

在此之后,我希望intFeature的值是一个值为1的整数,而stringFeature的值包含" 1"。

我怀疑这样的功能是可用的,因为EStructuralFeature :: defaultValueLiteral是一个String,所以它也必须以某种方式解析。

1 个答案:

答案 0 :(得分:2)

要做这种事情,你必须处理元模型和EFactory。通过查看setDefaultValue中的EStructuralFeature,您可以看到EFactory类型的EStructuralFeature用于构建值(仅当EStructuralFeature时type是EDatatype)。

这是一个通用代码段(我们假设我们有一个EObject eobj):

// We get the estructuralfeature
EStructuralFeature feature = eobj.eClass().getEStructuralFeature("myfeature");
// Is the feature type "primitive"
if (feature.getEType() instanceof EDataType) {
    EDataType dtype = (EDataType)ea.getEType();
    // We get the EFactory
    EFactory factory = feature.getEType().getEPackage().getEFactoryInstance();
    eobj.eSet(feature, factory.createFromString(dtype, "mystringvalue"));
}

这是UML的一个例子:

Property p = UMLFactory.eINSTANCE.createProperty();
EStructuralFeature ea = p.eClass().getEStructuralFeature("lower");
... // if and stuffs
EFactory factory = ea.getEType().getEPackage().getEFactoryInstance();
p.eSet(ea, factory.createFromString((EDataType)ea.getEType(), "1"));