Skip to content

Commit

Permalink
Fix some issues on unuserialize
Browse files Browse the repository at this point in the history
  • Loading branch information
benoitgaudou committed Apr 26, 2022
1 parent 350ed40 commit 57835d2
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 33 deletions.
24 changes: 19 additions & 5 deletions msi.gama.core/src/msi/gama/kernel/simulation/SimulationAgent.java
Expand Up @@ -972,6 +972,10 @@ public void executeAction(final IExecutable executable) {
@Override
public void updateWith(final IScope scope, final SavedAgent sa) {

Double seedValue = null;
String rngValue = null;
Integer usageValue = null;

// This list is updated during all the updateWith of the simulation.
// When all the agents will be created (end of this updateWith),
// all the references will be replaced by the corresponding agent.
Expand All @@ -989,9 +993,23 @@ public void updateWith(final IScope scope, final SavedAgent sa) {
((IReference) attrValue).setAgentAndAttrName(this, varName);
if (!list_ref.contains(attrValue)) { list_ref.add((IReference) attrValue); }
}

// If attributes are related to the RNG, we keep them to initialise the RNG later, in the proper order.
if(varName.equals(IKeyword.SEED)) {
seedValue = (Double) attrValue;
} else if(varName.equals(IKeyword.RNG)) {
rngValue = (String) attrValue;
} else if(varName.equals(SimulationAgent.USAGE)) {
usageValue = (Integer) attrValue;
} else {
this.setDirectVarValue(scope, varName, attrValue);
}

this.setDirectVarValue(scope, varName, attrValue);
}

// Update RNG
setRandomGenerator(new RandomUtils(seedValue, rngValue));
setUsage(usageValue);

// Update Clock
final Object cycle = sa.getAttributeValue("cycle");
Expand All @@ -1012,7 +1030,6 @@ public void updateWith(final IScope scope, final SavedAgent sa) {
if (savedAgentInnerPop != null) {
for (final String savedAgentMicroPopName : savedAgentInnerPop.keySet()) {
final IPopulation<? extends IAgent> simuMicroPop = getPopulationFor(savedAgentMicroPopName);
// final IPopulation<? extends IAgent> simuMicroPop = getMicroPopulation(savedAgentMicroPopName);

if (simuMicroPop != null) {
// Build a map name::innerPopAgentSavedAgt :
Expand Down Expand Up @@ -1061,9 +1078,6 @@ public void updateWith(final IScope scope, final SavedAgent sa) {
for (final IAgent remainingAgent : mapSimuAgtName.values()) {
// Kill them all
remainingAgent.dispose();
// simuMicroPop.killMembers();
// microPop.clear();
// microPop.firePopulationCleared();
}
}
}
Expand Down
Expand Up @@ -283,7 +283,11 @@ public void remove(final ISpecies species) {
public void update(final IScope scope, final Envelope envelope, final boolean parallel) {
this.bounds = envelope;
this.parallel = parallel;
for (ISpecies species : spatialIndexes.keySet()) {

final WeakHashMap<ISpecies, ISpatialIndex> spatialIndexesTmp = new WeakHashMap<>();
spatialIndexesTmp.putAll(spatialIndexes);

for (ISpecies species : spatialIndexesTmp.keySet()) {
remove(species);
add(scope, species, true);
}
Expand Down
Expand Up @@ -33,12 +33,17 @@
@SuppressWarnings ({ "rawtypes" })
public class GamaAgentConverter implements Converter {

/** The convert scope. */
ConverterScope convertScope;

/**
* Instantiates a new gama agent converter.
* Instantiates a new gama list converter.
*
* @param s the s
*/
public GamaAgentConverter(final ConverterScope s) {}
public GamaAgentConverter(final ConverterScope s) {
convertScope = s;
}

@Override
public boolean canConvert(final Class arg0) {
Expand All @@ -61,7 +66,6 @@ public boolean canConvert(final Class arg0) {
}

return false;
// return (arg0.equals(GamlAgent.class) || arg0.equals(MinimalAgent.class));
}

@Override
Expand All @@ -73,11 +77,9 @@ public void marshal(final Object arg0, final HierarchicalStreamWriter writer, fi
DEBUG.OUT("ConvertAnother : AgentConverter " + agt.getClass());
// System.out.println("ConvertAnother : AgentConverter " + agt.getClass());

// ReferenceSavedAgent refAft = new ReferenceSavedAgent(agt, null, (ReferenceToAgent) null);
final ReferenceAgent refAft = new ReferenceAgent(null, null, agt);
context.convertAnother(refAft);

// writer.setValue(agt.getName());
DEBUG.OUT("===========END ConvertAnother : GamaAgent");
// System.out.println("===========END ConvertAnother : GamaAgent");

Expand All @@ -88,27 +90,10 @@ public void marshal(final Object arg0, final HierarchicalStreamWriter writer, fi
public Object unmarshal(final HierarchicalStreamReader reader, final UnmarshallingContext arg1) {
// TODO manage MinimalAgent and MinimalGridAgent
reader.moveDown();
// SimulationAgent simAgt = convertScope.getSimulationAgent();
// List<IAgent> lagt;
// if(simAgt == null) {
// lagt = (convertScope.getScope()).getSimulation().getAgents(convertScope.getScope());
// } else {
// lagt = simAgt.getAgents(convertScope.getScope());
// }
final ReferenceAgent agt = (ReferenceAgent) arg1.convertAnother(null, ReferenceAgent.class);

// boolean found = false;
// int i = 0;
// IAgent agt = null;
// while(!found && (i < lagt.size())) {
// if(lagt.get(i).getName().equals(reader.getValue())) {
// found = true;
// agt = lagt.get(i);
// }
// i++;
// }
reader.moveUp();
return agt;

return agt.getReferencedAgent(convertScope.getScope().getSimulation());
}

}
Expand Up @@ -60,8 +60,12 @@ public IList constructObject(final IScope scope) {
i++;
}

return isReference ? new ReferenceList(this)
: GamaListFactory.create(scope, contentTypeListReducer, valuesListReducer);
if(isReference) {
unreferenceReducer(scope.getSimulation());
return new ReferenceList(this);
} else {
return GamaListFactory.create(scope, contentTypeListReducer, valuesListReducer);
}
}

/**
Expand Down
Expand Up @@ -102,6 +102,10 @@ public boolean equals(final Object o) {
if (o == this) return true;
return false;
}

public IAgent getReferencedAgent(final SimulationAgent sim) {
return attributeValue.getReferencedAgent(sim);
}

@Override
public int hashCode() {
Expand Down
Expand Up @@ -13,7 +13,6 @@
import java.util.ArrayList;

import msi.gama.kernel.simulation.SimulationAgent;
import msi.gama.metamodel.agent.IAgent;
import msi.gama.util.GamaList;
import msi.gama.util.IReference;
import ummisco.gama.serializer.gamaType.reduced.GamaListReducer;
Expand All @@ -36,6 +35,7 @@ public class ReferenceList extends GamaList implements IReference {
*/
public ReferenceList(GamaListReducer l) {
super(l.getValuesListReducer().size(), l.getContentTypeListReducer());
this.addAll(l.getValuesListReducer());
agtAttr = new ArrayList<AgentAttribute>();
listReducer = l;
}
Expand Down

0 comments on commit 57835d2

Please sign in to comment.