在此处查看涵盖您案例的截屏视频:http: //xtextcasts.org/episodes/18-model-optimization
您需要修改模型和元模型。要修改元模型,您需要定义一个后处理器。以下后处理器将type
属性目录添加到Variable
类中。
有关详细信息,请参阅:http ://christiandietrich.wordpress.com/tag/postprocessor/
class MyXtext2EcorePostProcessor implements IXtext2EcorePostProcessor {
override process(GeneratedMetamodel metamodel) {
metamodel.EPackage.process
}
def process(EPackage p) {
for (clazz : p.EClassifiers.filter(typeof(EClass))) {
if (clazz.name == typeof(Variable).simpleName) {
val typeAttribute = EcoreFactory::eINSTANCE.createEAttribute
typeAttribute.name = "type"
typeAttribute.EType = EcorePackage::eINSTANCE.EString
clazz.EStructuralFeatures += typeAttribute
}
}
}
}
然后你必须像这样绑定它Generator
:
public class ExtendedGenerator extends Generator {
public ExtendedGenerator() {
new XtextStandaloneSetup() {
@Override
public Injector createInjector() {
return Guice.createInjector(new XtextRuntimeModule() {
@Override
public Class<? extends IXtext2EcorePostProcessor>
bindIXtext2EcorePostProcessor() {
return MyXtext2EcorePostProcessor.class;
}
});
}
}.createInjectorAndDoEMFRegistration();
}
}
最后ExtendedGenerator
在你的 mwe2-workflow 中使用新的:
...
Workflow {
...
bean = StandaloneSetup {
...
component = postprocessor.ExtendedGenerator { // Set ExtendedGenerator!
...
}
...
}
...
}
...
然后你必须type
用数据填充你的新属性。您可以实现IDerivedStateComputer
接口。
class MyDerivedStateComputer implements IDerivedStateComputer {
override discardDerivedState(DerivedStateAwareResource resource) {
resource.allContents.filter(typeof(VariableDefinition)).forEach [
type = null
]
}
override installDerivedState(DerivedStateAwareResource resource,
boolean preLinkingPhase) {
resource.allContents.filter(typeof(VariableDefinition)).forEach [
type = (eContainer as DefinitionBlock).type
]
}
}
然后你必须像这样绑定它(第二个和第三个绑定方法只对非 Xbase 项目是必需的)
public class MyDslRuntimeModule extends AbstractMyDslRuntimeModule {
public Class<? extends IDerivedStateComputer> bindIDerivedStateComputer() {
return MyDerivedStateComputer.class;
}
// Not needed for Xbase-projects
@Override
public Class<? extends XtextResource> bindXtextResource() {
return DerivedStateAwareResource.class;
}
// Not needed for Xbase-projects
public Class<? extends IResourceDescription.Manager>
bindIResourceDescriptionManager() {
return DerivedStateAwareResourceDescriptionManager.class;
}
}
您可以走得更远,动态重组模型以满足您的需求。请在此处发布您的解决方案作为单独的答案,以防您走得太远。