diff --git a/msi.gama.core/src/msi/gama/kernel/experiment/ExperimentPlan.java b/msi.gama.core/src/msi/gama/kernel/experiment/ExperimentPlan.java index 049864eaa5..8a8975d42c 100644 --- a/msi.gama.core/src/msi/gama/kernel/experiment/ExperimentPlan.java +++ b/msi.gama.core/src/msi/gama/kernel/experiment/ExperimentPlan.java @@ -1,9 +1,9 @@ /******************************************************************************************************* * - * msi.gama.kernel.experiment.ExperimentPlan.java, in plugin msi.gama.core, is part of the source code of the GAMA - * modeling and simulation platform (v. 1.8.1) + * ExperimentPlan.java, in msi.gama.core, is part of the source code of the GAMA modeling and simulation platform + * (v.1.8.2). * - * (c) 2007-2020 UMI 209 UMMISCO IRD/SU & Partners + * (c) 2007-2021 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU) * * Visit https://github.com/gama-platform/gama for license information and contacts. * @@ -18,7 +18,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Set; import com.google.common.collect.Iterables; @@ -31,7 +30,6 @@ import msi.gama.kernel.experiment.ExperimentPlan.BatchValidator; import msi.gama.kernel.model.IModel; import msi.gama.kernel.simulation.SimulationAgent; -import msi.gama.metamodel.agent.IAgent; import msi.gama.metamodel.population.GamaPopulation; import msi.gama.metamodel.shape.GamaPoint; import msi.gama.metamodel.topology.continuous.AmorphousTopology; @@ -63,6 +61,7 @@ import msi.gaml.operators.Cast; import msi.gaml.species.GamlSpecies; import msi.gaml.species.ISpecies; +import msi.gaml.statements.RemoteSequence; import msi.gaml.types.IType; import msi.gaml.variables.IVariable; @@ -77,6 +76,10 @@ * @todo Description * */ + +/** + * The Class ExperimentPlan. + */ @symbol ( name = { IKeyword.EXPERIMENT }, kind = ISymbolKind.EXPERIMENT, @@ -180,6 +183,9 @@ @SuppressWarnings ({ "unchecked", "rawtypes" }) public class ExperimentPlan extends GamlSpecies implements IExperimentPlan { + /** + * The Class BatchValidator. + */ public static class BatchValidator implements IDescriptionValidator { /** @@ -207,62 +213,106 @@ public void validate(final IDescription desc) { } } + /** The controller. */ protected IExperimentController controller; // An original copy of the simualtion outputs (which will be eventually + /** The original simulation outputs. */ // duplicated in all the simulations) protected SimulationOutputManager originalSimulationOutputs; + + /** The experiment outputs. */ protected ExperimentOutputManager experimentOutputs; + + /** The parameters. */ // private ItemList parametersEditors; protected final Map parameters = GamaMapFactory.create(); + + /** The explorable parameters. */ protected final Map explorableParameters = GamaMapFactory.create(); + + /** The agent. */ protected ExperimentAgent agent; + + /** The my scope. */ protected final Scope myScope = new Scope("in ExperimentPlan"); + + /** The model. */ protected IModel model; + + /** The exploration. */ protected IExploration exploration; + + /** The log. */ private FileOutput log; + + /** The is headless. */ private boolean isHeadless; + + /** The keep seed. */ private final boolean keepSeed; + + /** The keep simulations. */ private final boolean keepSimulations; + + /** The experiment type. */ private final String experimentType; + + /** The autorun. */ private final boolean autorun; + + /** The benchmarkable. */ private final boolean benchmarkable; + /** + * The Class ExperimentPopulation. + */ public class ExperimentPopulation extends GamaPopulation { + /** + * Instantiates a new experiment population. + * + * @param expr + * the expr + */ public ExperimentPopulation(final ISpecies expr) { super(null, expr); } + @SuppressWarnings ("null") @Override public IList createAgents(final IScope scope, final int number, final List> initialValues, final boolean isRestored, - final boolean toBeScheduled) throws GamaRuntimeException { + final boolean toBeScheduled, final RemoteSequence sequence) throws GamaRuntimeException { + final boolean empty = initialValues == null || initialValues.isEmpty(); + Map inits = Collections.EMPTY_MAP; for (int i = 0; i < number; i++) { agent = GamaMetaModel.INSTANCE.createExperimentAgent(getExperimentType(), this, currentAgentIndex++); - // agent.setIndex(currentAgentIndex++); add(agent); scope.push(agent); - createVariables(scope, agent, initialValues.isEmpty() ? Collections.EMPTY_MAP : initialValues.get(i)); + if (!empty) { inits = initialValues.get(i); } + // List of the names of initialized variables. Those that do not belong to the experiment will be passed + // to the simulation (see #3198) + final List names = new ArrayList(inits.keySet()); + for (final IVariable var : orderedVars) { + String s = var.getName(); + final Object initGet = + empty /* || !allowVarInitToBeOverridenByExternalInit(var) */ ? null : inits.get(s); + var.initializeWith(scope, agent, initGet); + names.remove(s); + } + // Trick to initialize the simulation variables (and not only the experiment's variables) + // See discussion in #3198 + for (final String s : names) { + final Object initGet = + empty /* || !allowVarInitToBeOverridenByExternalInit(var) */ ? null : inits.get(s); + agent.getScope().setAgentVarValue(agent, s, initGet); + } + if (sequence != null && !sequence.isEmpty()) { scope.execute(sequence, agent, null); } scope.pop(agent); } return this; } - public void createVariables(final IScope scope, final IAgent a, final Map inits) - throws GamaRuntimeException { - final Set names = inits.keySet(); - for (final IVariable var : orderedVars) { - - String s = var.getName(); - var.initializeWith(scope, a, inits.get(s)); - names.remove(s); - } - for (final String s : names) { - a.getScope().setAgentVarValue(a, s, inits.get(s)); - } - - } - @Override protected boolean stepAgents(final IScope scope) { return scope.step(agent).passed(); @@ -281,20 +331,20 @@ public void computeTopology(final IScope scope) throws GamaRuntimeException { } @Override - public boolean isHeadless() { - return GAMA.isInHeadLessMode() || isHeadless; - } + public boolean isHeadless() { return GAMA.isInHeadLessMode() || isHeadless; } @Override - public void setHeadless(final boolean headless) { - isHeadless = headless; - } + public void setHeadless(final boolean headless) { isHeadless = headless; } @Override - public ExperimentAgent getAgent() { - return agent; - } + public ExperimentAgent getAgent() { return agent; } + /** + * Instantiates a new experiment plan. + * + * @param description + * the description + */ public ExperimentPlan(final IDescription description) { super(description); setName(description.getName()); @@ -326,9 +376,7 @@ public ExperimentPlan(final IDescription description) { } @Override - public boolean isAutorun() { - return autorun; - } + public boolean isAutorun() { return autorun; } @Override public boolean keepsSeed() { @@ -367,6 +415,12 @@ public void dispose() { // DEBUG.LOG("ExperimentPlan.dipose END"); } + /** + * Creates the agent. + * + * @param seed + * the seed + */ public void createAgent(final Double seed) { final ExperimentPopulation pop = new ExperimentPopulation(this); final IScope scope = getExperimentScope(); @@ -387,9 +441,7 @@ public void createAgent(final Double seed) { * Collections.EMPTY_LIST, false, true).get(0); addDefaultParameters(); } */ @Override - public IModel getModel() { - return model; - } + public IModel getModel() { return model; } @Override public void setModel(final IModel model) { @@ -417,16 +469,15 @@ public void setModel(final IModel model) { } } + /** + * Adds the default parameters. + */ protected void addDefaultParameters() { - for (final IParameter.Batch p : agent.getDefaultParameters()) { - addParameter(p); - } + for (final IParameter.Batch p : agent.getDefaultParameters()) { addParameter(p); } } @Override - public final IOutputManager getExperimentOutputs() { - return experimentOutputs; - } + public final IOutputManager getExperimentOutputs() { return experimentOutputs; } @Override public void setChildren(final Iterable children) { @@ -479,6 +530,14 @@ public void setChildren(final Iterable children) { } + /** + * Creates the output. + * + * @param output + * the output + * @throws GamaRuntimeException + * the gama runtime exception + */ private void createOutput(final BatchOutput output) throws GamaRuntimeException { // TODO revoir tout ceci. Devrait plut�t �tre une commande if (output == null) return; @@ -488,6 +547,12 @@ private void createOutput(final BatchOutput output) throws GamaRuntimeException log = new FileOutput(output.getLiteral(IKeyword.TO), dataString, new ArrayList(parameters.keySet()), this); } + /** + * Open. + * + * @param seed + * the seed + */ public synchronized void open(final Double seed) { createAgent(seed); @@ -499,10 +564,9 @@ public synchronized void open(final Double seed) { } else if (isBatch()) { agent.getScope().getGui().getStatus(agent.getScope()) .informStatus(isTest() ? "Tests ready. Click run to begin." : " Batch ready. Click run to begin."); - agent.getScope().getGui().updateExperimentState(agent.getScope()); - } + agent.getScope().getGui().updateExperimentState(agent.getScope()); } - + } @Override public synchronized void open() { @@ -546,40 +610,63 @@ public boolean hasParametersOrUserCommands() { // @Override @Override - public boolean isBatch() { - return exploration != null; - } + public boolean isBatch() { return exploration != null; } @Override - public boolean isTest() { - return TEST.equals(getExperimentType()); - } + public boolean isTest() { return TEST.equals(getExperimentType()); } @Override - public boolean isMemorize() { - return IKeyword.MEMORIZE.equals(getExperimentType()); - } + public boolean isMemorize() { return IKeyword.MEMORIZE.equals(getExperimentType()); } @Override - public boolean isGui() { - return true; - } + public boolean isGui() { return true; } @Override - public IScope getExperimentScope() { - return myScope; - } + public IScope getExperimentScope() { return myScope; } + /** + * Sets the parameter value. + * + * @param scope + * the scope + * @param name + * the name + * @param val + * the val + * @throws GamaRuntimeException + * the gama runtime exception + */ // @Override public void setParameterValue(final IScope scope, final String name, final Object val) throws GamaRuntimeException { checkGetParameter(name).setValue(scope, val); } + /** + * Sets the parameter value by title. + * + * @param scope + * the scope + * @param name + * the name + * @param val + * the val + * @throws GamaRuntimeException + * the gama runtime exception + */ public void setParameterValueByTitle(final IScope scope, final String name, final Object val) throws GamaRuntimeException { checkGetParameterByTitle(name).setValue(scope, val); } + /** + * Gets the parameter value. + * + * @param parameterName + * the parameter name + * @return the parameter value + * @throws GamaRuntimeException + * the gama runtime exception + */ // @Override public Object getParameterValue(final String parameterName) throws GamaRuntimeException { return checkGetParameter(parameterName).value(myScope); @@ -591,6 +678,13 @@ public boolean hasParameter(final String parameterName) { return getParameter(parameterName) != null; } + /** + * Gets the parameter by title. + * + * @param title + * the title + * @return the parameter by title + */ public IParameter.Batch getParameterByTitle(final String title) { for (final IParameter p : parameters.values()) { if (p.getTitle().equals(title) && p instanceof IParameter.Batch) return (IParameter.Batch) p; @@ -598,12 +692,25 @@ public IParameter.Batch getParameterByTitle(final String title) { return null; } + /** + * Gets the parameter. + * + * @param parameterName + * the parameter name + * @return the parameter + */ public IParameter.Batch getParameter(final String parameterName) { final IParameter p = parameters.get(parameterName); if (p instanceof IParameter.Batch) return (IParameter.Batch) p; return null; } + /** + * Adds the parameter. + * + * @param p + * the p + */ public void addParameter(final IParameter p) { final String parameterName = p.getName(); final IParameter already = parameters.get(parameterName); @@ -611,6 +718,15 @@ public void addParameter(final IParameter p) { parameters.put(parameterName, p); } + /** + * Check get parameter by title. + * + * @param parameterName + * the parameter name + * @return the i parameter. batch + * @throws GamaRuntimeException + * the gama runtime exception + */ protected IParameter.Batch checkGetParameterByTitle(final String parameterName) throws GamaRuntimeException { final IParameter.Batch v = getParameterByTitle(parameterName); if (v == null) throw GamaRuntimeException @@ -618,6 +734,15 @@ protected IParameter.Batch checkGetParameterByTitle(final String parameterName) return v; } + /** + * Check get parameter. + * + * @param parameterName + * the parameter name + * @return the i parameter. batch + * @throws GamaRuntimeException + * the gama runtime exception + */ protected IParameter.Batch checkGetParameter(final String parameterName) throws GamaRuntimeException { final IParameter.Batch v = getParameter(parameterName); if (v == null) throw GamaRuntimeException @@ -626,9 +751,7 @@ protected IParameter.Batch checkGetParameter(final String parameterName) throws } @Override - public Map getParameters() { - return parameters; - } + public Map getParameters() { return parameters; } @Override public SimulationAgent getCurrentSimulation() { @@ -646,6 +769,12 @@ public SimulationAgent getCurrentSimulation() { */ private class Scope extends ExecutionScope { + /** + * Instantiates a new scope. + * + * @param additionalName + * the additional name + */ public Scope(final String additionalName) { super(null, additionalName); } @@ -672,15 +801,17 @@ public Object getGlobalVarValue(final String varName) throws GamaRuntimeExceptio } @Override - public IExploration getExplorationAlgorithm() { - return exploration; - } + public IExploration getExplorationAlgorithm() { return exploration; } @Override - public FileOutput getLog() { - return log; - } + public FileOutput getLog() { return log; } + /** + * Adds the explorable parameter. + * + * @param p + * the p + */ public void addExplorableParameter(final IParameter.Batch p) { p.setCategory(EXPLORABLE_CATEGORY_NAME); p.setUnitLabel(null); @@ -688,9 +819,7 @@ public void addExplorableParameter(final IParameter.Batch p) { } @Override - public Map getExplorableParameters() { - return explorableParameters; - } + public Map getExplorableParameters() { return explorableParameters; } /** * Method getController() @@ -710,44 +839,32 @@ public IExperimentController getController() { */ @Override public void refreshAllOutputs() { - for (final IOutputManager manager : getActiveOutputManagers()) { - manager.forceUpdateOutputs(); - } + for (final IOutputManager manager : getActiveOutputManagers()) { manager.forceUpdateOutputs(); } } @Override public void pauseAllOutputs() { - for (final IOutputManager manager : getActiveOutputManagers()) { - manager.pause(); - } + for (final IOutputManager manager : getActiveOutputManagers()) { manager.pause(); } } @Override public void resumeAllOutputs() { - for (final IOutputManager manager : getActiveOutputManagers()) { - manager.resume(); - } + for (final IOutputManager manager : getActiveOutputManagers()) { manager.resume(); } } @Override public void synchronizeAllOutputs() { - for (final IOutputManager manager : getActiveOutputManagers()) { - manager.synchronize(); - } + for (final IOutputManager manager : getActiveOutputManagers()) { manager.synchronize(); } } @Override public void unSynchronizeAllOutputs() { - for (final IOutputManager manager : getActiveOutputManagers()) { - manager.unSynchronize(); - } + for (final IOutputManager manager : getActiveOutputManagers()) { manager.unSynchronize(); } } @Override public void closeAllOutputs() { - for (final IOutputManager manager : getActiveOutputManagers()) { - manager.close(); - } + for (final IOutputManager manager : getActiveOutputManagers()) { manager.close(); } } /** @@ -755,9 +872,7 @@ public void closeAllOutputs() { */ @Override public void recomputeAndRefreshAllOutputs() { - for (final IOutputManager manager : getActiveOutputManagers()) { - manager.step(getExperimentScope()); - } + for (final IOutputManager manager : getActiveOutputManagers()) { manager.step(getExperimentScope()); } } /** @@ -766,14 +881,10 @@ public void recomputeAndRefreshAllOutputs() { * @see msi.gama.kernel.experiment.IExperimentPlan#getOriginalSimulationOutputs() */ @Override - public IOutputManager getOriginalSimulationOutputs() { - return originalSimulationOutputs; - } + public IOutputManager getOriginalSimulationOutputs() { return originalSimulationOutputs; } @Override - public String getExperimentType() { - return experimentType; - } + public String getExperimentType() { return experimentType; } /** * Returns the output managers that are currently active. If no agent is defined, then an empty iterable is returned @@ -789,9 +900,7 @@ public Iterable getActiveOutputManagers() { } @Override - public ExperimentDescription getDescription() { - return (ExperimentDescription) super.getDescription(); - } + public ExperimentDescription getDescription() { return (ExperimentDescription) super.getDescription(); } @Override public boolean shouldBeBenchmarked() { diff --git a/msi.gama.core/src/msi/gama/metamodel/population/GamaPopulation.java b/msi.gama.core/src/msi/gama/metamodel/population/GamaPopulation.java index dc14b1e054..cedce50bcb 100644 --- a/msi.gama.core/src/msi/gama/metamodel/population/GamaPopulation.java +++ b/msi.gama.core/src/msi/gama/metamodel/population/GamaPopulation.java @@ -1,12 +1,12 @@ /******************************************************************************************************* * - * msi.gama.metamodel.population.GamaPopulation.java, in plugin msi.gama.core, is part of the source code of the GAMA - * modeling and simulation platform (v. 1.8.1) + * GamaPopulation.java, in msi.gama.core, is part of the source code of the + * GAMA modeling and simulation platform (v.1.8.2). * - * (c) 2007-2020 UMI 209 UMMISCO IRD/SU & Partners + * (c) 2007-2021 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU) * * Visit https://github.com/gama-platform/gama for license information and contacts. - * + * ********************************************************************************************************/ package msi.gama.metamodel.population; @@ -100,6 +100,15 @@ public class GamaPopulation extends GamaList implements IPo DEBUG.OFF(); } + /** + * Creates the population. + * + * @param the element type + * @param scope the scope + * @param host the host + * @param species the species + * @return the gama population + */ public static GamaPopulation createPopulation(final IScope scope, final IMacroAgent host, final ISpecies species) { if (species.isGrid()) { @@ -119,11 +128,23 @@ public static GamaPopulation createPopulation(final IScope * The object describing how the agents of this population are spatially organized */ protected ITopology topology; + + /** The species. */ protected final ISpecies species; + + /** The updatable vars. */ protected final IVariable[] orderedVars, updatableVars; + + /** The current agent index. */ protected int currentAgentIndex; + + /** The hash code. */ private final int hashCode; + + /** The is step overriden. */ private final boolean isInitOverriden, isStepOverriden; + + /** The mirror management. */ private final MirrorPopulationManagement mirrorManagement; /** @@ -131,12 +152,22 @@ public static GamaPopulation createPopulation(final IScope */ private LinkedList listeners = null; + /** The Constant isLiving. */ public final static IPopulation.IsLiving isLiving = new IPopulation.IsLiving(); + /** + * The Class MirrorPopulationManagement. + */ class MirrorPopulationManagement implements IExecutable { + /** The list of target agents. */ final IExpression listOfTargetAgents; + /** + * Instantiates a new mirror population management. + * + * @param exp the exp + */ MirrorPopulationManagement(final IExpression exp) { listOfTargetAgents = exp; } @@ -154,9 +185,7 @@ public Object executeOn(final IScope scope) throws GamaRuntimeException { toKill.add(agent); } } - for (final IAgent agent : toKill) { - agent.dispose(); - } + for (final IAgent agent : toKill) { agent.dispose(); } final List> attributes = new ArrayList<>(); for (final IAgent target : targets) { final Map att = GamaMapFactory.createUnordered(); @@ -168,6 +197,13 @@ public Object executeOn(final IScope scope) throws GamaRuntimeException { } + /** + * Try add. + * + * @param graph the graph + * @param v the v + * @param existing the existing + */ private void tryAdd(final DirectedAcyclicGraph graph, final String v, final String existing) { graph.addVertex(v); try { @@ -177,6 +213,14 @@ private void tryAdd(final DirectedAcyclicGraph graph, final Stri } } + /** + * Order attributes. + * + * @param ecd the ecd + * @param keep the keep + * @param facetsToConsider the facets to consider + * @return the i variable[] + */ public IVariable[] orderAttributes(final TypeDescription ecd, final Predicate keep, final Set facetsToConsider) { // AD Revised in Aug 2019 for Issue #2869: keep constraints between superspecies and subspecies @@ -200,6 +244,12 @@ public IVariable[] orderAttributes(final TypeDescription ecd, final Predicate o) { } @Override - public ITopology getTopology() { - return topology; - } + public ITopology getTopology() { return topology; } @Override - public String getName() { - return species.getName(); - } + public String getName() { return species.getName(); } @Override - public boolean isGrid() { - return species.isGrid(); - } + public boolean isGrid() { return species.isGrid(); } @Override - public ISpecies getSpecies() { - return species; - } + public ISpecies getSpecies() { return species; } @SuppressWarnings ("unchecked") @Override @@ -382,9 +428,7 @@ public IList createAgents(final IScope scope, final IContainer createAgents(final IScope scope, final int number, if (!isRestored) { for (final IAgent a : list) { // if agent is restored (on the capture or release); then don't - // need to run the "init" - // reflex + // need to run the "init" reflex a.schedule(scope); // a.scheduleAndExecute(sequence); } @@ -451,9 +494,7 @@ public IList createAgents(final IScope scope, final int number, // initialized // agents were "notified" if (sequence != null && !sequence.isEmpty()) { - for (final IAgent a : list) { - if (!scope.execute(sequence, a, null).passed()) { break; } - } + for (final IAgent a : list) { if (!scope.execute(sequence, a, null).passed()) { break; } } } } @@ -461,11 +502,28 @@ public IList createAgents(final IScope scope, final int number, return list; } + /** + * Creates the variables for. + * + * @param scope the scope + * @param agents the agents + * @param initialValues the initial values + * @throws GamaRuntimeException the gama runtime exception + */ public void createVariablesFor(final IScope scope, final List agents, final List> initialValues) throws GamaRuntimeException { createAndUpdateVariablesFor(scope, agents, initialValues, false); } + /** + * Creates the and update variables for. + * + * @param scope the scope + * @param agents the agents + * @param initialValues the initial values + * @param update the update + * @throws GamaRuntimeException the gama runtime exception + */ @SuppressWarnings ("null") public void createAndUpdateVariablesFor(final IScope scope, final List agents, final List> initialValues, final boolean update) throws GamaRuntimeException { @@ -487,6 +545,12 @@ public void createAndUpdateVariablesFor(final IScope scope, final List agents } } + /** + * Allow var init to be overriden by external init. + * + * @param var the var + * @return true, if successful + */ protected boolean allowVarInitToBeOverridenByExternalInit(final IVariable var) { return true; } @@ -514,9 +578,7 @@ public IExecutable getAspect(final String default1) { } @Override - public Collection getAspectNames() { - return species.getAspectNames(); - } + public Collection getAspectNames() { return species.getAspectNames(); } @Override public IVariable getVar(final String s) { @@ -575,6 +637,14 @@ protected void computeTopology(final IScope scope) throws GamaRuntimeException { } + /** + * Builds the grid topology. + * + * @param scope the scope + * @param species the species + * @param host the host + * @return the i topology + */ protected static ITopology buildGridTopology(final IScope scope, final ISpecies species, final IAgent host) { IExpression exp = species.getFacet(WIDTH); final Envelope3D env = scope.getSimulation().getGeometry().getEnvelope(); @@ -635,14 +705,10 @@ protected static ITopology buildGridTopology(final IScope scope, final ISpecies } @Override - public IMacroAgent getHost() { - return host; - } + public IMacroAgent getHost() { return host; } @Override - public void setHost(final IMacroAgent agt) { - host = agt; - } + public void setHost(final IMacroAgent agt) { host = agt; } // // @Override // public Iterator iterator() { @@ -662,9 +728,7 @@ public final int hashCode() { @Override public void killMembers() throws GamaRuntimeException { final T[] ag = toArray(); - for (final IAgent a : ag) { - if (a != null) { a.dispose(); } - } + for (final IAgent a : ag) { if (a != null) { a.dispose(); } } this.clear(); } @@ -687,9 +751,7 @@ public void addValueAtIndex(final IScope scope, final Object index, final T valu @Override public void addValues(final IScope scope, final IContainer values) { - for (final T o : (java.lang.Iterable) values.iterable(scope)) { - addValue(scope, o); - } + for (final T o : (java.lang.Iterable) values.iterable(scope)) { addValue(scope, o); } } @Override @@ -702,9 +764,7 @@ public void removeValue(final IScope scope, final Object value) { @Override public void removeValues(final IScope scope, final IContainer values) { - for (final Object o : values.iterable(scope)) { - removeValue(scope, o); - } + for (final Object o : values.iterable(scope)) { removeValue(scope, o); } } @Override @@ -724,6 +784,11 @@ public boolean contains(final IScope scope, final Object o) { return ((IAgent) o).getPopulation() == this; } + /** + * Checks for listeners. + * + * @return true, if successful + */ private boolean hasListeners() { return listeners != null && !listeners.isEmpty(); } @@ -740,49 +805,65 @@ public void removeListener(final IPopulation.Listener listener) { listeners.remove(listener); } + /** + * Fire agent added. + * + * @param scope the scope + * @param agent the agent + */ protected void fireAgentAdded(final IScope scope, final IAgent agent) { if (!hasListeners()) return; try { - for (final IPopulation.Listener l : listeners) { - l.notifyAgentAdded(scope, this, agent); - } + for (final IPopulation.Listener l : listeners) { l.notifyAgentAdded(scope, this, agent); } } catch (final RuntimeException e) { e.printStackTrace(); } } + /** + * Fire agents added. + * + * @param the generic type + * @param scope the scope + * @param container the container + */ protected void fireAgentsAdded(final IScope scope, final IList container) { if (!hasListeners()) return; // create list final Collection agents = new LinkedList<>(container); // send event try { - for (final IPopulation.Listener l : listeners) { - l.notifyAgentsAdded(scope, this, agents); - } + for (final IPopulation.Listener l : listeners) { l.notifyAgentsAdded(scope, this, agents); } } catch (final RuntimeException e) { e.printStackTrace(); } } + /** + * Fire agent removed. + * + * @param scope the scope + * @param agent the agent + */ protected void fireAgentRemoved(final IScope scope, final IAgent agent) { if (!hasListeners()) return; try { - for (final IPopulation.Listener l : listeners) { - l.notifyAgentRemoved(scope, this, agent); - } + for (final IPopulation.Listener l : listeners) { l.notifyAgentRemoved(scope, this, agent); } } catch (final RuntimeException e) { e.printStackTrace(); } } + /** + * Fire population cleared. + * + * @param scope the scope + */ protected void firePopulationCleared(final IScope scope) { if (!hasListeners()) return; // send event try { - for (final IPopulation.Listener l : listeners) { - l.notifyPopulationCleared(scope, this); - } + for (final IPopulation.Listener l : listeners) { l.notifyPopulationCleared(scope, this); } } catch (final RuntimeException e) { e.printStackTrace(); } @@ -880,9 +961,7 @@ public static Iterable allLivingAgents(final Iterable i * @see msi.gaml.species.ISpecies#isInitOverriden() */ @Override - public boolean isInitOverriden() { - return isInitOverriden; - } + public boolean isInitOverriden() { return isInitOverriden; } /** * Method isStepOverriden() @@ -890,8 +969,6 @@ public boolean isInitOverriden() { * @see msi.gaml.species.ISpecies#isStepOverriden() */ @Override - public boolean isStepOverriden() { - return isStepOverriden; - } + public boolean isStepOverriden() { return isStepOverriden; } } \ No newline at end of file