Module service

Modelio v3.6

Modelio module services provide a programmatic access to the modules deployed in the currently opened project:
– the module service interface is IModuleService
– the IModuleService instance can be obtained from the IModuleContext of the module via IModelioServices

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)

Listing the modules

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

1...
2IModuleService moduleService = MyModule.getInstance().getModuleContext().getModelioServices().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.

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 = MyModule.getInstance().getModuleContext().getModelioServices().getModuleService();
 5
 6IJavaDesignerPeerModule javaModule = moduleService.getPeerModule(IJavaDesignerPeerModule.class);
 7
 8// Next we can call the Java designer peer services on the javaModule returned object
 9
10// calling generic peer services getName() and getVersion()
11System.out.println(module.getName() + ", " + module.getVersion().toString()); 
12
13// calling a Java Designer specific service to generate Java code
14javaModule.generate(...);  
15
16...

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 = MyModule.getInstance().getModuleContext().getModelioServices().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.getParameterValue("JDKPath");
14  
15
16}
17...

The evil in the above code fragment is on line 13 where you have to know the exact key (here ‘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. Hopefully the key names should be clear enough.