Alchemist via Gradle

We recommend to start by generating a new GitHub repository through the Alchemist Primer template repository.

We report here the README.md file contents as reference

Alchemist Primer

This is a template project to spawn projects using the Alchemist Simulator. It provides a pre-configured gradle build.

This project is a quick start for the Alchemist simulator, it shows how to use the simulator via Gradle to run a simple simulation. More information can be found on the official Alchemist website.

Prerequisites

A Gradle-compatible Java version.

How to launch

To run the example you can rely on the pre-configured Gradle build script. It will automatically download all the required libraries, set up the environment, and execute the simulator via command line for you. As first step, use git to locally clone this repository.

Simulations can be included in the src/main/yaml folder, and executed via the runAll Gradle task.

For each YAML file in src/main/yaml a task runFileName will be created.

In order to launch, open a terminal and move to the project root folder, then on UNIX:

./gradlew runAll

On Windows:

gradlew.bat runAll

Press <kb>P</kb> to start the simulation. For further information about the gui, see the Alchemist documentation.

Note that the first launch will take some time, since Gradle will download all the required files. They will get cached in the user's home folder (as per Gradle normal behavior).

The build script

Let's explain how things work by looking at the build.gradle.kts script and the gradle/libs.versions.toml file. The latter is a Gradle dependency catalog file including the specification of the required libraries, including their name, their version, and how they get bundled; while the former is the actual script that launches the simulator.

The build script imports the bundled alchemist dependencies as:

dependencies {
    implementation(libs.bundles.alchemist)
}

You will either need to import the full version of alchemist (but it's not recommended, as it pulls in a lot of dependencies you probably don't want), or manually declare which modules you want to include into your Alchemist simulations.

At the very least, you want to pull in an incarnation. This can be done directly in the build file, or (recommended way) through the TOML catalog.

For instance, let's say that you want to pull in the module for simulating on real-world maps, and that you also need a library named bar at version 1.2.3, from group io.github.foo, available on Maven Central. You could import both of them by changing the catalog as follows:

[versions]
alchemist = &#34;&lt;the version of alchemist is here&gt;&#34;

[libraries]
alchemist = { module = &#34;it.unibo.alchemist:alchemist&#34;, version.ref = &#34;alchemist&#34; }
alchemist-incarnation-protelis = { module = &#34;it.unibo.alchemist:alchemist-incarnation-protelis&#34;, version.ref = &#34;alchemist&#34; }
alchemist-swingui = { module = &#34;it.unibo.alchemist:alchemist-swingui&#34;, version.ref = &#34;alchemist&#34; }
# The following two lines are additions, the first defines the maps module of alchemist...
alchemist-maps = { module = &#34;it.unibo.alchemist:alchemist-maps&#34;, version.ref = &#34;alchemist&#34; }
# ...the second the custom library with a more compact syntax as there is no need to reuse the version defined above.
foobar = &#34;io.github.foo:bar:1.2.3&#34;

[bundles]
alchemist = [
    &#34;alchemist&#34;,
    &#34;alchemist-incarnation-protelis&#34;,
    &#34;alchemist-swingui&#34;,
    # Adding the newly defined libraries to the alchemist bundle makes them available automatically
    &#34;alchemist-maps&#34;,
    &#34;foobar&#34;
]

The build script defines some tasks meant to run Alchemist from the command line through Gradle with ease. There will be one task for each simulation file in src/main/yaml, dynamically detected and prepared by Gradle. You can see a summary of the tasks for running Alchemist by issuing ./gradlew tasks --all (or gradlew.bat tasks --all under Windows) and looking for the Run Alchemist task group.

Internally, the task relies on the Alchemist command-line interface. Alchemist simulations are YAML files, more information about them can be found here.

Info

Although Alchemist requires Java 11 or above to run, Gradle users can rely on any Gradle-compatible Java version and leverage the Gradle toolchains to obtain an Alchemist-compatible Java Virtual Machine.