Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Diagnostician Validation only enforce unique Ids after saving the model(The validation method of the Diagnostician is not behaving in an expected way.)
Diagnostician Validation only enforce unique Ids after saving the model [message #1859213] Fri, 19 May 2023 22:12 Go to next message
Maxens Destiné is currently offline Maxens DestinéFriend
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 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
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/
Re: Diagnostician Validation only enforce unique Ids after saving the model [message #1859220 is a reply to message #1859217] Sat, 20 May 2023 18:03 Go to previous messageGo to next message
Maxens Destiné is currently offline Maxens DestinéFriend
Messages: 3
Registered: May 2023
Junior Member
Ah ok I see.

I found a workaround by using OCL in Ecore.

Thank you !
Re: Diagnostician Validation only enforce unique Ids after saving the model [message #1859221 is a reply to message #1859220] Sat, 20 May 2023 19:50 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

OCLinEcore may be a bit more aggressive in validating, particularly before the first edit, but I wouldn't expect it to validate Ecore constraints better than the Sample Ecore Model Editor's Validate menu entry.

Are you sure you are using the correct Validate? WTP unfortunately installs a second "Validate" that does nothing. This is a very longstanding bug that they now regard as a legacy requirement.

Regards

Ed Willink

Re: Diagnostician Validation only enforce unique Ids after saving the model [message #1859222 is a reply to message #1859221] Sat, 20 May 2023 20:05 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

Are you sure you mean to use the Ecore "id"? Whereas UML's "id" is specified to be unique within a namespace and so could be respected by a manual edit, Ecore's "id" is unique for the whole model and so can really only be accurate when computed programmatically. I have occasionally thought that EMF's "id" might solve a problem for me but no; it never seems useful.

To enforce uniqueness you are probably better off with an OCL constraint something like: groups->isUnique(name)

Regards

Ed Willink
Re: Diagnostician Validation only enforce unique Ids after saving the model [message #1859223 is a reply to message #1859222] Sat, 20 May 2023 20:50 Go to previous message
Maxens Destiné is currently offline Maxens DestinéFriend
Messages: 3
Registered: May 2023
Junior Member
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 :)
Previous Topic:Interplay of XML_MAP and XML_EXTENDED
Next Topic:Hierarchical constraint categories in plugin.xml
Goto Forum:
  


Current Time: Sat Apr 27 01:32:45 GMT 2024

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

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

Back to the top