Skip to content

Commit

Permalink
Merge pull request #247 from gama-platform/cleanup-saved-simulation-file
Browse files Browse the repository at this point in the history
Fixes restore documentation + various serialization models improvements
  • Loading branch information
lesquoyb authored Jul 8, 2024
2 parents d72e5cc + b61dfd5 commit 44c6293
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ experiment "Repeated Simulations" type: gui {
saved_step <- serialize(self.simulation);
}

reflex restore when: (cycle = 4) {
reflex restore when: (cycle mod 4) = 0 {
write "================ begin restore " + self + " - " + cycle;
restore simulation from: saved_step;
write "================ end restore " + self + " - " + cycle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ global {
init {

list objects <- [[1,2,3,4], "fff",rgb(100,100,100)];
write "Base object list: " + objects;

write "\nIndividually serialized and deserialized:";
loop o over: objects {
string s <- serialize(o);
write deserialize(s);
}

write deserialize(serialize(objects));
write "\nThe whole list serialized and deserialized: " + deserialize(serialize(objects));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,12 @@ public static void restoreFromString(final IAgent agent, final String string) {
byte[] all = string.getBytes(ISerialisationConstants.STRING_BYTE_ARRAY_CHARSET);
restoreFromBytes(agent, all);
} catch (Throwable e) {
e.printStackTrace();
// e.printStackTrace();
// The string is maybe a path ?
try {
restoreFromFile(agent, string);
} catch (Throwable ex) {
throw GamaRuntimeException.create(ex, agent.getScope());
GAMA.reportAndThrowIfNeeded(agent.getScope(),GamaRuntimeException.create(ex, agent.getScope()), true);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,8 @@
optional = false,
doc = @doc ("The file or the string from which to restore the agent")) },
omissible = IKeyword.TARGET)
@doc ("Allows to restore any agent that has been previously serialised or saved to a file, e.g. `string s <- serialize(a, 'json'); ... restore a from: s; `"
+ " or `save simulation to: 'sim.gsim' format: `binary`; ... restore simulation from: file('sim.gsim') `")
@SuppressWarnings ({ "unchecked", "rawtypes" })
@doc ("Allows to restore any agent that has been previously serialised or saved to a file, e.g. `string s <- serialize(a);` ... `restore a from: s;`"
+ " or `save simulation to: 'sim.gsim' format: 'binary';` ... `restore simulation from: file('sim.gsim');`")
public class RestoreStatement extends AbstractStatement {

/** The header. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import gama.core.common.interfaces.IKeyword;
import gama.core.common.util.StringUtils;
import gama.core.metamodel.agent.IAgent;
import gama.core.runtime.GAMA;
import gama.core.runtime.IScope;
import gama.core.runtime.exceptions.GamaRuntimeException;
import gama.core.util.IContainer;
Expand Down Expand Up @@ -153,10 +154,10 @@ public static String toGeoJSon(final IScope scope, final IExpression spec, final
@test ("to_json(font('Helvetica')) = '{\"gaml_type\":\"font\",\"name\":\"Helvetica\",\"style\":0,\"size\":12}'")
@test ("to_json(point(20,10)) = '{\"gaml_type\":\"point\",\"x\":20.0,\"y\":10.0,\"z\":0.0}'")
@doc (
value = "Serializes any object/agent/simulation into a string, using the json format. A flag can be passed to enable/disable pretty printing (false by default)."
+ "The format used by GAMA follows simple rules. int, float, bool, string values are outputted as they are. nil is outputted as 'null'. A list is outputted as a json array. Any other object or agent is outputted as a json object. If this object possesses the \"gaml_type\" attribute, "
+ "it is an instance of the corresponding type, and the members that follow contain the attributes and the values necessary to reconstruct it. If it has the \"agent_reference\" attribute, its value represent the reference to an agent. If any reference to an agent is found, the "
+ "json string returned will be an object with two attributes: \"gama_object\", the object containing the references, and \"reference_table\" a dictionary mapping the references to the json description of the agents (their species, name, index, and list of attributes). "
value = "Serializes any object/agent/simulation into a string, using the json format. A flag can be passed to enable/disable pretty printing (`false` by default)."
+ "The format used by GAMA follows simple rules. `int`, `float`, `bool`, `string` values are outputted as they are. `nil` is outputted as `null`. A list is outputted as a json array. Any other object or agent is outputted as a json object. If this object possesses the attribute `gaml_type`, "
+ "it is an instance of the corresponding type, and the members that follow contain the attributes and the values necessary to reconstruct it. If it has the `agent_reference` attribute, its value represents the reference to an agent. If any reference to an agent is found, the "
+ "json string returned will be an object with two attributes: `gama_object`, the object containing the references, and `reference_table` a dictionary mapping the references to the json description of the agents (their `species`, `name`, `index`, and list of attributes). "
+ "This choice allows to manage cross references between agents",
see = { "serialize", "to_gaml" })
public static String toJson(final IScope scope, final Object obj, final boolean pretty) {
Expand Down Expand Up @@ -297,8 +298,8 @@ public static Object opEvalGaml(final IScope scope, final String gaml) {
final IExpression e = GAML.getExpressionFactory().createExpr(gaml, d);
return scope.evaluate(e, agent).getValue();
} catch (final GamaRuntimeException e) {
scope.getGui().getConsole().informConsole("Error in evaluating Gaml code : '" + gaml + "' in "
+ scope.getAgent() + Strings.LN + "Reason: " + e.getMessage(), scope.getRoot());
GAMA.reportAndThrowIfNeeded(scope, GamaRuntimeException.error("Error in evaluating Gaml code : '" + gaml + "' in "
+ scope.getAgent() + Strings.LN + "Reason: " + e.getMessage(), scope), false);

return null;
}
Expand Down

0 comments on commit 44c6293

Please sign in to comment.