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   import java.util.HashSet;
8   import java.util.Set;
9   
10  import javax.naming.NamingException;
11  import javax.swing.JPanel;
12  import javax.swing.JScrollPane;
13  
14  import org.djunits.unit.LengthUnit;
15  import org.djunits.value.vdouble.scalar.Duration;
16  import org.djunits.value.vdouble.scalar.Length;
17  import org.djunits.value.vdouble.scalar.Time;
18  import org.djutils.io.URLResource;
19  import org.opentrafficsim.core.compatibility.Compatible;
20  import org.opentrafficsim.core.dsol.AbstractOTSModel;
21  import org.opentrafficsim.core.dsol.OTSAnimator;
22  import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
23  import org.opentrafficsim.core.gtu.RelativePosition;
24  import org.opentrafficsim.core.network.NetworkException;
25  import org.opentrafficsim.core.network.OTSNetwork;
26  import org.opentrafficsim.demo.trafficcontrol.TrafCODDemo.TrafCODModel;
27  import org.opentrafficsim.draw.core.OTSDrawingException;
28  import org.opentrafficsim.draw.road.TrafficLightAnimation;
29  import org.opentrafficsim.road.network.factory.xml.XmlNetworkLaneParser;
30  import org.opentrafficsim.road.network.lane.CrossSectionLink;
31  import org.opentrafficsim.road.network.lane.Lane;
32  import org.opentrafficsim.road.network.lane.object.sensor.TrafficLightSensor;
33  import org.opentrafficsim.road.network.lane.object.trafficlight.SimpleTrafficLight;
34  import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLight;
35  import org.opentrafficsim.swing.gui.OTSAnimationPanel;
36  import org.opentrafficsim.swing.gui.OTSSimulationApplication;
37  import org.opentrafficsim.trafficcontrol.TrafficController;
38  import org.opentrafficsim.trafficcontrol.trafcod.TrafCOD;
39  
40  import nl.tudelft.simulation.dsol.SimRuntimeException;
41  import nl.tudelft.simulation.event.EventInterface;
42  import nl.tudelft.simulation.event.EventListenerInterface;
43  import nl.tudelft.simulation.event.EventType;
44  
45  /**
46   * <p>
47   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
48   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
49   * <p>
50   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Nov 18, 2016 <br>
51   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
52   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
53   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
54   */
55  public class TrafCODDemo extends OTSSimulationApplication<TrafCODModel>
56  {
57      /** */
58      private static final long serialVersionUID = 20161118L;
59  
60      /**
61       * Create a TrafcodAndTurbo demo.
62       * @param title String; the title of the Frame
63       * @param panel OTSAnimationPanel; the tabbed panel to display
64       * @param model TrafCODModel; the model
65       * @throws OTSDrawingException on animation error
66       */
67      public TrafCODDemo(final String title, final OTSAnimationPanel panel, final TrafCODModel model) throws OTSDrawingException
68      {
69          super(model, panel);
70      }
71  
72      /**
73       * Main program.
74       * @param args String[]; the command line arguments (not used)
75       */
76      public static void main(final String[] args)
77      {
78          demo(true);
79      }
80  
81      /**
82       * Start the demo.
83       * @param exitOnClose boolean; when running stand-alone: true; when running as part of a demo: false
84       */
85      public static void demo(final boolean exitOnClose)
86      {
87          try
88          {
89              OTSAnimator simulator = new OTSAnimator();
90              final TrafCODModel trafcodModel = new TrafCODModel(simulator);
91              simulator.initialize(Time.ZERO, Duration.ZERO, Duration.createSI(3600.0), trafcodModel);
92              OTSAnimationPanel animationPanel = new OTSAnimationPanel(trafcodModel.getNetwork().getExtent(),
93                      new Dimension(800, 600), simulator, trafcodModel, DEFAULT_COLORER, trafcodModel.getNetwork());
94              TrafCODDemo app = new TrafCODDemo("TrafCOD demo simple crossing", animationPanel, trafcodModel);
95              app.setExitOnClose(exitOnClose);
96          }
97          catch (SimRuntimeException | NamingException | RemoteException | OTSDrawingException exception)
98          {
99              exception.printStackTrace();
100         }
101     }
102 
103     /**
104      * Add tab with trafCOD status.
105      */
106     protected 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                 XmlNetworkLaneParser nlp = new XmlNetworkLaneParser(getSimulator());
148                 this.network = nlp.build(url, true);
149 
150                 Lane laneNX = (Lane) ((CrossSectionLink) this.network.getLink("N", "X")).getCrossSectionElement("FORWARD");
151                 Lane laneWX = (Lane) ((CrossSectionLink) this.network.getLink("W", "X")).getCrossSectionElement("FORWARD");
152                 Set<TrafficLight> trafficLights = new HashSet<>();
153                 SimpleTrafficLight tl08 =
154                         new SimpleTrafficLight("TL08", laneWX, new Length(296, LengthUnit.METER), getSimulator());
155                 trafficLights.add(tl08);
156 
157                 try
158                 {
159                     new TrafficLightAnimation(tl08, this.simulator);
160                 }
161                 catch (RemoteException | NamingException exception)
162                 {
163                     throw new NetworkException(exception);
164                 }
165 
166                 SimpleTrafficLight tl11 =
167                         new SimpleTrafficLight("TL11", laneNX, new Length(296, LengthUnit.METER), getSimulator());
168                 trafficLights.add(tl11);
169 
170                 try
171                 {
172                     new TrafficLightAnimation(tl11, this.simulator);
173                 }
174                 catch (RemoteException | NamingException exception)
175                 {
176                     throw new NetworkException(exception);
177                 }
178 
179                 Set<TrafficLightSensor> sensors = new HashSet<>();
180                 sensors.add(new TrafficLightSensor("D081", laneWX, new Length(292, LengthUnit.METER), laneWX,
181                         new Length(294, LengthUnit.METER), null, RelativePosition.FRONT, RelativePosition.REAR, getSimulator(),
182                         Compatible.EVERYTHING));
183                 sensors.add(new TrafficLightSensor("D082", laneWX, new Length(260, LengthUnit.METER), laneWX,
184                         new Length(285, LengthUnit.METER), null, RelativePosition.FRONT, RelativePosition.REAR, getSimulator(),
185                         Compatible.EVERYTHING));
186                 sensors.add(new TrafficLightSensor("D111", laneNX, new Length(292, LengthUnit.METER), laneNX,
187                         new Length(294, LengthUnit.METER), null, RelativePosition.FRONT, RelativePosition.REAR, getSimulator(),
188                         Compatible.EVERYTHING));
189                 sensors.add(new TrafficLightSensor("D112", laneNX, new Length(260, LengthUnit.METER), laneNX,
190                         new Length(285, LengthUnit.METER), null, RelativePosition.FRONT, RelativePosition.REAR, getSimulator(),
191                         Compatible.EVERYTHING));
192                 String controllerName = "Simple TrafCOD controller";
193                 this.trafCOD = new TrafCOD(controllerName, URLResource.getResource("/TrafCODDemo1/simpleTest.tfc"),
194                         trafficLights, sensors, getSimulator(), this.controllerDisplayPanel);
195                 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_CONTROLLER_EVALUATING);
196                 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_CONTROLLER_WARNING);
197                 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_CONFLICT_GROUP_CHANGED);
198                 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_STATE_CHANGED);
199                 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_VARIABLE_CREATED);
200                 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_TRACED_VARIABLE_UPDATED);
201                 // Subscribe the TrafCOD machine to trace command events that we emit
202                 addListener(this.trafCOD, TrafficController.TRAFFICCONTROL_SET_TRACING);
203                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new Object[] {controllerName, "TGX", 8, true});
204                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new Object[] {controllerName, "XR1", 11, true});
205                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new Object[] {controllerName, "TD1", 11, true});
206                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new Object[] {controllerName, "TGX", 11, true});
207                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new Object[] {controllerName, "TL", 11, true});
208                 // System.out.println("demo: emitting a SET TRACING event for all variables related to stream 11");
209                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new Object[] { controllerName, "", 11, true });
210 
211                 // this.trafCOD.traceVariablesOfStream(TrafficController.NO_STREAM, true);
212                 // this.trafCOD.traceVariablesOfStream(11, true);
213                 // this.trafCOD.traceVariable("MRV", 11, true);
214             }
215             catch (Exception exception)
216             {
217                 exception.printStackTrace();
218             }
219         }
220 
221         /** {@inheritDoc} */
222         @Override
223         public final OTSNetwork getNetwork()
224         {
225             return this.network;
226         }
227 
228         /**
229          * @return trafCOD
230          */
231         public final TrafCOD getTrafCOD()
232         {
233             return this.trafCOD;
234         }
235 
236         /**
237          * @return controllerDisplayPanel
238          */
239         public final JPanel getControllerDisplayPanel()
240         {
241             return this.controllerDisplayPanel;
242         }
243 
244         /** {@inheritDoc} */
245         @Override
246         public void notify(final EventInterface event) throws RemoteException
247         {
248             EventType type = event.getType();
249             Object[] payload = (Object[]) event.getContent();
250             if (TrafficController.TRAFFICCONTROL_CONTROLLER_EVALUATING.equals(type))
251             {
252                 // System.out.println("Evalution starts at " + getSimulator().getSimulatorTime());
253                 return;
254             }
255             else if (TrafficController.TRAFFICCONTROL_CONFLICT_GROUP_CHANGED.equals(type))
256             {
257                 System.out.println("Conflict group changed from " + ((String) payload[1]) + " to " + ((String) payload[2]));
258             }
259             else if (TrafficController.TRAFFICCONTROL_TRACED_VARIABLE_UPDATED.equals(type))
260             {
261                 System.out.println(String.format("Variable changed %s <- %d   %s", payload[1], payload[4], payload[5]));
262             }
263             else if (TrafficController.TRAFFICCONTROL_CONTROLLER_WARNING.equals(type))
264             {
265                 System.out.println("Warning " + payload[1]);
266             }
267             else
268             {
269                 System.out.print("TrafCODDemo received event of type " + event.getType() + ", payload [");
270                 String separator = "";
271                 for (Object o : payload)
272                 {
273                     System.out.print(separator + o);
274                     separator = ",";
275                 }
276                 System.out.println("]");
277             }
278         }
279 
280     }
281 
282 }