1   package org.opentrafficsim.demo.trafficcontrol;
2   
3   import java.awt.BorderLayout;
4   import java.awt.Dimension;
5   import java.net.URL;
6   import java.rmi.RemoteException;
7   
8   import javax.naming.NamingException;
9   import javax.swing.JPanel;
10  import javax.swing.JScrollPane;
11  
12  import org.djunits.unit.LengthUnit;
13  import org.djunits.value.vdouble.scalar.Duration;
14  import org.djunits.value.vdouble.scalar.Length;
15  import org.djunits.value.vdouble.scalar.Time;
16  import org.djutils.io.URLResource;
17  import org.opentrafficsim.core.compatibility.Compatible;
18  import org.opentrafficsim.core.dsol.AbstractOTSModel;
19  import org.opentrafficsim.core.dsol.OTSAnimator;
20  import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
21  import org.opentrafficsim.core.gtu.RelativePosition;
22  import org.opentrafficsim.core.network.NetworkException;
23  import org.opentrafficsim.core.network.OTSNetwork;
24  import org.opentrafficsim.demo.trafficcontrol.TrafCODDemo.TrafCODModel;
25  import org.opentrafficsim.draw.core.OTSDrawingException;
26  import org.opentrafficsim.draw.road.TrafficLightAnimation;
27  import org.opentrafficsim.road.network.factory.xml.old.XmlNetworkLaneParserOld;
28  import org.opentrafficsim.road.network.lane.CrossSectionLink;
29  import org.opentrafficsim.road.network.lane.Lane;
30  import org.opentrafficsim.road.network.lane.object.sensor.TrafficLightSensor;
31  import org.opentrafficsim.road.network.lane.object.trafficlight.SimpleTrafficLight;
32  import org.opentrafficsim.swing.gui.OTSAnimationPanel;
33  import org.opentrafficsim.swing.gui.OTSSimulationApplication;
34  import org.opentrafficsim.trafficcontrol.TrafficController;
35  import org.opentrafficsim.trafficcontrol.trafcod.TrafCOD;
36  
37  import nl.tudelft.simulation.dsol.SimRuntimeException;
38  import nl.tudelft.simulation.event.EventInterface;
39  import nl.tudelft.simulation.event.EventListenerInterface;
40  import nl.tudelft.simulation.event.EventType;
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  public class TrafCODDemo extends OTSSimulationApplication<TrafCODModel>
53  {
54      
55      private static final long serialVersionUID = 20161118L;
56  
57      
58  
59  
60  
61  
62  
63  
64      public TrafCODDemo(final String title, final OTSAnimationPanel panel, final TrafCODModel model)
65              throws OTSDrawingException
66      {
67          super(model, panel);
68      }
69  
70      
71  
72  
73  
74      public static void main(final String[] args)
75      {
76          demo(true);
77      }
78  
79      
80  
81  
82  
83      public static void demo(final boolean exitOnClose)
84      {
85          try
86          {
87              OTSAnimator simulator = new OTSAnimator();
88              final TrafCODModel trafcodModel = new TrafCODModel(simulator);
89              simulator.initialize(Time.ZERO, Duration.ZERO, Duration.createSI(3600.0), trafcodModel);
90              OTSAnimationPanel animationPanel =
91                      new OTSAnimationPanel(trafcodModel.getNetwork().getExtent(), new Dimension(800, 600), simulator,
92                              trafcodModel, DEFAULT_COLORER, trafcodModel.getNetwork());
93              TrafCODDemo app = new TrafCODDemo("TrafCOD demo simple crossing", animationPanel, trafcodModel);
94              app.setExitOnClose(exitOnClose);
95          }
96          catch (SimRuntimeException | NamingException | RemoteException | OTSDrawingException exception)
97          {
98              exception.printStackTrace();
99          }
100     }
101 
102     
103 
104 
105     @Override
106     protected final void addTabs()
107     {
108         JScrollPane scrollPane = new JScrollPane(getModel().getControllerDisplayPanel());
109         JPanel wrapper = new JPanel(new BorderLayout());
110         wrapper.add(scrollPane);
111         getAnimationPanel().getTabbedPane().addTab(getAnimationPanel().getTabbedPane().getTabCount() - 1,
112                 getModel().getTrafCOD().getId(), wrapper);
113     }
114 
115     
116 
117 
118     static class TrafCODModel extends AbstractOTSModel implements EventListenerInterface
119     {
120         
121         private static final long serialVersionUID = 20161020L;
122 
123         
124         private OTSNetwork network;
125 
126         
127         private TrafCOD trafCOD;
128 
129         
130         private JPanel controllerDisplayPanel = new JPanel(new BorderLayout());
131 
132         
133 
134 
135         TrafCODModel(final OTSSimulatorInterface simulator)
136         {
137             super(simulator);
138         }
139 
140         
141         @Override
142         public void constructModel() throws SimRuntimeException
143         {
144             try
145             {
146                 URL url = URLResource.getResource("/TrafCODDemo1/TrafCODDemo1.xml");
147                 XmlNetworkLaneParserOld nlp = new XmlNetworkLaneParserOld(getSimulator());
148                 this.network = nlp.build(url, true);
149                 String controllerName = "TrafCOD_simple";
150 
151                 Lane laneNX = (Lane) ((CrossSectionLink) this.network.getLink("N", "X")).getCrossSectionElement("FORWARD");
152                 Lane laneWX = (Lane) ((CrossSectionLink) this.network.getLink("W", "X")).getCrossSectionElement("FORWARD");
153                 SimpleTrafficLight tl08 =
154                         new SimpleTrafficLight(String.format("%s.%02d", controllerName, 8), laneWX, new Length(296,
155                                 LengthUnit.METER), getSimulator());
156                 try
157                 {
158                     new TrafficLightAnimation(tl08, this.simulator);
159                 }
160                 catch (RemoteException | NamingException exception)
161                 {
162                     throw new NetworkException(exception);
163                 }
164 
165                 SimpleTrafficLight tl11 =
166                         new SimpleTrafficLight(String.format("%s.%02d", controllerName, 11), laneNX, new Length(296,
167                                 LengthUnit.METER), getSimulator());
168                 try
169                 {
170                     new TrafficLightAnimation(tl11, this.simulator);
171                 }
172                 catch (RemoteException | NamingException exception)
173                 {
174                     throw new NetworkException(exception);
175                 }
176 
177                 new TrafficLightSensor(String.format("%s.D%02d1", controllerName, 8), laneWX, new Length(292,
178                         LengthUnit.METER), laneWX, new Length(294, LengthUnit.METER), null, RelativePosition.FRONT,
179                         RelativePosition.REAR, getSimulator(), Compatible.EVERYTHING);
180                 new TrafficLightSensor(String.format("%s.D%02d2", controllerName, 8), laneWX, new Length(260,
181                         LengthUnit.METER), laneWX, new Length(285, LengthUnit.METER), null, RelativePosition.FRONT,
182                         RelativePosition.REAR, getSimulator(), Compatible.EVERYTHING);
183                 new TrafficLightSensor(String.format("%s.D%02d1", controllerName, 11), laneNX, new Length(292,
184                         LengthUnit.METER), laneNX, new Length(294, LengthUnit.METER), null, RelativePosition.FRONT,
185                         RelativePosition.REAR, getSimulator(), Compatible.EVERYTHING);
186                 new TrafficLightSensor(String.format("%s.D%02d2", controllerName, 11), laneNX, new Length(260,
187                         LengthUnit.METER), laneNX, new Length(285, LengthUnit.METER), null, RelativePosition.FRONT,
188                         RelativePosition.REAR, getSimulator(), Compatible.EVERYTHING);
189                 this.trafCOD =
190                         new TrafCOD(controllerName, URLResource.getResource("/TrafCODDemo1/simpleTest.tfc"), getSimulator(),
191                                 this.controllerDisplayPanel);
192                 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_CONTROLLER_EVALUATING);
193                 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_CONTROLLER_WARNING);
194                 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_CONFLICT_GROUP_CHANGED);
195                 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_STATE_CHANGED);
196                 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_VARIABLE_CREATED);
197                 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_TRACED_VARIABLE_UPDATED);
198                 
199                 addListener(this.trafCOD, TrafficController.TRAFFICCONTROL_SET_TRACING);
200                 
201                 
202                 
203                 
204                 
205                 
206                 
207 
208                 
209                 
210                 
211             }
212             catch (Exception exception)
213             {
214                 exception.printStackTrace();
215             }
216         }
217 
218         
219         @Override
220         public final OTSNetwork getNetwork()
221         {
222             return this.network;
223         }
224 
225         
226 
227 
228         public final TrafCOD getTrafCOD()
229         {
230             return this.trafCOD;
231         }
232 
233         
234 
235 
236         public final JPanel getControllerDisplayPanel()
237         {
238             return this.controllerDisplayPanel;
239         }
240 
241         
242         @Override
243         public void notify(final EventInterface event) throws RemoteException
244         {
245             EventType type = event.getType();
246             Object[] payload = (Object[]) event.getContent();
247             if (TrafficController.TRAFFICCONTROL_CONTROLLER_EVALUATING.equals(type))
248             {
249                 
250                 return;
251             }
252             else if (TrafficController.TRAFFICCONTROL_CONFLICT_GROUP_CHANGED.equals(type))
253             {
254                 System.out.println("Conflict group changed from " + ((String) payload[1]) + " to " + ((String) payload[2]));
255             }
256             else if (TrafficController.TRAFFICCONTROL_TRACED_VARIABLE_UPDATED.equals(type))
257             {
258                 System.out.println(String.format("Variable changed %s <- %d   %s", payload[1], payload[4], payload[5]));
259             }
260             else if (TrafficController.TRAFFICCONTROL_CONTROLLER_WARNING.equals(type))
261             {
262                 System.out.println("Warning " + payload[1]);
263             }
264             else
265             {
266                 System.out.print("TrafCODDemo received event of type " + event.getType() + ", payload [");
267                 String separator = "";
268                 for (Object o : payload)
269                 {
270                     System.out.print(separator + o);
271                     separator = ",";
272                 }
273                 System.out.println("]");
274             }
275         }
276 
277     }
278 
279 }