Skip to content

Commit

Permalink
Allows models to inherit from parent models (experimental)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisDrogoul committed May 20, 2021
1 parent ae03f63 commit b6444ca
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 267 deletions.
Expand Up @@ -30,6 +30,7 @@
import msi.gama.runtime.exceptions.GamaRuntimeException;
import msi.gama.util.GamaListFactory;
import msi.gama.util.IList;
import msi.gaml.compilation.IAgentConstructor;
import msi.gaml.species.ISpecies;
import msi.gaml.variables.IVariable;

Expand All @@ -45,7 +46,7 @@ public SimulationPopulation(final ExperimentAgent agent, final ISpecies species)
}

public int getMaxNumberOfConcurrentSimulations() {
if (getHost().getSpecies().isHeadless()) { return 1; }
if (getHost().getSpecies().isHeadless()) return 1;
return GamaExecutorService.getParallelism(getHost().getScope(), getSpecies().getConcurrency(),
Caller.SIMULATION);
}
Expand Down Expand Up @@ -87,16 +88,17 @@ public IList<SimulationAgent> createAgents(final IScope scope, final int number,

for (int i = 0; i < number; i++) {
scope.getGui().getStatus(scope).waitStatus("Initializing simulation");
currentSimulation = new SimulationAgent(this, currentAgentIndex++);
// Model do not only rely on SimulationAgent
final IAgentConstructor<SimulationAgent> constr = species.getDescription().getAgentConstructor();

currentSimulation = constr.createOneAgent(this, currentAgentIndex++);
currentSimulation.setScheduled(toBeScheduled);
currentSimulation.setName("Simulation " + currentSimulation.getIndex());
add(currentSimulation);
currentSimulation.setOutputs(((ExperimentPlan) host.getSpecies()).getOriginalSimulationOutputs());
if (scope.interrupted()) { return null; }
if (scope.interrupted()) return null;
initSimulation(scope, currentSimulation, initialValues, i, isRestored, toBeScheduled);
if (toBeScheduled) {
runner.add(currentSimulation);
}
if (toBeScheduled) { runner.add(currentSimulation); }
result.add(currentSimulation);
}
// Linked to Issue #2430. Should not return this, but the newly created simulations
Expand All @@ -108,9 +110,7 @@ private void initSimulation(final IScope scope, final SimulationAgent sim,
final List<? extends Map<String, Object>> initialValues, final int index, final boolean isRestored,
final boolean toBeScheduled) {
scope.getGui().getStatus(scope).waitStatus("Instantiating agents");
if (toBeScheduled) {
sim.prepareGuiForSimulation(scope);
}
if (toBeScheduled) { sim.prepareGuiForSimulation(scope); }

final Map<String, Object> firstInitValues = initialValues.isEmpty() ? null : initialValues.get(index);
final Object firstValue =
Expand Down
15 changes: 8 additions & 7 deletions msi.gama.core/src/msi/gaml/compilation/kernel/GamaMetaModel.java
Expand Up @@ -27,6 +27,7 @@
import msi.gama.common.interfaces.IKeyword;
import msi.gama.kernel.experiment.ExperimentAgent;
import msi.gama.kernel.model.GamlModelSpecies;
import msi.gama.kernel.simulation.SimulationAgent;
import msi.gama.metamodel.population.IPopulation;
import msi.gaml.compilation.IAgentConstructor;
import msi.gaml.descriptions.ModelDescription;
Expand Down Expand Up @@ -112,7 +113,8 @@ public void build() {

// We then create all other built-in species and attach them to "model"
for (final SpeciesProto proto : tempSpecies.values()) {
model.addChild(buildSpecies(proto, model, agent, false, false));
model.addChild(
buildSpecies(proto, model, agent, SimulationAgent.class.isAssignableFrom(proto.clazz), false));
}
tempSpecies.clear();
model.buildTypes();
Expand All @@ -121,7 +123,7 @@ public void build() {
}

public SpeciesDescription buildSpecies(final SpeciesProto proto, final SpeciesDescription macro,
final SpeciesDescription parent, final boolean isGlobal, final boolean isExperiment) {
final SpeciesDescription parent, final boolean isModel, final boolean isExperiment) {
final Class clazz = proto.clazz;
final String name = proto.name;
final IAgentConstructor helper = proto.helper;
Expand All @@ -134,7 +136,7 @@ public SpeciesDescription buildSpecies(final SpeciesProto proto, final SpeciesDe
desc = DescriptionFactory.createPlatformSpeciesDescription(name, clazz, macro, parent, helper, allSkills,
plugin);
} else {
if (!isGlobal) {
if (!isModel) {
if (isExperiment) {
desc = DescriptionFactory.createBuiltInExperimentDescription(name, clazz, macro, parent, helper,
allSkills, plugin);
Expand All @@ -143,7 +145,8 @@ public SpeciesDescription buildSpecies(final SpeciesProto proto, final SpeciesDe
allSkills, plugin);
}
} else {
desc = DescriptionFactory.createRootModelDescription(name, clazz, macro, parent);
// if it is a ModelDescription, then the macro represents the parent (except for root)
desc = DescriptionFactory.createRootModelDescription(name, clazz, macro, parent, helper);
}
}
desc.copyJavaAdditions();
Expand All @@ -165,9 +168,7 @@ public GamlModelSpecies getAbstractModelSpecies() {

public PlatformSpeciesDescription getPlatformSpeciesDescription() {
final IType platform = Types.get(IKeyword.PLATFORM);
if (platform != null && platform != Types.NO_TYPE) {
return (PlatformSpeciesDescription) platform.getSpecies();
}
if (platform != null && platform != Types.NO_TYPE) return (PlatformSpeciesDescription) platform.getSpecies();
return null;
}

Expand Down

0 comments on commit b6444ca

Please sign in to comment.