View Javadoc
1   package org.opentrafficsim.editor;
2   
3   import java.io.File;
4   import java.rmi.RemoteException;
5   import java.util.Collections;
6   
7   import org.opentrafficsim.core.dsol.OtsAnimator;
8   import org.opentrafficsim.core.dsol.OtsSimulatorInterface;
9   import org.opentrafficsim.editor.OtsRunner.OtsRunnerModel;
10  import org.opentrafficsim.road.network.factory.xml.OtsXmlModel;
11  import org.opentrafficsim.road.network.factory.xml.parser.XmlParser;
12  import org.opentrafficsim.swing.gui.AnimationToggles;
13  import org.opentrafficsim.swing.gui.OtsAnimationPanel;
14  import org.opentrafficsim.swing.gui.OtsSimulationApplication;
15  
16  import nl.tudelft.simulation.dsol.SimRuntimeException;
17  import nl.tudelft.simulation.language.DsolException;
18  
19  /**
20   * Simulation runner for when running from editor.
21   * <p>
22   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
23   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
24   * </p>
25   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
26   */
27  public class OtsRunner extends OtsSimulationApplication<OtsRunnerModel>
28  {
29  
30      /** */
31      private static final long serialVersionUID = 20231012;
32  
33      /**
34       * Run a simulation.
35       * @param panel the tabbed panel to display
36       * @param model the model
37       */
38      public OtsRunner(final OtsAnimationPanel panel, final OtsRunnerModel model)
39      {
40          // TODO colorer and markers based on user specification
41          super(model, panel, Collections.emptyMap());
42      }
43  
44      /**
45       * Run single run from a file.
46       * @param file XML file.
47       * @param scenario scenario, may be {@code null}.
48       */
49      public static void runSingle(final File file, final String scenario)
50      {
51          try
52          {
53              OtsAnimator simulator = new OtsAnimator("EditorRun");
54              final OtsRunnerModel runnerModel = new OtsRunnerModel(simulator, file, scenario);
55              OtsAnimationPanel animationPanel = new OtsAnimationPanel(runnerModel.getNetwork().getExtent(), simulator,
56                      runnerModel, DEFAULT_GTU_COLORERS, runnerModel.getNetwork());
57              OtsRunner app = new OtsRunner(animationPanel, runnerModel);
58              app.setExitOnClose(false);
59              animationPanel.enableSimulationControlButtons();
60          }
61          catch (SimRuntimeException | RemoteException | DsolException exception)
62          {
63              exception.printStackTrace();
64          }
65      }
66  
67      @Override
68      protected void setAnimationToggles()
69      {
70          AnimationToggles.setIconAnimationTogglesStandard(getAnimationPanel());
71      }
72  
73      /**
74       * The simulation model.
75       */
76      public static class OtsRunnerModel extends OtsXmlModel
77      {
78          /** Scenario. */
79          private String scenario;
80  
81          /**
82           * Constructor.
83           * @param simulator simulator.
84           * @param file XML file.
85           * @param scenario scenario, may be {@code null}.
86           */
87          public OtsRunnerModel(final OtsSimulatorInterface simulator, final File file, final String scenario)
88          {
89              super(simulator, file.toString());
90              this.scenario = scenario;
91          }
92  
93          @Override
94          public void constructModel(final XmlParser xmlParser) throws Exception
95          {
96              xmlParser.setScenario(this.scenario).build();
97          }
98      }
99  
100 }