문제

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

도움이 되었습니까?

해결책

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:

  • Two instances of EClass (one for Person, one for Dog)
  • Six instances of EAttribute (one for each attribute)
  • An EReference for Class.owner and a collection of EReferences for Person.dogs

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:

  • Export the class diagram ('metamodel') as ecore and load into EMF to create the model. This should be pretty straightforward.
  • Likewise export the object diagram as ecore and load, letting EMF validate on load. If you need to add further validation, @Charles' post provides a good link to the EMF validation framework.

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.

다른 팁

If you have defined a Meta-Model using ECore, EMF will generate the java API for creating instances (model) of the defined EClass. So by definition, your model will be compliant with its meta-model.

If your model is created by another application and you want to validate it against a meta-model created separately, at first your can try to open your model with the editor generated by EMF from your meta-model. If it is not compliant, you will have somme errors, that might be enough for a first try. If I remember well, thare is also a default validator that you can run by right clicking in your generated editor and selecting "Validate".

There is a another solution provided by EMF: EMF Validation Framework. This framework will allow you to define constraints that will be validated against your instances.

public class MyConstraint extends AbstractModelConstraint{
   @Override
   public IStatus validate(IValidationContext ctx) {
      try{
         //Get the object that will be validated
         Object obj = ctx.getTarget();
         //Here you can run you validation
         //Create Validation Status
         return validationStatus
      } catch (Exception e) {
         return ctx.createFailureStatus(e);
      }
   }
}

For more information you can take a look at this tutorial : http://help.eclipse.org/helios/index.jsp?topic=%2Forg.eclipse.emf.validation.doc%2Ftutorials%2FvalidationTutorial.html

I' pretty sure that you can define OCL constraint on your meta-model but this is part of another project. Take a look at this: http://www.eclipse.org/modeling/mdt/?project=ocl#ocl I never used it so I cannot help you for that. Sorry.

I hope it helps.

Charles

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top