Advanced Modelling Features

Interrupts

The Truck Process

   
 

There are some distinct changes to the Truck class, mostly regarding a truck's life cycle. In the constructor, we now have to determine if the truck is urgent and set its priority accordingly. The setQueueingPriority(int priority) and getQueueingPriority() methods are provided by DESMO-J's Entity class. To keep the code clear and easy to maintain, we define a constant HIGH to represent high priority.

import desmoj.core.simulator.*;
import co.paralleluniverse.fibers.SuspendExecution;

public class Truck extends SimProcess {

   /** a code for high priority (= urgent trucks) */
   public static final int HIGH = 2;

   /** a reference to the model this process is a part of */
   private InterruptsExample myModel;

   /** constructor of the truck process */
   public Truck(Model owner, String name, boolean showInTrace) {

      super(owner, name, showInTrace);
      // store a reference to the model this truck is associated with
      myModel = (InterruptsExample)owner;

      // determine if this truck is urgent
      // by drawing a sample of the boolean distribution
      if (myModel.getTruckIsUrgent()) {
         // set truck's priority to HIGH
         this.setQueueingPriority(HIGH);
      }
      // default priority of an entity is 0
   }

The behaviour of "normal" and urgent trucks is the same as far as arrival at the terminal, enqueueing in the queue for waiting trucks, and checking for available van carriers is concerned. Only if all van carriers are busy, an urgent truck has to get hold of one of the van carriers (by asking the busyVCQueue for a reference to the first one) and send it an interrupt() message. After that, it can just wait to be serviced as usual. This is possible because the different priorities of standard and urgent trucks make sure that urgent trucks are automatically inserted at the front of the truck queue. Thus the interrupted van carrier will always remove the right truck from the queue.


public void lifeCycle() throws SuspendExecution {

   // arrive at the loading zone --> insert into queue for waiting trucks
   myModel.truckQueue.insert(this);

   // check if a VC is available
   if (!myModel.idleVCQueue.isEmpty()) {
      // yes, it is
        // --> remove first van carrier from idle VC queue and activate it
        ...
   }
   // if not, check if this is an urgent truck
   else if (this.getQueueingPriority() == HIGH) {
      // yes, this truck is urgent --> interrupt the first busy VC

      // get a reference to the first  VC from the busy VC queue
      VanCarrier vanCarrier = myModel.busyVCQueue.first();
      // remove the van carrier from the queue
      myModel.busyVCQueue.remove(vanCarrier);
      // interrupt it
      vanCarrier.interrupt(myModel.urgentTruckArrived);
   }

   // wait for service
   passivate();

   // service completed --> leave the system
   ...
}



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