This page will look better in a graphical browser that supports web standards, but is accessible to any browser or internet device.

Served by Samwise.

Cardiac Physiome Society workshop: November 6-9, 2017 , Toronto

Calling Java Code From MML

This page is for the current JSim version 2.0. Click here for the earlier JSim 1.6 version.


This document describes the process of defining and using Java procedural code within MML models.


Java code can be called from MML via either class or java source F&P. Class F&P access pre-compiled Java classes and is recommended only for JSim internal developers. This document will address java source F&P in which Java source code is embedded within MML and compiled at model build time. This mechanism guarantees maximum compatibility with future JSim releases.

Note: Variables and realDomain variables that are labeled 'private' are not available to Java functions and procedures when run as a Java applet over the web from a remote server (By default all variables and domains are public). The JSim compiler will complain that the variable is 'unknown'. The same code will compile and run fine when run locally.

The remainder of this document shows several examples of calling Java code from MML. For the complete Application Programmer Interface (API), see The JSim Public API . Examples are:

Example 1

In the following example, a java source function "gmean" is defined that accepts two scalar arguments and returns their geometric mean. The main program defines a variable v(t) as the geometric mean of t and t^3, which should thus be equal to t^2:

(Java plugin required)

The first paragraph comprises the gmean function declaration. The keyword "real" indicates that the function returns a real number. (Currently, real is the only supported function data type. Other data types may be supported in the future.) The keyword "source" indicates that the function will be defined in terms on source code within MML. The name "gmean" follows the keyword "function", giving a unique name to this function. Function names must follow standard MML naming conventions. The argument list following the function name indicates this function takes exactly two scalar arguments. (Non-scalar arguments use the @ directive, described later in this document).

The "language" clause inside the function declaration indicates the procedural code will follow the JSim Java Model API . The "maincode" clause contains the java procedural code. Conventions for this code are fully described in The JSim Java API . In brief, the modeler specified maincode block is copied into a double returning dynamic Java method, whose calling arguments are RealNData objects named a and b. RealNData has a method realVal() returning the value of a scalar variable. The return statement is required for a real function. It's omission will cause a compiler error.

  The second paragraph comprises the main line of the MML model. In this case, gmean will be called once for each t. Although only a single equation using gmean is present in this example, multiple uses of gmean, including nested uses, are supported. It should be noted that incautious use of static methods within maincode can break this nesting ability.

Example 2

The first example is trivial because the desired functionality could more easily be obtained simply using MML. This is the case for most situations involving scalar arguments. Function and procedures become more useful when passing multi-dimensional arguments because the looping constructs within Java are more general than those available in MML.

In the following example, one-dimensional integration is implemented as a java source function:

(Java plugin required)

The MML @ construct is used here twice. In the function declaration, u@t indicates that the single function argument must be one-dimensional. When the function is called u@t, which should be read "u for all t", indicates that u values for all t should be passed to integral. If a function's declared argument dimensions do not match the arguments passed at call time, MML will alert the modeler at compile time.

RealNData have a method grid(int i), which provides access to the associated data grid(s) of a multidimensional variable. Since the function argument is not scalar, realVal(int[]) replaces realVal() as the method used to access argument values at various grid points. Again, see the JSim Public API for further information.

Multiple @ constructs can be used for higher dimensional function arguments.

Example 3

In the following example a procedure, rather than a function is demonstrated. The procedure "reverse" takes a one-dimensional input argument, reverses the order of the data, and places the result in a one-dimensional output argument:

(Java plugin required)

The keywords "java" and "procedure" indicate that the embedded code block(s) will follow the JSim Java Model API for procedures. Most comments above regarding functions apply equally to procedures, with a few differences noted below.

The procedure argument list separates input arguments from output arguments with a semi-colon, with inputs listed first. This example has one input and one output argument.

The maincode block returns no value as is appropriate for a procedure. The RealNData method set(int, double) provides a means to set the value of output arguments. Again, see the JSim Public API for further information.

Comments or Questions?

[This page was last modified 23Jun16, 9:53 am.]

Model development and archiving support at provided by the following grants: NIH/NIBIB BE08407 Software Integration, JSim and SBW 6/1/09-5/31/13; NIH/NHLBI T15 HL88516-01 Modeling for Heart, Lung and Blood: From Cell to Organ, 4/1/07-3/31/11; NSF BES-0506477 Adaptive Multi-Scale Model Simulation, 8/15/05-7/31/08; NIH/NHLBI R01 HL073598 Core 3: 3D Imaging and Computer Modeling of the Respiratory Tract, 9/1/04-8/31/09; as well as prior support from NIH/NCRR P41 RR01243 Simulation Resource in Circulatory Mass Transport and Exchange, 12/1/1980-11/30/01 and NIH/NIBIB R01 EB001973 JSim: A Simulation Analysis Platform, 3/1/02-2/28/07.