View Javadoc
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   * <p>
44   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
45   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
46   * <p>
47   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Nov 18, 2016 <br>
48   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
49   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
50   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
51   */
52  public class TrafCODDemo extends OTSSimulationApplication<TrafCODModel>
53  {
54      /** */
55      private static final long serialVersionUID = 20161118L;
56  
57      /**
58       * Create a TrafcodAndTurbo demo.
59       * @param title String; the title of the Frame
60       * @param panel OTSAnimationPanel; the tabbed panel to display
61       * @param model TrafCODModel; the model
62       * @throws OTSDrawingException on animation error
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       * Main program.
72       * @param args String[]; the command line arguments (not used)
73       */
74      public static void main(final String[] args)
75      {
76          demo(true);
77      }
78  
79      /**
80       * Start the demo.
81       * @param exitOnClose boolean; when running stand-alone: true; when running as part of a demo: false
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      * Add tab with trafCOD status.
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      * The simulation model.
117      */
118     static class TrafCODModel extends AbstractOTSModel implements EventListenerInterface
119     {
120         /** */
121         private static final long serialVersionUID = 20161020L;
122 
123         /** the model. */
124         private OTSNetwork network;
125 
126         /** The TrafCOD controller. */
127         private TrafCOD trafCOD;
128 
129         /** TrafCOD controller display. */
130         private JPanel controllerDisplayPanel = new JPanel(new BorderLayout());
131 
132         /**
133          * @param simulator OTSSimulatorInterface; the simulator for this model
134          */
135         TrafCODModel(final OTSSimulatorInterface simulator)
136         {
137             super(simulator);
138         }
139 
140         /** {@inheritDoc} */
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                 // Subscribe the TrafCOD machine to trace command events that we emit
199                 addListener(this.trafCOD, TrafficController.TRAFFICCONTROL_SET_TRACING);
200                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new Object[] {controllerName, "TGX", 8, true});
201                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new Object[] {controllerName, "XR1", 11, true});
202                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new Object[] {controllerName, "TD1", 11, true});
203                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new Object[] {controllerName, "TGX", 11, true});
204                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new Object[] {controllerName, "TL", 11, true});
205                 // System.out.println("demo: emitting a SET TRACING event for all variables related to stream 11");
206                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new Object[] { controllerName, "", 11, true });
207 
208                 // this.trafCOD.traceVariablesOfStream(TrafficController.NO_STREAM, true);
209                 // this.trafCOD.traceVariablesOfStream(11, true);
210                 // this.trafCOD.traceVariable("MRV", 11, true);
211             }
212             catch (Exception exception)
213             {
214                 exception.printStackTrace();
215             }
216         }
217 
218         /** {@inheritDoc} */
219         @Override
220         public final OTSNetwork getNetwork()
221         {
222             return this.network;
223         }
224 
225         /**
226          * @return trafCOD
227          */
228         public final TrafCOD getTrafCOD()
229         {
230             return this.trafCOD;
231         }
232 
233         /**
234          * @return controllerDisplayPanel
235          */
236         public final JPanel getControllerDisplayPanel()
237         {
238             return this.controllerDisplayPanel;
239         }
240 
241         /** {@inheritDoc} */
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                 // System.out.println("Evalution starts at " + getSimulator().getSimulatorTime());
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 }