View Javadoc
1   package nl.tudelft.simulation.examples.dsol.terminal;
2   
3   import java.rmi.RemoteException;
4   
5   import nl.tudelft.simulation.dsol.SimRuntimeException;
6   import nl.tudelft.simulation.dsol.simulators.DevsSimulatorInterface;
7   
8   /**
9    * The 'active' container object.
10   * <p>
11   * Copyright (c) 2002-2024 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
12   * for project information <a href="https://simulation.tudelft.nl/" target="_blank"> https://simulation.tudelft.nl</a>. The DSOL
13   * project is distributed under a three-clause BSD-style license, which can be found at
14   * <a href="https://https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">
15   * https://https://simulation.tudelft.nl/dsol/docs/latest/license.html</a>.
16   * </p>
17   * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs</a>
18   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
19   */
20  public class Container implements IntResourceRequestorInterface<Double>
21  {
22      /** the simulator. */
23      private final DevsSimulatorInterface<Double> simulator;
24  
25      /** the container number. */
26      private final int containerNumber;
27  
28      /** the QC resources. */
29      private final QuayCrane qc;
30  
31      /** the AGV resources. */
32      private final Agv agv;
33  
34      /** the ship. */
35      private final Ship ship;
36  
37      /** phase. */
38      private int phase = 0;
39  
40      /**
41       * @param simulator DevsSimulatorInterface&lt;Double&gt;; the simulator
42       * @param containerNumber int; the container number
43       * @param qc QC; the QC resources
44       * @param agv AGV; the AGV resources
45       * @param ship Ship; the ship
46       */
47      public Container(final DevsSimulatorInterface<Double> simulator, final int containerNumber, final QuayCrane qc, final Agv agv,
48              final Ship ship)
49      {
50          this.simulator = simulator;
51          this.containerNumber = containerNumber;
52          this.qc = qc;
53          this.agv = agv;
54          this.ship = ship;
55          synchronized (ship)
56          {
57              try
58              {
59                  if (Terminal.DEBUG)
60                  {
61                      System.out.println(
62                              "T = " + this.simulator.getSimulatorTime() + ", Claim AGV for container " + this.containerNumber);
63                  }
64                  this.simulator.scheduleEventAbs(39.0 * 60.0, this, "checkPhase", null);
65                  this.agv.requestCapacity(1, this);
66                  this.phase++;
67              }
68              catch (SimRuntimeException | RemoteException e)
69              {
70                  this.simulator.getLogger().always().error(e);
71              }
72          }
73      }
74  
75      /** {@inheritDoc} */
76      @Override
77      public synchronized void receiveRequestedResource(final long requestedCapacity,
78              final IntResource<Double> resource) throws RemoteException
79      {
80          try
81          {
82              if (resource instanceof Agv)
83              {
84                  this.phase++;
85                  this.simulator.scheduleEventRel(this.agv.drawDelay(), this, "agvReady", null);
86              }
87  
88              if (resource instanceof QuayCrane)
89              {
90                  if (Terminal.DEBUG)
91                  {
92                      System.out.println(
93                              "T = " + this.simulator.getSimulatorTime() + ", Claim QC for container " + this.containerNumber);
94                  }
95                  this.phase++;
96                  this.simulator.scheduleEventRel(this.qc.drawDelay(), this, "qcReady", null);
97              }
98          }
99          catch (SimRuntimeException e)
100         {
101             this.simulator.getLogger().always().error(e);
102         }
103     }
104 
105     /** */
106     protected synchronized void agvReady()
107     {
108         try
109         {
110             this.phase++;
111             if (Terminal.DEBUG)
112             {
113                 System.out.println(
114                         "T = " + this.simulator.getSimulatorTime() + ", AGV ready for container " + this.containerNumber);
115             }
116             this.agv.releaseCapacity(1);
117             this.qc.requestCapacity(1, this);
118         }
119         catch (SimRuntimeException | RemoteException e)
120         {
121             this.simulator.getLogger().always().error(e);
122         }
123     }
124 
125     /** */
126     protected synchronized void qcReady()
127     {
128         try
129         {
130             if (Terminal.DEBUG)
131             {
132                 System.out.println(
133                         "T = " + this.simulator.getSimulatorTime() + ", QC ready for container " + this.containerNumber);
134             }
135             this.qc.releaseCapacity(1);
136             this.phase++;
137             this.ship.incContainers();
138         }
139         catch (RemoteException e)
140         {
141             this.simulator.getLogger().always().error(e);
142         }
143     }
144 
145     /** */
146     protected void checkPhase()
147     {
148         if (this.phase != 5)
149         {
150             System.out.println("Container " + this.containerNumber + " was stuck in phase " + this.phase);
151         }
152     }
153 }