Model API – Accessor examples

Modelio v3.5

Quick outline:

  1. Getting the name of a Class
  2. Getting the sub-elements of a ModelTree

Example 1: Getting the name of a Class

Here is described how to find the method that will retrieve the name of a Class using the metamodel documentation.

A first look in the metamodel documentation for the Class metaclass shows the following figure:

Class diagram

From the above diagram, we can navigate the inheritance tree of the Class metaclass up to the ModelElement metaclass where the name attribute can be found.

The accessors naming rules state that to determine the name of an attribute getter method, just add the get prefix to to the name of the attribute. In our case this produces the getName() method which is defined on ModelElement but is perfectly callable on a Class object because of the inheritance tree. The method returns the class name.

The corresponding code is:

1Class aClass = ... // some code to get a Class instance
2String name = aClass.getName();

To change the Class object name, use a setter accessor. For a simple cardinality attribute, the accessors naming rules recommend to add the set prefix to the attribute name. Here we get the setName() method defined on IModelElement.

The corresponding code is:

1Class aClass = ... // some code to get a Class instance
2aClass.setName("new name");

Warning:

For accessors that modify the model, please carefully read the Transaction API page, which explains some major model modification rules.

Example 2: Getting the sub-elements of a ModelTree

Here is described how to find the name of the method that will enable you to retrieve all ModelTree type elements (Class, Package …) defined in a ModelTree, using the metamodel documentation.

The above diagram extracted from the metamodel documentation, shows a composition relationship between ModelTree objects. The role found on the component side of the composition is called OwnedElement.

fig3

The accessors naming rules state that to determine the name of the method to use, just add the get prefix to to the name of the role for which you want the getter method. This produces the getOwnedElement() which is defined on ModelTree and returns the ModelTree sub-elements.

The corresponding code is:

1Package owner = ... // some code to get a Package instance
2List<ModelTree> sub_elements = owner.getOwnedElement();

To add a ModelTree to a parent, just modify the list returned by getOwnedElement(). All modifications to the list do modify the model.

The corresponding code is:

1Package owner  = ...  // some code to get a Package instance
2Class   aClass = ...  // some code to get a Class instance
3owner.getOwnedElement().add( aClass );

For single cardinality association roles, use a setter accessor. The accessors naming rules recommends to add the set prefix to the role name. Here we get the setOwner() method defined on ModelTree.

The corresponding code is:

1Package owner  = ...  // some code to get a Package instance
2Class   aClass = ...  // some code to get a Class instance
3aClass.setOwner( owner );

Warning:

For accessors that modify the model or the lists returned by the getter accessors, please carefully read the Transaction API page, which explains some major model modification rules.

ClassMetamodel.png (6.59 KB) Philippe Vlaemynck, 02 February 2016 16:17

ModelTreeMetamodel.png (9.06 KB) Philippe Vlaemynck, 02 February 2016 16:25

ClassMetamodel.png (7.8 KB) Philippe Vlaemynck, 02 February 2016 16:27