Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Validator(Validator to avoid repetition)
Validator [message #1844281] Tue, 07 September 2021 21:31 Go to next message
Eclipse UserFriend
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.
Re: Validator [message #1844282 is a reply to message #1844281] Wed, 08 September 2021 01:06 Go to previous messageGo to next message
Eclipse UserFriend
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(.....);
..

[Updated on: Wed, 08 September 2021 01:52] by Moderator

Re: Validator [message #1844297 is a reply to message #1844282] Wed, 08 September 2021 07:24 Go to previous messageGo to next message
Eclipse UserFriend
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);
}

}
}

[Updated on: Wed, 08 September 2021 07:37] by Moderator

Re: Validator [message #1844302 is a reply to message #1844297] Wed, 08 September 2021 09:28 Go to previous messageGo to next message
Eclipse UserFriend
@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?

[Updated on: Wed, 08 September 2021 09:29] by Moderator

Re: Validator [message #1844316 is a reply to message #1844302] Wed, 08 September 2021 14:02 Go to previous message
Eclipse UserFriend
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
Previous Topic:Cannot Reference One Xcore Model from Another
Next Topic:No Xtext languages have been registered
Goto Forum:
  


Current Time: Thu Apr 17 13:29:21 EDT 2025

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

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

Back to the top