Skip to content

Element_creation_example

Antonin Abhervé edited this page Sep 3, 2020 · 1 revision

Element creation example

Modelio v3.5

Goal:

To create a new Class instance named “HelloWorld” under the root package, and stereotyped “Java”.

Note that the following code example fragments modify the model, they are therefore run in a Transaction FIXME LINK.

Step 1: creating the class

 1// get the modeling session
 2IModelingSession session = MyModule.getModuleContext().getModelingSession();
 3
 4try (ITransaction transaction = session.createTransaction("a transaction")) {
 5    // get the factory
 6    IUmlModel factory = session.getModel();
 7
 8    // use the factory to create elements, here a simple class
 9    Class newClass = factory.createClass();
10
11    // commit the transaction
12    transaction.commit();
13}

The step 1 code simply creates a new class model element.
Although this code will correctly create a new Class element (line 9), it will be censored by the Modelio audit because the created class has no owner in the model, ie the new Class element is ‘floating’ in the model, not being owned by any other element.
In modelio, all model elements must have a unique owner. So the step 1 code is not sufficient as the created Class is an orphan.

Step 2: fixing the owner

Let’s try to fix the step 1 issue about our new Class being an orphan.

 1IModelingSession session = MyModule.getModuleContext().getModelingSession();
 2
 3try (ITransaction transaction = session.createTransaction("a transaction")) {
 4    // get the factory
 5    IUmlModel factory = session.getModel();
 6
 7    for (MObject root : factory.getModelRoots()) {
 8        if (root instanceof Project) {
 9            // get the model root package (to be used as the new class owner)
10            Package rootPackage = ((Project) root).getModel();
11
12            // use the factory to create elements, here a simple class
13            Class newClass = factory.createClass();
14
15            // attach the Class to the root package
16            rootPackage.getOwnedElement().add(newClass);
17
18            // commit the transaction
19            transaction.commit();
20            break;
21        }
22    }
23}

The metamodel states that classes belongs to packages at the ModelTree level using the association OwnedElement. Therefore, to add the new created class to the root package we use the getOwnedElement().add(…​) (line 15). Now the new Class is owned by the root package, this code will successfully pass the audit.
But still, we are not yet full-filling our initial requirement a new Class instance named HelloWorld under the root package and stereotyped “Java”

Step 3: setting the name and adding the stereotype

Let’s name the class and add a “Java” stereotype to it.

 1IModelingSession session = MyModule.getModuleContext().getModelingSession();
 2
 3try (ITransaction transaction = session.createTransaction("a transaction");) {
 4    // get the factory
 5    IUmlModel factory = session.getModel();
 6    for (MObject root : factory.getModelRoots()) {
 7        if (root instanceof Project) {
 8            // get the model root package (to be used as the new class owner)
 9            Package rootPackage = ((Project) root).getModel();
10
11            // use the factory to create elements, here a simple class
12            Class newClass = factory.createClass();
13
14            // attach the Class to the root package
15            rootPackage.getOwnedElement().add(newClass);
16
17            // set the class name
18            newClass.setName("HelloWorld");
19
20            // add the stereotype, the first thing to do is to get the "Java" stereotype
21            IMetamodelExtensions metamodelExtensions = session.getMetamodelExtensions();
22            Stereotype javaStereotype =  metamodelExtensions.getStereotype("JavaDesigner", "Java", Metamodel.getMClass(Class.class));
23
24            newClass.getExtension().add(javaStereotype);
25
26            // commit the transaction
27            transaction.commit();
28            break;
29        }
30    }
31}
  • Line 18: Setting the name of the created class is “HelloWorld”, just use the setName() accessor on Class.

Adding the “Java” stereotype is a two steps process:

  • Line 21: to get the Stereotype object representing the “Java” stereotype on Class we first ask the modeling session for the metamodel known extensions (ie the different stereotypes, tag and note types brought in the project by the deployed modules). The returned IMetamodelExtensions object can provide a Stereotype given its name, defining module name and supported metaclass.

  • Line 22: here we finally get the Stereotype object representing the “Java” stereotype applicable on classes.

  • Line 24: this is where the java stereotype is added to the newly created class. The association that links a Class to its Stereotypes is named “Extensions”, its max cardinality is *, therefore, applying the accessor naming rules, we use the getExtension().add(…​) to add the stereotype.

Example 4: using the convenience factory methods

Creating a Class, attaching it to its owner, naming it and adding a stereotype is quite a burden. Even worse, this situation is frequent.
The model element creation factory provides some convenience creation methods for the most frequent use cases (mainly for the most used metaclasses).

Here is what our code becomes by using the proper convenience method:

 1IModelingSession session = MyModule.getModuleContext().getModelingSession();
 2
 3try (ITransaction transaction = session.createTransaction("a transaction")) {
 4    // get the factory
 5    IUmlModel factory = session.getModel();
 6
 7    for (MObject root : factory.getModelRoots()) {
 8        if (root instanceof Project) {
 9            // get the model root package (to be used as the new class owner)
10            Package rootPackage = ((Project) root).getModel();
11
12            // add the stereotype, the first thing to do is to get the "Java" stereotype
13            Stereotype javaStereotype =  session.getMetamodelExtensions().getStereotype("Java", Metamodel.getMClass(Class.class));
14
15            // use the factory convenience create method
16            Class newClass = factory.createClass("HelloWorld", rootPackage, javaStereotype);
17
18            // commit the transaction
19            transaction.commit();
20            break;
21        }
22    }
23} catch (ExtensionNotFoundException e) {
24    System.err.print(e);
25}
26

The convenience `createClass(String name, NameSpace owner, Stereotype s)` method carries out the different tasks of creating the class, naming it, setting its owner and applying the stereotype.

Easier isn’t it ? An ExtensionNotFoundException will be thrown if there is no stereotype matching the name and the metaclass.

Clone this wiki locally