mmjava



The mmjava module for Mosel is intended for users who integrate their Mosel models into Java applications. To use this module, the following line must be included in the header of a Mosel model file:

 uses 'mmjava'

Note that Mosel may fail loading mmjava if it is used from outside of a Java enabled application.

I/O drivers

This module provides the java and jraw IO drivers. The first one can be used to link a Mosel output (input) stream to a Java OutputStream (InputStream) or a Java ByteBuffer. The second driver is a modified version of the raw driver suitable for Java: instead of an address, this driver takes as input a reference to an object.

For both drivers, file names are replaced by references to objects. These references are of two kinds: direct references to public static objects (e.g. "java.lang.Sytem.out") and names defined using the XPRM.bind method. The second technique will be used with non static objects: the method XPRM.bind establishes a link between a name and an object. This name can then be used as an object reference for mmjava drivers.

When using Java object from Mosel, it is important to make sure objects and related fields can be accessed: in particular the class and its fields must be public.

Driver java

java:static object|named object

With this driver a Java stream (OutputStream or InputStream) as well as a ByteBuffer can be used in place of a file in Mosel. This facility is specially useful for redirecting default Mosel streams to Java objects. Note that the Mosel Java interface uses this driver for redirecting default streams (in, out, and error) to the corresponding Java streams (System.in, System.out and System.err).

Example:

mosel=new XPRM();
mosel.bind("out", myout);   /* Associate 'myout' object with string "out" */
	                    /* Redirect default output to 'myout' */
mosel.setDefaultStream(XPRM.F_OUTPUT|XPRM.F_LINBUF, "java:out")
	                    /* Redirect error stream to Java output stream */
mosel.setDefaultStream(XPRM.F_ERROR, "java:java.lang.System.out"

Driver jraw

jraw:[noindex,all]

The driver can only be used in `initializations' blocks. In the opening part of the block, no file name has to be provided, but general options can be stated at this point: they will be applied to all labels. Two options are supported:

all
forces output of all cells of an array even if it is dynamic (by default only existing cells are considered).
noindex
indicates that only data (no indices) are transfered between the Java objects and Mosel. By default, the first fields of each object are interpreted as index values for the array to be transfered. This behavior is changed by this option.

In the block, each label entry is understood as an object reference to use for the actual processing. Note that, before the object reference, one can add further options separated by comas, that are effective to the particular entry.

If the Model object to be initialized (or saved) is a scalar or an array with option noindex, the driver expects a Java object of a corresponding type (i.e. same basic type and scalar or one dimension array). If the option noindex is not used and the Mosel object is an array, the label must specify which fields of the class have to be taken into account for the mapping. This is indicated by a list of field names separated by commas and noted in brackets (e.g. "myobj(fi1,fi2,fi3)").

In the following example the jraw driver is used to initialize an array of reals, a, and an array of integers, ia, with data held in the Java application that executes the model.

Java part:

public class MyData {   /* A class to store an `array(string, int) of real' */
 public String s; public int r; public double v;
 MyData(String i1, int i2, double v0) { s=i1; r=i2; v=v0; }
}
...
MyData[] data; 
int[] intarr; 
...
mosel=new XPRM();
mosel.bind("data", data);   /* Associate `data' object with string "data" */
mosel.bind("ia", intarr);   /* Associate `intarr' object with string "ia" */

Mosel part:

declarations
 a:array(string, range) of real
 ia:array(range) of integer
end-declarations
...
initializations from "jraw:"
 aa as "data(s,r,v)" ! Initialize `aa' with fields s,r,v of object `data'
 ia as "noindex,ia"  ! Initialize `ia' with array `ia'; no index (only values)
end-initializations 


If you have any comments or suggestions about these pages, please send mail to docs@dashoptimization.com.