Xtend validation a unique MAC address value in language [message #1814309] |
Mon, 09 September 2019 09:08 |
Andrea Sorrentino 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 |
|
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 #1814506 is a reply to message #1814504] |
Thu, 12 September 2019 12:33 |
|
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
|
|
|
|
Powered by
FUDForum. Page generated in 0.04524 seconds