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
10
11
12
13
14
15
16
17
18
19
20 public class Container implements IntResourceRequestorInterface<Double>
21 {
22
23 private final DevsSimulatorInterface<Double> simulator;
24
25
26 private final int containerNumber;
27
28
29 private final QuayCrane qc;
30
31
32 private final Agv agv;
33
34
35 private final Ship ship;
36
37
38 private int phase = 0;
39
40
41
42
43
44
45
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
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 }