Skip to content

Commit

Permalink
Makes sure the scope is correctly passed to GamaRuntimeException.error
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisDrogoul committed Mar 9, 2024
1 parent 6121223 commit ffd0ce9
Show file tree
Hide file tree
Showing 18 changed files with 189 additions and 219 deletions.
41 changes: 22 additions & 19 deletions gama.core/src/gama/core/kernel/batch/exploration/Exploration.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/*******************************************************************************************************
*
* Exploration.java, in gama.core, is part of the source code of the GAMA modeling and simulation platform
* .
* Exploration.java, in gama.core, is part of the source code of the GAMA modeling and simulation platform (v.1.9.3).
*
* (c) 2007-2024 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
*
Expand All @@ -21,15 +20,15 @@
import java.util.List;
import java.util.Map;

import gama.annotations.precompiler.IConcept;
import gama.annotations.precompiler.ISymbolKind;
import gama.annotations.precompiler.GamlAnnotations.doc;
import gama.annotations.precompiler.GamlAnnotations.example;
import gama.annotations.precompiler.GamlAnnotations.facet;
import gama.annotations.precompiler.GamlAnnotations.facets;
import gama.annotations.precompiler.GamlAnnotations.inside;
import gama.annotations.precompiler.GamlAnnotations.symbol;
import gama.annotations.precompiler.GamlAnnotations.usage;
import gama.annotations.precompiler.IConcept;
import gama.annotations.precompiler.ISymbolKind;
import gama.core.common.interfaces.IKeyword;
import gama.core.kernel.batch.exploration.sampling.LatinhypercubeSampling;
import gama.core.kernel.batch.exploration.sampling.MorrisSampling;
Expand All @@ -38,9 +37,9 @@
import gama.core.kernel.batch.exploration.sampling.SaltelliSampling;
import gama.core.kernel.experiment.BatchAgent;
import gama.core.kernel.experiment.IParameter;
import gama.core.kernel.experiment.IParameter.Batch;
import gama.core.kernel.experiment.ParameterAdapter;
import gama.core.kernel.experiment.ParametersSet;
import gama.core.kernel.experiment.IParameter.Batch;
import gama.core.metamodel.shape.GamaPoint;
import gama.core.runtime.GAMA;
import gama.core.runtime.IScope;
Expand Down Expand Up @@ -242,8 +241,9 @@ public void explore(final IScope scope) throws GamaRuntimeException {
new ArrayList<>());
default -> buildParameterSets(scope, new ArrayList<>(), 0);
};
if (sets.isEmpty()) { sets.add(new ParametersSet()); }
else if(sample_size == 132) {sample_size = sets.size();}
if (sets.isEmpty()) {
sets.add(new ParametersSet());
} else if (sample_size == 132) { sample_size = sets.size(); }

if (GamaExecutorService.shouldRunAllSimulationsInParallel(currentExperiment)) {
currentExperiment.launchSimulationsWithSolution(sets);
Expand Down Expand Up @@ -274,24 +274,28 @@ public List<ParametersSet> buildParameterSets(final IScope scope, final List<Par
if (index == variables.size() - 1) return sets2;
return buildParameterSets(scope, sets2, index + 1);
}

@Override
public void addParametersTo(List<Batch> exp, BatchAgent agent) {
public void addParametersTo(final List<Batch> exp, final BatchAgent agent) {
super.addParametersTo(exp, agent);

exp.add(new ParameterAdapter("Sampled points", BatchAgent.EXPLORATION_EXPERIMENT, IType.STRING) {
@Override public Object value() { return sample_size; }
@Override
public Object value() {
return sample_size;
}
});

exp.add(new ParameterAdapter("Sampling method", BatchAgent.EXPLORATION_EXPERIMENT, IType.STRING) {
@Override public Object value() {
if (hasFacet(IKeyword.FROM)) { return FROM_FILE;}
if (hasFacet(IKeyword.WITH)) { return FROM_LIST;}
return hasFacet(Exploration.METHODS) ?
Cast.asString(agent.getScope(), getFacet(METHODS).value(agent.getScope())) : "exhaustive";
@Override
public Object value() {
if (hasFacet(IKeyword.FROM)) return FROM_FILE;
if (hasFacet(IKeyword.WITH)) return FROM_LIST;
return hasFacet(Exploration.METHODS)
? Cast.asString(agent.getScope(), getFacet(METHODS).value(agent.getScope())) : "exhaustive";
}
});

}

/**
Expand All @@ -307,9 +311,8 @@ public void addParametersTo(List<Batch> exp, BatchAgent agent) {
private List<ParametersSet> buildParameterFromMap(final IScope scope, final List<ParametersSet> sets,
final int index) {
IExpression psexp = getFacet(IKeyword.WITH);
if (psexp.getDenotedType() != Types.LIST) {
GamaRuntimeException.error("You cannot use "+IKeyword.WITH+" facet without input a list of maps as parameters inputs", scope);
}
if (psexp.getDenotedType() != Types.LIST) throw GamaRuntimeException.error(
"You cannot use " + IKeyword.WITH + " facet without input a list of maps as parameters inputs", scope);
List<Map<String, Object>> parameterSets = Cast.asList(scope, psexp.value(scope));

for (Map<String, Object> parameterSet : parameterSets) {
Expand Down
20 changes: 10 additions & 10 deletions gama.core/src/gama/core/kernel/batch/exploration/sobol/Sobol.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************************************************************
*
* Sobol.java, in gama.core, is part of the source code of the GAMA modeling and simulation platform .
* Sobol.java, in gama.core, is part of the source code of the GAMA modeling and simulation platform (v.1.9.3).
*
* (c) 2007-2024 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
*
Expand Down Expand Up @@ -314,12 +314,12 @@ public void saveResult(final File file) {
/**
* Build the string that contains the report of the Sobol analysis
*/
public String buildReportString(String extension) {
public String buildReportString(final String extension) {

StringBuilder sb = new StringBuilder();
char sep = ',';
if (extension.equalsIgnoreCase("csv")) {

if ("csv".equalsIgnoreCase(extension)) {
// Build header
sb.append("output").append(sep);
sb.append("parameter").append(sep);
Expand All @@ -338,7 +338,7 @@ public String buildReportString(String extension) {
}
sb.append(Strings.LN);
}
}
}
} else {
sb.append("SOBOL ANALYSIS:\n");
for (String output_name : sobol_analysis.keySet()) {
Expand All @@ -359,7 +359,7 @@ public String buildReportString(String extension) {
}
}
}

