Diagnostician Validation only enforce unique Ids after saving the model [message #1859213] |
Fri, 19 May 2023 18:12  |
Eclipse User |
|
|
|
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 00:51   |
Eclipse User |
|
|
|
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;
}
|
|
|
|
|
|
Re: Diagnostician Validation only enforce unique Ids after saving the model [message #1859223 is a reply to message #1859222] |
Sat, 20 May 2023 16:50  |
Eclipse User |
|
|
|
Quote: Are you sure you are using the correct Validate? WTP unfortunately installs a second "Validate" that does nothing.
I'm using
Diagnostician.INSTANCE.validate(EObject);
It works well to validate the ocl constraints I create. It also works for the class diagram constraints (but with the resource thing for unique ids like explained earlier)
Quote:
To enforce uniqueness you are probably better off with an OCL constraint something like: groups->isUnique(name)
Yes this is exactly what I did and it works well
invariant
uniqueNames('[Group:uniqueNames]Each group\'s name must be different within the same tournament.'):
groups->isUnique(name);
I also love how easy it is to make custom error messages so I'm actually glad the diagnostician did not work as I expected with the class diagram :)
|
|
|
Powered by
FUDForum. Page generated in 0.25843 seconds