Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Making validator work(xtext validator)
Making validator work [message #1057838] Thu, 09 May 2013 08:26 Go to next message
chris yo is currently offline chris yoFriend
Messages: 146
Registered: February 2013
Senior Member
I have a grammar like this:

Task_info:
 'TASK' 'INFO' name=ID ';'


So, the user can configure a couple of task information.
E.g.
TASK INFO explorer.exe;
TASK INFO winword.exe;
TASK INFO excel.exe;


Then I added a validator check for duplicate task names.

In my JavaValidator.java:
@Check
public void checktask(Task_info xtask)
{
 Task_Checker tsk = new Task_Checker();
 tsk.check_dupe(xtask);
}


Then I created a new file called Task_Checker.xtend which contain this class:

class Task_Checker {
 def check_dupe(Task_info xtask)
 {
  System.out.println("size is" + xtask.size); <-- I am always getting zero here. I was expecting 3 task names.
 }
}


Anyone have any idea how to get the list of defined items?

Re: Making validator work [message #1057872 is a reply to message #1057838] Thu, 09 May 2013 13:20 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

am not quite sure on your semantics (scope of uniqueness), since you posted only a (partial) grammar

if you want to check on a certain context you can walk up/down the EObjects Containment hierarchy.

E.g. MyParent p = (MyParent) x.eContainer();
List<Stuff> stuffs = p.getStuffs();

alternatively to check globally have a look at this thread

http://www.eclipse.org/forums/index.php/mv/msg/267004/766678/#msg_766678


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Making validator work [message #1058173 is a reply to message #1057872] Mon, 13 May 2013 03:51 Go to previous messageGo to next message
chris yo is currently offline chris yoFriend
Messages: 146
Registered: February 2013
Senior Member
Hi Christian,

Thank you for the reply.

Basically, I want to check that all task names are unique.

So, the user can have:
TASK INFO explorer.exe;
TASK INFO winword.exe;
TASK INFO excel.exe;

But, the user should NOT have
TASK INFO excel.exe;
TASK INFO winword.exe;
TASK INFO excel.exe;

Task_Checker::check_dupe should be able to check if there are duplicate task names. Initially, I created a for loop but I always get an error and later on, I found out its because size is 0.

class Task_Checker {
 def check_dupe(Task_info xtask)
 {
  val int iSize = xtask.size
  for (i:0..iSize-1)
  {
   for (j:i+1..iSize)
   {
    if (xtask.get(i).name.equals(xtask.get(j).name))
     error(duplicate task)
   }
  }
 }
}


Is there another way to do it in xtend?
Re: Making validator work [message #1058181 is a reply to message #1058173] Mon, 13 May 2013 06:17 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Please share all relevant grammar

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Making validator work [message #1058193 is a reply to message #1058181] Mon, 13 May 2013 07:43 Go to previous messageGo to next message
chris yo is currently offline chris yoFriend
Messages: 146
Registered: February 2013
Senior Member
Grammar:
Task_grammar:
 'TASK' (Task_define | Task_use)
;

Task_define:
 'INFO' taskname=ID ';'
;

Task_use:
  'USE' taskname=ID PRIORITY=INT (save = 'SAVE')? ';'
;


I want to run the validator such that (1) it will check if all tasknames in Task_define grammar are unique, and (2) it will check if all tasknames in Task_use grammar is defined in Task_define.

Is this possible to do in the validator? Or should this be done in the generator?
Re: Making validator work [message #1058205 is a reply to message #1058193] Mon, 13 May 2013 08:32 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

this makes no sense to me, where does the multiplicty come from ?????

Task_grammar:
'TASK' (defs+=Task_define | use+=Task_use)*
;

then do the check on Task_grammar and ask it for its defs.

btw you may simple switch on unique validation in the workflow.


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Making validator work [message #1058439 is a reply to message #1058205] Tue, 14 May 2013 06:37 Go to previous messageGo to next message
chris yo is currently offline chris yoFriend
Messages: 146
Registered: February 2013
Senior Member
Hi,

I am confused with your proposed grammar.

Task_grammar:
'TASK' (defs+=Task_define | use+=Task_use)*
;

This grammar means that these will be valid:

TASK INFO explorer.exe INFO win.exe;

Is my assumption correct?

I want the user to be able to do it like this:
TASK INFO explorer.exe;
TASK INFO word.exe;



Re: Making validator work [message #1058446 is a reply to message #1058439] Tue, 14 May 2013 07:18 Go to previous messageGo to next message
Claudio Heeg is currently offline Claudio HeegFriend
Messages: 75
Registered: April 2013
Member
I reckon Christian was irritated because the root/start element of your grammar has been missing?
That snippet you've shown only applies to one specific task. Task_grammar's super element would probably contain the list of tasks?

[Updated on: Tue, 14 May 2013 07:19]

Report message to a moderator

Re: Making validator work [message #1058460 is a reply to message #1058446] Tue, 14 May 2013 08:42 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Yes,

it was just a dummy.
just use the container as start.


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Making validator work [message #1058472 is a reply to message #1058460] Tue, 14 May 2013 09:48 Go to previous messageGo to next message
chris yo is currently offline chris yoFriend
Messages: 146
Registered: February 2013
Senior Member
Hmm. This is the whole grammar I have.. Sorry, I am new to xtext.. I might be doing this wrong..

Model += Task_grammar*

Task_grammar:
'TASK' (defs+=Task_define | use+=Task_use)*
;

Task_define:
 'INFO' taskname=ID ';'
;

Task_use:
  'USE' taskname=ID PRIORITY=INT (save = 'SAVE')? ';'
;


for my validator:
	def void check(Task_grammar x)
	{
		var EList<Task_define> y = x.defs;
		System.out.println("TEST Size is " + y.size);
	}


How will I get all the defined tasks? y.size returns either 1 (if its defs) or 0 (if its use)...
Re: Making validator work [message #1058483 is a reply to message #1058472] Tue, 14 May 2013 10:53 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
def void check(Model x)
!!!!!!!

or
def void check(Task_grammar x)
{

val m = x.Econtainer as Model


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Making validator work [message #1058532 is a reply to message #1058483] Tue, 14 May 2013 12:13 Go to previous message
Ian McDevitt is currently offline Ian McDevittFriend
Messages: 70
Registered: December 2012
Location: Belfast
Member
Firstly your "whole grammar" has syntax errors so maybe you're not supplying exactly what you have?
Model += Task_grammar*

this isn't actually a rule. It should be something like
Model: tasks += Task_grammar*
;


Also your names can't currently be filenames like explorer.exe
taskname=ID 

ID will not allow a dot in the name so I assume you're actually using a rule like taskname=TASKNAME and
TASKNAME: ID '.' ID ;

in it.

After that you may be able to solve your original problem with the builtin uniqueness validation for 'names' by uncommenting it in your workflow file (.mwe2).
      composedCheck = "org.eclipse.xtext.validation.NamesAreUniqueValidator"

This checks references called 'name' so you just need to use name=TASKNAME instead of taskname=TASKNAME

Don't forget to regenerate the language after such changes.

[Updated on: Tue, 14 May 2013 12:37]

Report message to a moderator

Previous Topic:Common applicable quick fix
Next Topic:NoClassDefFoundError -&gt; JavaModelException running Xtext without JDT.core
Goto Forum:
  


Current Time: Fri Apr 19 16:31:38 GMT 2024

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

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

Back to the top