CellML API
Working with CellML models using Java
About the Java documentation
This documentation starts off with a few examples, and then moves into Java-specific reference documentation. Much of the CellML API documentation is language independent, so once you have a grasp of how the language independent documentation relates to Java, refer to the documentation (for example, interfaces listed in the class listing) for more details on how to use the services the API provides.
Loading java_cellml library
Here we create a class to load the java_cellml library.
Loading and creating CellML models
Here we create a class with methods that we could call to create or load CellML models.
Iterating CellML elements
The example shown below iterates through components, variables, and connections defined in the Hodgkin-Huxley model that is available at http://www.cellml.org/models/hodgkin_huxley_1952_version07/download.
To compile and run this code on Linux (with /path/to/sdk/ changed to the appropriate path):
export CLASSPATH=/path/to/sdk/lib/cellml.jar:.:$CLASSPATH export LD_LIBRARY_PATH=/path/to/sdk/lib:$LD_LIBRARY_PATH javac IterateElementTest.java java -Djava.library.path=/path/to/sdk/lib IterateElementTest
On Windows (with c:/path/to/sdk/ changed to the appropriate path):
set CLASSPATH="c:/path/to/sdk/lib/cellml.jar;.;%CLASSPATH%" set PATH="c:/path/to/sdk/lib;%PATH%" javac IterateElementTest.java java "-Djava.library.path=c:/path/to/sdk/lib" IterateElementTest
On Mac OS X:
export CLASSPATH=/path/to/sdk/lib/cellml.jar:.:$CLASSPATH export DYLD_LIBRARY_PATH=/path/to/sdk/lib:$LD_LIBRARY_PATH javac IterateElementTest.java java -Djava.library.path=/path/to/sdk/lib IterateElementTest
Writing CellML models to files
Here we create a class with a method that we could call to write CellML models to files.
Creating a CellML 1.1 model
The example code below creates a CellML 1.1 model with two components.
General concepts when using the CellML API Reference Implementation from Java
Every language-independent interface defined in this operation is mapped to a Java interface. Operations defined on the interfaces map to interface methods of the same name. Attributes map to a getter and a setter (or just a getter in the case of readonly attributes). The getter is named by pre-pending 'get' to the attribute name, capitalising the first letter of the attribute name. The setter is named similarly, except with a prefix of 'set'.
Java is a garbage collected language, and the CellML API Reference Implementation uses reference counting. The Java bridges make these work together by ensuring that a reference is kept whenever you reference a CellML API object from Java, and so for the most part a Java programmer does not need to worry about memory management with the CellML API. The one exception is that cycles, where Java code references the API, and the API references a callback to Java, need to be explicitly broken or the objects in the cycle will never be released.
The same CellML API object can implement multiple interfaces, but doing a Java cast between different interfaces supported by an object will not work, because access to the CellML API object is through a bridge, and each bridge object only supports one chain of interfaces. To get access to other interfaces supported by an object, you need to do a query_interface operation. In Java, you do this using a static method on the bridge of the interface you want to obtain. These bridge classes are named pjm2pcm.namespace.interfaceName, and the static method is called queryInterface. The return value is an object of that class; these classes implement the corresponding Java interfaces.
Note that before you use any objects from a particular Java service, you must load the bridge using System.loadLibrary("java_modulename"), where modulename is the name of the module for the service (for example, cellml for the core of the API, cis for the integration service, ccgs for the code generation service).
Bootstrapping in Java
The language-independent part of the CellML API documentation describes what interfaces objects can support, and how to get different objects by performing operations and retrieving attributes on the existing objects. However, there is a chicken-and-egg problem - you need to get the first object before you can do anything on an interface. This problem is solved differently in different language bindings. In Java, the reference implementation solves the problem using static native methods, called Bootstrap Methods, that return a Java interface.
Here are some of the bootstrap methods available in the CellML API:
Service | Bootstrap method | Return type | Module to load |
---|---|---|---|
Core | cellml_bootstrap.CellMLBootstrap.createCellMLBootstrap | cellml_api.CellMLBootstrap | java_cellml |
AnnoTools | cellml_bootstrap.AnnoToolsBootstrap.createAnnotationToolService | cellml_services.AnnotationToolService | java_annotools |
CCGS | cellml_bootstrap.CCGSBootstrap.createCodeGeneratorBootstrap | cellml_services.CodeGeneratorBootstrap | java_ccgs |
CGRS | cellml_bootstrap.CGRSBootstrap.createGenericsService | cellml_services.GenericsService | java_cgrs |
CIS | cellml_bootstrap.CISBootstrap.createIntegrationService | cellml_services.CellMLIntegrationService | java_cis |
CUSES | cellml_bootstrap.CUSESBootstrap.createCUSESBootstrap | cellml_services.CUSESBootstrap | java_cuses |
CeLEDS | cellml_bootstrap.CeLEDSBootstrap.createCeLEDSBootstrap | cellml_services.CeLEDSBootstrap | java_celeds |
CeLEDSExporter | cellml_bootstrap.CeLEDSExporterBootstrap.createCeLEDSExporterBootstrap | cellml_services.CeLEDSExporterBootstrap | java_celedsexporter |
CeVAS | cellml_bootstrap.CeVASBootstrap.createCeVASBootstrap | cellml_services.CeVASBootstrap | java_cevas |
MaLAES | cellml_bootstrap.MaLaESBootstrap.createMaLaESBootstrap | cellml_services.MaLaESBootstrap | java_malaes |
SProS | cellml_bootstrap.SProSBootstrap.createSProSBootstrap | SProS.Bootstrap | java_spros |
SRuS | cellml_bootstrap.SRuSBootstrap.createSRuSBootstrap | SRuS.Bootstrap | java_srus |
TeLICeMS | cellml_bootstrap.TeLICeMService.createTeLICeMService | cellml_services.TeLICeMService | java_telicems |
VACSS | cellml_bootstrap.VACSSBootstrap.createVACSService | cellml_services.VACSService | java_vacss |