Java 为什么';链接在我基于Xtext的DSL中是否有效?
下面是我的DSL的Xtext语法Java 为什么';链接在我基于Xtext的DSL中是否有效?,java,eclipse,dsl,eclipse-emf,xtext,Java,Eclipse,Dsl,Eclipse Emf,Xtext,下面是我的DSL的Xtext语法 Model: variableTypes=VariableTypes predicateTypes=PredicateTypes variableDeclarations= VariableDeclarations rules=Rules; VariableType: name=ID; VariableTypes: 'var types' (variableTypes+=VariableType)+; PredicateTypes: '
Model:
variableTypes=VariableTypes predicateTypes=PredicateTypes variableDeclarations=
VariableDeclarations rules=Rules;
VariableType:
name=ID;
VariableTypes:
'var types' (variableTypes+=VariableType)+;
PredicateTypes:
'predicate types' (predicateTypes+=PredicateType)+;
PredicateType:
name=ID '(' (variableTypes+=[VariableType|ID])+ ')';
VariableDeclarations:
'vars' (variableDeclarations+=VariableDeclaration)+;
VariableDeclaration:
name=ID ':' type=[VariableType|ID];
Rules:
'rules' (rules+=Rule)+;
Rule:
head=Head ':-' body=Body;
Head:
predicate=Predicate;
Body:
(predicates+=Predicate)+;
Predicate:
predicateType=[PredicateType|ID] '(' (terms+=Term)+ ')';
Term:
variable=Variable;
Variable:
variableDeclaration=[VariableDeclaration|ID];
terminal WS:
(' ' | '\t' | '\r' | '\n' | ',')+;
并且,以下是上述DSL中的程序
var types
Node
predicate types
Edge(Node, Node)
Path(Node, Node)
vars
x : Node
y : Node
z : Node
rules
Path(x, y) :- Edge(x, y)
Path(x, y) :- Path(x, z) Path(z, y)
下面是生成的开关
类的子类,该类演示了getPredicateType()
在谓词
节点上返回null
public class ModelPrinter extends MyDSLSwitch<Object> {
protected Object visitChildren(EObject object) {
for (EObject eobj : object.eContents()) {
doSwitch(eobj);
}
return object;
}
@Override
public Object casePredicate(Predicate object) {
System.out.println(object.getPredicateType());
return object;
}
@Override
public Object defaultCase(EObject object) {
return visitChildren(object);
}
}
公共类ModelPrinter扩展了MyDSLSwitch{
受保护对象访问子对象(EOObject对象){
for(EObject-eobj:object.eContents()){
doSwitch(eobj);
}
返回对象;
}
@凌驾
公共对象casePredicate(谓词对象){
System.out.println(object.getPredicateType());
返回对象;
}
@凌驾
公共对象defaultCase(EOObject对象){
返回访问儿童(对象);
}
}
当我使用
ModelPrinter
类遍历与上述程序对应的EMF对象模型时,我意识到节点没有正确地链接在一起。例如,谓词
节点上的getPredicateType()
方法返回null
。阅读了Xtext用户指南后,我的印象是Xtext默认链接语义应该适用于我的DSL。但是,由于某些原因,我的DSL的AST节点没有正确地连接在一起。有人能帮我诊断这个问题吗 我已经试过了,但我不熟悉这个开关,而是使用Xpand/Xtend从谓词访问谓词类型并生成它们的名称
Template.xpt:
«IMPORT myDsl»;
«DEFINE main FOR Model-»
«FILE "output.txt"-»
«FOREACH this.rules.rules.body.last().predicates AS p-»
«p.predicateType.name»
«ENDFOREACH-»
«ENDFILE-»
«ENDDEFINE»
«导入myDsl»;
«为模型定义主节点-»
«文件“output.txt”-»
«FOREACH this.rules.rules.body.last().谓词为p-»
«p.predicateType.name»
«ENDFOREACH-»
«结束文件-»
«ENDDEFINE»
以及output.txt:
Path
Path
路径
路径
我想这是预期的行为。我已经尝试过了,但我不熟悉开关,而是使用Xpand/Xtend从谓词访问谓词类型并生成它们的名称 Template.xpt: «IMPORT myDsl»; «DEFINE main FOR Model-» «FILE "output.txt"-» «FOREACH this.rules.rules.body.last().predicates AS p-» «p.predicateType.name» «ENDFOREACH-» «ENDFILE-» «ENDDEFINE» «导入myDsl»; «为模型定义主节点-» «文件“output.txt”-» «FOREACH this.rules.rules.body.last().谓词为p-» «p.predicateType.name» «ENDFOREACH-» «结束文件-» «ENDDEFINE» 以及output.txt: Path Path 路径 路径
我猜这是预期的行为。最后,我解决了问题。链接设置不正确,因为我没有正确加载模型。我刚刚使用了解析器来加载模型。所以,我没有得到链接。因此,我从中使用了以下代码片段来正确加载模型。然后,我将返回的模型传递给我的switch类
// "workspace" is a string that contains the path to the workspace containing the DSL program.
new org.eclipse.emf.mwe.utils.StandaloneSetup().setPlatformUri(workspace);
Injector injector = new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration();
XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
// "DSLProgram" is a string that contains the path to the file of the DSL program relative to the workspace set above.
Resource resource = resourceSet.getResource(URI.createURI("platform:/resource/" + DSLProgram), true);
Model model = (Model) resource.getContents().get(0);
最后,我解决了这个问题。链接设置不正确,因为我没有正确加载模型。我刚刚使用了解析器来加载模型。所以,我没有得到链接。因此,我从中使用了以下代码片段来正确加载模型。然后,我将返回的模型传递给我的switch类
// "workspace" is a string that contains the path to the workspace containing the DSL program.
new org.eclipse.emf.mwe.utils.StandaloneSetup().setPlatformUri(workspace);
Injector injector = new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration();
XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
// "DSLProgram" is a string that contains the path to the file of the DSL program relative to the workspace set above.
Resource resource = resourceSet.getResource(URI.createURI("platform:/resource/" + DSLProgram), true);
Model model = (Model) resource.getContents().get(0);
@GabrielŠčerbák,我不想从我的DSL生成代码。相反,我想将输入DSL程序的语义模型转换为其他一些域模型。我已经用一段代码更新了我的问题,这段代码表明链接设置不正确。@重新编程正如我在回答您的另一个问题时提到的,可以使用Xtend进行模型转换(从一个模型到另一个模型,如果它们共享同一个元模型,则重新命名,只要支持该元模型…)(显然现在是Xpand的一部分.)与您通过Java的方法相比,它有一定的优势。但是,要想对您的确切问题给出一个好的答案,请听Sebastian Zarnekow的话,他是Xtext AFAIK的核心开发人员。@GabrielŠčerbák,我不想从我的DSL生成代码。相反,我想将输入DSL程序的语义模型转换为其他一些域模型。我已经准备好了用一段代码注明我的问题的日期,这段代码表明链接设置不正确。@正如我在回答你的另一个问题时提到的那样,可以使用Xtend进行模型转换(从模型到模型,如果它们共享相同的元模型,则重新命名,只要支持元模型…)(这显然是Xpand的一部分…)并且它比您通过Java的方法有一定的优势。但是,要想得到关于您确切问题的好答案,请听Sebastian Zarnekow的话,他是Xtext AFAIK的核心开发人员。