Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Xtend validation a unique MAC address value in language(I write a language to abtract a virtual machine and i would a unique attribute type)
Xtend validation a unique MAC address value in language [message #1814309] Mon, 09 September 2019 09:08 Go to next message
Andrea  Sorrentino is currently offline Andrea SorrentinoFriend
Messages: 3
Registered: September 2019
Junior Member
Dear,
I have a big problem, I am writing a language to abstract a virtual machine with all component like cpu, ram, network card etc. I would create a validation rule that allow to recognize if in the language exists equals MAC address. The code below.
Model:
	'scenario' name=ID '{' scenari+=Scenario* '}'
	
	;

Scenario:
	'asset' name=ID '{' assets+=Asset* '}' |
	'networkAsset' name=ID '{' networkassets+=networktAsset* '}'	
;

Asset:
	'name:'		names=STRING		|
	'vendor:'	vendor=STRING		|
	'version:'	version=STRING		|
	'owner:'	ownerid=STRING		|
	'value:'	valueid=STRING		| 
	HardwareAsset					|
	PhisicalInfraStructureAsset		|
	SoftwareAsset					
	;
PhisicalInfraStructureAsset:
	'phisicalInfrastructure' name=ID '{' phisicalInfrastructueModules+=phisicalInfrastructueModule* '}'
	
;

phisicalInfrastructueModule:
	'phisicalType:' 	phisicalType=PHISICALENUM	';'		|		/*wlan/eth*/
	'phisicalModel:' 	phisicalModel=STRING		';'		| 		/*realtek, intel, broadcom..*/
	'phisicalAddress:' 	phisicalAddress=RULEMAC		';'		|
	'isConnected:'		isConnected=BOOLEAN			';'		
	
;


I am interested to check this phisicalAddress in phisicalInfrastructueModule. I have read the guide on the "implementing domain-specific languages whit xtex and xtend" book by Lorenzo bettini at page 74, i search any documentation, but not fuction. I read also https://eclipse.org/Xtext/documentation/103_domainmodelnextsteps.html but nothing

This is a proof of xtend code
@Check
	def checkConnection(phisicalInfrastructueModule pmodule){
		val visitedModule = <phisicalInfrastructueModule>newHashSet()
		
		visitedModule.add(pmodule)
		var current = pmodule.phisicalAddress
		
		while(current !== null){
			if (visitedModule.contains(current)){
				error('equal MAC address, ATTENTION!', 
					ThreatarrestPackage.Literals.PHISICAL_INFRASTRUCTUE_MODULE__PHISICAL_ADDRESS
					)
				return
			}
			
		}
		
	}


Thank so much!
Re: Xtend validation a unique MAC address value in language [message #1814400 is a reply to message #1814309] Wed, 11 September 2019 03:44 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
Hi, here are a few tips / counter questions

- how does the RULEMAC look like
- you currently validate each phisicalInfrastructueModule by itself so it won't find duplicates are there is only one
- so you should start the check and thus the traversal on model and search all its modules ( you can use EcoreUtil2.getAllContentsOfType or traverse along scenarios assets etc
- there are obverloads for the error methods that take an object as well if you mark a specific object with an error
- the naming convention in Xtext is to start parser rules with an upper case letter


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtend validation a unique MAC address value in language [message #1814504 is a reply to message #1814400] Thu, 12 September 2019 12:11 Go to previous messageGo to next message
Andrea  Sorrentino is currently offline Andrea SorrentinoFriend
Messages: 3
Registered: September 2019
Junior Member
Hi, thank so much to answer. This is the code of my check
@Check
	def checkNameandTypephisicalInfrastructure(Scenario scenario){
		var check = newHashSet()
		var tutto = EcoreUtil2.getAllContentsOfType(scenario, PhisicalInfraStructureAsset)
		for (g : tutto){
			var phisicalAddress = g.phisicalInfrastructueModules.get(2).phisicalAddress
			
			if(check.add(phisicalAddress)){
				
			}else{
				error("check the mac: "+phisicalAddress+" it's equal to another", g, ThreatarrestPackage.Literals.PHISICAL_INFRA_STRUCTURE_ASSET__PHISICAL_INFRASTRUCTUE_MODULES
					)
			}
		}
	}


but in this way the error show at first line, it's wrong
https://i.ibb.co/qCL8j7G/Screenshot-2019-09-12-at-14-09-21.png
Re: Xtend validation a unique MAC address value in language [message #1814506 is a reply to message #1814504] Thu, 12 September 2019 12:33 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
would expect something like


@Check
def checkNameandTypephisicalInfrastructure(Scenario scenario) {
var check = newHashSet()
var modules = EcoreUtil2.getAllContentsOfType(scenario, phisicalInfrastructueModule)
for (module : modules) {
if (check.add(module.phisicalAddress)) {
} else {
error(
"check the mac: " + module.phisicalAddress + " it's equal to another",
module,
MyDslPackage.Literals.PHISICAL_INFRASTRUCTUE_MODULE__PHISICAL_ADDRESS
)
}
}
}


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtend validation a unique MAC address value in language [message #1814514 is a reply to message #1814506] Thu, 12 September 2019 13:35 Go to previous message
Andrea  Sorrentino is currently offline Andrea SorrentinoFriend
Messages: 3
Registered: September 2019
Junior Member
Perfect, DONE!
Thanks so much!
I attach final code
@Check
	def checkNameandTypephisicalInfrastructure(Scenario scenario){
		var check = newHashSet()
		var modules = EcoreUtil2.getAllContentsOfType(scenario, phisicalInfrastructueModule)
		for (module : modules){
			var phisicalAddress = module.phisicalAddress
			if (phisicalAddress !== null){
				if(!check.add(phisicalAddress)){
					error("check the mac: "+phisicalAddress+" it's equal to another", module, ThreatarrestPackage.Literals.PHISICAL_INFRASTRUCTUE_MODULE__PHISICAL_ADDRESS
					)
				}
			}
			
		}
	}
Previous Topic:HOWTO on separating EMF model into its own Xcore project?
Next Topic:var dependent on other var
Goto Forum:
  


Current Time: Fri Sep 13 12:26:38 GMT 2024

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

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

Back to the top