Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Begginer on Xtext Validation(How to check if a class was referenced)
Begginer on Xtext Validation [message #1838700] Wed, 03 March 2021 20:31 Go to next message
lili Mising name is currently offline lili Mising nameFriend
Messages: 8
Registered: July 2011
Junior Member
Hello,
I searched and did not found a reference..so
I have a grammar where with cross-references and there are two entities that relate to each other. Something like this..

Domainmodel:
(elements+=Type)*;

Type:
DataType | Entity;

DataType:
'datatype' name=ID;

Input:
'Input' name = EID ('{' (
('default' defaultValue = STRING)?
('label' label = [Label])?
'}';

Label:
'Label' name = EID '{' (
'text' text = STRING &
('labelFor' labelFor = [Entity])?

) '}'
;

I want to write a validation rule to input objects that verify if an Input has been referenced by a Label or references by some label. If not, show a warning, to check on input if there is a label is easy. But how can I check if there is a label that references it?


Thanks for your support.


Re: Begginer on Xtext Validation [message #1838715 is a reply to message #1838700] Thu, 04 March 2021 05:24 Go to previous messageGo to next message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 747
Registered: July 2009
Location: Dortmund, Germany
Senior Member

You'll need to find that through the Xtext index when the Labels that reference an input could potentially be defined in another file.

Inject an IResourceDescriptionsProvider. This grants you access to the right IResourceDescriptions instance (AKA the 'index').

From there iterate over getAllResourceDescriptions() and for each IResourceDescription loop over getReferenceDescriptions().

For an IReferenceDescription collect the target references. If you don't find any where the target is a given Label then its is unreferenced.

This is a costly validation, you should mark the Check with CheckType NORMAL, otherwise it would run with each edit.
Re: Begginer on Xtext Validation [message #1838732 is a reply to message #1838715] Thu, 04 March 2021 09:00 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13939
Registered: July 2009
Senior Member
this usecase is the most advanced usecase you can find at least when you use more than one file (which this answer and karstens assume)

please note: getReferenceDescriptions wont work during build as they may not be available yet
thus you would have to store the names of the Entity in the user data of index entry of the Label (retriving the text using NodeModelUtils) via DefaultResourceDescriptionStrategy.
and then query the index in the validation and check if there is an index entry which index entries contain the Entity and if none error it.

the approach karsten proposed works in an expensive validation only and not in a not in CheckType FAST or CheckTYPE NORMAL


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Thu, 04 March 2021 09:01]

Report message to a moderator

Re: Begginer on Xtext Validation [message #1838734 is a reply to message #1838732] Thu, 04 March 2021 09:06 Go to previous messageGo to next message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 747
Registered: July 2009
Location: Dortmund, Germany
Senior Member

Christian's approach is the better one. Please follow his advice.
Re: Begginer on Xtext Validation [message #1838854 is a reply to message #1838734] Sat, 06 March 2021 18:00 Go to previous message
lili Mising name is currently offline lili Mising nameFriend
Messages: 8
Registered: July 2011
Junior Member
Thanks for your support guys!
I really tried to follow your approaches, but I have not a positive result yet. I´m testing some examples, and your answers give me directions. It is really tuff because I never did validations before and still don't have the result I need, but I think I am close.
Previous Topic:Mismatched character
Next Topic:Alternatives can never be matched
Goto Forum:
  


Current Time: Fri May 14 11:25:02 GMT 2021

Powered by FUDForum. Page generated in 0.02203 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top