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.demo.trafficcontrol.TrafCODDemo.TrafCODModel;
24  import org.opentrafficsim.draw.core.OTSDrawingException;
25  import org.opentrafficsim.draw.road.TrafficLightAnimation;
26  import org.opentrafficsim.road.network.OTSRoadNetwork;
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) throws OTSDrawingException
65      {
66          super(model, panel);
67      }
68  
69      /**
70       * Main program.
71       * @param args String[]; the command line arguments (not used)
72       */
73      public static void main(final String[] args)
74      {
75          demo(true);
76      }
77  
78      /**
79       * Start the demo.
80       * @param exitOnClose boolean; when running stand-alone: true; when running as part of a demo: false
81       */
82      public static void demo(final boolean exitOnClose)
83      {
84          try
85          {
86              OTSAnimator simulator = new OTSAnimator();
87              final TrafCODModel trafcodModel = new TrafCODModel(simulator);
88              simulator.initialize(Time.ZERO, Duration.ZERO, Duration.createSI(3600.0), trafcodModel);
89              OTSAnimationPanel animationPanel = new OTSAnimationPanel(trafcodModel.getNetwork().getExtent(),
90                      new Dimension(800, 600), simulator, trafcodModel, DEFAULT_COLORER, trafcodModel.getNetwork());
91              TrafCODDemo app = new TrafCODDemo("TrafCOD demo simple crossing", animationPanel, trafcodModel);
92              app.setExitOnClose(exitOnClose);
93          }
94          catch (SimRuntimeException | NamingException | RemoteException | OTSDrawingException exception)
95          {
96              exception.printStackTrace();
97          }
98      }
99  
100     /**
101      * Add tab with trafCOD status.
102      */
103     @Override
104     protected final void addTabs()
105     {
106         JScrollPane scrollPane = new JScrollPane(getModel().getControllerDisplayPanel());
107         JPanel wrapper = new JPanel(new BorderLayout());
108         wrapper.add(scrollPane);
109         getAnimationPanel().getTabbedPane().addTab(getAnimationPanel().getTabbedPane().getTabCount() - 1,
110                 getModel().getTrafCOD().getId(), wrapper);
111     }
112 
113     /**
114      * The simulation model.
115      */
116     static class TrafCODModel extends AbstractOTSModel implements EventListenerInterface
117     {
118         /** */
119         private static final long serialVersionUID = 20161020L;
120 
121         /** the model. */
122         private OTSRoadNetwork network;
123 
124         /** The TrafCOD controller. */
125         private TrafCOD trafCOD;
126 
127         /** TrafCOD controller display. */
128         private JPanel controllerDisplayPanel = new JPanel(new BorderLayout());
129 
130         /**
131          * @param simulator OTSSimulatorInterface; the simulator for this model
132          */
133         TrafCODModel(final OTSSimulatorInterface simulator)
134         {
135             super(simulator);
136         }
137 
138         /** {@inheritDoc} */
139         @Override
140         public void constructModel() throws SimRuntimeException
141         {
142             try
143             {
144                 URL url = URLResource.getResource("/TrafCODDemo1/TrafCODDemo1.xml");
145                 XmlNetworkLaneParserOld nlp = new XmlNetworkLaneParserOld(getSimulator());
146                 this.network = nlp.build(url, true);
147                 String controllerName = "TrafCOD_simple";
148 
149                 Lane laneNX = (Lane) ((CrossSectionLink) this.network.getLink("N", "X")).getCrossSectionElement("FORWARD");
150                 Lane laneWX = (Lane) ((CrossSectionLink) this.network.getLink("W", "X")).getCrossSectionElement("FORWARD");
151                 SimpleTrafficLight tl08 = new SimpleTrafficLight(String.format("%s.%02d", controllerName, 8), laneWX,
152                         new Length(296, LengthUnit.METER), getSimulator());
153                 try
154                 {
155                     new TrafficLightAnimation(tl08, this.simulator);
156                 }
157                 catch (RemoteException | NamingException exception)
158                 {
159                     throw new NetworkException(exception);
160                 }
161 
162                 SimpleTrafficLight tl11 = new SimpleTrafficLight(String.format("%s.%02d", controllerName, 11), laneNX,
163                         new Length(296, LengthUnit.METER), getSimulator());
164                 try
165                 {
166                     new TrafficLightAnimation(tl11, this.simulator);
167                 }
168                 catch (RemoteException | NamingException exception)
169                 {
170                     throw new NetworkException(exception);
171                 }
172 
173                 new TrafficLightSensor(String.format("%s.D%02d1", controllerName, 8), laneWX, new Length(292, LengthUnit.METER),
174                         laneWX, new Length(294, LengthUnit.METER), null, RelativePosition.FRONT, RelativePosition.REAR,
175                         getSimulator(), Compatible.EVERYTHING);
176                 new TrafficLightSensor(String.format("%s.D%02d2", controllerName, 8), laneWX, new Length(260, LengthUnit.METER),
177                         laneWX, new Length(285, LengthUnit.METER), null, RelativePosition.FRONT, RelativePosition.REAR,
178                         getSimulator(), Compatible.EVERYTHING);
179                 new TrafficLightSensor(String.format("%s.D%02d1", controllerName, 11), laneNX,
180                         new Length(292, LengthUnit.METER), laneNX, new Length(294, LengthUnit.METER), null,
181                         RelativePosition.FRONT, RelativePosition.REAR, getSimulator(), Compatible.EVERYTHING);
182                 new TrafficLightSensor(String.format("%s.D%02d2", controllerName, 11), laneNX,
183                         new Length(260, LengthUnit.METER), laneNX, new Length(285, LengthUnit.METER), null,
184                         RelativePosition.FRONT, RelativePosition.REAR, getSimulator(), Compatible.EVERYTHING);
185                 this.trafCOD = new TrafCOD(controllerName, URLResource.getResource("/TrafCODDemo1/simpleTest.tfc"),
186                         getSimulator(), this.controllerDisplayPanel);
187                 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_CONTROLLER_EVALUATING);
188                 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_CONTROLLER_WARNING);
189                 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_CONFLICT_GROUP_CHANGED);
190                 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_STATE_CHANGED);
191                 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_VARIABLE_CREATED);
192                 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_TRACED_VARIABLE_UPDATED);
193                 // Subscribe the TrafCOD machine to trace command events that we emit
194                 addListener(this.trafCOD, TrafficController.TRAFFICCONTROL_SET_TRACING);
195                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new Object[] {controllerName, "TGX", 8, true});
196                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new Object[] {controllerName, "XR1", 11, true});
197                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new Object[] {controllerName, "TD1", 11, true});
198                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new Object[] {controllerName, "TGX", 11, true});
199                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new Object[] {controllerName, "TL", 11, true});
200                 // System.out.println("demo: emitting a SET TRACING event for all variables related to stream 11");
201                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new Object[] { controllerName, "", 11, true });
202 
203                 // this.trafCOD.traceVariablesOfStream(TrafficController.NO_STREAM, true);
204                 // this.trafCOD.traceVariablesOfStream(11, true);
205                 // this.trafCOD.traceVariable("MRV", 11, true);
206             }
207             catch (Exception exception)
208             {
209                 exception.printStackTrace();
210             }
211         }
212 
213         /** {@inheritDoc} */
214         @Override
215         public final OTSRoadNetwork getNetwork()
216         {
217             return this.network;
218         }
219 
220         /**
221          * @return trafCOD
222          */
223         public final TrafCOD getTrafCOD()
224         {
225             return this.trafCOD;
226         }
227 
228         /**
229          * @return controllerDisplayPanel
230          */
231         public final JPanel getControllerDisplayPanel()
232         {
233             return this.controllerDisplayPanel;
234         }
235 
236         /** {@inheritDoc} */
237         @Override
238         public void notify(final EventInterface event) throws RemoteException
239         {
240             EventType type = event.getType();
241             Object[] payload = (Object[]) event.getContent();
242             if (TrafficController.TRAFFICCONTROL_CONTROLLER_EVALUATING.equals(type))
243             {
244                 // System.out.println("Evalution starts at " + getSimulator().getSimulatorTime());
245                 return;
246             }
247             else if (TrafficController.TRAFFICCONTROL_CONFLICT_GROUP_CHANGED.equals(type))
248             {
249                 System.out.println("Conflict group changed from " + ((String) payload[1]) + " to " + ((String) payload[2]));
250             }
251             else if (TrafficController.TRAFFICCONTROL_TRACED_VARIABLE_UPDATED.equals(type))
252             {
253                 System.out.println(String.format("Variable changed %s <- %d   %s", payload[1], payload[4], payload[5]));
254             }
255             else if (TrafficController.TRAFFICCONTROL_CONTROLLER_WARNING.equals(type))
256             {
257                 System.out.println("Warning " + payload[1]);
258             }
259             else
260             {
261                 System.out.print("TrafCODDemo received event of type " + event.getType() + ", payload [");
262                 String separator = "";
263                 for (Object o : payload)
264                 {
265                     System.out.print(separator + o);
266                     separator = ",";
267                 }
268                 System.out.println("]");
269             }
270         }
271 
272     }
273 
274 }