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
27
28
29
30
31
32
33 public class OtsRunner extends OtsSimulationApplication<OtsRunnerModel>
34 {
35
36 private static final long serialVersionUID = 20231012;
37
38
39
40
41
42
43
44 public OtsRunner(final OtsAnimationPanel panel, final OtsRunnerModel model)
45 throws OtsDrawingException
46 {
47 super(model, panel);
48 }
49
50
51
52
53
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
76 @Override
77 protected void setAnimationToggles()
78 {
79 AnimationToggles.setIconAnimationTogglesStandard(getAnimationPanel());
80 }
81
82
83
84
85 public static class OtsRunnerModel extends AbstractOtsModel
86 {
87
88 private static final long serialVersionUID = 20231012L;
89
90
91 private File file;
92
93
94 private String scenario;
95
96
97 private RoadNetwork network;
98
99
100
101
102
103
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
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
133 @Override
134 public RoadNetwork getNetwork()
135 {
136 return this.network;
137 }
138
139 }
140 }