Events Step by StepModel Implementation (2)
The Truck Arrival Event |
||
To implement the truck arrival event derive a class named TruckArrivalEvent from Event: This is the subclass of AbstractEvent able to handle a single entity (the truck), 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.*; /** * This class represents the truck arrival event * in the EventsExample model. * It occures when a truck arrives at the terminal * to request loading of a container. */ public class TruckArrivalEvent extends Event<Truck> { /** * A reference to the model this event is a part of. * Useful shortcut to access the model's static components */ private EventsExample myModel; /** * Constructor of the truck arrival event * * Used to create a new truck arrival event * * @param owner the model this event belongs to * @param name this event's name * @param showInTrace flag to indicate if this event shall produce output * for the trace */ public TruckArrivalEvent(Model owner, String name, boolean showInTrace) { super(owner, name, showInTrace); // store a reference to the model this event is associated with myModel = (EventsExample)owner; } Now we have to implement the eventRoutine() method. Please refer to the conceptual model if you need a reminder of what happens when a truck arrives in the loading zone. We start with inserting the truck entity into the queue of waiting trucks (which models the parking lot) and checking if there is a van carrier availabe for service. /** * This eventRoutine() describes what happens when a truck * enters the terminal. * * On arrival, the truck will enter the queue (parking lot). It will then * check if the van carrier is available. * If this is the case, it will occupy the van carrier and schedule a * service end event. * Otherwise the truck just waits (does nothing). */ public void eventRoutine(Truck truck) { // truck enters parking-lot myModel.truckQueue.insert(truck); sendTraceNote("TruckQueueLength: "+ myModel.truckQueue.length()); // check if a VC is available if (!myModel.idleVCQueue.isEmpty()) We will now follow the "YES" branch of the statement, meaning there is at least one van carrier available, so service can start immediately. For this we have to
We already mentioned that the reason for inserting the van carrier into the busyVCQueue is that we need to keep a reference to the van carrier past the termination of the current eventRoutine(). { // yes, it is // get a reference to the first VC from the idle VC queue VanCarrier vanCarrier = myModel.idleVCQueue.first(); // remove it from the queue myModel.idleVCQueue.remove(vanCarrier); // remove the truck from the queue myModel.truckQueue.remove(truck); // create a service end event ServiceEndEvent serviceEnd = new ServiceEndEvent (myModel, "ServiceEndEvent", true); // and place it on the event list serviceEnd.schedule(vanCarrier, truck, new TimeSpan(myModel.getServiceTime(), TimeUnit.MINUTES)); } } } Observe that two entities (a van carrier and a truck) are passed to the ServiceEndEvent when calling it's schedule(...) method: A ServiceEndEvent refers to two entities as we will see on the next page. We don't need to implement a "NO" branch for the if-statement, because in case there is no VC available, there is nothing the truck has to do at the moment; remember we have already inserted it into the appropriate queue at the beginning of the event routine - preliminarily, the truck will stay in this queue. |
||
http://desmoj.sourceforge.net/tutorial/events/impl22.html |