Advanced Modelling Features

Interrupts

The Van Carrier Process

   
 

In the life cycle of a van carrier, we now have to add code to handle interrupts. In our model there is only one kind of interrupt: an urgent truck interrupts a busy van carrier in the service of a non-urgent truck. As the service is modelled via a hold() statement, the van carrier process now has to check for interruption when regaining control after this statement.

The method isInterrupted() returns true if the process was interrupted; otherwise false. If there has been an interrupt, the van carrier has to stop servicing the current truck and start service for the urgent truck. We model this by having the van carrier insert the current truck back into the queue of waiting trucks after raising its priority by 1. This ensures that an interrupted truck has a higher priority than a standard truck but still a lower priority than an urgent truck -- which in turn ensures that it is sorted into the queue behind any urgent trucks but in front of any standard trucks. Thus the van carrier will resume service on the interrupted truck right after finishing the urgent truck.

Since the van carrier's life cycle operates in a constant loop, placing the interrupted truck into the waiting queue at an approriate place is enough to handle the interrupt. The van carrier will now return to the top of the while loop, find that the truck queue is NOT empty and proceed with servicing the first truck in the queue. Due to the priority-based order of the queue, the first truck is just the urgent truck that has caused the interruption!

So all left to do in the interrupt handling is re-setting the status of the van carrier process from interrupted to non-interrupted. This is done via the clearInterruptCode() method.

public void lifeCycle() throws SuspendExecution {

   // the van carrier is always on duty and will never stop working
   while (true) {
      // check if there is someone waiting
      if (myModel.truckQueue.isEmpty()) {

         // NO, there is no one waiting
         // --> insert self into the idle VC queue and passivate
         ...
      }
      else {

         // YES, here is a customer (truck) waiting
         // --> remove the first truck from the queue of waiting trucks
         Truck truck = myModel.truckQueue.first();
         myModel.truckQueue.remove(truck);

         // insert self into busy VC queue
         myModel.busyVCQueue.insert(this);

         // fetch container and load it onto truck
         hold(new TimeSpan(myModel.getServiceTime(), TimeUnit.MINUTES));

         // check if an urgent truck interrupted the service
         if (isInterrupted()) {

            // yes, there has been an interrupt
            // --> handle the interrupt
            sendTraceNote("interrupted!");

            // raise priority of the interrupted truck
            truck.setQueueingPriority(truck.getQueueingPriority() + 1);
            // insert it back into the queue of waiting trucks
            // --> due to higher priority it will be inserted before any
            //     "normal" trucks so that it will be served prior to them
            myModel.truckQueue.insert(truck);

            // clear interrupt code
            this.clearInterruptCode();
         }
         else {
            // no, there has been no interrupt
            // --> the service is completed and the truck may leave
            truck.activate();
         }
      }
   }
}


   
  http://desmoj.sourceforge.net/tutorial/advanced/interrupt4.html