Module services

Modelio module services provide a programmatic access to the modules deployed in the currently opened project.

The module API provides functions to:

  1. List the modules of the project
  2. Call the services provided by a module
  3. Manage a module’s configuration (parameters)

IModuleService

The IModuleService object provided by Modelio represents the access point to the project modules.

Using the module API consists in:

  1. getting the IModuleService
  2. calling one of the provided methods

Example

1...
2IModuleService moduleService = Modelio.getInstance().getModuleService();
3...

Listing the modules

From the IModuleService instance, listing the currently installed modules is straightforward.

1...
2IModuleService moduleService = Modelio.getInstance().getModuleService();
3
4for (IPeerModule module: moduleService.getAllPeerModules() ) {
5    System.out.println("  - " + module.getName() + ", " + module.getVersion().toString());
6}
7...

Note the type of the returned values which is IPeerModule. See IPeerModule and peer services for full details

Calling the peer services of a module

The first thing to do is to get the peer interface of the module whose services we need to call. There is a convenient method to directly get a module known by its peer interface.

See how we can easily get in touch with the Java Designer module once we “know” its IJavaDesignerPeerModule peer interface.

 1import com.modeliosoft.modelio.javadesigner.IJavaDesignerPeerModule;
 2
 3...
 4IModuleService moduleService = Modelio.getInstance().getModuleService();
 5
 6IJavaDesignerPeerModule javaModule = moduleService.getPeerModule(IJavaDesignerPeerModule.class);
 7
 8// next we can call the Java designer services on the javaModule returned object
 9  
10System.out.println(module.getName() + ", " + module.getVersion().toString()); // callig a peer service
11
12javaModule.generate(...);  // calling a Java Designer specific service
13
14...

As you might have already guessed, the key point in the above code fragment is line 1 which requires the knowledge and the availability of the IJavaDesignerPeerModule Java interface. To say it shortly here, this is obtained using the module dependencies mechanism by which a module A can declare that it requires a module B, making the peer interface of B available to A. Note that the getPeerModule() method is generic and returns directly a properly typed object.

Accessing a module parameters

A module can access to the parameters of another module. This is a standard feature provided by the IPeerModule interface itself. Module parameters are accessible by a IModuleAPIConfiguration object returned by the peer interface of a module.

 1import org.modelio.module.javadesigner.api.IJavaDesignerPeerModule;
 2
 3...
 4IModuleService moduleService = Modelio.getInstance().getModuleService();
 5
 6// Get the Java Designer peer interface
 7IPeerModule javaModule = moduleService.getPeerModule(IJavaDesignerPeerModule.class);
 8
 9// Get the Java Designer configuration
10IModuleAPIConfiguration javaConfiguration = javaModule.getConfiguration();
11
12// Print out the path of the JDK currently configured in the Java Designer module settings
13System.out.println("JDK path = " + javaConfiguration.getParameter("JDKPath");
14  
15
16}
17...

The evil in the above code fragment is on line 13 where you have to know the exact key (JDKPath) to get the value of a module parameter. The module documentation can help here, or you have to look in the model browser for the corresponding ModuleParameter from the ModuleComponent you want to use…

Alternatively try a call to Map<String, String> getParameters() that will return the complete set of the module parameters as a map of (key, value) pairs. The key names should be clear enough.