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