Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Validator(Validator to avoid repetition)
Validator [message #1844281] Wed, 08 September 2021 01:31 Go to next message
Muhammad Salman Tayyab is currently offline Muhammad Salman TayyabFriend
Messages: 31
Registered: March 2020
Location: Germany
Member
Hi, I am writing a validator to avoid repetition.
Variable names should be unique.

In the grammar screenshot (attached below), the "videos" variable should always be unique.
In the other screenshot, I tried to write the custom validation rule for it. Please see the validation file screenshot and guide me, what I have to do. I am new to Xtext and confused about how to write the correct code for it. Thank you.


Salman
Re: Validator [message #1844282 is a reply to message #1844281] Wed, 08 September 2021 05:06 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14111
Registered: July 2009
Senior Member
the snippet you posted non proper java code in a java files.
you have to write the check in pure java then.
e.g.

video.getVideos()
and have a proper java map (or set) decl

e.g. Set<String> existing = new HashSet<>();
if (existing.add(v.getName())) {
error(.....);
..


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: Wed, 08 September 2021 05:52]

Report message to a moderator

Re: Validator [message #1844297 is a reply to message #1844282] Wed, 08 September 2021 11:24 Go to previous messageGo to next message
Muhammad Salman Tayyab is currently offline Muhammad Salman TayyabFriend
Messages: 31
Registered: March 2020
Location: Germany
Member
Do you mean that I have to write this repetition check in the dslgenerator.xtend file?

Below I have modified the validation file. Please have a look and guide me because I am confused in this.

@Check
public void checkDuplicateVarName(Video video) {
///var map = new HashMap <String, Video >
Set<String> existing = new HashSet<>();
for(Videodef v: video.getVideos()) {
if(!existing.add(v.getName())) {
error("Variable Name should be unique",
VideoDslPackage.Literals.VIDEO__VIDEOS,
DUPLICATE_VAR);
}

}
}


Salman

[Updated on: Wed, 08 September 2021 11:37]

Report message to a moderator

Re: Validator [message #1844302 is a reply to message #1844297] Wed, 08 September 2021 13:28 Go to previous messageGo to next message
Muhammad Salman Tayyab is currently offline Muhammad Salman TayyabFriend
Messages: 31
Registered: March 2020
Location: Germany
Member
@Check
public void checkDuplicateVarName(Video video) {
///var map = new HashMap <String, Video >
Set<String> existing = new HashSet<>();
for(Videodef v: video.getVideos()) {
if(!existing.add(v.getName())) {
error("Variable Name should be unique",
VideoDslPackage.Literals.VIDEO__VIDEOS,
DUPLICATE_VAR);
}
}
}


Hello, From the above code, error always appears on the first line.
How can I add the error message to the line on which we are writing the duplicate variable?


Salman

[Updated on: Wed, 08 September 2021 13:29]

Report message to a moderator

Re: Validator [message #1844316 is a reply to message #1844302] Wed, 08 September 2021 18:02 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14111
Registered: July 2009
Senior Member
You can call the overloads of
Error that also take an eobject and estructuralfesture

You can pass v as eobject
And YouOackage.literals.videodef_name as feature


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
Previous Topic:Cannot Reference One Xcore Model from Another
Next Topic:No Xtext languages have been registered
Goto Forum:
  


Current Time: Thu Jan 20 22:21:12 GMT 2022

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

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

Back to the top