Stopping the submodel execution
If a submodel execution takes a long time it may be desirable to interrupt the submodel without stopping the master model itself. The following modified version of our master model (file runsubwait.mos) shows how this can be achieved by adding a duration (in seconds) to the wait statement. If the submodel has not yet sent the termination event message after executing for one second it is stopped by the call to stop with the model reference.
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(1) ! Wait 1 second for an event if isqueueempty then ! No event has been sent: model still runs writeln_("Stopping the submodel") stop(modSub) ! Stop the model wait ! Wait for model termination end-if dropnextevent ! Ignore termination event message end-model
A more precise time measurement can be obtained by retrieving a "model ready" user event from the submodel before we begin to wait for a given duration. With heavy operating system loads the actual submodel start may be delayed, and the event sent by the submodel tells the master model the exact point of time when its processing is started. Class codes for user events can take any integer value greater than 1 (values 0 and 1 are reserved respectively for the nullevent and the predefined class EVENT_END).
model "Run model testsub" uses "mmjobs" declarations modSub: Model ev: Event SUBMODREADY = 2 ! User event class code end-declarations ! Compile the model file if compile("testsubev.mos")<>0 then exit(1); end-if load(modSub, "testsubev.bim") ! Load the bim file run(modSub) ! Start model execution wait ! Wait for an event if getclass(getnextevent) <> SUBMODREADY then writeln_("Problem with submodel run") exit(1) end-if wait(1) ! Let the submodel run for 1 second if isqueueempty then ! No event has been sent: model still runs stop(modSub) ! Stop the model wait ! Wait for model termination end-if ev:=getnextevent ! An event is available: model finished writeln_("Event class: ", getclass(ev)) writeln_("Event value: ", getvalue(ev)) writeln_("Exit code : ", getexitcode(modSub)) end-model
The modified submodel testsubev.mos now looks as follows. The user event class must be the same as in the master model. In this example we are not interested in the value sent with the event and therefore simply leave it at 0.
model "Test submodel (Event)" uses "mmjobs" declarations SUBMODREADY = 2 ! User event class code end-declarations send(SUBMODREADY, 0) ! Send "submodel ready" event forall(i in 10..20) write(i^2, " ") writeln end-model