1010import java .util .Iterator ;
1111import java .util .LinkedHashMap ;
1212import java .util .Map ;
13+ import java .util .concurrent .TimeUnit ;
1314import javax .swing .BorderFactory ;
1415import javax .swing .BoxLayout ;
1516import 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