Initializing help system before first use

XPRD Java

For the remote execution of Mosel models we need to augment the standard execution sequence for Mosel models (that we have seen, for example, in Section Java) of compile load run to the sequence connect – compile load run wait (this remark equally applies to submodels launched via mmjobs). The meaning of these additions is the following:

  • We need to connect to a remote machine and create a new Mosel instance (XPRDmosel) prior to working with any Mosel models. Remote machines are specified by their name or IP address, the empty string in the example below indicates that we want to use the local machine.
  • The submodel is executed in an independent process and we therefore need to wait for its termination.

The program example runprimedistr.java below shows how to run the model prime.mos using the XPRD Java library. If the submodel has not terminated after 2 seconds (i.e., not termination message has been received from this model), then it is stopped by the application. After termination of the submodel (either by finishing its calculations within less than 2 seconds or stopped by the master model) the application reports the full event information and also displays the termination status and the exit value of the Mosel model. Unloading a model explicitly as shown here is only really necessary in larger applications that continue after the termination of the submodel, so as to free the memory used by it.

import com.dashoptimization.*;
import java.lang.*;
import java.io.*;

public class runprimedistr
{
 public static void main(String[] args) throws Exception
 {
  XPRD xprd=new XPRD();
  XPRDMosel moselInst;
  XPRDModel modPrime;
  XPRDEvent event;

  moselInst=xprd.connect("");    // Open connection to remote nodes
                                 // "" means the node running this program

                                 // Compile the model file on remote instance
  moselInst.compile("", "rmt:prime.mos", "rmt:prime.bim");

	                         // Load the bim file into remote instance
  modPrime=moselInst.loadModel("rmt:prime.bim");

  modPrime.execParams = "LIMIT=50000";
  modPrime.run();                // Start execution and
  xprd.waitForEvent(2);          // wait 2 seconds for an event

  if (xprd.isQueueEmpty())       // No event has been sent...
  {
   System.out.println("Model too slow: stopping it!");
   modPrime.stop();              // ... stop the model, then wait
   xprd.waitForEvent();
  }
                                 // An event is available: model finished
  event=xprd.getNextEvent();
  System.out.println("Event value: " + event.value +
                     " sent by model " + event.sender.getNumber());
  System.out.println("Exit status: " + modPrime.getExecStatus());
  System.out.println("Exit code  : " + modPrime.getResult());

  moselInst.unloadModel(modPrime);  // Unload the submodel
  moselInst.disconnect();           // Terminate the connection

  new File("prime.bim").delete();   // Clean up temporary files
 }
} 

The model source file prime.mos used by this example is saved on the local machine running XPRD, and the BIM file is written back to this machine (indicated by the I/O driver prefix rmt: in the 'compile' and 'load' function calls that are executed on the remote instance). Alternatively, we might choose to save the BIM file on the remote machine, e.g. in memory (shmem:primebim) or in Mosel's temporary directory (tmp:prime.bim).