AnyLogic
Expand
Font size

Accessing the presentation

At model runtime you can access the presentation that is currently shown in the model window via the function getExperimentHost(). Having accessed the presentation, you can do a number of things with it: zoom it in or out, navigate to the presentation’s center or to some view area, show an inspect window for a specific model element, save the model state in a snapshot file, etc.

The usage example: by calling the getExperimentHost().zoomIn(2); function you zoom in the presentation two times.

In this article we list all the functions provided by AnyLogic presentation (that implements the AnyLogic’s IExperimentHost interface).

Presentation functions

Navigation, zoom, and panning
Function Description
void setCenter(double x, double y) Centers the animation (places the animation center in the point with the given coordinates).

x — X-coordinate of the center point.
y — Y-coordinate of the center point.
void navigateHome() Navigates the presentation to “home location”, i.e. moves the presentation coordinate origin (0,0) to the top left corner of the window, and sets zoom to match the initial frame.
void navigateTo(ViewArea viewArea) Shows the given view area in the model animation panel.

viewArea — the view area to show.
void zoomIn(double coefficient) Zooms in the animation view.

coefficient — how much to zoom in, positive number. E.g. 2.0 will result in 2x scaling compared to the current view.
void zoomOut(double coefficient) Zooms out the animation view.

coefficient — how much to zoom out, positive number. E.g. 2.0 will result in 0.5x scaling compared to the current view.
void setZoomAndPanningEnabled(boolean yes) Enables or disables zoom & panning initiated from the GUI (button, mouse, or keyboard).

yes — if true, zoom & panning should be enabled, if false — disabled.
boolean isZoomAndPanningEnabled() Tests if zoom & panning from the GUI is enabled.
Returns true if zoom & panning are enabled, false if not.
Experiment
Function Description
Experiment getExperiment() Returns the experiment.
Experiment termination
Function Description
void close() This function returns immediately and performs the following actions in a separate thread:
  • stops experiment if it is not stopped;
  • destroys the model;
  • closes experiment window (only if the model is started in the application mode).
Presentation context
Function Description
Presentable getPresentable() Returns the object currently displayed by the model animation.
void setPresentable(Presentable p) Sets the presentable object to be displayed by the model animation. Should be called when a different object should be displayed or when the current (active) object is destroyed.
Developer panel
Function Description
void setDeveloperPanelEnabled(boolean yes) Enables or disables the developer panel.

yes — if true, developer panel should be enabled, if false — disabled.
void setDeveloperPanelVisibleOnStart(boolean yes) Shows the developer panel (if enabled) when the model window is shown.

yes — if true, developer panel is shown, if false — hidden.
Control panel
Function Description
void setRunControlEnabled(boolean runControlEnabled) Enables or disables Run, Pause, and Stop buttons on the model window control panel. If the buttons are disabled, the Disabled by model designer tooltip is shown.

runControlEnabled — if true the buttons will be enabled, if false — disabled.
boolean isRunControlEnabled() Tests if Run, Pause, and Stop buttons on the model window control panel are enabled.
If the function returns true, the buttons are enabled. If the function returns false, they are disabled.
void setSpeedControlEnabled(boolean speedControlEnabled) Enables or disables the model execution speed buttons on the model window control panel. If the buttons are disabled, the Disabled by model designer tooltip is shown.

speedControlEnabled — if true, the buttons are enabled, if false — disabled.
boolean isSpeedControlEnabled() Tests if the model execution speed buttons on the model window control panel are enabled.
If the function returns true, the buttons are enabled. If the function returns false, they are disabled.
Saving the model state (snapshot)
The functions are available only in AnyLogic Professional. Read more about AnyLogic model snapshots in Saving and restoring model snapshots.
Function Description
void saveSnapshot(String fileName) Pauses experiment if it is currently running, saves snapshot and then resumes experiment if it was running.
On any error throws nothing. If you need custom error processing, please use another function notation (see below).

fileName — the name of the snapshot file.
void saveSnapshot(String fileName, java.lang.Runnable successfulCallback, java.util.function.Consumer<java.lang.throwable> errorCallback) Pauses experiment if it is currently running, saves snapshot and then resumes experiment if it was running.

fileName — the name of the snapshot file.
successfulCallback — called when the snapshot is successfully saved.
errorCallback — called in case of any error.

Usage example. If the snapshot was successfully saved, we print the "Saved!" text to the console. In case of an error, we print the "Error!" text and the error stack trace to the console.
getExperimentHost().saveSnapshot(
            "file.als",
            () -> traceln("Saved!"),
            e -> {
            traceln("Error!");
            e.printStackTrace();
            }
            );
void loadSnapshot (String fileName) Stops experiment and loads snapshot (in its “not running” state), doesn’t resume simulation of loaded snapshot.
On any error throws nothing, silently rollbacks to the current experiment and resumes it if it was running. If you need custom error processing, please use another function notation (see below).
When snapshot is loaded, presentation forgets everything about the model which was running before (including the engine, experiment, and agents), therefore, it is recommended not to keep references to model objects after this function call.

fileName — the name of the snapshot file.
void loadSnapshot(String fileName, java.lang.Runnable successfulCallback, java.util.function.Consumer<java.lang.throwable> errorCallback) Stops experiment and loads snapshot (in its “not running” state), doesn’t resume simulation of loaded snapshot. On any error throws nothing, silently rollbacks to the current experiment and resumes it if it was running. When snapshot is loaded, presentation forgets everything about the model which was running before (including the engine, experiment, and agents), therefore, it is recommended not to keep references to model objects after this function call.

fileName — the name of the snapshot file.
successfulCallback — called when the snapshot is successfully loaded.
errorCallback — called in case of any error.

Usage example. If the snapshot was successfully loaded, we print the "Loaded!" text to the console, set the window to display the top-level agent’s presentation and launch the model. In case of an error, we print the "Error!" text and the error stack trace to the console.
getExperimentHost().loadSnapshot(
            "file.als",
            () -> {
            traceln("Loaded!");
            getExperimentHost().setPresentable( getExperiment().getEngine().getRoot() );
            getExperiment().run();
            },
            e -> {
            traceln("Error!");
            e.printStackTrace();
            }
            );
Opening web pages
Function Description
void openWebSite (String url) Opens web page with the given URL in the browser.

url — the URL to open.
Inspect windows
Function Description
void addInspect(double x, double y, Presentable p, String name) Creates an inspect window at a particular location for an element of a Presentable object. If there is another inspect with the same Presentable and same element name, it is brought to front and a new inspect is not created. The initial size of the window is adjusted to fit the initial contents.

x — the x coordinate in the presentable object system.
y — the y coordinate in the presentable object system.
p — the Presentable object whose element is being inspected.
name — the name of the element being inspected.
void removeInspect(Presentable p, String name) Removes the inspect window for the given element.

p — the Presentable object whose element is being inspected.
name — the name of the element being inspected.
Message windows
Function Description
void showMessageDialog(String text) Shows a standard message dialog box with the given text.
The function usually returns immediately and does not wait for the dialog to be closed (except the cases when the function is called from UI thread).

text — the text to display.
How can we improve this article?