View Javadoc
1   package org.opentrafficsim.trafficcontrol.trafcod;
2   
3   import java.awt.BorderLayout;
4   import java.awt.geom.Rectangle2D;
5   import java.awt.geom.Rectangle2D.Double;
6   import java.rmi.RemoteException;
7   import java.util.ArrayList;
8   import java.util.HashMap;
9   import java.util.HashSet;
10  import java.util.Map;
11  import java.util.Set;
12  
13  import javax.naming.NamingException;
14  import javax.swing.JPanel;
15  import javax.swing.JScrollPane;
16  import javax.swing.SwingUtilities;
17  
18  import nl.tudelft.simulation.dsol.SimRuntimeException;
19  import nl.tudelft.simulation.dsol.simulators.DEVSSimulator;
20  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
21  
22  import org.djunits.unit.LengthUnit;
23  import org.djunits.unit.SpeedUnit;
24  import org.djunits.unit.TimeUnit;
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.OTSPoint3D;
35  import org.opentrafficsim.core.gtu.GTUType;
36  import org.opentrafficsim.core.gtu.RelativePosition;
37  import org.opentrafficsim.core.gtu.animation.GTUColorer;
38  import org.opentrafficsim.core.network.LongitudinalDirectionality;
39  import org.opentrafficsim.core.network.Network;
40  import org.opentrafficsim.core.network.OTSNetwork;
41  import org.opentrafficsim.core.network.OTSNode;
42  import org.opentrafficsim.road.network.factory.LaneFactory;
43  import org.opentrafficsim.road.network.lane.Lane;
44  import org.opentrafficsim.road.network.lane.LaneType;
45  import org.opentrafficsim.road.network.lane.object.sensor.TrafficLightSensor;
46  import org.opentrafficsim.road.network.lane.object.trafficlight.SimpleTrafficLight;
47  import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLight;
48  import org.opentrafficsim.simulationengine.AbstractWrappableAnimation;
49  import org.opentrafficsim.simulationengine.OTSSimulationException;
50  import org.opentrafficsim.simulationengine.SimpleSimulatorInterface;
51  
52  /**
53   * <p>
54   * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
55   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
56   * <p>
57   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Nov 18, 2016 <br>
58   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
59   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
60   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
61   */
62  public class TrafCODDemo extends AbstractWrappableAnimation
63  {
64  
65      /** */
66      private static final long serialVersionUID = 20161118L;
67  
68      /**
69       * Main program.
70       * @param args String[]; the command line arguments (not used)
71       * @throws SimRuntimeException should never happen
72       */
73      public static void main(final String[] args) throws SimRuntimeException
74      {
75          SwingUtilities.invokeLater(new Runnable()
76          {
77              @Override
78              public void run()
79              {
80                  try
81                  {
82                      TrafCODDemo model = new TrafCODDemo();
83                      // 1 hour simulation run for testing
84                      model.buildAnimator(new Time(0.0, TimeUnit.SECOND), new Duration(0.0, TimeUnit.SECOND), new Duration(60.0,
85                              TimeUnit.MINUTE), new ArrayList<Property<?>>(), null, true);
86                  }
87                  catch (SimRuntimeException | NamingException | OTSSimulationException | PropertyException exception)
88                  {
89                      exception.printStackTrace();
90                  }
91              }
92          });
93      }
94  
95      /** TrafCOD controller display. */
96      JPanel controllerDisplayPanel = new JPanel(new BorderLayout());
97  
98      /** {@inheritDoc} */
99      @Override
100     public String shortName()
101     {
102         return "TrafCOD demonstration";
103     }
104 
105     /** {@inheritDoc} */
106     @Override
107     public String description()
108     {
109         return "TrafCOD demonstration";
110     }
111 
112     /** {@inheritDoc} */
113     @Override
114     protected JPanel makeCharts(SimpleSimulatorInterface simulator) throws OTSSimulationException, PropertyException
115     {
116         JScrollPane scrollPane = new JScrollPane(this.controllerDisplayPanel);
117         JPanel wrapper = new JPanel(new BorderLayout());
118         wrapper.add(scrollPane);
119         return wrapper;
120     }
121 
122     /** {@inheritDoc} */
123     @Override
124     protected OTSModelInterface makeModel(GTUColorer colorer) throws OTSSimulationException
125     {
126         return new TrafCODModel();
127     }
128 
129     /** {@inheritDoc} */
130     @Override
131     protected Double makeAnimationRectangle()
132     {
133         return new Rectangle2D.Double(-110, -110, 220, 220);
134     }
135 
136     /**
137      * The simulation model.
138      */
139     class TrafCODModel implements OTSModelInterface
140     {
141         /** */
142         private static final long serialVersionUID = 20161020L;
143 
144         /** The TrafCOD evaluator. */
145         private TrafCOD trafCOD;
146 
147         @Override
148         public void constructModel(SimulatorInterface<Time, Duration, OTSSimTimeDouble> theSimulator)
149                 throws SimRuntimeException, RemoteException
150         {
151             try
152             {
153                 Network network = new OTSNetwork("TrafCOD test network");
154                 Map<GTUType, LongitudinalDirectionality> directionalityMap = new HashMap<>();
155                 directionalityMap.put(GTUType.ALL, LongitudinalDirectionality.DIR_PLUS);
156                 OTSNode nodeX = new OTSNode(network, "Crossing", new OTSPoint3D(0, 0, 0));
157                 OTSNode nodeS = new OTSNode(network, "South", new OTSPoint3D(0, -100, 0));
158                 OTSNode nodeE = new OTSNode(network, "East", new OTSPoint3D(100, 0, 0));
159                 OTSNode nodeN = new OTSNode(network, "North", new OTSPoint3D(0, 100, 0));
160                 OTSNode nodeW = new OTSNode(network, "West", new OTSPoint3D(-100, 0, 0));
161                 Speed speedLimit = new Speed(50, SpeedUnit.KM_PER_HOUR);
162                 Set<GTUType> compatibility = new HashSet<GTUType>();
163                 compatibility.add(GTUType.ALL);
164                 LaneType laneType = new LaneType("CarLane", compatibility);
165                 Lane laneNX =
166                         LaneFactory.makeMultiLane(network, "LinkNX", nodeN, nodeX, null, 1, laneType, speedLimit,
167                                 (OTSDEVSSimulatorInterface) theSimulator, LongitudinalDirectionality.DIR_PLUS)[0];
168                 Lane laneWX =
169                         LaneFactory.makeMultiLane(network, "LinkWX", nodeW, nodeX, null, 1, laneType, speedLimit,
170                                 (OTSDEVSSimulatorInterface) theSimulator, LongitudinalDirectionality.DIR_PLUS)[0];
171                 LaneFactory.makeMultiLane(network, "LinkXE", nodeX, nodeE, null, 1, laneType, speedLimit,
172                         (OTSDEVSSimulatorInterface) theSimulator, LongitudinalDirectionality.DIR_PLUS);
173                 LaneFactory.makeMultiLane(network, "LinkXS", nodeX, nodeS, null, 1, laneType, speedLimit,
174                         (OTSDEVSSimulatorInterface) theSimulator, LongitudinalDirectionality.DIR_PLUS);
175                 Set<TrafficLight> trafficLights = new HashSet<>();
176                 trafficLights.add(new SimpleTrafficLight("TL08", laneNX, new Length(90, LengthUnit.METER),
177                         (OTSDEVSSimulatorInterface) theSimulator));
178                 trafficLights.add(new SimpleTrafficLight("TL11", laneWX, new Length(90, LengthUnit.METER),
179                         (OTSDEVSSimulatorInterface) theSimulator));
180                 Set<TrafficLightSensor> sensors = new HashSet<>();
181                 sensors.add(new TrafficLightSensor("D081", laneWX, new Length(86, LengthUnit.METER), laneWX, new Length(88,
182                         LengthUnit.METER), null, RelativePosition.FRONT, RelativePosition.REAR,
183                         (OTSDEVSSimulatorInterface) theSimulator));
184                 sensors.add(new TrafficLightSensor("D082", laneWX, new Length(50, LengthUnit.METER), laneWX, new Length(70,
185                         LengthUnit.METER), null, RelativePosition.FRONT, RelativePosition.REAR,
186                         (OTSDEVSSimulatorInterface) theSimulator));
187                 sensors.add(new TrafficLightSensor("D111", laneNX, new Length(86, LengthUnit.METER), laneNX, new Length(88,
188                         LengthUnit.METER), null, RelativePosition.FRONT, RelativePosition.REAR,
189                         (OTSDEVSSimulatorInterface) theSimulator));
190                 sensors.add(new TrafficLightSensor("D112", laneNX, new Length(50, LengthUnit.METER), laneNX, new Length(70,
191                         LengthUnit.METER), null, RelativePosition.FRONT, RelativePosition.REAR,
192                         (OTSDEVSSimulatorInterface) theSimulator));
193                 this.trafCOD =
194                         new TrafCOD("Simple TrafCOD controller", "file:///d:/cppb/trafcod/otsim/simpleTest.tfc", trafficLights,
195                                 sensors, (DEVSSimulator<Time, Duration, OTSSimTimeDouble>) theSimulator,
196                                 TrafCODDemo.this.controllerDisplayPanel);
197 
198                 // this.trafCOD.traceVariablesOfStream(TrafCOD.NO_STREAM, true);
199                 // this.trafCOD.traceVariablesOfStream(11, true);
200                 // this.trafCOD.traceVariable("MRV", 11, true);
201             }
202             catch (Exception exception)
203             {
204                 exception.printStackTrace();
205             }
206         }
207 
208         @Override
209         public SimulatorInterface<Time, Duration, OTSSimTimeDouble> getSimulator() throws RemoteException
210         {
211             return this.trafCOD.getSimulator();
212         }
213 
214     }
215 
216 }