Skip to content

Commit

Permalink
Fixes a leak (listener) that was polluting the metamodel of GAMA
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisDrogoul committed Jun 17, 2021
1 parent a9c882c commit 20f59fc
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 48 deletions.
35 changes: 1 addition & 34 deletions msi.gama.core/src/msi/gaml/descriptions/SpeciesDescription.java
Expand Up @@ -13,7 +13,6 @@
import static com.google.common.collect.Iterables.transform;
import static msi.gaml.compilation.AbstractGamlAdditions.getAllChildrenOf;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
Expand All @@ -28,7 +27,6 @@
import msi.gama.common.interfaces.IGamlIssue;
import msi.gama.common.interfaces.ISkill;
import msi.gama.common.preferences.GamaPreferences;
import msi.gama.common.util.JavaUtils;
import msi.gama.metamodel.agent.GamlAgent;
import msi.gama.metamodel.agent.IAgent;
import msi.gama.metamodel.agent.IMacroAgent;
Expand All @@ -39,9 +37,7 @@
import msi.gama.util.GamaMapFactory;
import msi.gama.util.IMap;
import msi.gaml.architecture.reflex.AbstractArchitecture;
import msi.gaml.compilation.AbstractGamlAdditions;
import msi.gaml.compilation.GAML;
import msi.gaml.compilation.GamaHelper;
import msi.gaml.compilation.IAgentConstructor;
import msi.gaml.compilation.IGamaHelper;
import msi.gaml.compilation.kernel.GamaSkillRegistry;
Expand Down Expand Up @@ -667,39 +663,10 @@ public boolean finalizeDescription() {
if (!visitMicroSpecies(visitor)) return false;
// Calling sortAttributes later (in compilation)
// add the listeners to the variables (if any)
addListenersToVariables();
// addListenersToVariables();
return true;
}

/**
* // AD 2021: addition of the listeners
*/
private void addListenersToVariables() {
if (isBuiltIn()) return;
if (javaBase == null) return;
Iterable<Class<? extends ISkill>> skillClasses = transform(getSkills(), TO_CLASS);
this.visitAllAttributes(v -> {
VariableDescription var = (VariableDescription) v;
String varName = var.getName();
if (AbstractGamlAdditions.LISTENERS_BY_NAME.containsKey(varName)) {
List<Class> classes = JavaUtils.collectImplementationClasses(javaBase, skillClasses,
AbstractGamlAdditions.LISTENERS_BY_NAME.get(varName));
if (!classes.isEmpty()) {
List<GamaHelper> listeners = new ArrayList();
for (Class c : classes) {
Set<GamaHelper> helpers = AbstractGamlAdditions.LISTENERS_BY_CLASS.get(c);
for (GamaHelper h : helpers) {
if (h.getName().equals(varName)) { listeners.add(h); }
}
}
if (!listeners.isEmpty()) { var.addListeners(listeners); }
}

}
return true;
});
}

