Metamodel services

Modelio v3.5

Metamodel services are used to query the Modelio metamodel.

The metamodel API model

The following diagram summarizes the Metamodel API structure. The model is highly simplified for the purpose of the following explanations.

fig1

The diagrams mainly shows that:

  • The Modelio metamodel (MMetamodel) is composed of metaclasses (MClass)
  • A metaclass (MClass) has several attributes (MAttribute)
  • A metaclass (MClass) has several relations to other metaclasses which are represented by dependencies (MDependency)

This model can be queried or navigated using the Metamodel API.

Metamodel API

Accessing the metamodel

The Modelio metamodel can be accessed from the IMetamodelService. Here is the corresponding code snippet.

 1IModuleContext ctx = myModule.getModuleContext();
 2
 3IMetamodelService mmService = ctx.getModelioServices().getMetamodelService();
 4
 5// Get the metamodel
 6MMetamodel mm = mmService.getMetamodel();
 7
 8// Loop on all registered metaclasses
 9for (MClass metaclass : mm.getRegisteredMClasses() ) {
10    System.out.println(metaclass.getName());
11}
12

line 3 : the IMetamodelService' can be obtained from the module context **line 6 **: theIMetamodelService' can provide the MMetamodel object representing the Modelio metamodel
line 9-10: this loop simply prints out the names of all the metaclasses currently composing the metamodel. (ok, using System.out is an heresy see log services)

Exploring the metamodel

Finding a metaclass by its Java interface

Use the MClass getMClass(Class<? extends MObject> interf) method of MMetamodel.

 1IModuleContext ctx = myModule.getModuleContext();
 2IMetamodelService mmService = ctx.getModelioServices().getMetamodelService();
 3
 4// Get the metamodel
 5MMetamodel mm = mmService.getMetamodel();
 6
 7// Get the metaclass 'Operation' 
 8MClass metaclass = mm.getMClass(org.modelio.metamodel.uml.statik.Operation.class);
 9

line 8: get the metaclass from its Java interface. Note the use of the fully qualified name for Operation to avoid ambiguities.

Finding a metaclass by its name

Use the MClass getMClass(final String name) method of MMetamodel.

 1IModuleContext ctx = myModule.getModuleContext();
 2IMetamodelService mmService = ctx.getModelioServices().getMetamodelService();
 3
 4// Get the metamodel
 5MMetamodel mm = mmService.getMetamodel();
 6
 7// Get the metaclass 'Operation'
 8MClass metaclass = mmService.getMClass("Operation");
 9

line 8: get the metaclass from its Java interface. The lookup is carried out by name.

Finding a metaclass from a model element

Use the MClass getMClass() method of MObject.

1Element elt = ... // a given model element
2
3// Get the metaclass of 'elt'
4MClass metaclass = elt.getMClass();
5

line 4: get the metaclass from the model element using the MObject#getMClass() public method. Since any model element is a MObject, this call is perfectly applicable.

Exploring a metaclass

The properties (attributes) and relations (dependencies) of a metaclass can be explored too using the Metamodel API.
The following code fragment displays all the attributes and dependencies of the ‘Operation’ UML metaclass.

 1IModuleContext ctx = myModule.getModuleContext();
 2IMetamodelService mmService = ctx.getModelioServices().getMetamodelService();
 3
 4// Get the metamodel
 5MMetamodel mm = mmService.getMetamodel();
 6
 7// Get the metaclass 'Operation' 
 8MClass metaclass = mm.getMClass(org.modelio.metamodel.uml.statik.Operation.class);
 9
10// Loop on the metaclass attributes
11for (MAttribute mAtt : metaclass.getAttributes(false) ) {
12   System.out.println( mAtt.getName() );
13}
14
15// Loop on the metaclass dependencies
16for (MDependency mDep: metaclass.getDependencies(false) ) {
17   System.out.println( mDep.getName() );
18}

line 11: the getAttributes() method returns all the attributes of the metaclass. The boolean parameter (false) indicates that we do not want the inherited attributes (the attributes from the parent metaclasses) to be returned.
line 12: the MAttribute class has several accessors. Here we use the getName() accessor that returns the name of the attribute. See the Java Documentation for more details about the MAttribute accessors.
line 16: the getDependencies() method returns all the dependencies of the metaclass. The boolean parameter (false) indicates that we do not want the inherited dependencies (the dependencies from the parent metaclasses) to be returned.
line 17: the MDependency class has several accessors. Here we use the getName() accessor that returns the name of the dependency. See the Java Documentation for more details about the MDependency accessors.

Meta navigation

It is possible to access and modify the model in a fully generic way by using meta-navigation.

The MObject class which is the topmost parent of any model element provides three generic accessors:

  • Object mGet(MAttribute att) – Get the value of the attribute ‘att’ for the model element
  • void mSet(MAttribute att, Object value) – Set the value of the attribute ‘att’ for the model element
  • List<MObject> mGet(MDependency dep) – Get the list of objects linked to the model element by its ‘dep’ dependency

Let’s illustrate the use of some of these generic accessors in the following code fragment which lists the values of all the attributes and dependencies of a given element without knowing anything a priori about its type.

 1Element elt = ...;       // Given model element, no matter its concrete type
 2IMetamodelService mmService = ctx.getModelioServices().getMetamodelService();
 3
 4// Get the metamodel
 5MMetamodel mm = mmService.getMetamodel();
 6
 7// Loop on the element's attributes using the generic accessor eGet() to print their values
 8for (MAttribute mAtt : elt.getMClass().getAttributes() ) {
 9    System.out.println("attribute '%s' value is '%s'\n", mAtt.getName(), elt.eGet(mAtt) );
10}
11
12// Loop on the element's dependencies using the generic accessor eGet() to print their values
13for (MDependency mDep : elt.getMClass().getDependencies() ) {
14    System.out.println("dependency '%s' values are:\n", mDep.getName());
15    for (MObject mObj : elt.eGet(mDep)) {
16       System.out.println(" - %s\n", mObj.toString()); 
17    }
18}
19
20

line 9: the eGet() generic accessor is called on ‘element’. eGet() works at the model level, returning the current value of an attribute of the ‘element’ object.
The exact attribute whose value is returned is specified by the ‘mAtt’ parameter which comes from the definition of the metaclass typing ‘element’.
line 15: the eGet()generic accessor called on ‘element’ return a list of objects which are linked to ‘element’ by the dependency specified by ‘mDep’.
‘mDep’ comes from definition of the metaclass typing ‘element’. Being a list the returned values is looped on to display each value individually (line 16).

MetamodelAPI.png (13.5 KB) Philippe Vlaemynck, 02 February 2016 17:11