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