Skip to content

The_module_context

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

Runtime environment of a Module

Modelio v4.0

Modelio provides the modules with a initialized environment that is used by module to access to the Modelio services.
The two main provided objects are the module context which is the top-most anchor of the module to the Modelio API and the modelling session which is relevant to accessing and manipulating the model itself.

IModuleContext : the module context

The IModuleContext object is initialized and provided by Modelio to the module, it represents the very core access point to the Modelio API.

The Java lifecycle (creation, finalization) of the IModuleContext object is controlled by Modelio itself along with its guaranteed unicity as a singleton.

The IModuleContext singleton instance can be obtained by calling the getModuleContext() method of the module main class as shown in the code snippet below.

 1//
 2// Suppose that the module being developed is named 'MyModule'.
 3// Its main class is named 'MyModule'
 4//
 5
 6// Get the MyModule instance
 7MyModule myModule = MyModule.getInstance();
 8
 9// Get the module context
10IModuleContext ctx = myModule.getModuleContext();
11

line 7: In a project a module is deployed only once, only one instance of its main class is instantiated by Modelio.
This unique instance (singleton) is returned by the getInstance() static method of the module main class.
line 10: The module context is available as a property of the module main class instance using the getModuleContext() accessor.

Note that the IModuleContext object is not valid if:

  • no project is opened in Modelio

  • the module is not started in the currently opened project

IModelingSession : the Modelio session

The IModelingSession object represents the most primitive access point to the model.
A modeling session is a transaction-based access to the model contained in the currently opened project. Without a valid modeling session you have neither read nor write access to the model.

A modeling session can be opened or closed. A closed session is useless as it does not provide more help than no session at all. An opened session is really what you need to work with the model. Note that,however, a “closing-in-progress” session might be useful as it can mean that it is time to stop certain activities of your module or to free some resources and so on.

For now let us stick to simple principles, Modelio can provide you with a valid modeling session using the following code:

1// Get the module context
2IModuleContext ctx = myModule.getModuleContext();
3
4// Get the modeling session
5IModelingSession session = ctx.getModelingSession();
6
Clone this wiki locally