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.TrafCODDemo2.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.road.network.lane.object.trafficlight.TrafficLight;
33  import org.opentrafficsim.swing.gui.OTSAnimationPanel;
34  import org.opentrafficsim.swing.gui.OTSSimulationApplication;
35  import org.opentrafficsim.trafficcontrol.TrafficController;
36  import org.opentrafficsim.trafficcontrol.trafcod.TrafCOD;
37  
38  import nl.tudelft.simulation.dsol.SimRuntimeException;
39  import nl.tudelft.simulation.event.EventInterface;
40  import nl.tudelft.simulation.event.EventListenerInterface;
41  import nl.tudelft.simulation.event.EventType;
42  
43  /**
44   * <p>
45   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
46   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
47   * <p>
48   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Dec 06, 2016 <br>
49   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
50   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
51   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
52   */
53  public class TrafCODDemo2 extends OTSSimulationApplication<TrafCODModel>
54  {
55      /** */
56      private static final long serialVersionUID = 20161118L;
57  
58      /**
59       * Create a TrafcodAndTurbo demo.
60       * @param title String; the title of the Frame
61       * @param panel OTSAnimationPanel; the tabbed panel to display
62       * @param model TrafCODModel; the model
63       * @throws OTSDrawingException on animation error
64       */
65      public TrafCODDemo2(final String title, final OTSAnimationPanel panel, final TrafCODModel model) 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 = new OTSAnimationPanel(trafcodModel.getNetwork().getExtent(),
91                      new Dimension(800, 600), simulator, trafcodModel, DEFAULT_COLORER, trafcodModel.getNetwork());
92              TrafCODDemo2 app = new TrafCODDemo2("TrafCOD demo complex crossing", animationPanel, trafcodModel);
93              app.setExitOnClose(exitOnClose);
94          }
95          catch (SimRuntimeException | NamingException | RemoteException | OTSDrawingException exception)
96          {
97              exception.printStackTrace();
98          }
99      }
100 
101     /**
102      * Add tab with trafCOD status.
103      */
104     @Override
105     protected final void addTabs()
106     {
107         JScrollPane scrollPane = new JScrollPane(getModel().getControllerDisplayPanel());
108         JPanel wrapper = new JPanel(new BorderLayout());
109         wrapper.add(scrollPane);
110         getAnimationPanel().getTabbedPane().addTab(getAnimationPanel().getTabbedPane().getTabCount() - 1,
111                 getModel().getTrafCOD().getId(), wrapper);
112     }
113 
114     /**
115      * The simulation model.
116      */
117     static class TrafCODModel extends AbstractOTSModel implements EventListenerInterface
118     {
119         /** */
120         private static final long serialVersionUID = 20161020L;
121 
122         /** The network. */
123         private OTSNetwork network;
124 
125         /** The TrafCOD controller. */
126         private TrafCOD trafCOD;
127 
128         /** TrafCOD controller display. */
129         private JPanel controllerDisplayPanel = new JPanel(new BorderLayout());
130 
131         /**
132          * @param simulator OTSSimulatorInterface; the simulator for this model
133          */
134         TrafCODModel(final OTSSimulatorInterface simulator)
135         {
136             super(simulator);
137         }
138 
139         /** {@inheritDoc} */
140         @Override
141         public void constructModel() throws SimRuntimeException
142         {
143             try
144             {
145                 URL url = URLResource.getResource("/TrafCODDemo2/Network.xml");
146                 XmlNetworkLaneParserOld nlp = new XmlNetworkLaneParserOld(getSimulator());
147                 this.network = nlp.build(url, true);
148                 String[] directions = { "E", "S", "W", "N" };
149                 // Add the traffic lights and the detectors
150                 Length stopLineMargin = new Length(0.1, LengthUnit.METER);
151                 Length headDetectorLength = new Length(1, LengthUnit.METER);
152                 Length headDetectorMargin = stopLineMargin.plus(headDetectorLength).plus(new Length(3, LengthUnit.METER));
153                 Length longDetectorLength = new Length(30, LengthUnit.METER);
154                 Length longDetectorMargin = stopLineMargin.plus(longDetectorLength).plus(new Length(10, LengthUnit.METER));
155                 int stream = 1;
156                 String controllerName = "TrafCOD_complex";
157                 for (String direction : directions)
158                 {
159                     for (int laneNumber = 3; laneNumber >= 1; laneNumber--)
160                     {
161                         Lane lane = (Lane) ((CrossSectionLink) this.network.getLink(direction, direction + "C"))
162                                 .getCrossSectionElement("FORWARD" + laneNumber);
163                         TrafficLight tl = new SimpleTrafficLight(String.format("%s.%02d", controllerName, stream), lane,
164                                 lane.getLength().minus(stopLineMargin), getSimulator());
165 
166                         try
167                         {
168                             new TrafficLightAnimation(tl, this.simulator);
169                         }
170                         catch (RemoteException | NamingException exception)
171                         {
172                             throw new NetworkException(exception);
173                         }
174 
175                         new TrafficLightSensor(String.format("%s.D%02d1", controllerName, stream), lane,
176                                 lane.getLength().minus(headDetectorMargin), lane,
177                                 lane.getLength().minus(headDetectorMargin).plus(headDetectorLength), null,
178                                 RelativePosition.FRONT, RelativePosition.REAR, getSimulator(), Compatible.EVERYTHING);
179                         new TrafficLightSensor(String.format("%s.D%02d2", controllerName, stream), lane,
180                                 lane.getLength().minus(longDetectorMargin), lane,
181                                 lane.getLength().minus(longDetectorMargin).plus(longDetectorLength), null,
182                                 RelativePosition.FRONT, RelativePosition.REAR, getSimulator(), Compatible.EVERYTHING);
183                         stream++;
184                     }
185                 }
186                 this.trafCOD = new TrafCOD(controllerName, URLResource.getResource("/TrafCODDemo2/Intersection12Dir.tfc"),
187                         getSimulator(), this.controllerDisplayPanel);
188                 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_CONTROLLER_EVALUATING);
189                 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_CONTROLLER_WARNING);
190                 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_CONFLICT_GROUP_CHANGED);
191                 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_STATE_CHANGED);
192                 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_VARIABLE_CREATED);
193                 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_TRACED_VARIABLE_UPDATED);
194                 // Subscribe the TrafCOD machine to trace command events that we emit
195                 addListener(this.trafCOD, TrafficController.TRAFFICCONTROL_SET_TRACING);
196                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new Object[] {controllerName, "TGX", 8, true});
197                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new Object[] {controllerName, "XR1", 11, true});
198                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new Object[] {controllerName, "TD1", 11, true});
199                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new Object[] {controllerName, "TGX", 11, true});
200                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new Object[] {controllerName, "TL", 11, true});
201                 // System.out.println("demo: emitting a SET TRACING event for all variables related to stream 11");
202                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new Object[] { controllerName, "", 11, true });
203 
204                 // this.trafCOD.traceVariablesOfStream(TrafficController.NO_STREAM, true);
205                 // this.trafCOD.traceVariablesOfStream(11, true);
206                 // this.trafCOD.traceVariable("MRV", 11, true);
207             }
208             catch (Exception exception)
209             {
210                 exception.printStackTrace();
211             }
212         }
213 
214         /** {@inheritDoc} */
215         @Override
216         public final OTSNetwork getNetwork()
217         {
218             return this.network;
219         }
220 
221         /**
222          * @return trafCOD
223          */
224         public final TrafCOD getTrafCOD()
225         {
226             return this.trafCOD;
227         }
228 
229         /**
230          * @return controllerDisplayPanel
231          */
232         public final JPanel getControllerDisplayPanel()
233         {
234             return this.controllerDisplayPanel;
235         }
236 
237         /** {@inheritDoc} */
238         @Override
239         public void notify(final EventInterface event) throws RemoteException
240         {
241             EventType type = event.getType();
242             Object[] payload = (Object[]) event.getContent();
243             if (TrafficController.TRAFFICCONTROL_CONTROLLER_EVALUATING.equals(type))
244             {
245                 // System.out.println("Evaluation starts at " + getSimulator().getSimulatorTime());
246                 return;
247             }
248             else if (TrafficController.TRAFFICCONTROL_CONFLICT_GROUP_CHANGED.equals(type))
249             {
250                 System.out.println("Conflict group changed from " + ((String) payload[1]) + " to " + ((String) payload[2]));
251             }
252             else if (TrafficController.TRAFFICCONTROL_TRACED_VARIABLE_UPDATED.equals(type))
253             {
254                 System.out.println(String.format("Variable changed %s <- %d   %s", payload[1], payload[4], payload[5]));
255             }
256             else if (TrafficController.TRAFFICCONTROL_CONTROLLER_WARNING.equals(type))
257             {
258                 System.out.println("Warning " + payload[1]);
259             }
260             else
261             {
262                 System.out.print("TrafCODDemo received event of type " + event.getType() + ", payload [");
263                 String separator = "";
264                 for (Object o : payload)
265                 {
266                     System.out.print(separator + o);
267                     separator = ",";
268                 }
269                 System.out.println("]");
270             }
271         }
272 
273     }
274 
275 }