Skip to content

Commit

Permalink
Improves the documentation and robustness of autosave in display/output
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisDrogoul committed Feb 20, 2022
1 parent 5b2e4e6 commit f130003
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 44 deletions.
37 changes: 25 additions & 12 deletions msi.gama.core/src/msi/gama/outputs/AbstractOutputManager.java
Expand Up @@ -20,6 +20,7 @@

import msi.gama.common.interfaces.IKeyword;
import msi.gama.runtime.IScope;
import msi.gama.runtime.exceptions.GamaRuntimeException;
import msi.gama.util.GamaMapFactory;
import msi.gama.util.IMap;
import msi.gaml.compilation.ISymbol;
Expand Down Expand Up @@ -188,29 +189,41 @@ public String toString() {
@Override
public boolean init(final IScope scope) {
name = scope.getRoot().getName();
boolean oneOutputAutosaving = false;

boolean atLeastOneOutputAutosaving = false;
for (final IOutput output : ImmutableList.copyOf(this)) {
if (!open(scope, output)) return false;
if (output instanceof IDisplayOutput && ((IDisplayOutput) output).isAutoSave()) {
oneOutputAutosaving = true;
atLeastOneOutputAutosaving = true;
}
}
atLeastOneOutputAutosaving |= evaluateAutoSave(scope);
if (atLeastOneOutputAutosaving) { synchronize(); }

return true;
}

/**
* Evaluate auto save.
*
* @param scope
* the scope
* @return true, if successful
* @throws GamaRuntimeException
* the gama runtime exception
*/
private boolean evaluateAutoSave(final IScope scope) throws GamaRuntimeException {
boolean isAutosaving = false;
if (autosave != null) {
boolean isAutosaving = false;
String autosavingPath = null;
oneOutputAutosaving = true;
String path = null;
if (autosave.getGamlType().equals(Types.STRING)) {
isAutosaving = true;
autosavingPath = Cast.asString(scope, autosave.value(scope));
path = Cast.asString(scope, autosave.value(scope));
isAutosaving = path != null && !path.isBlank();
} else {
isAutosaving = Cast.asBool(scope, autosave.value(scope));
}
if (isAutosaving) { SnapshotMaker.getInstance().doSnapshot(scope, autosavingPath); }
if (isAutosaving) { SnapshotMaker.getInstance().doSnapshot(scope, path); }
}
if (oneOutputAutosaving) { synchronize(); }

return true;
return isAutosaving;
}

/**
Expand Down
58 changes: 31 additions & 27 deletions msi.gama.core/src/msi/gama/outputs/LayeredDisplayData.java
@@ -1,12 +1,12 @@
/*******************************************************************************************************
*
* LayeredDisplayData.java, in msi.gama.core, is part of the source code of the
* GAMA modeling and simulation platform (v.1.8.2).
* LayeredDisplayData.java, in msi.gama.core, is part of the source code of the GAMA modeling and simulation platform
* (v.1.8.2).
*
* (c) 2007-2022 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.outputs;

Expand All @@ -25,6 +25,7 @@
import msi.gama.kernel.simulation.SimulationAgent;
import msi.gama.metamodel.shape.GamaPoint;
import msi.gama.runtime.IScope;
import msi.gama.runtime.exceptions.GamaRuntimeException;
import msi.gama.util.GamaColor;
import msi.gama.util.GamaListFactory;
import msi.gaml.descriptions.IDescription;
Expand Down Expand Up @@ -1066,18 +1067,7 @@ public void initWith(final IScope scope, final IDescription desc) {
setEnvHeight(env.getHeight());
env.dispose();

final IExpression auto = facets.getExpr(IKeyword.AUTOSAVE);
if (auto != null) {
if (auto.getGamlType().equals(Types.POINT)) {
setAutosave(true);
setImageDimension(Cast.asPoint(scope, auto.value(scope)));
} else if (auto.getGamlType().equals(Types.STRING)) {
setAutosave(true);
setAutosavePath(Cast.asString(scope, auto.value(scope)));
} else {
setAutosave(Cast.asBool(scope, auto.value(scope)));
}
}
updateAutoSave(scope, facets.getExpr(IKeyword.AUTOSAVE));
final IExpression toolbar = facets.getExpr(IKeyword.TOOLBAR);
if (toolbar != null) {
if (toolbar.getGamlType() == Types.BOOL) {
Expand Down Expand Up @@ -1236,6 +1226,31 @@ public void initWith(final IScope scope, final IDescription desc) {

}

/**
* Update auto save.
*
* @param scope
* the scope
* @param auto
* the auto
* @throws GamaRuntimeException
* the gama runtime exception
*/
private void updateAutoSave(final IScope scope, final IExpression auto) throws GamaRuntimeException {
if (auto == null) return;
if (auto.getGamlType().equals(Types.POINT)) {
GamaPoint result = Cast.asPoint(scope, auto.value(scope));
setAutosave(result != null);
setImageDimension(result);
} else if (auto.getGamlType().equals(Types.STRING)) {
String result = Cast.asString(scope, auto.value(scope));
setAutosave(result != null && !result.isBlank());
setAutosavePath(result);
} else {
setAutosave(Cast.asBool(scope, auto.value(scope)));
}
}

/**
* Sets the z far.
*
Expand Down Expand Up @@ -1264,18 +1279,7 @@ private void setZFar(final Double zF) {
* the facets
*/
public void update(final IScope scope, final Facets facets) {
final IExpression auto = facets.getExpr(IKeyword.AUTOSAVE);
if (auto != null) {
if (auto.getGamlType().equals(Types.POINT)) {
setAutosave(true);
setImageDimension(Cast.asPoint(scope, auto.value(scope)));
} else if (auto.getGamlType().equals(Types.STRING)) {
setAutosave(true);
setAutosavePath(Cast.asString(scope, auto.value(scope)));
} else {
setAutosave(Cast.asBool(scope, auto.value(scope)));
}
}
updateAutoSave(scope, facets.getExpr(IKeyword.AUTOSAVE));
// /////////////// dynamic Lighting ///////////////////

if (!constantBackground) {
Expand Down
13 changes: 8 additions & 5 deletions msi.gama.core/src/msi/gama/outputs/LayeredDisplayOutput.java
@@ -1,12 +1,12 @@
/*******************************************************************************************************
*
* LayeredDisplayOutput.java, in msi.gama.core, is part of the source code of the
* GAMA modeling and simulation platform (v.1.8.2).
* LayeredDisplayOutput.java, in msi.gama.core, is part of the source code of the GAMA modeling and simulation platform
* (v.1.8.2).
*
* (c) 2007-2022 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.outputs;

Expand Down Expand Up @@ -272,14 +272,17 @@
name = IKeyword.AUTOSAVE,
type = { IType.BOOL, IType.POINT, IType.STRING },
optional = true,
doc = @doc ("Allows to save this display on disk. A value of true/false will save it at a resolution of 500x500. A point can be passed to personalize these dimensions. Note that setting autosave to true (or to any other value than false) in a display will synchronize all the displays defined in the experiment")), },
doc = @doc ("Allows to save this display on disk. This facet accepts bool, point or string values. "
+ "If it is false or nil, nothing happens. 'true' will save it at a resolution of 500x500 with a standard name "
+ "(containing the name of the model, display, resolution, cycle and time). A non-nil point will change that resolution. A non-nil string will keep 500x500 and change the filename "
+ "(if it is not dynamically built, the previous file will be erased). Note that setting autosave to true in a display will synchronize all the displays defined in the experiment")), },
omissible = IKeyword.NAME)
@inside (
symbols = { IKeyword.OUTPUT, IKeyword.PERMANENT })
@validator (InfoValidator.class)
@serializer (DisplaySerializer.class)
@doc (
value = "A display refers to a independent and mobile part of the interface that can display species, images, texts or charts.",
value = "A display refers to an independent and mobile part of the interface that can display species, images, texts or charts.",
usages = { @usage (
value = "The general syntax is:",
examples = @example (
Expand Down

0 comments on commit f130003

Please sign in to comment.