/**
*
*/
Expand Down
21 changes: 10 additions & 11 deletions msi.gama.core/src/msi/gaml/descriptions/VariableDescription.java
Expand Up @@ -12,7 +12,6 @@

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -57,7 +56,7 @@ public class VariableDescription extends SymbolDescription {
// for variables automatically added to species for containing micro-agents
private boolean _isSyntheticSpeciesContainer;
private IGamaHelper<?> get, init, set;
private GamaHelper<?>[] listeners;
// private GamaHelper<?>[] listeners;

public VariableDescription(final String keyword, final IDescription superDesc, final EObject source,
final Facets facets) {
Expand Down Expand Up @@ -111,14 +110,14 @@ public void copyFrom(final VariableDescription v2) {
if (get == null) { get = v2.get; }
if (set == null) { set = v2.set; }
if (init == null) { init = v2.init; }
if (listeners == null) { listeners = v2.listeners; }
// if (listeners == null) { listeners = v2.listeners; }
}

@Override
public VariableDescription copy(final IDescription into) {
final VariableDescription vd = new VariableDescription(getKeyword(), into, element, getFacetsCopy());
vd.addHelpers(get, init, set);
vd.listeners = listeners;
// vd.listeners = listeners;
vd.originName = getOriginName();
return vd;
}
Expand Down Expand Up @@ -310,13 +309,13 @@ public void addHelpers(final Class<?> skill, final IGamaHelper<?> get, final IGa
set != null ? new GamaHelper<>(name, skill, set) : null);
}

public void addListeners(final List<GamaHelper> listeners) {
this.listeners = listeners.toArray(new GamaHelper[listeners.size()]);
}

public GamaHelper[] getListeners() {
return listeners;
}
// public void addListeners(final List<GamaHelper> listeners) {
// this.listeners = listeners.toArray(new GamaHelper[listeners.size()]);
// }
//
// public GamaHelper[] getListeners() {
// return listeners;
// }

public IGamaHelper<?> getGetter() {
return get;
Expand Down
42 changes: 39 additions & 3 deletions msi.gama.core/src/msi/gaml/variables/Variable.java
Expand Up @@ -10,17 +10,22 @@
********************************************************************************************************/
package msi.gaml.variables;

import static com.google.common.collect.Iterables.transform;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.google.common.base.Objects;

import msi.gama.common.interfaces.IGamlIssue;
import msi.gama.common.interfaces.IKeyword;
import msi.gama.common.interfaces.ISkill;
import msi.gama.common.interfaces.IVarAndActionSupport;
import msi.gama.common.util.JavaUtils;
import msi.gama.metamodel.agent.IAgent;
import msi.gama.precompiler.GamlAnnotations.doc;
import msi.gama.precompiler.GamlAnnotations.facet;
Expand All @@ -34,6 +39,7 @@
import msi.gama.runtime.benchmark.StopWatch;
import msi.gama.runtime.exceptions.GamaRuntimeException;
import msi.gama.util.GamaColor;
import msi.gaml.compilation.AbstractGamlAdditions;
import msi.gaml.compilation.GamaHelper;
import msi.gaml.compilation.IDescriptionValidator;
import msi.gaml.compilation.IGamaHelper;
Expand All @@ -43,6 +49,7 @@
import msi.gaml.descriptions.ConstantExpressionDescription;
import msi.gaml.descriptions.IDescription;
import msi.gaml.descriptions.IExpressionDescription;
import msi.gaml.descriptions.SpeciesDescription;
import msi.gaml.descriptions.VariableDescription;
import msi.gaml.expressions.IExpression;
import msi.gaml.expressions.ListExpression;
Expand Down Expand Up @@ -399,15 +406,44 @@ private void buildHelpers(final AbstractSpecies species) {
initer = getDescription().getIniter();
setter = getDescription().getSetter();
if (setter != null) { sSkill = species.getSkillInstanceFor(setter.getSkillClass()); }
GamaHelper[] helpers = getDescription().getListeners();
mustNotifyOfChanges = helpers != null && helpers.length > 0 || onChangeExpression != null || on_changer != null;
if (helpers != null && helpers.length > 0) {
addListeners(species);
mustNotifyOfChanges =
listeners != null && listeners.size() > 0 || onChangeExpression != null || on_changer != null;
}

/**
* // AD 2021: addition of the listeners
*/
private void addListeners(final AbstractSpecies species) {
VariableDescription var = (VariableDescription) description;
SpeciesDescription sp = species.getDescription();
if (var.isBuiltIn()) return;
Class base = sp.getJavaBase();
if (base == null) return;
List<GamaHelper> helpers = new ArrayList<>();
Iterable<Class<? extends ISkill>> skillClasses = transform(sp.getSkills(), SpeciesDescription.TO_CLASS);
if (AbstractGamlAdditions.LISTENERS_BY_NAME.containsKey(getName())) {
List<Class> classes = JavaUtils.collectImplementationClasses(base, skillClasses,
AbstractGamlAdditions.LISTENERS_BY_NAME.get(getName()));
if (!classes.isEmpty()) {
for (Class c : classes) {
Set<GamaHelper> set = AbstractGamlAdditions.LISTENERS_BY_CLASS.get(c);
for (GamaHelper h : set) {
if (h.getName().equals(getName())) { helpers.add(h); }
}
}
}

}

if (!helpers.isEmpty()) {
listeners = new HashMap<>();
for (GamaHelper helper : helpers) {
listeners.put(helper, species.getSkillInstanceFor(helper.getSkillClass()));
}

}

}

protected Object coerce(final IAgent agent, final IScope scope, final Object v) throws GamaRuntimeException {
Expand Down

0 comments on commit 20f59fc

Please sign in to comment.