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.demo.conflict.TJunctionDemo.TJunctionModel;
17  import org.opentrafficsim.draw.OtsDrawingException;
18  import org.opentrafficsim.road.network.RoadNetwork;
19  import org.opentrafficsim.road.network.factory.xml.parser.XmlParser;
20  import org.opentrafficsim.swing.gui.OtsAnimationPanel;
21  import org.opentrafficsim.swing.gui.OtsSimulationApplication;
22  
23  import nl.tudelft.simulation.dsol.SimRuntimeException;
24  import nl.tudelft.simulation.language.DsolException;
25  
26  /**
27   * <p>
28   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
29   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
30   * </p>
31   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
32   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
33   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
34   */
35  public class TJunctionDemo extends OtsSimulationApplication<TJunctionModel>
36  {
37      /** */
38      private static final long serialVersionUID = 20161211L;
39  
40      /**
41       * Create a T-Junction demo.
42       * @param title String; the title of the Frame
43       * @param panel OtsAnimationPanel; the tabbed panel to display
44       * @param model TJunctionModel; the model
45       * @throws OtsDrawingException on animation error
46       */
47      public TJunctionDemo(final String title, final OtsAnimationPanel panel, final TJunctionModel model)
48              throws OtsDrawingException
49      {
50          super(model, panel);
51      }
52  
53      /**
54       * Main program.
55       * @param args String[]; the command line arguments (not used)
56       */
57      public static void main(final String[] args)
58      {
59          demo(true);
60      }
61  
62      /**
63       * Start the demo.
64       * @param exitOnClose boolean; when running stand-alone: true; when running as part of a demo: false
65       */
66      public static void demo(final boolean exitOnClose)
67      {
68          Gtu.ALIGNED = false;
69          try
70          {
71              OtsAnimator simulator = new OtsAnimator("TJunctionDemo");
72              final TJunctionModel junctionModel = new TJunctionModel(simulator);
73              simulator.initialize(Time.ZERO, Duration.ZERO, Duration.instantiateSI(3600.0), junctionModel);
74              OtsAnimationPanel animationPanel = new OtsAnimationPanel(junctionModel.getNetwork().getExtent(),
75                      new Dimension(800, 600), simulator, junctionModel, DEFAULT_COLORER, 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 | OtsDrawingException | 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 OtsSimulatorInterface; the simulator for this model
99           */
100         public TJunctionModel(final OtsSimulatorInterface simulator)
101         {
102             super(simulator);
103         }
104 
105         /** {@inheritDoc} */
106         @Override
107         public void constructModel() throws SimRuntimeException
108         {
109             try
110             {
111                 URL xmlURL = URLResource.getResource("/resources/conflict/TJunction.xml");
112                 this.network = new RoadNetwork("TJunction", getSimulator());
113                 new XmlParser(this.network).setUrl(xmlURL).setScenario("1").build();
114             }
115             catch (Exception exception)
116             {
117                 exception.printStackTrace();
118             }
119         }
120 
121         /** {@inheritDoc} */
122         @Override
123         public RoadNetwork getNetwork()
124         {
125             return this.network;
126         }
127 
128     }
129 }