Skip to content

Commit

Permalink
Adresses #3085 by reporting an error (instead of a warning)
Browse files Browse the repository at this point in the history
when a local variable uses the name of a species, and by correctly
retrieving the local value if ever it could be compiled.
  • Loading branch information
AlexisDrogoul committed Apr 17, 2021
1 parent 2924b55 commit b29f9fe
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 325 deletions.
73 changes: 26 additions & 47 deletions msi.gama.core/src/msi/gama/kernel/experiment/ExperimentAgent.java
Expand Up @@ -206,9 +206,8 @@ public void reset() {
}

public String getDefinedRng() {
if (GamaPreferences.External.CORE_RND_EDITABLE.getValue()) {
if (GamaPreferences.External.CORE_RND_EDITABLE.getValue())
return (String) ((ExperimentPlan) getSpecies()).parameters.get(IKeyword.RNG).value(ownScope);
}
return GamaPreferences.External.CORE_RNG.getValue();
}

Expand All @@ -225,9 +224,7 @@ public Double getDefinedSeed() {
public void closeSimulations() {
// We unschedule the simulation if any
executer.executeDisposeActions();
if (getSimulationPopulation() != null) {
getSimulationPopulation().dispose();
}
if (getSimulationPopulation() != null) { getSimulationPopulation().dispose(); }
if (!getSpecies().isBatch()) {
ownScope.getGui().setSelectedAgent(null);
ownScope.getGui().setHighlightedAgent(null);
Expand All @@ -248,7 +245,7 @@ public void closeSimulations() {

@Override
public void dispose() {
if (dead) { return; }
if (dead) return;
closeSimulations();
GAMA.releaseScope(ownScope);
super.dispose();
Expand All @@ -261,10 +258,8 @@ public void dispose() {
*/
@Override
public Object _init_(final IScope scope) {
if (scope.interrupted()) { return null; }
if (automaticallyCreateFirstSimulation()) {
createSimulation(getParameterValues(), scheduled);
}
if (scope.interrupted()) return null;
if (automaticallyCreateFirstSimulation()) { createSimulation(getParameterValues(), scheduled); }
// We execute any behavior defined in GAML.
super._init_(scope);
return this;
Expand All @@ -279,9 +274,7 @@ protected boolean automaticallyCreateFirstSimulation() {
public boolean init(final IScope scope) {
super.init(scope);
final IOutputManager outputs = getOutputManager();
if (outputs != null) {
outputs.init(scope);
}
if (outputs != null) { outputs.init(scope); }
scope.getGui().getStatus(scope).informStatus("Experiment ready");
scope.getGui().updateExperimentState(scope);
return true;
Expand All @@ -294,7 +287,7 @@ public boolean init(final IScope scope) {
*/
@Override
public Object primDie(final IScope scope) throws GamaRuntimeException {
if (dying) { return null; }
if (dying) return null;
dying = true;
getSpecies().getArchitecture().abort(scope);
GAMA.closeExperiment(getSpecies());
Expand All @@ -304,7 +297,7 @@ public Object primDie(final IScope scope) throws GamaRuntimeException {

public SimulationAgent createSimulation(final ParametersSet parameters, final boolean scheduleIt) {
final IPopulation<? extends IAgent> pop = getSimulationPopulation();
if (pop == null) { return null; }
if (pop == null) return null;
final ParametersSet ps = getParameterValues();
ps.putAll(parameters);
final IList<Map<String, Object>> list = GamaListFactory.create(Types.MAP);
Expand Down Expand Up @@ -381,9 +374,8 @@ public void setGeometry(final IShape newGlobalGeometry) {}
*/

public List<? extends IParameter.Batch> getDefaultParameters() {
if (!getSpecies().isHeadless() && !GamaPreferences.External.CORE_RND_EDITABLE.getValue()) {
if (!getSpecies().isHeadless() && !GamaPreferences.External.CORE_RND_EDITABLE.getValue())
return new ArrayList<>();
}
final List<ExperimentParameter> params = new ArrayList<>();
final String cat = getExperimentParametersCategory();
ExperimentParameter p = new ExperimentParameter(getScope(), getSpecies().getVar(IKeyword.RNG),
Expand Down Expand Up @@ -422,9 +414,7 @@ public void setMinimumDuration(final Double d) {
// d is in seconds, but the slider expects milleseconds
// DEBUG.LOG("Minimum duration set to " + d);
setMinimumDurationExternal(d);
if (initialMinimumDuration == null) {
initialMinimumDuration = d;
}
if (initialMinimumDuration == null) { initialMinimumDuration = d; }
ownScope.getGui().updateSpeedDisplay(ownScope, currentMinimumDuration * 1000, false);
}

Expand All @@ -446,9 +436,7 @@ public Double getInitialMinimumDuration() {
value = ExperimentAgent.MODEL_PATH,
initializer = true)
public String getWorkingPath() {
if (ownModelPath == null) {
ownModelPath = getModel().getWorkingPath() + "/";
}
if (ownModelPath == null) { ownModelPath = getModel().getWorkingPath() + "/"; }
return ownModelPath;
}

Expand Down Expand Up @@ -554,9 +542,7 @@ public Integer getUsage() {
@setter (SimulationAgent.USAGE)
public void setUsage(final Integer s) {
Integer usage = s;
if (s == null) {
usage = 0;
}
if (s == null) { usage = 0; }
getRandomGenerator().setUsage(usage);
}

Expand Down Expand Up @@ -592,7 +578,7 @@ public void setSimulations(final IList<IAgent> simulations) {
@Override
@getter (IKeyword.SIMULATION)
public SimulationAgent getSimulation() {
if (getSimulationPopulation() != null) { return getSimulationPopulation().lastSimulationCreated(); }
if (getSimulationPopulation() != null) return getSimulationPopulation().lastSimulationCreated();
return null;
}

Expand All @@ -611,9 +597,9 @@ public void setOnUserHold(final boolean state) {

@Override
public IPopulation<? extends IAgent> getPopulationFor(final ISpecies species) {
if (species == getModel()) { return getSimulationPopulation(); }
if (species == getModel()) return getSimulationPopulation();
final SimulationAgent sim = getSimulation();
if (sim == null) { return IPopulation.createEmpty(species); }
if (sim == null) return IPopulation.createEmpty(species);
return sim.getPopulationFor(species.getName());

}
Expand All @@ -631,9 +617,7 @@ protected void postStep(final IScope scope) {
executer.executeEndActions();
executer.executeOneShotActions();
final IOutputManager outputs = getOutputManager();
if (outputs != null) {
outputs.step(scope);
}
if (outputs != null) { outputs.step(scope); }
ownClock.step(this.ownScope);
informStatus();
}
Expand Down Expand Up @@ -714,37 +698,33 @@ public IExperimentAgent getExperiment() {
public Object getGlobalVarValue(final String varName) {

// First case: we have the variable inside the experiment.
if (ExperimentAgent.this.hasAttribute(varName) || getSpecies().hasVar(varName)) {
if (ExperimentAgent.this.hasAttribute(varName) || getSpecies().hasVar(varName))
return super.getGlobalVarValue(varName);
}
// Second case: the simulation is not null, so it should handle it
final SimulationAgent sim = getSimulation();
if (sim != null && !sim.dead()) { return sim.getScope().getGlobalVarValue(varName); }
if (sim != null && !sim.dead()) return sim.getScope().getGlobalVarValue(varName);
// Third case, the simulation is null but the model defines this variable (see #2044). We then grab its
// initial value if possible
// However, if the experiment is defined with keep_simulations: false, we should not give access to the
// value, as no simulations can be made available (see #2727)
if (this.getModel().getSpecies().hasVar(varName)) {
final IVariable var = getModel().getSpecies().getVar(varName);
if (!var.isNotModifiable() && !getExperiment().getSpecies().keepsSimulations()) {
if (!var.isNotModifiable() && !getExperiment().getSpecies().keepsSimulations())
throw GamaRuntimeException.error("This experiment does not keep its simulations. " + varName
+ " cannot be retrieved in this context", this);
}
return var.getInitialValue(this);
}
// Fourth case: this is a parameter, so we get it from the species
if (getSpecies().hasParameter(varName)) {
return getSpecies().getExperimentScope().getGlobalVarValue(varName);
}
if (getSpecies().hasParameter(varName)) return getSpecies().getExperimentScope().getGlobalVarValue(varName);
// Fifth case: it is an extra parameter
return extraParametersMap.get(varName);
}

@Override
public boolean hasAccessToGlobalVar(final String varName) {
if (ExperimentAgent.this.hasAttribute(varName) || getSpecies().hasVar(varName)) { return true; }
if (this.getModel().getSpecies().hasVar(varName)) { return true; }
if (getSpecies().hasParameter(varName)) { return true; }
if (ExperimentAgent.this.hasAttribute(varName) || getSpecies().hasVar(varName)) return true;
if (this.getModel().getSpecies().hasVar(varName)) return true;
if (getSpecies().hasParameter(varName)) return true;
return extraParametersMap.containsKey(varName);
}

Expand Down Expand Up @@ -773,13 +753,13 @@ public void setAgentVarValue(final IAgent a, final String name, final Object val

@Override
public Object getAgentVarValue(final IAgent a, final String varName) {
if (a == ExperimentAgent.this) { return getGlobalVarValue(varName); }
if (a == ExperimentAgent.this) return getGlobalVarValue(varName);
return super.getAgentVarValue(a, varName);
}

@Override
public IGui getGui() {
if (getSpecies().isHeadless()) { return GAMA.getHeadlessGui(); }
if (getSpecies().isHeadless()) return GAMA.getHeadlessGui();
return GAMA.getRegularGui();
}

Expand All @@ -790,10 +770,9 @@ public IGui getGui() {
*/
public Iterable<IOutputManager> getAllSimulationOutputs() {
final SimulationPopulation pop = getSimulationPopulation();
if (pop != null) {
if (pop != null)
return Iterables.filter(Iterables.concat(Iterables.transform(pop, each -> each.getOutputManager()),
Collections.singletonList(getOutputManager())), each -> each != null);
}
return Collections.EMPTY_LIST;
}

Expand Down

0 comments on commit b29f9fe

Please sign in to comment.