Diagnostician Validation only enforce unique Ids after saving the model [message #1859213] |
Fri, 19 May 2023 22:12 |
Maxens Destiné Messages: 3 Registered: May 2023 |
Junior Member |
|
|
I have a meta model and I want to create and validate a model programmatically. I was verifying if marking the "name" attribute as id would prevent 2 groups with the same name from being stored in the model and it works but only after saving the model (it doesn't matter when I save the model, only as long as it's before validating).
Tournament tournament = TournamentFactory.eINSTANCE.createTournament();
tournament.setName("FIFA World Cup 2014");
//ResourceHelper.INSTANCE.saveResource(tournament, "examples/WorldCup2014.tournament");
Group groupG = TournamentFactory.eINSTANCE.createGroup();
groupG.setName("Group G");
Group groupA = TournamentFactory.eINSTANCE.createGroup();
groupA.setName("Group G");
validate(tournament);
When running the above, I get no validation errors but if I uncomment the line where the empty model is saved, I get:
Validation Error for: com.mattsch.emf.examples.tournament.impl.TournamentImpl@68c72235 (name: FIFA World Cup 2014)
[com.mattsch.emf.examples.tournament.impl.GroupImpl@4738a206 (name: Group G), com.mattsch.emf.examples.tournament.impl.GroupImpl@66d3eec0 (name: Group G), Group G]
The ID 'Group G' of 'com.mattsch.emf.examples.tournament.impl.GroupImpl@4738a206{examples/WorldCup2014.tournament#Group G}' collides with that of 'com.mattsch.emf.examples.tournament.impl.GroupImpl@66d3eec0{examples/WorldCup2014.tournament#Group G}'
I don't know why it only flags the error after saving the model and I would like it to be flagged without having to save the model. Is that possible?
The "name" attribute is marked as ordered, unique, changeable and ID in the class diagram. Here is the save() method as reference:
public void saveResource(EObject model, String file) {
Resource resource = resourceSet.createResource(URI.createFileURI(file));
resource.getContents().add(model);
try {
resource.save(Collections.EMPTY_MAP);
} catch (IOException e) {
System.err.println("Error saving model: " + e.getLocalizedMessage());
}
}
Thank you in advance :)
|
|
|
Re: Diagnostician Validation only enforce unique Ids after saving the model [message #1859217 is a reply to message #1859213] |
Sat, 20 May 2023 04:51 |
Ed Merks Messages: 32985 Registered: July 2009 |
Senior Member |
|
|
It's not only validated after saving, it's validated by org.eclipse.emf.ecore.util.EObjectValidator.validate_UniqueID(EObject, DiagnosticChain, Map<Object, Object>) that it's unique within in the scope of the containing resource: public boolean validate_UniqueID(EObject eObject, DiagnosticChain diagnostics, Map<Object, Object> context)
{
boolean result = true;
String id = EcoreUtil.getID(eObject);
if (id != null)
{
Resource resource = eObject.eResource();
if (resource != null)
{
EObject otherEObject = resource.getEObject(id);
if (eObject != otherEObject && otherEObject != null)
{
result = false;
if (diagnostics != null)
{
diagnostics.add
(createDiagnostic
(Diagnostic.ERROR,
DIAGNOSTIC_SOURCE,
EOBJECT__UNIQUE_ID,
"_UI_DuplicateID_diagnostic",
new Object []
{
id,
getObjectLabel(eObject, context),
getObjectLabel(otherEObject, context)
},
new Object [] { eObject, otherEObject, id },
context));
}
}
}
}
return result;
}
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02154 seconds