【问题标题】:xtext validation of subobjects子对象的 xtext 验证
【发布时间】:2013-05-31 05:41:40
【问题描述】:

我在 Xtext 中有闲置的 DSL: 我想验证,如果 ObjectB 有 Element,那么包含的对象(ObjectA)没有 Element。我收到了对 ObjectB 的警告,但没有收到对 Object A 的警告。

Domainmodel:
    ObjectA | ObjectB
    ;

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

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

;

 Element:
    'Element' name=ID
 ;

我也想在 ObjectA 中收到类似休耕的警告:

@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)
              }
     }
  }
}

【问题讨论】:

    标签: validation xtext


    【解决方案1】:

    warningerrorinfo 有几个“组”。一组确实在参数列表中有EObject,其他组没有。

    你已经使用了没有的那个。在这种情况下,消息会附加到EObject,这是检查方法的参数。

    因此,为了将消息附加到 any 随机 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);
    

    【讨论】:

    • 谢谢你帮了我很多。我试了一下,让它工作了。我没有 OBJECT_A__OBJECTS 作为文字,只有 Object_B__OBJECTS,但这也不起作用。实际上是:warning('warning2',e.element , DomainmodelPackage$Literals::ELEMENT__NAME)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-09
    • 1970-01-01
    相关资源
    最近更新 更多