Processes Step by Step

Model Implementation (2)

The Van Carrier Process

   
 

To implement the van carrier simulation process derive a class named VanCarrier from SimProcess, add the appropriate import statement and define the constructor. This time, we also define a field (attribute) to store a reference to the model in and assign it a value inside the constructor.

import desmoj.core.simulator.*;
import co.paralleluniverse.fibers.SuspendExecution;
/**
 * This class represents a van carrier in the ProcessesExample
 * model.
 * The VC waits until a truck requests its service.
 * It will then fetch the container and load it onto
 * the truck.
 * If there is another truck waiting it starts serving this
 * truck. Otherwise it waits again for the next truck to arrive.
 */
public class VanCarrier extends SimProcess {

   /** a reference to the model this process is a part of
    *  useful shortcut to access the model's static components
    */
   private ProcessesExample myModel;
   /**
    * Constructor of the van carrier process
    *
    * Used to create a new VC to serve trucks.
    *
    * @param owner the model this process belongs to
    * @param name this VC's name
    * @param showInTrace flag to indicate if this process shall produce output
    *                    for the trace
    */
   public VanCarrier(Model owner, String name, boolean showInTrace) {

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

   ...

} /* end of process class */

Now we have to implement the lifeCycle() method. Please refer to the conceptual model if you need a reminder of what a van carrier does. There is an endless service loop and we choose the "while{ }" construct of the Java language to implement it. We will then use an "if"-statement for the decision whether there is a truck waiting or not.

   /**
    * Describes this van carrier's life cycle.
    *
    * It will continually loop through the following steps:
    * Check if there is a customer waiting.
    * If there is someone waiting
    *   a) remove customer from queue
    *   b) serve customer
    *   c) return to top
    * If there is no one waiting
    *   a) wait (passivate) until someone arrives (who reactivates the VC)
    *   b) return to top
    */
   public void lifeCycle() throws SuspendExecution {

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

            ...

First we will follow the "YES" branch of the statement, meaning "No, there is no truck waiting for service". In this case we have to put the VC in its queue in order to wait for the next truck. We will access the idleVCQueue via the model class where it is defined.

            // ... lifeCycle() continued

            // NO, there is no one waiting

            // insert yourself into the idle VC queue
            myModel.idleVCQueue.insert(this);
            // and wait for things to happen
            passivate();
         }

Now we will follow the "NO" branch, meaning "Yes, there is a truck waiting". We start with removing the first truck in line to get it serviced.

Due to the definition of the first() method of desmoj.core.simulator.ProcessQueue (API), we will only get a reference to the first object in line. But it will not yet be removed from the queue. Therefore we will have to do this afterwards in a separate step using the remove() method of this object.

         // ... lifeCycle() continued

         else {

            // YES, there is a customer (truck) waiting

            // get a reference to the first truck from the truck queue
            Truck nextTruck = myModel.truckQueue.first();
            // remove the truck from the queue
            myModel.truckQueue.remove(nextTruck);

            ...

Last, but not least, we are going to service the truck just removed from the queue. We will ask the model for the time we are going to need to service the truck and hold the execution of this lifeCycle() for the time determined. After that, we reactivate the just serviced truck to enable it to do some clean-up operation and to leave the system.

            // ... lifeCycle() continued

            // now serve it (fetch container and load it onto truck)
            // service time is represented by a hold of the VC process
            hold(new TimeSpan(myModel.getServiceTime(), TimeUnit.MINUTES));
            // from inside to outside...
            // ...draw a new period of service time
            // ...make a TimeSpan object out of it
            // ...and hold for this amount of time

            // now the truck has received its container and can leave
            // we will reactivate it, to allow it to finish its life cycle
            nextTruck.activate();
            // the VC can return to top and check for a new customer
         }
      }
   }


   
  http://desmoj.sourceforge.net/tutorial/processes/impl21.html