如何在 Xcore 中定义地图
How to define a map in Xcore
根据EMF FAQ,可以在EMF中创建一个Map:
An EMap is basically a List of java.util.Map$Entry instances.
Therefore to create a Map you need to first model your map entry by
following these steps:
- Create an EClass with the name [Type1]To[Type2]Map where [Type1] represents the key's type and the [Type2] represents the value's type.
- Set the Instance Class Name property of the newly created EClass to java.util.Map$Entry.
- Create an EAttribute or EReference named "key" and set the EDataType or EClass for it.
- Create an EAttribute or EReference called "value" and set the EDataType or EClass for it.
Now, when you create an EReference somewhere that uses this map entry
class as its EClass, the EMF code generator will detect this special
case and generate a properly typed EMap getter/setter for you instead
of a normal EList getter/setter.
我可以将它用于 Xcore 模型吗?我不确定步骤 #2 在 Xcore 中是否可行,或者它是否支持地图。
对我来说这行得通。
DataPoints.xcore
:
...
class KeyValuePair wraps java.util.Map$Entry {
String key
String value
}
class KeyValueList {
contains KeyValuePair[] entries
}
上面的结果是 KeyValueListImpl
class 和 getEntries
方法,如下所示:
public EMap<String, String> getEntries() {
if (entries == null) {
entries = new EcoreEMap<String,String>(DataPointsPackage.Literals.KEY_VALUE_PAIR, KeyValuePairImpl.class, this, DataPointsPackage.KEY_VALUE_LIST__ENTRIES);
}
return entries;
}
根据EMF FAQ,可以在EMF中创建一个Map:
An EMap is basically a List of java.util.Map$Entry instances. Therefore to create a Map you need to first model your map entry by following these steps:
- Create an EClass with the name [Type1]To[Type2]Map where [Type1] represents the key's type and the [Type2] represents the value's type.
- Set the Instance Class Name property of the newly created EClass to java.util.Map$Entry.
- Create an EAttribute or EReference named "key" and set the EDataType or EClass for it.
- Create an EAttribute or EReference called "value" and set the EDataType or EClass for it.
Now, when you create an EReference somewhere that uses this map entry class as its EClass, the EMF code generator will detect this special case and generate a properly typed EMap getter/setter for you instead of a normal EList getter/setter.
我可以将它用于 Xcore 模型吗?我不确定步骤 #2 在 Xcore 中是否可行,或者它是否支持地图。
对我来说这行得通。
DataPoints.xcore
:
...
class KeyValuePair wraps java.util.Map$Entry {
String key
String value
}
class KeyValueList {
contains KeyValuePair[] entries
}
上面的结果是 KeyValueListImpl
class 和 getEntries
方法,如下所示:
public EMap<String, String> getEntries() {
if (entries == null) {
entries = new EcoreEMap<String,String>(DataPointsPackage.Literals.KEY_VALUE_PAIR, KeyValuePairImpl.class, this, DataPointsPackage.KEY_VALUE_LIST__ENTRIES);
}
return entries;
}