return sb.toString();
}

Expand Down Expand Up @@ -478,19 +478,19 @@ private void parseSaltelli(final File file) {
private void roll(final String param, final Double saltelli, final List<Object> info) {
Object val = null;
// Double
if (info.stream().allMatch(p -> p instanceof Double)) {
if (info.stream().allMatch(Double.class::isInstance)) {
Double min = (Double) info.get(0);
Double max = (Double) info.get(1);
val = min + saltelli * (max - min);
}
// Intege
else if (info.stream().allMatch(p -> p instanceof Integer)) {
else if (info.stream().allMatch(Integer.class::isInstance)) {
int min = (int) info.get(0);
int max = (int) info.get(1);
val = (int) Math.floor(min + saltelli * (max - min));
}
// Boolean
else if (info.stream().allMatch(p -> p instanceof Boolean)) {
else if (info.stream().allMatch(Boolean.class::isInstance)) {
val = saltelli > 0.5;
}
// Discrete variable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*******************************************************************************************************
*
* SobolExploration.java, in gama.core, is part of the source code of the GAMA modeling and simulation platform
* .
* (v.1.9.3).
*
* (c) 2007-2024 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
*
Expand All @@ -17,22 +17,22 @@
import java.util.List;
import java.util.Map;

import gama.annotations.precompiler.IConcept;
import gama.annotations.precompiler.ISymbolKind;
import gama.annotations.precompiler.GamlAnnotations.doc;
import gama.annotations.precompiler.GamlAnnotations.example;
import gama.annotations.precompiler.GamlAnnotations.facet;
import gama.annotations.precompiler.GamlAnnotations.facets;
import gama.annotations.precompiler.GamlAnnotations.inside;
import gama.annotations.precompiler.GamlAnnotations.symbol;
import gama.annotations.precompiler.GamlAnnotations.usage;
import gama.annotations.precompiler.IConcept;
import gama.annotations.precompiler.ISymbolKind;
import gama.core.common.interfaces.IKeyword;
import gama.core.common.util.FileUtils;
import gama.core.kernel.batch.exploration.AExplorationAlgorithm;
import gama.core.kernel.experiment.BatchAgent;
import gama.core.kernel.experiment.IParameter.Batch;
import gama.core.kernel.experiment.ParameterAdapter;
import gama.core.kernel.experiment.ParametersSet;
import gama.core.kernel.experiment.IParameter.Batch;
import gama.core.runtime.IScope;
import gama.core.runtime.concurrent.GamaExecutorService;
import gama.core.runtime.exceptions.GamaRuntimeException;
Expand Down Expand Up @@ -142,9 +142,8 @@ public void setChildren(final Iterable<? extends ISymbol> children) {}
public void explore(final IScope scope) {
List<ParametersSet> solutions =
this.solutions == null ? buildParameterSets(scope, new ArrayList<>(), 0) : this.solutions;
if (solutions.size() != _sample) {
GamaRuntimeException.error("Saltelli sample should be " + _sample + " but is " + solutions.size(), scope);
}
if (solutions.size() != _sample) throw GamaRuntimeException
.error("Saltelli sample should be " + _sample + " but is " + solutions.size(), scope);

/* Disable repetitions / repeat argument */
currentExperiment.setSeeds(new Double[1]);
Expand Down Expand Up @@ -275,15 +274,18 @@ public List<ParametersSet> buildParameterSets(final IScope scope, final List<Par

return sets;
}

@Override
public void addParametersTo(List<Batch> exp, BatchAgent agent) {
public void addParametersTo(final List<Batch> exp, final BatchAgent agent) {
super.addParametersTo(exp, agent);

exp.add(new ParameterAdapter("Saltelli sample", IKeyword.SOBOL, IType.STRING) {
@Override public Object value() { return _sample; }
@Override
public Object value() {
return _sample;
}
});

}

/**
Expand Down
37 changes: 13 additions & 24 deletions gama.core/src/gama/core/outputs/ValuedDisplayOutputFactory.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*******************************************************************************************************
*
* ValuedDisplayOutputFactory.java, in gama.core, is part of the source code of the GAMA modeling and simulation
* platform .
* platform (v.1.9.3).
*
* (c) 2007-2024 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
*
Expand Down Expand Up @@ -49,10 +49,8 @@ public static void browse(final Collection<? extends IAgent> agents) {
}
if (root == null) return;
final IMacroAgent realRoot = findRootOf(root, agents);
if (realRoot == null) {
GamaRuntimeException.error("Impossible to find a common host agent for " + agents, root.getScope());
return;
}
if (realRoot == null) throw GamaRuntimeException
.error("Impossible to find a common host agent for " + agents, root.getScope());
InspectDisplayOutput.browse(realRoot, agents, null).launch(realRoot.getScope());
}
}
Expand Down Expand Up @@ -95,13 +93,10 @@ public static void browse(final IMacroAgent root, final ISpecies species) {
return;
}
if (!root.getSpecies().getMicroSpecies().contains(species)) {
if (root instanceof ExperimentAgent) {
final IMacroAgent realRoot = ((ExperimentAgent) root).getSimulation();
browse(realRoot, species);
} else {
GamaRuntimeException.error("Agent " + root + " has no access to populations of " + species.getName(),
root.getScope());
}
if (!(root instanceof ExperimentAgent)) throw GamaRuntimeException
.error("Agent " + root + " has no access to populations of " + species.getName(), root.getScope());
final IMacroAgent realRoot = ((ExperimentAgent) root).getSimulation();
browse(realRoot, species);
return;
}
InspectDisplayOutput.browse(root, species, null).launch(root.getScope());
Expand All @@ -118,20 +113,14 @@ public static void browse(final IMacroAgent root, final ISpecies species) {
public static void browse(final IMacroAgent root, final IExpression expr, final IExpression attributes) {
final SpeciesDescription species = expr.getGamlType().isContainer()
? expr.getGamlType().getContentType().getSpecies() : expr.getGamlType().getSpecies();
if (species == null) {
GamaRuntimeException.error("Expression '" + expr.serializeToGaml(true) + "' does not reference agents",
root.getScope());
return;
}
if (species == null) throw GamaRuntimeException
.error("Expression '" + expr.serializeToGaml(true) + "' does not reference agents", root.getScope());
final ISpecies rootSpecies = root.getSpecies();
if (rootSpecies.getMicroSpecies(species.getName()) == null) {
if (root instanceof ExperimentAgent) {
final IMacroAgent realRoot = ((ExperimentAgent) root).getSimulation();
browse(realRoot, expr, attributes);
} else {
GamaRuntimeException.error("Agent " + root + " has no access to populations of " + species.getName(),
root.getScope());
}
if (!(root instanceof ExperimentAgent)) throw GamaRuntimeException
.error("Agent " + root + " has no access to populations of " + species.getName(), root.getScope());
final IMacroAgent realRoot = ((ExperimentAgent) root).getSimulation();
browse(realRoot, expr, attributes);
return;
}
InspectDisplayOutput.browse(root, expr, attributes).launch(root.getScope());
Expand Down
6 changes: 2 additions & 4 deletions gama.core/src/gama/core/outputs/layers/ImageLayer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************************************************************
*
* ImageLayer.java, in gama.core, is part of the source code of the GAMA modeling and simulation platform .
* ImageLayer.java, in gama.core, is part of the source code of the GAMA modeling and simulation platform (v.1.9.3).
*
* (c) 2007-2024 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
*
Expand All @@ -9,8 +9,6 @@
********************************************************************************************************/
package gama.core.outputs.layers;

import static gama.core.runtime.exceptions.GamaRuntimeException.error;

import java.awt.image.BufferedImage;

import gama.core.common.geometry.Envelope3D;
Expand Down Expand Up @@ -161,7 +159,7 @@ private IImageProvider createFileFromString(final IScope scope, final String ima
private IImageProvider verifyFile(final IScope scope, final Object input) {
if (input == cachedImageProvider) return cachedImageProvider;
if (!(input instanceof IImageProvider result))
throw error("Not a provider of images: " + provider.serializeToGaml(false), scope);
throw GamaRuntimeException.error("Not a provider of images: " + provider.serializeToGaml(false), scope);
try {
result.getImage(scope, !getData().getRefresh());
} catch (final GamaRuntimeFileException ex) {
Expand Down
7 changes: 3 additions & 4 deletions gama.core/src/gama/core/util/file/GamaDXFFile.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/*******************************************************************************************************
*
* GamaDXFFile.java, in gama.core, is part of the source code of the GAMA modeling and simulation platform
* .
* GamaDXFFile.java, in gama.core, is part of the source code of the GAMA modeling and simulation platform (v.1.9.3).
*
* (c) 2007-2024 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
*
Expand All @@ -18,10 +17,10 @@

import org.locationtech.jts.geom.Envelope;

import gama.annotations.precompiler.IConcept;
import gama.annotations.precompiler.GamlAnnotations.doc;
import gama.annotations.precompiler.GamlAnnotations.example;
import gama.annotations.precompiler.GamlAnnotations.file;
import gama.annotations.precompiler.IConcept;
import gama.core.common.geometry.Envelope3D;
import gama.core.metamodel.shape.GamaPoint;
import gama.core.metamodel.shape.IShape;
Expand Down Expand Up @@ -119,7 +118,7 @@ public GamaDXFFile(final IScope scope, final String pathName) throws GamaRuntime
isExecutable = false) })
public GamaDXFFile(final IScope scope, final String pathName, final Double unit) throws GamaRuntimeException {
super(scope, pathName);
if (unit <= 0) { GamaRuntimeException.error("the unity given has to be higher than 0", scope); }
if (unit <= 0) throw GamaRuntimeException.error("the unity given has to be higher than 0", scope);
this.unit = unit;
}

Expand Down
Loading

0 comments on commit ffd0ce9

Please sign in to comment.