View Javadoc
1   package org.opentrafficsim.swing.gui;
2   
3   import java.awt.Dimension;
4   import java.rmi.RemoteException;
5   import java.util.Map;
6   
7   import org.opentrafficsim.core.definitions.DefaultsNl;
8   import org.opentrafficsim.core.dsol.AbstractOtsModel;
9   import org.opentrafficsim.core.dsol.OtsAnimator;
10  import org.opentrafficsim.core.dsol.OtsSimulatorInterface;
11  import org.opentrafficsim.draw.gtu.DefaultCarAnimation.GtuData.GtuMarker;
12  import org.opentrafficsim.road.network.RoadNetwork;
13  
14  import nl.tudelft.simulation.dsol.SimRuntimeException;
15  import nl.tudelft.simulation.language.DsolException;
16  
17  /**
18   * Custom simulation uses the custom model class where the network and other simulation aspects are externally specified.
19   * <p>
20   * Copyright (c) 2024-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
21   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
22   * </p>
23   */
24  public class CustomSimulation extends OtsSimulationApplication<CustomSimulation.CustomModel>
25  {
26      /** */
27      private static final long serialVersionUID = 20240418L;
28  
29      /**
30       * Create a custom simulation.
31       * @param title the title of the Frame
32       * @param panel the tabbed panel to display
33       * @param model the model
34       */
35      public CustomSimulation(final String title, final OtsAnimationPanel panel, final CustomModel model)
36      {
37          super(model, panel, Map.of(DefaultsNl.TRUCK, GtuMarker.SQUARE));
38      }
39  
40      /**
41       * Start the simulation.
42       * @param exitOnClose when running stand-alone: true; when running as part of a demo: false
43       * @param simulator simulator.
44       * @param model model.
45       */
46      public static void demo(final boolean exitOnClose, final OtsAnimator simulator, final CustomModel model)
47      {
48          try
49          {
50              OtsAnimationPanel animationPanel = new OtsAnimationPanel(model.getNetwork().getExtent(), new Dimension(800, 600),
51                      simulator, model, DEFAULT_GTU_COLORERS, model.getNetwork());
52              CustomSimulation app = new CustomSimulation("Custom Simulation", animationPanel, model);
53              app.setExitOnClose(exitOnClose);
54              animationPanel.enableSimulationControlButtons();
55          }
56          catch (SimRuntimeException | RemoteException | DsolException exception)
57          {
58              exception.printStackTrace();
59          }
60      }
61  
62      /**
63       * Custom simulation.
64       * <p>
65       * Copyright (c) 2024-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
66       * <br>
67       * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
68       * </p>
69       */
70      public static class CustomModel extends AbstractOtsModel
71      {
72          /** */
73          private static final long serialVersionUID = 20240418L;
74  
75          /** The network. */
76          private RoadNetwork network;
77  
78          /**
79           * Constructor.
80           * @param simulator the simulator for this model
81           */
82          public CustomModel(final OtsSimulatorInterface simulator)
83          {
84              super(simulator);
85          }
86  
87          /**
88           * Set network.
89           * @param network the network
90           */
91          public void setNetwork(final RoadNetwork network)
92          {
93              this.network = network;
94          }
95  
96          @Override
97          public void constructModel() throws SimRuntimeException
98          {
99              // custom through external code
100         }
101 
102         @Override
103         public RoadNetwork getNetwork()
104         {
105             return this.network;
106         }
107     }
108 
109 }