Xtext:在XExpression中使用EClass

时间:2017-08-20 17:30:42

标签: xtext ecore xbase

我正在撰写 Xtext 语法,该语法使用 XExpressions ,并且还在 Eclasses 上运行。现在我还希望能够从 XExpression 访问 Eclasses ,例如我写一个这样的表达式:

const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connection.openUri('mongodb://127.0.0.1:27017/app_db', { /* options */ });

我想知道,如何在 XExpression 中使用 Eclass

语法

var article = new Article(Obj);
article.save().then(function(result) {
    return res.status(201).json({
        message: 'Saved message',
        obj: result
    });
}, function (err) {
    if (err) {
        return res.status(500).json({
            title: 'Ac error occurred',
            error: err
        });
    }
});

Inferrer / Infer方法

Eclass1.attribute1 = Eclass2.attribute1

RuntimeModule

grammar org.xtext.example.mydsl.Mydsl with 
org.eclipse.xtext.xbase.Xbase

import "http://www.eclipse.org/emf/2002/Ecore" as ecore

generate mydsl "http://www.xtext.org/example/mydsl/Mydsl"

Model:
(operations += Operation)*;

terminal ATTR : ID ('.' ID)+;

Operation:
'operation' left=[ecore::EClass|ATTR] 'and' right=
 [ecore::EClass|ATTR] 'defined' 'as' condition=XExpression
;

MyImportFeature

def dispatch void infer(Model element, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
      acceptor.accept(element.toClass("example.mydsl")) [
        for (operation : element.operations) {
            left = operation.left
            right = operation.right
            if (left.eIsProxy()) {
                left = EcoreUtil.resolve(left, operation) as EClass
            }   
            if (right.eIsProxy()) {
                right = EcoreUtil.resolve(right, operation) as EClass
            }
            //field for right class left out, but works the same
            members += left.toField(left.name,typeRef(left.EPackage.name+"."+left.name))    
            members += operation.toMethod("conditionExpr", 
            typeRef(Void.TYPE)) [
                body = operation.condition
            ]       
        }
    ]

}

1 个答案:

答案 0 :(得分:0)

我不确定我是否收到你的问题。 通常,EMF会为EAttributes生成常量,因此如果您想要访问自己的属性 所以你可以做到 MyDslPackage.Literals.GREETING__NAME 要么 MyDslPackage.eINSTANCE.getGreeting_Name()

你能否提供一些关于你真正想要做什么的提示

update:这是一个关于如何从对eclass的引用中获取java类的片段

Thingy:{
    val EClass eclazz = f.clazz
    val uri = EcorePlugin.getEPackageNsURIToGenModelLocationMap(true).get(eclazz.EPackage.nsURI)
    val rs = new ResourceSetImpl
    val r = rs.getResource(uri, true)
    r.load(null)
    val p = r.contents.head
    if (p instanceof GenModel) {
        val genClass = p.findGenClassifier(eclazz)
        if (genClass instanceof GenClass) {
            println(genClass.qualifiedInterfaceName)
            members+=f.toField(eclazz.name, genClass.qualifiedInterfaceName.typeRef)
        }
    }
}