View Javadoc
1   package org.opentrafficsim.demo.trafficcontrol;
2   
3   import java.awt.BorderLayout;
4   import java.awt.Dimension;
5   import java.io.ByteArrayInputStream;
6   import java.io.IOException;
7   import java.io.Serializable;
8   import java.net.URL;
9   import java.nio.charset.StandardCharsets;
10  import java.rmi.RemoteException;
11  import java.util.Scanner;
12  
13  import javax.naming.NamingException;
14  import javax.swing.JPanel;
15  
16  import org.djunits.value.vdouble.scalar.Duration;
17  import org.djunits.value.vdouble.scalar.Time;
18  import org.djutils.event.EventInterface;
19  import org.djutils.event.EventListenerInterface;
20  import org.djutils.event.EventTypeInterface;
21  import org.djutils.io.URLResource;
22  import org.opentrafficsim.core.dsol.AbstractOTSModel;
23  import org.opentrafficsim.core.dsol.OTSAnimator;
24  import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
25  import org.opentrafficsim.demo.trafficcontrol.TrafCODDemo2_Generators.TrafCODModel;
26  import org.opentrafficsim.draw.core.OTSDrawingException;
27  import org.opentrafficsim.road.network.OTSRoadNetwork;
28  import org.opentrafficsim.road.network.factory.xml.parser.XmlNetworkLaneParser;
29  import org.opentrafficsim.swing.gui.OTSAnimationPanel;
30  import org.opentrafficsim.swing.gui.OTSSimulationApplication;
31  import org.opentrafficsim.trafficcontrol.TrafficController;
32  import org.opentrafficsim.trafficcontrol.trafcod.TrafCOD;
33  
34  import nl.tudelft.simulation.dsol.SimRuntimeException;
35  import nl.tudelft.simulation.language.DSOLException;
36  
37  /**
38   * <p>
39   * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
40   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
41   * <p>
42   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Dec 06, 2016 <br>
43   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
44   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
45   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
46   */
47  public class TrafCODDemo2_Generators extends OTSSimulationApplication<TrafCODModel>
48  {
49      /** */
50      private static final long serialVersionUID = 20161118L;
51  
52      /**
53       * Create a TrafcodAndTurbo demo.
54       * @param title String; the title of the Frame
55       * @param panel OTSAnimationPanel; the tabbed panel to display
56       * @param model TrafCODModel; the model
57       * @throws OTSDrawingException on animation error
58       */
59      public TrafCODDemo2_Generators(final String title, final OTSAnimationPanel panel, final TrafCODModel model)
60              throws OTSDrawingException
61      {
62          super(model, panel);
63      }
64  
65      /**
66       * Main program.
67       * @param args String[]; the command line arguments (not used)
68       * @throws IOException ...
69       */
70      public static void main(final String[] args) throws IOException
71      {
72          demo(true);
73      }
74  
75      /**
76       * Open an URL, read it and store the contents in a string. Adapted from
77       * https://stackoverflow.com/questions/4328711/read-url-to-string-in-few-lines-of-java-code
78       * @param url URL; the URL
79       * @return String
80       * @throws IOException when reading the file fails
81       */
82      public static String readStringFromURL(final URL url) throws IOException
83      {
84          try (Scanner scanner = new Scanner(url.openStream(), StandardCharsets.UTF_8.toString()))
85          {
86              scanner.useDelimiter("\\A");
87              return scanner.hasNext() ? scanner.next() : "";
88          }
89      }
90  
91      /**
92       * Start the demo.
93       * @param exitOnClose boolean; when running stand-alone: true; when running as part of a demo: false
94       * @throws IOException when reading the file fails
95       */
96      public static void demo(final boolean exitOnClose) throws IOException
97      {
98          try
99          {
100             OTSAnimator simulator = new OTSAnimator("TrafCODDemo2_Generators");
101             URL url = URLResource.getResource("/TrafCODDemo2/TrafCODDemo2_Generators.xml");
102             String xml = readStringFromURL(url);
103             final TrafCODModel trafcodModel = new TrafCODModel(simulator, "TrafCODModel", "TrafCOD demonstration Model", xml);
104             simulator.initialize(Time.ZERO, Duration.ZERO, Duration.instantiateSI(3600.0), trafcodModel);
105             OTSAnimationPanel animationPanel = new OTSAnimationPanel(trafcodModel.getNetwork().getExtent(),
106                     new Dimension(800, 600), simulator, trafcodModel, DEFAULT_COLORER, trafcodModel.getNetwork());
107             TrafCODDemo2_Generators app =
108                     new TrafCODDemo2_Generators("TrafCOD demo complex crossing", animationPanel, trafcodModel);
109             app.setExitOnClose(exitOnClose);
110             animationPanel.enableSimulationControlButtons();
111         }
112         catch (SimRuntimeException | NamingException | RemoteException | OTSDrawingException | DSOLException exception)
113         {
114             exception.printStackTrace();
115         }
116     }
117 
118     /**
119      * Add tab with trafCOD status.
120      */
121     @Override
122     protected final void addTabs()
123     {
124         // This version does not properly construct the console panel ...
125         // JScrollPane scrollPane = new JScrollPane(getModel().getControllerDisplayPanel());
126         // JPanel wrapper = new JPanel(new BorderLayout());
127         // wrapper.add(scrollPane);
128         // getAnimationPanel().getTabbedPane().addTab(getAnimationPanel().getTabbedPane().getTabCount() - 1,
129         // getModel().getTrafCOD().getId(), wrapper);
130     }
131 
132     /**
133      * The simulation model.
134      */
135     static class TrafCODModel extends AbstractOTSModel implements EventListenerInterface
136     {
137         /** */
138         private static final long serialVersionUID = 20161020L;
139 
140         /** The network. */
141         private OTSRoadNetwork network;
142 
143         /** The XML. */
144         private final String xml;
145 
146         /** The TrafCOD controller. */
147         private TrafCOD trafCOD;
148 
149         /** TrafCOD controller display. */
150         private JPanel controllerDisplayPanel = new JPanel(new BorderLayout());
151 
152         /**
153          * @param simulator OTSSimulatorInterface; the simulator
154          * @param shortName String; name of the model
155          * @param description String; description of the model
156          * @param xml String; the XML string
157          */
158         TrafCODModel(final OTSSimulatorInterface simulator, final String shortName, final String description, final String xml)
159         {
160             super(simulator, shortName, description);
161             this.xml = xml;
162         }
163 
164         /** {@inheritDoc} */
165         @Override
166         public void constructModel() throws SimRuntimeException
167         {
168             try
169             {
170                 this.network = new OTSRoadNetwork(getShortName(), true, getSimulator());
171                 XmlNetworkLaneParser.build(new ByteArrayInputStream(this.xml.getBytes(StandardCharsets.UTF_8)), this.network,
172                         false);
173 
174                 String controllerName = "TrafCOD_complex";
175                 this.trafCOD = new TrafCOD(controllerName, URLResource.getResource("/TrafCODDemo2/Intersection12Dir.tfc"),
176                         getSimulator(), this.controllerDisplayPanel, null, null);
177                 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_CONTROLLER_EVALUATING);
178                 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_CONTROLLER_WARNING);
179                 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_CONFLICT_GROUP_CHANGED);
180                 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_STATE_CHANGED);
181                 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_VARIABLE_CREATED);
182                 this.trafCOD.addListener(this, TrafficController.TRAFFICCONTROL_TRACED_VARIABLE_UPDATED);
183                 // Subscribe the TrafCOD machine to trace command events that we emit
184                 addListener(this.trafCOD, TrafficController.TRAFFICCONTROL_SET_TRACING);
185                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new Object[] {controllerName, "TGX", 8, true});
186                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new Object[] {controllerName, "XR1", 11, true});
187                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new Object[] {controllerName, "TD1", 11, true});
188                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new Object[] {controllerName, "TGX", 11, true});
189                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new Object[] {controllerName, "TL", 11, true});
190                 // System.out.println("demo: emitting a SET TRACING event for all variables related to stream 11");
191                 // fireEvent(TrafficController.TRAFFICCONTROL_SET_TRACING, new Object[] { controllerName, "", 11, true });
192 
193                 // this.trafCOD.traceVariablesOfStream(TrafficController.NO_STREAM, true);
194                 // this.trafCOD.traceVariablesOfStream(11, true);
195                 // this.trafCOD.traceVariable("MRV", 11, true);
196             }
197             catch (Exception exception)
198             {
199                 exception.printStackTrace();
200             }
201         }
202 
203         /** {@inheritDoc} */
204         @Override
205         public final OTSRoadNetwork getNetwork()
206         {
207             return this.network;
208         }
209 
210         /**
211          * @return trafCOD
212          */
213         public final TrafCOD getTrafCOD()
214         {
215             return this.trafCOD;
216         }
217 
218         /**
219          * @return controllerDisplayPanel
220          */
221         public final JPanel getControllerDisplayPanel()
222         {
223             return this.controllerDisplayPanel;
224         }
225 
226         /** {@inheritDoc} */
227         @Override
228         public void notify(final EventInterface event) throws RemoteException
229         {
230             EventTypeInterface type = event.getType();
231             Object[] payload = (Object[]) event.getContent();
232             if (TrafficController.TRAFFICCONTROL_CONTROLLER_EVALUATING.equals(type))
233             {
234                 // System.out.println("Evaluation starts at " + getSimulator().getSimulatorTime());
235                 return;
236             }
237             else if (TrafficController.TRAFFICCONTROL_CONFLICT_GROUP_CHANGED.equals(type))
238             {
239                 System.out.println("Conflict group changed from " + ((String) payload[1]) + " to " + ((String) payload[2]));
240             }
241             else if (TrafficController.TRAFFICCONTROL_TRACED_VARIABLE_UPDATED.equals(type))
242             {
243                 System.out.println(String.format("Variable changed %s <- %d   %s", payload[1], payload[4], payload[5]));
244             }
245             else if (TrafficController.TRAFFICCONTROL_CONTROLLER_WARNING.equals(type))
246             {
247                 System.out.println("Warning " + payload[1]);
248             }
249             else
250             {
251                 System.out.print("TrafCODDemo received event of type " + event.getType() + ", payload [");
252                 String separator = "";
253                 for (Object o : payload)
254                 {
255                     System.out.print(separator + o);
256                     separator = ",";
257                 }
258                 System.out.println("]");
259             }
260         }
261 
262         /** {@inheritDoc} */
263         @Override
264         public Serializable getSourceId()
265         {
266             return "TrafCODModel";
267         }
268 
269     }
270 
271 }