Events Step by StepModel Implementation (2)
The Service End Event |
||
Implementing the service end event is quite similar to implementing the truck arrival event; note, though, the the class named ServiceEndEvent is derived from EventOf2Entities as both a van carrier and a truck are referred to. Similarly to the truck arrival event, add the appropriate import statement and define the constructor. Then 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 service end event * in the EventsExample model. * It occurs when a van carrier finishes loading a truck. */ public class ServiceEndEvent extends EventOf2Entities<VanCarrier,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 service end event * * Used to create a new service end 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 ServiceEndEvent(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 that refers to a van carrier and a truck. Please refer to the conceptual model if you need a reminder of what happens when a van carrier finishes (un)loading a truck. First, the van carrier releases the truck so that it may leave the system. The van carrier is now free to (un)load another truck so we have to check if there are any trucks waiting. /** * This eventRoutine() describes what happens when a van carrier finishes * loading a truck. * * The truck leaves the system. * The van carrier will then check if there is another truck * waiting for service. * If there is another truck waiting it will service it. * If not it will wait on its parking spot for the next * customer to arrive. */ public void eventRoutine(VanCarrier vanCarrier, Truck truck) { // pass the departure of the truck to the trace sendTraceNote(truck + " leaves the terminal"); // check if there are other trucks waiting if (!myModel.truckQueue.isEmpty()) If there is at least one truck waiting (so that the queue is not empty), it can immediately proceed to be (un)loaded by the van carrier. This means, we take the first waiting truck out of the queue, have the van carrier keep a reference to it and schedule a new service end event (similar to the actions performed in the truck arrival event). { // YES, there is at least one other truck waiting // remove the first waiting truck from the queue Truck nextTruck = myModel.truckQueue.first(); myModel.truckQueue.remove(nextTruck); // create a new service end event ServiceEndEvent event = new ServiceEndEvent(myModel, "ServiceEndEvent", true); // and schedule it for at the appropriate time event.schedule(vanCarrier, nextTruck, new TimeSpan(myModel.getServiceTime(), TimeUnit.MINUTES)); } If there are no trucks waiting, the van carrier just returns to its parking spot to wait for a new truck to arrive. else { // NO, there are no trucks waiting // --> the van carrier is placed on its parking spot myModel.idleVCQueue.insert(vanCarrier); // the VC is now waiting for a new customer to arrive } } } |
||
http://desmoj.sourceforge.net/tutorial/events/impl23.html |