View Javadoc
1   package org.opentrafficsim.demo.conflictAndControl;
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.LinkedHashSet;
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.animation.gtu.colorer.DefaultSwitchableGTUColorer;
20  import org.opentrafficsim.core.compatibility.Compatible;
21  import org.opentrafficsim.core.dsol.AbstractOTSModel;
22  import org.opentrafficsim.core.dsol.OTSAnimator;
23  import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
24  import org.opentrafficsim.core.gtu.GTUType;
25  import org.opentrafficsim.core.gtu.RelativePosition;
26  import org.opentrafficsim.core.network.NetworkException;
27  import org.opentrafficsim.demo.conflictAndControl.DemoTrafcodAndTurbo.TrafCODModel;
28  import org.opentrafficsim.draw.core.OTSDrawingException;
29  import org.opentrafficsim.draw.road.TrafficLightAnimation;
30  import org.opentrafficsim.road.network.OTSRoadNetwork;
31  import org.opentrafficsim.road.network.factory.xml.parser.XmlNetworkLaneParser;
32  import org.opentrafficsim.road.network.lane.CrossSectionLink;
33  import org.opentrafficsim.road.network.lane.CrossSectionLink.Priority;
34  import org.opentrafficsim.road.network.lane.Lane;
35  import org.opentrafficsim.road.network.lane.conflict.ConflictBuilder;
36  import org.opentrafficsim.road.network.lane.object.sensor.TrafficLightSensor;
37  import org.opentrafficsim.road.network.lane.object.trafficlight.SimpleTrafficLight;
38  import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLight;
39  import org.opentrafficsim.swing.gui.OTSAnimationPanel;
40  import org.opentrafficsim.swing.gui.OTSSimulationApplication;
41  import org.opentrafficsim.trafficcontrol.TrafficController;
42  import org.opentrafficsim.trafficcontrol.trafcod.TrafCOD;
43  
44  import nl.tudelft.simulation.dsol.SimRuntimeException;
45  import nl.tudelft.simulation.event.EventInterface;
46  import nl.tudelft.simulation.event.EventListenerInterface;
47  import nl.tudelft.simulation.event.EventType;
48  
49  /**
50   * <p>
51   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
52   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
53   * <p>
54   * @version $Revision: 5717 $, $LastChangedDate: 2019-11-14 14:36:06 +0100 (Thu, 14 Nov 2019) $, by $Author: averbraeck $,
55   *          initial version Dec 06, 2016 <br>
56   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
57   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
58   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
59   */
60  public class DemoTrafcodAndTurbo extends OTSSimulationApplication<TrafCODModel>
61  {
62      /** */
63      private static final long serialVersionUID = 20161118L;
64  
65      /**
66       * Create a TrafcodAndTurbo demo.
67       * @param title String; the title of the Frame
68       * @param panel OTSAnimationPanel; the tabbed panel to display
69       * @param model TrafCODModel; the model
70       * @throws OTSDrawingException on animation error
71       */
72      public DemoTrafcodAndTurbo(final String title, final OTSAnimationPanel panel, final TrafCODModel model)
73              throws OTSDrawingException
74      {
75          super(model, panel);
76      }
77  
78      /**
79       * Main program.
80       * @param args String[]; the command line arguments (not used)
81       */
82      public static void main(final String[] args)
83      {
84          demo(true);
85      }
86  
87      /**
88       * Start the demo.
89       * @param exitOnClose boolean; when running stand-alone: true; when running as part of a demo: false
90       */
91      public static void demo(final boolean exitOnClose)
92      {
93          try
94          {
95              OTSAnimator simulator = new OTSAnimator();
96              final TrafCODModel junctionModel = new TrafCODModel(simulator);
97              simulator.initialize(Time.ZERO, Duration.ZERO, Duration.instantiateSI(3600.0), junctionModel);
98              OTSAnimationPanel animationPanel =
99                      new OTSAnimationPanel(junctionModel.getNetwork().getExtent(), new Dimension(800, 600), simulator,
100                             junctionModel, new DefaultSwitchableGTUColorer(), junctionModel.getNetwork());
101             DemoTrafcodAndTurbotrol/DemoTrafcodAndTurbo.html#DemoTrafcodAndTurbo">DemoTrafcodAndTurbo app = new DemoTrafcodAndTurbo("TrafCOD Turbo demo", animationPanel, junctionModel);
102             app.setExitOnClose(exitOnClose);
103         }
104         catch (SimRuntimeException | NamingException | RemoteException | OTSDrawingException exception)
105         {
106             exception.printStackTrace();
107         }
108     }
109 
110     /**
111      * Add tab with trafCOD status.
112      */
113     @Override
114     protected void addTabs()
115     {
116         JScrollPane scrollPane = new JScrollPane(getModel().getControllerDisplayPanel());
117         JPanel wrapper = new JPanel(new BorderLayout());
118         wrapper.add(scrollPane);
119         getAnimationPanel().getTabbedPane().addTab(getAnimationPanel().getTabbedPane().getTabCount() - 1,
120                 getModel().getTrafCOD().getId(), wrapper);
121     }
122 
123     /**
124      * The simulation model.
125      */
126     static class TrafCODModel extends AbstractOTSModel implements EventListenerInterface
127     {
128         /** */
129         private static final long serialVersionUID = 20161020L;
130 
131         /** The network. */
132         private OTSRoadNetwork network;
133 
134         /** The TrafCOD controller. */
135         private TrafCOD trafCOD;
136 
137         /** TrafCOD controller display. */
138         private JPanel controllerDisplayPanel = new JPanel(new BorderLayout());
139 
140         /**
141          * @param simulator OTSSimulatorInterface; the simulator for this model
142          */
143         TrafCODModel(final OTSSimulatorInterface simulator)
144         {
145             super(simulator);
146         }
147 
148         /** {@inheritDoc} */
149         @Override
150         public void constructModel() throws SimRuntimeException
151         {
152             try
153             {
154                 URL xmlURL = URLResource.getResource("/conflictAndControl/TurboRoundaboutAndSignal.xml");
155                 this.network = new OTSRoadNetwork("TurboRoundaboutAndSignal", true);
156                 XmlNetworkLaneParser.build(xmlURL, this.network, getSimulator());
157 
158                 // add conflicts
159                 ((CrossSectionLink) this.network.getLink("EBNA")).setPriority(Priority.PRIORITY);
160                 ((CrossSectionLink) this.network.getLink("NBWA")).setPriority(Priority.PRIORITY);
161                 ((CrossSectionLink) this.network.getLink("WBSA")).setPriority(Priority.PRIORITY);
162                 ((CrossSectionLink) this.network.getLink("SBEA")).setPriority(Priority.PRIORITY);
163                 ConflictBuilder.buildConflicts(this.network, this.network.getGtuType(GTUType.DEFAULTS.VEHICLE), this.simulator,
164                         new ConflictBuilder.FixedWidthGenerator(new Length(2.0, LengthUnit.SI)));
165 
166                 // CrossSectionLink csLink = ((CrossSectionLink)
167                 // this.network.getLink("WWW"));
168                 // Lane lane = (Lane) csLink.getCrossSectionElement("RIGHT");
169                 // GTUColorer gtuColorer = null;
170                 // setupBlock(lane, (DEVSSimulatorInterface.TimeDoubleUnit) this.simulator,
171                 // gtuColorer );
172 
173                 String[] directions = {"E", "S", "W", "N"};
174                 // Add the traffic lights and the detectors
175                 Set<TrafficLight> trafficLights = new LinkedHashSet<>();
176                 Set<TrafficLightSensor> sensors = new LinkedHashSet<>();
177                 Length stopLineMargin = new Length(0.1, LengthUnit.METER);
178                 Length headDetectorLength = new Length(1, LengthUnit.METER);
179                 Length headDetectorMargin = stopLineMargin.plus(headDetectorLength).plus(new Length(3, LengthUnit.METER));
180                 Length longDetectorLength = new Length(30, LengthUnit.METER);
181                 Length longDetectorMargin = stopLineMargin.plus(longDetectorLength).plus(new Length(10, LengthUnit.METER));
182                 int stream = 1;
183                 for (String direction : directions)
184                 {
185                     for (int laneNumber = 3; laneNumber >= 1; laneNumber--)
186                     {
187                         Lane lane = (Lane) ((CrossSectionLink) this.network.getLink(direction + "S", direction + "C"))
188                                 .getCrossSectionElement("FORWARD" + laneNumber);
189                         if (lane != null)
190                         {
191                             if (stream != 7)
192                             {
193                                 TrafficLight tl = new SimpleTrafficLight(String.format("%02d", stream), lane,
194                                         lane.getLength().minus(stopLineMargin), this.simulator);
195                                 trafficLights.add(tl);
196 
197                                 try
198                                 {
199                                     new TrafficLightAnimation(tl, this.simulator);
200                                 }
201                                 catch (RemoteException | NamingException exception)
202                                 {
203                                     throw new NetworkException(exception);
204                                 }
205 
206                                 sensors.add(new TrafficLightSensor(String.format("D%02d1", stream), lane,
207                                         lane.getLength().minus(headDetectorMargin), lane,
208                                         lane.getLength().minus(headDetectorMargin).plus(headDetectorLength), null,
209                                         RelativePosition.FRONT, RelativePosition.REAR, this.simulator, Compatible.EVERYTHING));
210                                 sensors.add(new TrafficLightSensor(String.format("D%02d2", stream), lane,
211                                         lane.getLength().minus(longDetectorMargin), lane,
212                                         lane.getLength().minus(longDetectorMargin).plus(longDetectorLength), null,
213                                         RelativePosition.FRONT, RelativePosition.REAR, this.simulator, Compatible.EVERYTHING));
214                             }
215                             else
216                             {
217                                 lane = (Lane) ((CrossSectionLink) this.network.getLink("ESS1", "ESS"))
218                                         .getCrossSectionElement("FORWARD");
219                                 TrafficLight tl = new SimpleTrafficLight(String.format("%02d", stream), lane,
220                                         lane.getLength().minus(stopLineMargin), this.simulator);
221                                 trafficLights.add(tl);
222 
223                                 try
224                                 {
225                                     new TrafficLightAnimation(tl, this.simulator);
226                                 }
227                                 catch (RemoteException | NamingException exception)
228                                 {
229                                     throw new NetworkException(exception);
230                                 }
231 
232                                 sensors.add(new TrafficLightSensor(String.format("D%02d1", stream), lane,
233                                         lane.getLength().minus(headDetectorMargin), lane,
234                                         lane.getLength().minus(headDetectorMargin).plus(headDetectorLength), null,
235                                         RelativePosition.FRONT, RelativePosition.REAR, this.simulator, Compatible.EVERYTHING));
236                                 sensors.add(new TrafficLightSensor(String.format("D%02d2", stream), lane,
237                                         lane.getLength().minus(longDetectorMargin), lane,
238                                         lane.getLength().minus(longDetectorMargin).plus(longDetectorLength), null,
239                                         RelativePosition.FRONT, RelativePosition.REAR, this.simulator, Compatible.EVERYTHING));
240 
241                             }
242 
243                         }
244                         stream++;
245                     }
246                 }
247                 String controllerName = "Not so simple TrafCOD controller";
248                 this.trafCOD = new TrafCOD(controllerName, URLResource.getResource("/conflictAndControl/Intersection12Dir.tfc"),
249                         this.simulator, this.controllerDisplayPanel, null, null);
250                 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_CONTROLLER_EVALUATING);
251                 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_CONTROLLER_WARNING);
252                 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_CONFLICT_GROUP_CHANGED);
253                 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_STATE_CHANGED);
254                 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_VARIABLE_CREATED);
255                 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_TRACED_VARIABLE_UPDATED);
256                 // Subscribe the TrafCOD machine to trace command events that we
257                 // emit
258                 addListener(this.trafCOD, TrafficController.TRAFFICCONTROL_SET_TRACING);
259                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new
260                 // Object[] {controllerName, "TGX", 8, true});
261                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new
262                 // Object[] {controllerName, "XR1", 11, true});
263                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new
264                 // Object[] {controllerName, "TD1", 11, true});
265                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new
266                 // Object[] {controllerName, "TGX", 11, true});
267                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new
268                 // Object[] {controllerName, "TL", 11, true});
269                 // System.out.println("demo: emitting a SET TRACING event for
270                 // all variables related to stream 11");
271                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new
272                 // Object[] { controllerName, "", 11, true });
273 
274                 // TrafCODDemo2.this.trafCOD.traceVariablesOfStream(TrafficController.NO_STREAM,
275                 // true);
276                 // TrafCODDemo2.this.trafCOD.traceVariablesOfStream(11, true);
277                 // TrafCODDemo2.this.trafCOD.traceVariable("MRV", 11, true);
278             }
279             catch (Exception exception)
280             {
281                 exception.printStackTrace();
282             }
283         }
284 
285         /** {@inheritDoc} */
286         @Override
287         public final OTSRoadNetwork getNetwork()
288         {
289             return this.network;
290         }
291 
292         /**
293          * @return trafCOD
294          */
295         public final TrafCOD getTrafCOD()
296         {
297             return this.trafCOD;
298         }
299 
300         /**
301          * @return controllerDisplayPanel
302          */
303         public final JPanel getControllerDisplayPanel()
304         {
305             return this.controllerDisplayPanel;
306         }
307 
308         /** {@inheritDoc} */
309         @Override
310         public void notify(final EventInterface event) throws RemoteException
311         {
312             EventType type = event.getType();
313             Object[] payload = (Object[]) event.getContent();
314             if (TrafficController.TRAFFICCONTROL_CONTROLLER_EVALUATING.equals(type))
315             {
316                 // System.out.println("Evaluation starts at " +
317                 // getSimulator().getSimulatorTime());
318                 return;
319             }
320             else if (TrafficController.TRAFFICCONTROL_CONFLICT_GROUP_CHANGED.equals(type))
321             {
322                 System.out.println("Conflict group changed from " + ((String) payload[1]) + " to " + ((String) payload[2]));
323             }
324             else if (TrafficController.TRAFFICCONTROL_TRACED_VARIABLE_UPDATED.equals(type))
325             {
326                 System.out.println(String.format("Variable changed %s <- %d   %s", payload[1], payload[4], payload[5]));
327             }
328             else if (TrafficController.TRAFFICCONTROL_CONTROLLER_WARNING.equals(type))
329             {
330                 System.out.println("Warning " + payload[1]);
331             }
332             else
333             {
334                 System.out.print("TrafCODDemo received event of type " + event.getType() + ", payload [");
335                 String separator = "";
336                 for (Object o : payload)
337                 {
338                     System.out.print(separator + o);
339                     separator = ",";
340                 }
341                 System.out.println("]");
342             }
343         }
344     }
345 }