Skip to main content



      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 13:48 Go to next message
Eclipse UserFriend
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 12:30 Go to previous messageGo to next message
Eclipse UserFriend
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
Re: Validating x,y coordinates [message #1787114 is a reply to message #1787090] Sat, 19 May 2018 00:05 Go to previous messageGo to next message
Eclipse UserFriend
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 00:06 Go to previous messageGo to next message
Eclipse UserFriend
Also thank you for the unit test idea.

[Updated on: Sat, 19 May 2018 00:07] by Moderator

Re: Validating x,y coordinates [message #1787116 is a reply to message #1787115] Sat, 19 May 2018 01:14 Go to previous message
Eclipse UserFriend
You can also have a look at the overloaded error methods that allow you to pass a explicit context
Previous Topic:How do I re-import a maven Xtext project?
Next Topic:Generated code
Goto Forum:
  


Current Time: Sun Jun 15 11:26:24 EDT 2025

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

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

Back to the top