2013-05-30 145 views
1

我在Xtext中有下面的DSL: 我想驗證,如果ObjectB有元素,那包含的對象(ObjectA)沒有元素。我得到的警告到對象B,但不反對A.子對象的xtext驗證

Domainmodel: 
    ObjectA | ObjectB 
    ; 

ObjectB: 
    'ObjectB' 
    '{' 
    (element = Element)? 
    (objects += ObjectA)* 
    '}' 
; 

ObjectA: 
'ObjectA' 
    '{' 
    (element = Element)? 
    '}' 

; 

Element: 
    'Element' name=ID 
; 

我想就像休耕也是對象A警告:

@check 
def ObjectinObject(ObjectB object) 
{ 
    if(object.element != null) 
    { 
    for (ObjectA e : object.objects) 
    { 
     if(e.element != null) 
       {//The fallowing Code will make Warning at the element and the subelement 
       warning('warning', DomainmodelPackage$Literals::DOMAINMODEL__ELEMENT) 
       warning('warning2',e.element ,DomainmodelPackage$Literals::ELEMENT__NAME) 
       } 
    } 
    } 
} 

回答

3

有幾個「羣」爲warningerrorinfo 。一個組在參數列表中有EObject,其他組沒有。

您已經在使用而不是。在這種情況下,該消息附加到EObject這是檢查方法的參數。

所以爲了給附加信息任何隨機EObject你必須使用一個方法與EObject參數。你的情況:

protected void warning(String message, EObject source, EStructuralFeature feature); 

,並在行動:

warning('warning', e, DomainmodelPackage$Literals::OBJECT_A__OBJECTS) 

的消息,該方法第二組只因爲的Xtext 2.4提供。如果你碰巧使用的是舊版本,你可以嘗試這個節(在Java中,請自己採用Xtend語法):

getMessageAcceptor().acceptWarning('warning', e, 
    DomainmodelPackage$Literals::OBJECT_A__OBJECTS, -1, 
    null); 
+0

謝謝,幫了我很多。我嘗試了一下,並使其工作。我沒有OBJECT_A__OBJECTS作爲文字,只是Object_B__OBJECTS,但也沒有工作。它實際上是:warning('warning2',e.element,DomainmodelPackage $ Literals :: ELEMENT__NAME) – Iron