umleclipse-emfmetamodelobject-diagram

How to validate an Object Diagram (aka "Instance Diagram") against a Meta Model?


I have to develop an API that take as an input a Meta Model (class diagram) and his instance (object diagram) but I don't know how to do it in a programaticly way.

I made some research and I found that I can play with EMF API but I'm not sure how to do it and how to insert my diagram before the validation.

I'm a beginner and lost between all the document that are present on the internet, so excuse me if my subject aren't clear

I just need some orientation, advise to understand what I have to do.

EDIT: The subject is to validate an instance against his metamodel that are created with Magic Draw. So I have to export the file (ecore? XMI? I don't know who's the best) then create a Java API that parse these two file (I need to figure out how to inject/import them also) and return a boolean value about the validation. This is for the first step, after that I think I'll add OCL constraint to validate the instance, but for now I need to focus on a simple validation. How can I do that with EMF or other tool if required


Solution

  • Your question can be answered at two levels: conceptual and implementation. I'm not sure which you're looking for help on so I'll try to cover both. Apologies if either is superfluous (hopefully not both!).

    First off: for the example you give it would be more normal to refer to a class diagram as a 'model' and an object diagram as an instance of that model. Strictly speaking, 'metamodels' are used to describe models which in turn have instances. I'll stick with your terminology below but probably worth noting.

    Conceptual Level

    By 'conceptual' I mean answering the question "what does it mean to validate a model against a metamodel'?

    The answer is pretty simple. The metamodel is a schema, or set of rules, that define what constitutes a valid model. xml provides a great example. An xml schema defines the structure and rules for some subject matter: what types are allowed, what attributes they can have, whether an attribute can have multiple values, the set of possible values an attribute can hold. And so on. An xml document is compliant with the schema if - and only if - it doesn't violate any of the rules defined in the schema.

    Generalising to models: a model is compliant with its metamodel if - and only if - it doesn't violate any of the rules defined in the metamodel.

    Implementation Level

    You don't elaborate on what the 'API' will be used for. It could be that your simplest solution is just to use xml: metamodels are then just xml schema, and models are xml documents compliant with those schema. Your 'implementation' would then just involve picking one of the many validating xml parser libraries and calling it from your client code.

    However you mention EMF so perhaps you need to use it. To simplify things, assume we want to define a metamodel as follows:

    Class Dog {
      name: String
      gender: String
      owner: Person
    }
    
    Class Person {
      name: String
      address: Address
      dogs: Set<Dog>
    }
    

    EMF provides an API - the eCore API - for defining such things. Think of it like java's reflection API. You define the metamodel by creating instances of the eCore API (either programmatically or via one of the editors). So you'd create:

    and so on. The eCore API also provides the ability to create instances of your model using the API. (It will also auto-generate an editor for you to create the instances in the eclipse GUI if required). You can also read in instances of your model in various concrete syntaxes. If the model you're reading doesn't comply with the metamodel, EMF will raise exceptions.

    Sorry if that's a bit long. There's a good article here that walks through an example in more detail if required.

    EDIT

    Addition in response to amended question:

    The subject is to validate an instance against his metamodel that are created with Magic Draw.

    OK. I don't know much about MagicDraw's export capabilities, but if it can export ecore then that should be a good place to start:

    If you've a lot of work to do it's probably worth investing in the EMF book. It's not the most accessible but it does provide decent coverage of the various elements of EMF.

    hth.