View Javadoc
1   package org.opentrafficsim.editor;
2   
3   import java.awt.Dimension;
4   import java.io.File;
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.opentrafficsim.core.dsol.AbstractOtsModel;
12  import org.opentrafficsim.core.dsol.OtsAnimator;
13  import org.opentrafficsim.core.dsol.OtsSimulatorInterface;
14  import org.opentrafficsim.draw.OtsDrawingException;
15  import org.opentrafficsim.editor.OtsRunner.OtsRunnerModel;
16  import org.opentrafficsim.road.network.RoadNetwork;
17  import org.opentrafficsim.road.network.factory.xml.parser.XmlParser;
18  import org.opentrafficsim.swing.gui.AnimationToggles;
19  import org.opentrafficsim.swing.gui.OtsAnimationPanel;
20  import org.opentrafficsim.swing.gui.OtsSimulationApplication;
21  
22  import nl.tudelft.simulation.dsol.SimRuntimeException;
23  import nl.tudelft.simulation.language.DsolException;
24  
25  /**
26   * Simulation runner for when running from editor.
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/wjschakel">Wouter Schakel</a>
32   */
33  public class OtsRunner extends OtsSimulationApplication<OtsRunnerModel>
34  {
35      /** */
36      private static final long serialVersionUID = 20231012;
37  
38      /**
39       * Run a simulation.
40       * @param panel OtsAnimationPanel; the tabbed panel to display
41       * @param model TJunctionModel; the model
42       * @throws OtsDrawingException on animation error
43       */
44      public OtsRunner(final OtsAnimationPanel panel, final OtsRunnerModel model)
45              throws OtsDrawingException
46      {
47          super(model, panel);
48      }
49  
50      /**
51       * Run single run from a file.
52       * @param file File; XML file.
53       * @param scenario String; scenario, may be {@code null}.
54       */
55      public static void runSingle(final File file, final String scenario)
56      {
57          try
58          {
59              OtsAnimator simulator = new OtsAnimator("EditorRun");
60              final OtsRunnerModel runnerModel = new OtsRunnerModel(simulator, file, scenario);
61              simulator.initialize(Time.ZERO, Duration.ZERO, Duration.instantiateSI(3600.0), runnerModel);
62              OtsAnimationPanel animationPanel = new OtsAnimationPanel(runnerModel.getNetwork().getExtent(),
63                      new Dimension(800, 600), simulator, runnerModel, DEFAULT_COLORER, runnerModel.getNetwork());
64              OtsRunner app = new OtsRunner(animationPanel, runnerModel);
65              app.setExitOnClose(false);
66              animationPanel.enableSimulationControlButtons();
67          }
68          catch (SimRuntimeException | NamingException | RemoteException | OtsDrawingException | DsolException exception)
69          {
70              exception.printStackTrace();
71          }
72      }
73      
74      
75      /** {@inheritDoc} */
76      @Override
77      protected void setAnimationToggles()
78      {
79          AnimationToggles.setIconAnimationTogglesStandard(getAnimationPanel());
80      }
81  
82      /**
83       * The simulation model.
84       */
85      public static class OtsRunnerModel extends AbstractOtsModel
86      {
87          /** */
88          private static final long serialVersionUID = 20231012L;
89  
90          /** File. */
91          private File file;
92          
93          /** Scenario. */
94          private String scenario;
95          
96          /** The network. */
97          private RoadNetwork network;
98          
99          /**
100          * Constructor.
101          * @param simulator OtsSimulatorInterface; simulator.
102          * @param file File; XML file.
103          * @param scenario String; scenario, may be {@code null}.
104          */
105         public OtsRunnerModel(final OtsSimulatorInterface simulator, final File file, final String scenario)
106         {
107             super(simulator);
108             this.file = file;
109             this.scenario = scenario;
110         }
111 
112         /** {@inheritDoc} */
113         @Override
114         public void constructModel() throws SimRuntimeException
115         {
116             try
117             {
118                 this.network = new RoadNetwork("EditorNetwork", getSimulator());
119                 XmlParser parser = new XmlParser(this.network).setUrl(this.file.toURI().toURL());
120                 if (this.scenario != null)
121                 {
122                     parser.setScenario(this.scenario);
123                 }
124                 parser.build();
125             }
126             catch (Exception exception)
127             {
128                 exception.printStackTrace();
129             }
130         }
131 
132         /** {@inheritDoc} */
133         @Override
134         public RoadNetwork getNetwork()
135         {
136             return this.network;
137         }
138 
139     }
140 }