Allow setting random seed as parameter

This commit is contained in:
Moritz 'Morty' Strübe 2013-10-31 13:19:23 +01:00
parent ce0b2cacac
commit 56afc6d437
2 changed files with 41 additions and 29 deletions

View file

@ -633,7 +633,7 @@ public class GUI extends Observable {
private void doLoadConfigAsync(final boolean ask, final boolean quick, final File file) { private void doLoadConfigAsync(final boolean ask, final boolean quick, final File file) {
new Thread(new Runnable() { new Thread(new Runnable() {
public void run() { public void run() {
myGUI.doLoadConfig(ask, quick, file); myGUI.doLoadConfig(ask, quick, file, null);
} }
}).start(); }).start();
} }
@ -1321,7 +1321,7 @@ public class GUI extends Observable {
return desktop; return desktop;
} }
public static Simulation quickStartSimulationConfig(File config, boolean vis) { public static Simulation quickStartSimulationConfig(File config, boolean vis, Long manualRandomSeed) {
logger.info("> Starting Cooja"); logger.info("> Starting Cooja");
JDesktopPane desktop = createDesktopPane(); JDesktopPane desktop = createDesktopPane();
if (vis) { if (vis) {
@ -1333,11 +1333,11 @@ public class GUI extends Observable {
} }
if (vis) { if (vis) {
gui.doLoadConfig(false, true, config); gui.doLoadConfig(false, true, config, manualRandomSeed);
return gui.getSimulation(); return gui.getSimulation();
} else { } else {
try { try {
Simulation newSim = gui.loadSimulationConfig(config, true); Simulation newSim = gui.loadSimulationConfig(config, true, manualRandomSeed);
if (newSim == null) { if (newSim == null) {
return null; return null;
} }
@ -1356,7 +1356,7 @@ public class GUI extends Observable {
* @param source Contiki application file name * @param source Contiki application file name
* @return True if simulation was created * @return True if simulation was created
*/ */
private static boolean quickStartSimulation(String source) { private static Simulation quickStartSimulation(String source) {
logger.info("> Starting Cooja"); logger.info("> Starting Cooja");
JDesktopPane desktop = createDesktopPane(); JDesktopPane desktop = createDesktopPane();
frame = new JFrame(WINDOW_TITLE); frame = new JFrame(WINDOW_TITLE);
@ -1364,14 +1364,14 @@ public class GUI extends Observable {
configureFrame(gui, false); configureFrame(gui, false);
logger.info("> Creating simulation"); logger.info("> Creating simulation");
Simulation simulation = new Simulation(gui); Simulation sim = new Simulation(gui);
simulation.setTitle("Quickstarted simulation: " + source); sim.setTitle("Quickstarted simulation: " + source);
boolean simOK = CreateSimDialog.showDialog(GUI.getTopParentContainer(), simulation); boolean simOK = CreateSimDialog.showDialog(GUI.getTopParentContainer(), sim);
if (!simOK) { if (!simOK) {
logger.fatal("No simulation, aborting quickstart"); logger.fatal("No simulation, aborting quickstart");
System.exit(1); System.exit(1);
} }
gui.setSimulation(simulation, true); gui.setSimulation(sim, true);
logger.info("> Creating mote type"); logger.info("> Creating mote type");
ContikiMoteType moteType = new ContikiMoteType(); ContikiMoteType moteType = new ContikiMoteType();
@ -1379,20 +1379,20 @@ public class GUI extends Observable {
moteType.setDescription("Cooja mote type (" + source + ")"); moteType.setDescription("Cooja mote type (" + source + ")");
try { try {
boolean compileOK = moteType.configureAndInit(GUI.getTopParentContainer(), simulation, true); boolean compileOK = moteType.configureAndInit(GUI.getTopParentContainer(), sim, true);
if (!compileOK) { if (!compileOK) {
logger.fatal("Mote type initialization failed, aborting quickstart"); logger.fatal("Mote type initialization failed, aborting quickstart");
return false; return null;
} }
} catch (MoteTypeCreationException e1) { } catch (MoteTypeCreationException e1) {
logger.fatal("Mote type initialization failed, aborting quickstart"); logger.fatal("Mote type initialization failed, aborting quickstart");
return false; return null;
} }
simulation.addMoteType(moteType); sim.addMoteType(moteType);
logger.info("> Adding motes"); logger.info("> Adding motes");
gui.doAddMotes(moteType); gui.doAddMotes(moteType);
return true; return sim;
} }
//// PROJECT CONFIG AND EXTENDABLE PARTS METHODS //// //// PROJECT CONFIG AND EXTENDABLE PARTS METHODS ////
@ -2258,7 +2258,7 @@ public class GUI extends Observable {
* @param quick Quick-load simulation * @param quick Quick-load simulation
* @param configFile Configuration file to load, if null a dialog will appear * @param configFile Configuration file to load, if null a dialog will appear
*/ */
public void doLoadConfig(boolean askForConfirmation, final boolean quick, File configFile) { public void doLoadConfig(boolean askForConfirmation, final boolean quick, File configFile, Long manualRandomSeed) {
if (isVisualizedInApplet()) { if (isVisualizedInApplet()) {
return; return;
} }
@ -2278,7 +2278,7 @@ public class GUI extends Observable {
if (!configFile.exists() || !configFile.canRead()) { if (!configFile.exists() || !configFile.canRead()) {
logger.fatal("No read access to file: " + configFile.getAbsolutePath()); logger.fatal("No read access to file: " + configFile.getAbsolutePath());
/* File does not exist, open dialog */ /* File does not exist, open dialog */
doLoadConfig(askForConfirmation, quick, null); doLoadConfig(askForConfirmation, quick, null, manualRandomSeed);
return; return;
} }
} else { } else {
@ -2394,7 +2394,7 @@ public class GUI extends Observable {
shouldRetry = false; shouldRetry = false;
myGUI.doRemoveSimulation(false); myGUI.doRemoveSimulation(false);
PROGRESS_WARNINGS.clear(); PROGRESS_WARNINGS.clear();
newSim = loadSimulationConfig(fileToLoad, quick); newSim = loadSimulationConfig(fileToLoad, quick, manualRandomSeed);
myGUI.setSimulation(newSim, false); myGUI.setSimulation(newSim, false);
/* Optionally show compilation warnings */ /* Optionally show compilation warnings */
@ -3121,6 +3121,9 @@ public class GUI extends Observable {
*/ */
public static void main(String[] args) { public static void main(String[] args) {
String logConfigFile = null; String logConfigFile = null;
Long randomSeed = null;
for (String element : args) { for (String element : args) {
if (element.startsWith("-log4j=")) { if (element.startsWith("-log4j=")) {
String arg = element.substring("-log4j=".length()); String arg = element.substring("-log4j=".length());
@ -3178,6 +3181,15 @@ public class GUI extends Observable {
GUI.externalToolsUserSettingsFileReadOnly = true; GUI.externalToolsUserSettingsFileReadOnly = true;
} }
} }
if (element.startsWith("-random-seed=")) {
String arg = element.substring("-random-seed=".length());
try {
randomSeed = Long.valueOf(arg);
} catch (Exception e) {
logger.error("Failed to convert \"" + arg +"\" to an integer.");
}
}
} }
// Check if simulator should be quick-started // Check if simulator should be quick-started
@ -3190,11 +3202,9 @@ public class GUI extends Observable {
contikiApp = contikiApp.replace("/cygdrive/" + driveCharacter + "/", driveCharacter + ":/"); contikiApp = contikiApp.replace("/cygdrive/" + driveCharacter + "/", driveCharacter + ":/");
} }
boolean ok = false; Simulation sim = null;
if (contikiApp.endsWith(".csc")) { if (contikiApp.endsWith(".csc")) {
sim = quickStartSimulationConfig(new File(contikiApp), true, randomSeed);
ok = quickStartSimulationConfig(new File(contikiApp), true) != null;
} else { } else {
if (contikiApp.endsWith(".cooja")) { if (contikiApp.endsWith(".cooja")) {
contikiApp = contikiApp.substring(0, contikiApp.length() - ".cooja".length()); contikiApp = contikiApp.substring(0, contikiApp.length() - ".cooja".length());
@ -3203,19 +3213,20 @@ public class GUI extends Observable {
contikiApp += ".c"; contikiApp += ".c";
} }
ok = quickStartSimulation(contikiApp); sim = quickStartSimulation(contikiApp);
} }
if (!ok) { if (sim == null) {
System.exit(1); System.exit(1);
} }
} else if (args.length > 0 && args[0].startsWith("-nogui=")) { } else if (args.length > 0 && args[0].startsWith("-nogui=")) {
/* Load simulation */ /* Load simulation */
String config = args[0].substring("-nogui=".length()); String config = args[0].substring("-nogui=".length());
File configFile = new File(config); File configFile = new File(config);
Simulation sim = quickStartSimulationConfig(configFile, false); Simulation sim = quickStartSimulationConfig(configFile, false, randomSeed);
if (sim == null) { if (sim == null) {
System.exit(1); System.exit(1);
} }
@ -3253,6 +3264,7 @@ public class GUI extends Observable {
System.exit(1); System.exit(1);
} }
} }
sim.setSpeedLimit(null); sim.setSpeedLimit(null);
sim.startSimulation(); sim.startSimulation();
@ -3319,7 +3331,7 @@ public class GUI extends Observable {
* @throws UnsatisfiedLinkError * @throws UnsatisfiedLinkError
* If associated libraries could not be loaded * If associated libraries could not be loaded
*/ */
public Simulation loadSimulationConfig(File file, boolean quick) public Simulation loadSimulationConfig(File file, boolean quick, Long manualRandomSeed)
throws UnsatisfiedLinkError, SimulationCreationException { throws UnsatisfiedLinkError, SimulationCreationException {
this.currentConfigFile = file; /* Used to generate config relative paths */ this.currentConfigFile = file; /* Used to generate config relative paths */
try { try {
@ -3337,7 +3349,7 @@ public class GUI extends Observable {
Element root = doc.getRootElement(); Element root = doc.getRootElement();
in.close(); in.close();
return loadSimulationConfig(root, quick, null); return loadSimulationConfig(root, quick, manualRandomSeed);
} catch (JDOMException e) { } catch (JDOMException e) {
throw (SimulationCreationException) new SimulationCreationException("Config not wellformed").initCause(e); throw (SimulationCreationException) new SimulationCreationException("Config not wellformed").initCause(e);
} catch (IOException e) { } catch (IOException e) {
@ -4434,7 +4446,7 @@ public class GUI extends Observable {
final File file = getLastOpenedFile(); final File file = getLastOpenedFile();
new Thread(new Runnable() { new Thread(new Runnable() {
public void run() { public void run() {
myGUI.doLoadConfig(true, true, file); myGUI.doLoadConfig(true, true, file, null);
} }
}).start(); }).start();
return; return;

View file

@ -102,7 +102,7 @@ public class ExecuteJAR {
GUI.externalToolsUserSettingsFile = new File( GUI.externalToolsUserSettingsFile = new File(
System.getProperty("user.home"), System.getProperty("user.home"),
GUI.EXTERNAL_TOOLS_USER_SETTINGS_FILENAME); GUI.EXTERNAL_TOOLS_USER_SETTINGS_FILENAME);
Simulation s = GUI.quickStartSimulationConfig(config, false); Simulation s = GUI.quickStartSimulationConfig(config, false, null);
if (s == null) { if (s == null) {
throw new RuntimeException( throw new RuntimeException(
"Error when creating simulation" "Error when creating simulation"
@ -187,7 +187,7 @@ public class ExecuteJAR {
logger.info("Starting simulation"); logger.info("Starting simulation");
GUI.setLookAndFeel(); GUI.setLookAndFeel();
GUI.quickStartSimulationConfig(new File(executeDir, SIMCONFIG_FILENAME), false); GUI.quickStartSimulationConfig(new File(executeDir, SIMCONFIG_FILENAME), false, null);
} }
/** /**