Skip to content

Commit f841dfa

Browse files
authored
Merge pull request #179 from centreformicrosimulation/experimental/adding-singlerun-headless-capabilty
Experimental/adding singlerun headless capabilty
2 parents 1e6d59d + a17c829 commit f841dfa

2 files changed

Lines changed: 60 additions & 6 deletions

File tree

.github/workflows/SimPathsBuild.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,28 @@ jobs:
4343
- name: Run integration tests
4444
run: mvn verify
4545

46+
run-simpaths-start:
47+
needs: build
48+
runs-on: [ ubuntu-latest ]
49+
steps:
50+
- uses: actions/checkout@v4
51+
- name: Set up JDK 19
52+
uses: actions/setup-java@v4
53+
with:
54+
java-version: '19'
55+
distribution: 'temurin'
56+
- uses: actions/download-artifact@v4
57+
with:
58+
name: simpaths_jars
59+
path: .
60+
- name: Do one full Setup and Run with SimPathsStart (mimicking GUI run)
61+
run: java -jar singlerun.jar -c UK -s 2019 -g false --rewrite-policy-schedule
62+
- name: Check input db exists
63+
id: check_file
64+
uses: thebinaryfelix/check-file-existence-action@1.0.0
65+
with:
66+
files: 'input/input.mv.db, input/EUROMODpolicySchedule.xlsx, input/DatabaseCountryYear.xlsx'
67+
4668
run-simpaths-persist-root:
4769
needs: build
4870
runs-on: [ ubuntu-latest ]

src/main/java/simpaths/experiment/SimPathsStart.java

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.Iterator;
1111
import java.util.LinkedHashMap;
1212
import java.util.Map;
13+
import java.util.concurrent.TimeUnit;
1314
import javax.swing.BorderFactory;
1415
import javax.swing.BoxLayout;
1516
import javax.swing.JInternalFrame;
@@ -47,7 +48,8 @@ public class SimPathsStart implements ExperimentBuilder {
4748

4849
private static boolean showGui = true; // Show GUI by default
4950

50-
private static boolean setupOnly = false;
51+
private static boolean doSetup = true;
52+
private static boolean doRun = true;
5153

5254
private static boolean rewritePolicySchedule = false;
5355

@@ -70,13 +72,13 @@ public static void main(String[] args) {
7072
runGUIdialog();
7173
} else {
7274
try {
73-
runGUIlessSetup(4);
75+
if (doSetup) runGUIlessSetup(4);
7476
} catch (FileNotFoundException f) {
7577
System.err.println(f.getMessage());
7678
};
7779
}
7880

79-
if (setupOnly) {
81+
if (!doRun) {
8082
System.out.println("Setup complete, exiting.");
8183
return;
8284
}
@@ -93,6 +95,7 @@ public static void main(String[] args) {
9395
startYear = Integer.parseInt(valueYear);
9496

9597
// start the JAS-mine simulation engine
98+
System.out.println("Starting simulation...");
9699
final SimulationEngine engine = SimulationEngine.getInstance();
97100
MicrosimShell gui = null;
98101
if (showGui) {
@@ -102,6 +105,22 @@ public static void main(String[] args) {
102105
SimPathsStart experimentBuilder = new SimPathsStart();
103106
engine.setExperimentBuilder(experimentBuilder);
104107
engine.setup();
108+
109+
if (!showGui) {
110+
engine.startSimulation();
111+
try {
112+
while (engine.getRunningStatus()) {
113+
try {
114+
TimeUnit.SECONDS.sleep(10);
115+
} catch (InterruptedException e) {
116+
System.err.println("Interrupted while waiting for simulation to complete.");
117+
return;
118+
}
119+
}
120+
} finally {
121+
engine.quit();
122+
}
123+
}
105124
}
106125

107126
private static boolean parseCommandLineArgs(String[] args) {
@@ -115,9 +134,12 @@ private static boolean parseCommandLineArgs(String[] args) {
115134
startYearOption.setArgName("year");
116135
options.addOption(startYearOption);
117136

118-
Option setupOption = new Option("Setup", "Setup only");
137+
Option setupOption = new Option("Setup", "Setup only (no run)");
119138
options.addOption(setupOption);
120139

140+
Option runOption = new Option("Run", "Run only (no setup)");
141+
options.addOption(runOption);
142+
121143
Option rewritePolicyScheduleOption = new Option("r", "rewrite-policy-schedule",false, "Re-write policy schedule from detected policy files");
122144
options.addOption(rewritePolicyScheduleOption);
123145

@@ -135,6 +157,10 @@ private static boolean parseCommandLineArgs(String[] args) {
135157
try {
136158
CommandLine cmd = parser.parse(options, args);
137159

160+
if(cmd.hasOption("Setup") && cmd.hasOption("Run")) {
161+
throw new ParseException("'Run only' and 'Setup only' options cannot be used together.");
162+
}
163+
138164
if (cmd.hasOption("h")) {
139165
printHelpMessage(formatter, options);
140166
return false; // Exit without reporting an error
@@ -157,7 +183,13 @@ private static boolean parseCommandLineArgs(String[] args) {
157183
}
158184

159185
if (cmd.hasOption("Setup")) {
160-
setupOnly = true;
186+
doSetup = true;
187+
doRun = false;
188+
}
189+
190+
if (cmd.hasOption("Run")) {
191+
doRun = true;
192+
doSetup = false;
161193
}
162194

163195
if (cmd.hasOption("r")) {
@@ -199,7 +231,7 @@ public void buildExperiment(SimulationEngine engine) {
199231

200232
engine.addSimulationManager(model);
201233
engine.addSimulationManager(collector);
202-
engine.addSimulationManager(observer);
234+
if (showGui) engine.addSimulationManager(observer);
203235

204236
model.setCollector(collector);
205237
}

0 commit comments

Comments
 (0)