View Javadoc
1   package org.opentrafficsim.demo.conflictAndControl;
2   
3   import static org.opentrafficsim.core.gtu.GTUType.VEHICLE;
4   import static org.opentrafficsim.road.gtu.lane.RoadGTUTypes.CAR;
5   
6   import java.awt.BorderLayout;
7   import java.awt.geom.Rectangle2D;
8   import java.awt.geom.Rectangle2D.Double;
9   import java.net.URL;
10  import java.rmi.RemoteException;
11  import java.util.ArrayList;
12  import java.util.HashSet;
13  import java.util.LinkedHashSet;
14  import java.util.Set;
15  
16  import javax.naming.NamingException;
17  import javax.swing.JPanel;
18  import javax.swing.JScrollPane;
19  import javax.swing.SwingUtilities;
20  
21  import org.djunits.unit.AccelerationUnit;
22  import org.djunits.unit.DurationUnit;
23  import org.djunits.unit.LengthUnit;
24  import org.djunits.value.vdouble.scalar.Acceleration;
25  import org.djunits.value.vdouble.scalar.Duration;
26  import org.djunits.value.vdouble.scalar.Length;
27  import org.djunits.value.vdouble.scalar.Speed;
28  import org.djunits.value.vdouble.scalar.Time;
29  import org.opentrafficsim.base.modelproperties.Property;
30  import org.opentrafficsim.base.modelproperties.PropertyException;
31  import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
32  import org.opentrafficsim.core.dsol.OTSModelInterface;
33  import org.opentrafficsim.core.dsol.OTSSimTimeDouble;
34  import org.opentrafficsim.core.geometry.OTSGeometryException;
35  import org.opentrafficsim.core.gtu.GTUDirectionality;
36  import org.opentrafficsim.core.gtu.GTUException;
37  import org.opentrafficsim.core.gtu.GTUType;
38  import org.opentrafficsim.core.gtu.RelativePosition;
39  import org.opentrafficsim.core.gtu.animation.GTUColorer;
40  import org.opentrafficsim.core.gtu.behavioralcharacteristics.BehavioralCharacteristics;
41  import org.opentrafficsim.core.network.NetworkException;
42  import org.opentrafficsim.core.network.OTSNetwork;
43  import org.opentrafficsim.demo.carFollowing.DefaultsFactory;
44  import org.opentrafficsim.road.animation.AnimationToggles;
45  import org.opentrafficsim.road.gtu.animation.DefaultCarAnimation;
46  import org.opentrafficsim.road.gtu.lane.LaneBasedIndividualGTU;
47  import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedGTUFollowingTacticalPlanner;
48  import org.opentrafficsim.road.gtu.lane.tactical.following.IDMPlusOld;
49  import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner;
50  import org.opentrafficsim.road.gtu.strategical.route.LaneBasedStrategicalRoutePlanner;
51  import org.opentrafficsim.road.network.factory.xml.XmlNetworkLaneParser;
52  import org.opentrafficsim.road.network.lane.CrossSectionLink;
53  import org.opentrafficsim.road.network.lane.CrossSectionLink.Priority;
54  import org.opentrafficsim.road.network.lane.DirectedLanePosition;
55  import org.opentrafficsim.road.network.lane.Lane;
56  import org.opentrafficsim.road.network.lane.conflict.ConflictBuilder;
57  import org.opentrafficsim.road.network.lane.object.sensor.TrafficLightSensor;
58  import org.opentrafficsim.road.network.lane.object.trafficlight.SimpleTrafficLight;
59  import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLight;
60  import org.opentrafficsim.simulationengine.AbstractWrappableAnimation;
61  import org.opentrafficsim.simulationengine.OTSSimulationException;
62  import org.opentrafficsim.simulationengine.SimpleSimulatorInterface;
63  import org.opentrafficsim.trafficcontrol.TrafficController;
64  import org.opentrafficsim.trafficcontrol.trafcod.TrafCOD;
65  
66  import nl.tudelft.simulation.dsol.SimRuntimeException;
67  import nl.tudelft.simulation.dsol.simulators.DEVSSimulator;
68  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
69  import nl.tudelft.simulation.event.EventInterface;
70  import nl.tudelft.simulation.event.EventListenerInterface;
71  import nl.tudelft.simulation.event.EventProducer;
72  import nl.tudelft.simulation.event.EventType;
73  import nl.tudelft.simulation.language.io.URLResource;
74  
75  /**
76   * <p>
77   * Copyright (c) 2013-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
78   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
79   * <p>
80   * @version $Revision: 3579 $, $LastChangedDate: 2017-04-29 15:37:05 +0200 (Sat, 29 Apr 2017) $, by $Author: averbraeck $,
81   *          initial version Dec 06, 2016 <br>
82   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
83   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
84   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
85   */
86  public class DemoTrafcodAndTurbo extends AbstractWrappableAnimation
87  {
88  
89      /** */
90      private static final long serialVersionUID = 20161118L;
91  
92      /**
93       * Main program.
94       * @param args String[]; the command line arguments (not used)
95       * @throws SimRuntimeException should never happen
96       */
97      public static void main(final String[] args) throws SimRuntimeException
98      {
99          SwingUtilities.invokeLater(new Runnable()
100         {
101             @Override
102             public void run()
103             {
104                 try
105                 {
106                     DemoTrafcodAndTurbo model = new DemoTrafcodAndTurbo();
107                     // 1 hour simulation run for testing
108                     model.buildAnimator(Time.ZERO, Duration.ZERO, new Duration(60.0, DurationUnit.MINUTE),
109                             new ArrayList<Property<?>>(), null, true);
110                 }
111                 catch (SimRuntimeException | NamingException | OTSSimulationException | PropertyException exception)
112                 {
113                     exception.printStackTrace();
114                 }
115             }
116         });
117     }
118 
119     /** TrafCOD controller display. */
120     private JPanel controllerDisplayPanel = new JPanel(new BorderLayout());
121 
122     /** The TrafCOD controller. */
123     TrafCOD trafCOD;
124 
125     /** {@inheritDoc} */
126     @Override
127     public final String shortName()
128     {
129         return "TrafCOD demonstration 2";
130     }
131 
132     /** {@inheritDoc} */
133     @Override
134     public final String description()
135     {
136         return "TrafCOD demonstration";
137     }
138 
139     /** {@inheritDoc} */
140     @Override
141     protected final void addTabs(final SimpleSimulatorInterface simulator) throws OTSSimulationException, PropertyException
142     {
143         JScrollPane scrollPane = new JScrollPane(DemoTrafcodAndTurbo.this.controllerDisplayPanel);
144         JPanel wrapper = new JPanel(new BorderLayout());
145         wrapper.add(scrollPane);
146         addTab(getTabCount() - 1, this.trafCOD.getId(), wrapper);
147     }
148 
149     /** {@inheritDoc} */
150     @Override
151     protected final OTSModelInterface makeModel(final GTUColorer colorer) throws OTSSimulationException
152     {
153         return new TrafCODModel();
154     }
155 
156     /** {@inheritDoc} */
157     @Override
158     protected final void addAnimationToggles()
159     {
160         AnimationToggles.setTextAnimationTogglesStandard(this);
161     }
162 
163     /** {@inheritDoc} */
164     @Override
165     protected final Double makeAnimationRectangle()
166     {
167         return new Rectangle2D.Double(-200, -200, 400, 400);
168     }
169 
170     /**
171      * The simulation model.
172      */
173     class TrafCODModel extends EventProducer implements OTSModelInterface, EventListenerInterface
174     {
175         /** */
176         private static final long serialVersionUID = 20161020L;
177 
178         /** The network. */
179         private OTSNetwork network;
180 
181         @SuppressWarnings("synthetic-access")
182         @Override
183         public void constructModel(final SimulatorInterface<Time, Duration, OTSSimTimeDouble> theSimulator)
184                 throws SimRuntimeException, RemoteException
185         {
186             try
187             {
188                 URL url = URLResource.getResource("/conflictAndControl/TurboRoundaboutAndSignal.xml");
189                 XmlNetworkLaneParser nlp = new XmlNetworkLaneParser((OTSDEVSSimulatorInterface) theSimulator);
190                 this.network = nlp.build(url);
191                 // add conflicts
192                 ((CrossSectionLink) this.network.getLink("EBNA")).setPriority(Priority.PRIORITY);
193                 ((CrossSectionLink) this.network.getLink("NBWA")).setPriority(Priority.PRIORITY);
194                 ((CrossSectionLink) this.network.getLink("WBSA")).setPriority(Priority.PRIORITY);
195                 ((CrossSectionLink) this.network.getLink("SBEA")).setPriority(Priority.PRIORITY);
196                 ConflictBuilder.buildConflicts(this.network, VEHICLE, (OTSDEVSSimulatorInterface) theSimulator,
197                         new ConflictBuilder.FixedWidthGenerator(new Length(2.0, LengthUnit.SI)));
198 
199                 // CrossSectionLink csLink = ((CrossSectionLink)
200                 // this.network.getLink("WWW"));
201                 // Lane lane = (Lane) csLink.getCrossSectionElement("RIGHT");
202                 // GTUColorer gtuColorer = null;
203                 // setupBlock(lane, (OTSDEVSSimulatorInterface) theSimulator,
204                 // gtuColorer );
205 
206                 String[] directions = { "E", "S", "W", "N" };
207                 // Add the traffic lights and the detectors
208                 Set<TrafficLight> trafficLights = new HashSet<>();
209                 Set<TrafficLightSensor> sensors = new HashSet<>();
210                 Length stopLineMargin = new Length(0.1, LengthUnit.METER);
211                 Length headDetectorLength = new Length(1, LengthUnit.METER);
212                 Length headDetectorMargin = stopLineMargin.plus(headDetectorLength).plus(new Length(3, LengthUnit.METER));
213                 Length longDetectorLength = new Length(30, LengthUnit.METER);
214                 Length longDetectorMargin = stopLineMargin.plus(longDetectorLength).plus(new Length(10, LengthUnit.METER));
215                 int stream = 1;
216                 for (String direction : directions)
217                 {
218                     for (int laneNumber = 3; laneNumber >= 1; laneNumber--)
219                     {
220                         Lane lane = (Lane) ((CrossSectionLink) this.network.getLink(direction + "S", direction + "C"))
221                                 .getCrossSectionElement("FORWARD" + laneNumber);
222                         if (lane != null)
223                         {
224                             if (stream != 7)
225                             {
226                                 trafficLights.add(new SimpleTrafficLight(String.format("TL%02d", stream), lane,
227                                         lane.getLength().minus(stopLineMargin), (OTSDEVSSimulatorInterface) theSimulator));
228                                 sensors.add(new TrafficLightSensor(String.format("D%02d1", stream), lane,
229                                         lane.getLength().minus(headDetectorMargin), lane,
230                                         lane.getLength().minus(headDetectorMargin).plus(headDetectorLength), null,
231                                         RelativePosition.FRONT, RelativePosition.REAR,
232                                         (OTSDEVSSimulatorInterface) theSimulator));
233                                 sensors.add(new TrafficLightSensor(String.format("D%02d2", stream), lane,
234                                         lane.getLength().minus(longDetectorMargin), lane,
235                                         lane.getLength().minus(longDetectorMargin).plus(longDetectorLength), null,
236                                         RelativePosition.FRONT, RelativePosition.REAR,
237                                         (OTSDEVSSimulatorInterface) theSimulator));
238                             }
239                             else
240                             {
241                                 lane = (Lane) ((CrossSectionLink) this.network.getLink("ESS1", "ESS"))
242                                         .getCrossSectionElement("FORWARD");
243                                 trafficLights.add(new SimpleTrafficLight(String.format("TL%02d", stream), lane,
244                                         lane.getLength().minus(stopLineMargin), (OTSDEVSSimulatorInterface) theSimulator));
245                                 sensors.add(new TrafficLightSensor(String.format("D%02d1", stream), lane,
246                                         lane.getLength().minus(headDetectorMargin), lane,
247                                         lane.getLength().minus(headDetectorMargin).plus(headDetectorLength), null,
248                                         RelativePosition.FRONT, RelativePosition.REAR,
249                                         (OTSDEVSSimulatorInterface) theSimulator));
250                                 sensors.add(new TrafficLightSensor(String.format("D%02d2", stream), lane,
251                                         lane.getLength().minus(longDetectorMargin), lane,
252                                         lane.getLength().minus(longDetectorMargin).plus(longDetectorLength), null,
253                                         RelativePosition.FRONT, RelativePosition.REAR,
254                                         (OTSDEVSSimulatorInterface) theSimulator));
255 
256                             }
257 
258                         }
259                         stream++;
260                     }
261                 }
262                 String controllerName = "Not so simple TrafCOD controller";
263                 DemoTrafcodAndTurbo.this.trafCOD =
264                         new TrafCOD(controllerName, URLResource.getResource("/conflictAndControl/Intersection12Dir.tfc"),
265                                 trafficLights, sensors, (DEVSSimulator<Time, Duration, OTSSimTimeDouble>) theSimulator,
266                                 DemoTrafcodAndTurbo.this.controllerDisplayPanel);
267                 DemoTrafcodAndTurbo.this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_CONTROLLER_EVALUATING);
268                 DemoTrafcodAndTurbo.this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_CONTROLLER_WARNING);
269                 DemoTrafcodAndTurbo.this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_CONFLICT_GROUP_CHANGED);
270                 DemoTrafcodAndTurbo.this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_STATE_CHANGED);
271                 DemoTrafcodAndTurbo.this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_VARIABLE_CREATED);
272                 DemoTrafcodAndTurbo.this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_TRACED_VARIABLE_UPDATED);
273                 // Subscribe the TrafCOD machine to trace command events that we
274                 // emit
275                 addListener(DemoTrafcodAndTurbo.this.trafCOD, TrafficController.TRAFFICCONTROL_SET_TRACING);
276                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new
277                 // Object[] {controllerName, "TGX", 8, true});
278                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new
279                 // Object[] {controllerName, "XR1", 11, true});
280                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new
281                 // Object[] {controllerName, "TD1", 11, true});
282                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new
283                 // Object[] {controllerName, "TGX", 11, true});
284                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new
285                 // Object[] {controllerName, "TL", 11, true});
286                 // System.out.println("demo: emitting a SET TRACING event for
287                 // all variables related to stream 11");
288                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new
289                 // Object[] { controllerName, "", 11, true });
290 
291                 // TrafCODDemo2.this.trafCOD.traceVariablesOfStream(TrafficController.NO_STREAM,
292                 // true);
293                 // TrafCODDemo2.this.trafCOD.traceVariablesOfStream(11, true);
294                 // TrafCODDemo2.this.trafCOD.traceVariable("MRV", 11, true);
295             }
296             catch (Exception exception)
297             {
298                 exception.printStackTrace();
299             }
300         }
301 
302         @SuppressWarnings("synthetic-access")
303         @Override
304         public SimulatorInterface<Time, Duration, OTSSimTimeDouble> getSimulator() throws RemoteException
305         {
306             return DemoTrafcodAndTurbo.this.trafCOD.getSimulator();
307         }
308 
309         /** {@inheritDoc} */
310         @Override
311         public final OTSNetwork getNetwork()
312         {
313             return this.network;
314         }
315 
316         /** {@inheritDoc} */
317         @Override
318         public void notify(final EventInterface event) throws RemoteException
319         {
320             EventType type = event.getType();
321             Object[] payload = (Object[]) event.getContent();
322             if (TrafficController.TRAFFICCONTROL_CONTROLLER_EVALUATING.equals(type))
323             {
324                 // System.out.println("Evaluation starts at " +
325                 // getSimulator().getSimulatorTime().getTime());
326                 return;
327             }
328             else if (TrafficController.TRAFFICCONTROL_CONFLICT_GROUP_CHANGED.equals(type))
329             {
330                 System.out.println("Conflict group changed from " + ((String) payload[1]) + " to " + ((String) payload[2]));
331             }
332             else if (TrafficController.TRAFFICCONTROL_TRACED_VARIABLE_UPDATED.equals(type))
333             {
334                 System.out.println(String.format("Variable changed %s <- %d   %s", payload[1], payload[4], payload[5]));
335             }
336             else if (TrafficController.TRAFFICCONTROL_CONTROLLER_WARNING.equals(type))
337             {
338                 System.out.println("Warning " + payload[1]);
339             }
340             else
341             {
342                 System.out.print("TrafCODDemo received event of type " + event.getType() + ", payload [");
343                 String separator = "";
344                 for (Object o : payload)
345                 {
346                     System.out.print(separator + o);
347                     separator = ",";
348                 }
349                 System.out.println("]");
350             }
351         }
352 
353         /**
354          * Put a block at the end of a Lane.
355          * @param lane Lane; the lane on which the block is placed
356          * @param theSimulator the simulator
357          * @param gtuColorer the gtu colorer to use
358          * @return Lane; the lane
359          * @throws NamingException on ???
360          * @throws NetworkException on network inconsistency
361          * @throws SimRuntimeException on ???
362          * @throws GTUException when construction of the GTU (the block is a GTU) fails
363          * @throws OTSGeometryException when the initial path is wrong
364          */
365         private Lane setupBlock(final Lane lane, OTSDEVSSimulatorInterface theSimulator, GTUColorer gtuColorer)
366                 throws NamingException, NetworkException, SimRuntimeException, GTUException, OTSGeometryException
367         {
368             Length initialPosition = lane.getLength();
369             Duration tSafe = new Duration(0, DurationUnit.SECOND);
370             Acceleration ac1 = new Acceleration(1.0, AccelerationUnit.METER_PER_SECOND_2);
371             Length l = new Length(1.0, LengthUnit.METER);
372             IDMPlusOld carFollowingModelCars = new IDMPlusOld(ac1, ac1, l, tSafe, 1.0);
373             Set<DirectedLanePosition> initialPositions = new LinkedHashSet<>(1);
374             initialPositions.add(new DirectedLanePosition(lane, initialPosition, GTUDirectionality.DIR_PLUS));
375             GTUType gtuType = CAR;
376             BehavioralCharacteristics behavioralCharacteristics = DefaultsFactory.getDefaultBehavioralCharacteristics();
377             LaneBasedIndividualGTU block = new LaneBasedIndividualGTU("999999", gtuType, new Length(1, LengthUnit.METER),
378                     lane.getWidth(1), Speed.ZERO, theSimulator, (OTSNetwork) this.network);
379             LaneBasedStrategicalPlanner strategicalPlanner = new LaneBasedStrategicalRoutePlanner(behavioralCharacteristics,
380                     new LaneBasedGTUFollowingTacticalPlanner(carFollowingModelCars, block), block);
381             block.initWithAnimation(strategicalPlanner, initialPositions, Speed.ZERO, DefaultCarAnimation.class, gtuColorer);
382             return lane;
383         }
384 
385     }
386 
387 }