bugfix: random generator was initialized differently at load vs reload

This commit is contained in:
fros4943 2009-06-24 07:56:15 +00:00
parent 92f8ac6a2b
commit 5d20b01f04
2 changed files with 26 additions and 22 deletions

View File

@ -24,7 +24,7 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* *
* $Id: GUI.java,v 1.137 2009/06/15 18:13:45 fros4943 Exp $ * $Id: GUI.java,v 1.138 2009/06/24 07:56:15 fros4943 Exp $
*/ */
package se.sics.cooja; package se.sics.cooja;
@ -2222,9 +2222,8 @@ public class GUI extends Observable {
try { try {
shouldRetry = false; shouldRetry = false;
myGUI.doRemoveSimulation(false); myGUI.doRemoveSimulation(false);
Simulation newSim = loadSimulationConfig(root, true); Simulation newSim = loadSimulationConfig(root, true, new Long(randomSeed));
myGUI.setSimulation(newSim, false); myGUI.setSimulation(newSim, false);
myGUI.getSimulation().setRandomSeed(randomSeed);
if (autoStart) { if (autoStart) {
newSim.startSimulation(); newSim.startSimulation();
@ -3064,7 +3063,7 @@ public class GUI extends Observable {
Document doc = builder.build(file); Document doc = builder.build(file);
Element root = doc.getRootElement(); Element root = doc.getRootElement();
return loadSimulationConfig(root, quick); return loadSimulationConfig(root, quick, null);
} catch (JDOMException e) { } catch (JDOMException e) {
logger.fatal("Config not wellformed: " + e.getMessage()); logger.fatal("Config not wellformed: " + e.getMessage());
return null; return null;
@ -3081,7 +3080,7 @@ public class GUI extends Observable {
Document doc = builder.build(stringReader); Document doc = builder.build(stringReader);
Element root = doc.getRootElement(); Element root = doc.getRootElement();
return loadSimulationConfig(root, quick); return loadSimulationConfig(root, quick, null);
} catch (JDOMException e) { } catch (JDOMException e) {
throw (SimulationCreationException) new SimulationCreationException( throw (SimulationCreationException) new SimulationCreationException(
"Configuration file not wellformed: " + e.getMessage()).initCause(e); "Configuration file not wellformed: " + e.getMessage()).initCause(e);
@ -3091,7 +3090,7 @@ public class GUI extends Observable {
} }
} }
private Simulation loadSimulationConfig(Element root, boolean quick) private Simulation loadSimulationConfig(Element root, boolean quick, Long manualRandomSeed)
throws SimulationCreationException { throws SimulationCreationException {
Simulation newSim = null; Simulation newSim = null;
@ -3153,7 +3152,7 @@ public class GUI extends Observable {
Collection<Element> config = ((Element) element).getChildren(); Collection<Element> config = ((Element) element).getChildren();
newSim = new Simulation(this); newSim = new Simulation(this);
System.gc(); System.gc();
boolean createdOK = newSim.setConfigXML(config, !quick); boolean createdOK = newSim.setConfigXML(config, !quick, manualRandomSeed);
if (!createdOK) { if (!createdOK) {
logger.info("Simulation not loaded"); logger.info("Simulation not loaded");
return null; return null;

View File

@ -24,7 +24,7 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* *
* $Id: Simulation.java,v 1.47 2009/05/26 14:15:41 fros4943 Exp $ * $Id: Simulation.java,v 1.48 2009/06/24 07:56:15 fros4943 Exp $
*/ */
package se.sics.cooja; package se.sics.cooja;
@ -278,7 +278,7 @@ public class Simulation extends Observable implements Runnable {
} }
/** /**
* Creates a new simulation with a delay time of 100 ms. * Creates a new simulation
*/ */
public Simulation(GUI gui) { public Simulation(GUI gui) {
myGUI = gui; myGUI = gui;
@ -348,6 +348,13 @@ public class Simulation extends Observable implements Runnable {
return randomSeed; return randomSeed;
} }
/**
* @return Random seed (converted to a string)
*/
public String getRandomSeedString() {
return Long.toString(randomSeed);
}
/** /**
* @param randomSeed Random seed * @param randomSeed Random seed
*/ */
@ -462,20 +469,16 @@ public class Simulation extends Observable implements Runnable {
} }
/** /**
* Sets the current simulation config depending on the given XML elements. * Sets the current simulation config depending on the given configuration.
* *
* @see #getConfigXML() * @param configXML Simulation configuration
* @param configXML * @param visAvailable True if simulation is allowed to show visualizers
* Config XML elements * @param manualRandomSeed Simulation random seed. May be null, in which case the configuration is used
* @param visAvailable * @return True if simulation was configured successfully
* True if simulation is allowed to show visualizers while loading * @throws Exception If configuration could not be loaded
* the given config
* @return True if simulation config set successfully
* @throws Exception
* If configuration could not be loaded
*/ */
public boolean setConfigXML(Collection<Element> configXML, public boolean setConfigXML(Collection<Element> configXML,
boolean visAvailable) throws Exception { boolean visAvailable, Long manualRandomSeed) throws Exception {
// Parse elements // Parse elements
for (Element element : configXML) { for (Element element : configXML) {
@ -492,7 +495,9 @@ public class Simulation extends Observable implements Runnable {
// Random seed // Random seed
if (element.getName().equals("randomseed")) { if (element.getName().equals("randomseed")) {
if (element.getText().equals("generated")) { if (manualRandomSeed != null) {
setRandomSeed(manualRandomSeed);
} else if (element.getText().equals("generated")) {
randomSeedGenerated = true; randomSeedGenerated = true;
setRandomSeed(new Random().nextLong()); setRandomSeed(new Random().nextLong());
} else { } else {