Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Validating x,y coordinates(Having issues figuring out how to validate x and y coordinates.)
Validating x,y coordinates [message #1787047] Thu, 17 May 2018 17:48 Go to next message
Jack Lee is currently offline Jack LeeFriend
Messages: 3
Registered: May 2018
Junior Member
Hello, I am working on an assignment and have a slight issue understanding something about validation and the grammar.

Currently this is my grammar (only showing relevant parts, ... is omitted code):

Game:
    'Game' name=ID grid=GridSize ...
;

GridSize:
   'Grid' 'size' size=Coordinates
;

Coordinates:
'('x=INT','y=INT')'
;
...


And this is my validation definition:
@Check
    def void checkGridCoordinates(Game game) {
    	val minValue = 1
    	val maxValue = 10
    	var x = game.grid.size.x
    	var y = game.grid.size.y
    	var literal = MetaGameLanguagePackage.Literals.GRID_SIZE__SIZE
    	
    	if(x < minValue || y < minValue){
    		error("Grid size must be at least 1.",literal)	
    	} else if(x > maxValue || y > maxValue) {
    		this.error("Grid size must be 10 or under.", literal)
    	}
    }


I get the following error, which happens if I try to set the grid x and y to be either 0 or under or above 10 (which is expected but I want it to display the error message and not fail to validate):
Description	Resource	Path	Location	Type
Error executing EValidator	TestingFile.gl	/IndividualExtension/src	/IndividualExtension/src/TestingFile.gl	GameLang Problem


Since this is an assignment I would appreciate it if you could hint me in a direction in which I can experiment, instead of handing me a solution :)
Re: Validating x,y coordinates [message #1787090 is a reply to message #1787047] Fri, 18 May 2018 16:30 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
first of all you should write a unit test for your validator
the classes parsehelper and validationtesthelper will help

@RunWith(XtextRunner)
@InjectWith(MyDslInjectorProvider)
class MyDslParsingTest {
	@Inject
	ParseHelper<Game> parseHelper
	
	@Inject
	extension ValidationTestHelper
	
	@Test
	def void loadModel() {
		val result = parseHelper.parse('''
			Game HelloWorld Grid size (1,11)
		''')
		Assert.assertNotNull(result)
		val errors = result.eResource.errors
		Assert.assertTrue('''Unexpected errors: «errors.join(", ")»''', errors.isEmpty)
		result.assertNoErrors // Triggers validation
	}
}


this will give you an actual stack trace (you would find it in the eclipse runtime error log too)
the stacktrace should hint you to your problem


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Validating x,y coordinates [message #1787114 is a reply to message #1787090] Sat, 19 May 2018 04:05 Go to previous messageGo to next message
Jack Lee is currently offline Jack LeeFriend
Messages: 3
Registered: May 2018
Junior Member
Thank you very much! Turns out the issue was I was in the wrong context and thus the feature was not being exposed. However as you can see in my below code I had to use eINSTANCE and not
MetaGameLanguagePackage.Literals.GRID_SIZE__SIZE
. I have yet to understand why Literals didn't do the job.

@Check
def void checkGridCoordinates(GridSize grid) {
    	// Variables
    	var minValue = 1
    	var maxValue = 10
    	var x = grid.size.x
    	var y = grid.size.y
    	var gridSize = MetaGameLanguagePackage.eINSTANCE.gridSize_Size
    	
    	if(x < minValue || y < minValue){
    		error("Grid size must be at least 1.", gridSize)	
    	} else if(x > maxValue || y > maxValue) {
			error("Grid size must be 10 or under.", gridSize)
    	}
    }
Re: Validating x,y coordinates [message #1787115 is a reply to message #1787090] Sat, 19 May 2018 04:06 Go to previous messageGo to next message
Jack Lee is currently offline Jack LeeFriend
Messages: 3
Registered: May 2018
Junior Member
Also thank you for the unit test idea.

[Updated on: Sat, 19 May 2018 04:07]

Report message to a moderator

Re: Validating x,y coordinates [message #1787116 is a reply to message #1787115] Sat, 19 May 2018 05:14 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
You can also have a look at the overloaded error methods that allow you to pass a explicit context

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:How do I re-import a maven Xtext project?
Next Topic:Generated code
Goto Forum:
  


Current Time: Wed Apr 24 16:18:58 GMT 2024

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

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

Back to the top