View Javadoc
1   package org.opentrafficsim.demo.conflict;
2   
3   import java.awt.Dimension;
4   import java.net.URL;
5   import java.rmi.RemoteException;
6   
7   import javax.naming.NamingException;
8   
9   import org.djunits.value.vdouble.scalar.Duration;
10  import org.djunits.value.vdouble.scalar.Time;
11  import org.djutils.io.URLResource;
12  import org.opentrafficsim.core.dsol.AbstractOtsModel;
13  import org.opentrafficsim.core.dsol.OtsAnimator;
14  import org.opentrafficsim.core.dsol.OtsSimulatorInterface;
15  import org.opentrafficsim.core.gtu.Gtu;
16  import org.opentrafficsim.core.perception.HistoryManagerDevs;
17  import org.opentrafficsim.demo.DefaultsFactory;
18  import org.opentrafficsim.demo.conflict.TJunctionDemo.TJunctionModel;
19  import org.opentrafficsim.road.network.RoadNetwork;
20  import org.opentrafficsim.road.network.factory.xml.parser.XmlParser;
21  import org.opentrafficsim.swing.gui.OtsAnimationPanel;
22  import org.opentrafficsim.swing.gui.OtsSimulationApplication;
23  
24  import nl.tudelft.simulation.dsol.SimRuntimeException;
25  import nl.tudelft.simulation.language.DsolException;
26  
27  /**
28   * <p>
29   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
30   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
31   * </p>
32   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
33   * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
34   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
35   */
36  public class TJunctionDemo extends OtsSimulationApplication<TJunctionModel>
37  {
38      /** */
39      private static final long serialVersionUID = 20161211L;
40  
41      /**
42       * Create a T-Junction demo.
43       * @param title the title of the Frame
44       * @param panel the tabbed panel to display
45       * @param model the model
46       */
47      public TJunctionDemo(final String title, final OtsAnimationPanel panel, final TJunctionModel model)
48      {
49          super(model, panel, DefaultsFactory.GTU_TYPE_MARKERS.toMap());
50      }
51  
52      /**
53       * Main program.
54       * @param args the command line arguments (not used)
55       */
56      public static void main(final String[] args)
57      {
58          demo(true);
59      }
60  
61      /**
62       * Start the demo.
63       * @param exitOnClose when running stand-alone: true; when running as part of a demo: false
64       */
65      public static void demo(final boolean exitOnClose)
66      {
67          Gtu.ALIGNED = false;
68          try
69          {
70              OtsAnimator simulator = new OtsAnimator("TJunctionDemo");
71              final TJunctionModel junctionModel = new TJunctionModel(simulator);
72              simulator.initialize(Time.ZERO, Duration.ZERO, Duration.instantiateSI(3600.0), junctionModel,
73                      HistoryManagerDevs.noHistory(simulator));
74              OtsAnimationPanel animationPanel = new OtsAnimationPanel(junctionModel.getNetwork().getExtent(),
75                      new Dimension(800, 600), simulator, junctionModel, DEFAULT_GTU_COLORERS, junctionModel.getNetwork());
76              TJunctionDemo app = new TJunctionDemo("T-Junction demo", animationPanel, junctionModel);
77              app.setExitOnClose(exitOnClose);
78              animationPanel.enableSimulationControlButtons();
79          }
80          catch (SimRuntimeException | NamingException | RemoteException | DsolException exception)
81          {
82              exception.printStackTrace();
83          }
84      }
85  
86      /**
87       * The simulation model.
88       */
89      public static class TJunctionModel extends AbstractOtsModel
90      {
91          /** */
92          private static final long serialVersionUID = 20161211L;
93  
94          /** The network. */
95          private RoadNetwork network;
96  
97          /**
98           * @param simulator the simulator for this model
99           */
100         public TJunctionModel(final OtsSimulatorInterface simulator)
101         {
102             super(simulator);
103         }
104 
105         @Override
106         public void constructModel() throws SimRuntimeException
107         {
108             try
109             {
110                 URL xmlURL = URLResource.getResource("/resources/conflict/TJunction.xml");
111                 this.network = new RoadNetwork("TJunction", getSimulator());
112                 new XmlParser(this.network).setUrl(xmlURL).setScenario("1").build();
113             }
114             catch (Exception exception)
115             {
116                 exception.printStackTrace();
117             }
118         }
119 
120         @Override
121         public RoadNetwork getNetwork()
122         {
123             return this.network;
124         }
125 
126     }
127 }