Validation 子对象的xtext验证
我在Xtext中使用了空闲DSL: 我想验证,如果ObjectB有元素,则包含对象(ObjectA)没有元素。我得到了ObjectB的警告,但没有得到ObjectA的警告Validation 子对象的xtext验证,validation,xtext,Validation,Xtext,我在Xtext中使用了空闲DSL: 我想验证,如果ObjectB有元素,则包含对象(ObjectA)没有元素。我得到了ObjectB的警告,但没有得到ObjectA的警告 Domainmodel: ObjectA | ObjectB ; ObjectB: 'ObjectB' '{' (element = Element)? (objects += ObjectA)* '}' ; ObjectA: 'ObjectA' '{' (e
Domainmodel:
ObjectA | ObjectB
;
ObjectB:
'ObjectB'
'{'
(element = Element)?
(objects += ObjectA)*
'}'
;
ObjectA:
'ObjectA'
'{'
(element = Element)?
'}'
;
Element:
'Element' name=ID
;
我想要一个警告,就像ObjectA中的fallowing一样:
@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)
}
}
}
}
有几个“组”用于警告
、错误
和信息
。一个组在参数列表中没有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中,请自行采用to-Xtend语法):
谢谢,这对我帮助很大。我试了一下,终于成功了。我没有将OBJECT\u A\u对象作为文本,只有OBJECT\u B\u对象,但这也不起作用。实际上是:warning('warning2',e.element,DomainmodelPackage$Literals::element\uu NAME)
getMessageAcceptor().acceptWarning('warning', e,
DomainmodelPackage$Literals::OBJECT_A__OBJECTS, -1,
null);