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  
45  
46  
47  
48  
49  
50  
51  
52  
53  public class TrafCODDemo2 extends OTSSimulationApplication<TrafCODModel>
54  {
55      
56      private static final long serialVersionUID = 20161118L;
57  
58      
59  
60  
61  
62  
63  
64  
65      public TrafCODDemo2(final String title, final OTSAnimationPanel panel, final TrafCODModel model) throws OTSDrawingException
66      {
67          super(model, panel);
68      }
69  
70      
71  
72  
73  
74      public static void main(final String[] args)
75      {
76          demo(true);
77      }
78  
79      
80  
81  
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 
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 
116 
117     static class TrafCODModel extends AbstractOTSModel implements EventListenerInterface
118     {
119         
120         private static final long serialVersionUID = 20161020L;
121 
122         
123         private OTSNetwork network;
124 
125         
126         private TrafCOD trafCOD;
127 
128         
129         private JPanel controllerDisplayPanel = new JPanel(new BorderLayout());
130 
131         
132 
133 
134         TrafCODModel(final OTSSimulatorInterface simulator)
135         {
136             super(simulator);
137         }
138 
139         
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                 
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                 
195                 addListener(this.trafCOD, TrafficController.TRAFFICCONTROL_SET_TRACING);
196                 
197                 
198                 
199                 
200                 
201                 
202                 
203 
204                 
205                 
206                 
207             }
208             catch (Exception exception)
209             {
210                 exception.printStackTrace();
211             }
212         }
213 
214         
215         @Override
216         public final OTSNetwork getNetwork()
217         {
218             return this.network;
219         }
220 
221         
222 
223 
224         public final TrafCOD getTrafCOD()
225         {
226             return this.trafCOD;
227         }
228 
229         
230 
231 
232         public final JPanel getControllerDisplayPanel()
233         {
234             return this.controllerDisplayPanel;
235         }
236 
237         
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                 
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 }