Initializing help system before first use

Executing a submodel

Assume we are given the following simple model testsub.mos (the submodel) that we wish to run from a second model (its master model):

model "Test submodel"

 forall(i in 10..20) write(i^2, " ")
 writeln

end-model 

The reader is certainly familiar with the standard compile-load-run sequence that is always required for the execution of a Mosel model independent of the place from where it is executed (be it the Mosel command line, a host application, or another Mosel model). In the case of a Mosel model executing a second model, we need to augment this standard sequence to compile-load-run-wait. The rationale behind this is that the submodel is started as a separate thread and we thus make sure that the submodel has terminated before the master model ends (or continues its execution with results from the submodel, see Section Communication of data between models below).

moselpar

Figure 1: Executing a submodel

The following model runtestsub.mos uses the compile-load-run-wait sequence in its basic form to execute the model testsub.mos printed above. The corresponding Mosel subroutines are defined by the module mmjobs that needs to be included with a uses statement. The submodel remains as is (that is, there is no need to include mmjobs if the submodel itself does not use any functionality of this module). The last statement of the master model, dropnextevent, may require some further explanation: the termination of the submodel is indicated by an event (of class EVENT_END) that is sent to the master model. The wait statement pauses the execution of the master model until it receives an event. Since in the present case the only event sent by the submodel is this termination message we simply remove the message from the event queue of the master model without checking its nature.

model "Run model testsub"
 uses "mmjobs"

 declarations
  modSub: Model
 end-declarations
                                   ! Compile the model file
 if compile("testsub.mos")<>0 then exit(1); end-if
 load(modSub, "testsub.bim")       ! Load the bim file
 run(modSub)                       ! Start model execution
 wait                              ! Wait for model termination
 dropnextevent                     ! Ignore termination event message

end-model 

The two models are run by executing the master model in any standard way of executing Mosel models (from the Mosel command line, within Xpress Workbench, or from a host application). With the Mosel command line you may use, for instance, the following command:

mosel exec runtestsub.mos

As a result you should see the following output printed by the submodel (see Section Output from the submodel for a discussion of output handling):

100 121 144 169 196 225 256 289 324 361 400

Retrieving events

If we wish to obtain more precise information about the termination status of the submodel we could replace the statement dropnextevent by the following lines that retrieve the event sent by the submodel and print out its class (the termination event has the predefined class EVENT_END) and the value attached to it (here the default value 0). In addition, we display the exit code of the model (value sent by an exit statement terminating the model execution or the default value 0).

 declarations
  ev: Event
 end-declarations

 ev:=getnextevent
 writeln_("Event class: ", getclass(ev))
 writeln_("Event value: ", getvalue(ev))
 writeln_("Exit code  : ", getexitcode(modSub))

Events also provide information about the sending model, identified via its internal ID, and optionally the user ID and the group ID of the sending model (provided that the corresponding information has previously been defined for this model using setuid or setgid respectively):

 writeln_("Event sent by : ", getfromid(ev))
 writeln_("Model user ID : ", getfromuid(ev))
 writeln_("Model group ID: ", getfromgid(ev)