Skip to main content



      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 15:31 Go to next message
Eclipse UserFriend
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 00:24 Go to previous messageGo to next message
Eclipse UserFriend
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 04:00 Go to previous messageGo to next message
Eclipse UserFriend
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

[Updated on: Thu, 04 March 2021 04:01] by Moderator

Re: Begginer on Xtext Validation [message #1838734 is a reply to message #1838732] Thu, 04 March 2021 04:06 Go to previous messageGo to next message
Eclipse UserFriend
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 13:00 Go to previous message
Eclipse UserFriend
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: Wed Jul 23 15:17:43 EDT 2025

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

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

Back to the top