Skip to content

Introduction To Gama Java API

RoiArthurB edited this page Sep 11, 2023 · 18 revisions

Introduction to GAMA Java API

This introduction to the Java API is dedicated to programmers that want to participate in the java code of GAMA. The main purpose is to describe the main packages and classes of the API to makes it simple to find such crucial information such as: how GAMA create containers, agent and geometries, how exceptions and log are managed, how java code maintain Type safety, etc.

Table of content

Concepts

  1. Factories
  2. Spatial
  3. Type
  4. IScope
  5. Exception
  6. Debug
  7. Test

Packages


Concepts


Factories

Container factories

GAMA provides 2 factories for containers: GamaListFactory and GamaMapFactory. Each of them has create methods to create objects of type IList and IMap. The types of elements in the container can be specified at creation using one of the elements defined in Types.

Warning: the create method is used to create the container, with elements of a given type, but it also converts elements added in this type. To avoid conversion (not recommended), use the method createWithoutCasting.

  1. GamaListFactory : factory to create list of different type (see Java class)

As an example:

IList<Double> distribution = GamaListFactory.create(Types.FLOAT);

To create List object without specifying the type, use Types.NO_TYPE:

IList<Object> result = GamaListFactory.create(Types.NO_TYPE);

or only:

IList<Object> result = GamaListFactory.create();
  1. GamaMapFactory : factory to create map of different type (see Java class)

As an example:

final IMap<String, IList<?>> ncdata = GamaMapFactory.create(Types.STRING, Types.LIST);

To create Map object without specifying the type, use Types.NO_TYPE:

IMap<Object, Object> result = GamaMapFactory.create(Types.NO_TYPE, Types.NO_TYPE);

or only:

IMap<Object, Object> result = GamaMapFactory.create();

If you want to use map or set, try to the best to rely on collection that ensure order, so to avoid unconsistency in container access. Try the most to avoid returning high order hash based collection, e.g. Set or Map; in this case, rely on standard definition in Gama:

  1. TOrderedHashMap : see trove api.

  2. TLinkedHashSet : see trove api

  3. Stream : you can use java build-in streams but there is a special version in Gama taken from StreamEx that should be preferred.

my_container.stream(my_scope)

If you want to get a stream back to a Gama container, you can use the collector in Factories:

my_container.stream(my_scope).collect(GamaListFactory.toGamaList())

Geometry factory

Gama geometry is based on the well established Jstor geometric library, while geographic aspect are handle using GeoTools library

  1. Spatial.Creation : provide several static method to initialize geometries

Spatial

The Spatial class provide several static access to the main methods to create, query, manipulate and transform geometries

Operators

Use as Spatial.Operators follow by the operator, usually one of Gaml language:

union, intersection, minus, and other cross geometry operations

Queries

closest, distance, overlapping, and other relative spatial relationship

Transpositions

enlarge, transpose, rotate, reduce and other specific transposition (like triangulation, squarification, etc.)

Punctal

operations relative to points

Type

IType: The main class to manipulate GamaType (main implementation of IType) is Types, that provides access to most common type manipulated in Gama

Opérateur de cast:

Types.get(IType.class)

IScope interface

An object of type IScope represents the context of execution of an agent (including experiments, simulations, and "regular" agents). Everywhere it is accessible (either passed as a parameter or available as an instance variable in some objects), it provides an easy access to a number of features: the current active agent, the shared random number generator, the global clock, the current simulation and experiment agents, the local variables declared in the current block, etc.

It also allows modifying this context, like changing values of local variables, adding new variables, although these functions should be reserved to very specific usages. Ordinarily, the scope is simply passed to core methods that allow to evaluate expressions, cast values, and so on.

Use of an IScope

A variable scope of type IScope can be used to:

  • get the current agent with: scope.getAgentScope()
IAgent agent = scope.getAgentScope();
  • evaluate an expression in the current scope:
String mes = Cast.asString(scope, message.value(scope));
  • know whether the scope has been interrupted:
boolean b = scope.interrupted();

Exception

Exceptions in GAMA

An exception that can appear in the GAMA platform can be run using the GamaRuntimeException class. This class allows throwing an error (using error(String,IScope) method) or a warning (using warning(String,IScope) method).

In particular, it can be useful to catch the Java Exception and to throw a GAMA exception.

try {
        ...
} catch(Exception e) {
	throw GamaRuntimeException.error("informative message", scope);
}

Debug

Main class for debug is in ummisco.gama.dev.utils : DEBUG

  • To turn GAMA Git version to debug mode change variable of the Debug class like: GLOBAL_OFF = false

  • Turn on or off the debug for one class: DEBUG.ON() or DEBUG.OFF()

  • You can benchmark a method call using : DEBUG.TIME("Title to log", () -> methodToBenchmark(...))

  • You can use different built-in level to print: DEBUG.ERR(string s) DEBUG.LOG(string s) DEBUG.OUT(Object message)

Test

There are Gaml primitives and statement to define test:

test "Operator + (1)" {
	assert (circle(5) + 5).height with_precision 1 = 20.0;
	assert (circle(5) + 5).location with_precision 9 = (circle(10)).location with_precision 9;
}

Everything can be made using Java Annotation (translated to Gaml test) :

examples = { @example (value="...",equals="..." )  }
test = { "..." } // don't forget to turn test arg of examples to false

Packages


Core

The main plugin of the GAMA Platform that defines the core functionalities: most Gaml operators, statements, skills, types, etc.

Metamodel


IAgent, IPopulation, IShape, ITopology,

Ouputs


Util


  1. Randomness in Gama: msi.gama.util.random

GamaRND is the main class that implements Random java class. It has several implementations and is mainly used with RandomUtils that define all the Gaml random operators

  1. Graph in Gama:

  2. File in Gama:

Operators

The packages where you can find all the operators defined in the core of Gama

  1. What's new (Changelog)
  1. Installation and Launching
    1. Installation
    2. Launching GAMA
    3. Updating GAMA
    4. Installing Plugins
  2. Workspace, Projects and Models
    1. Navigating in the Workspace
    2. Changing Workspace
    3. Importing Models
  3. Editing Models
    1. GAML Editor (Generalities)
    2. GAML Editor Tools
    3. Validation of Models
  4. Running Experiments
    1. Launching Experiments
    2. Experiments User interface
    3. Controls of experiments
    4. Parameters view
    5. Inspectors and monitors
    6. Displays
    7. Batch Specific UI
    8. Errors View
  5. Running Headless
    1. Headless Batch
    2. Headless Server
    3. Headless Legacy
  6. Preferences
  7. Troubleshooting
  1. Introduction
    1. Start with GAML
    2. Organization of a Model
    3. Basic programming concepts in GAML
  2. Manipulate basic Species
  3. Global Species
    1. Regular Species
    2. Defining Actions and Behaviors
    3. Interaction between Agents
    4. Attaching Skills
    5. Inheritance
  4. Defining Advanced Species
    1. Grid Species
    2. Graph Species
    3. Mirror Species
    4. Multi-Level Architecture
  5. Defining GUI Experiment
    1. Defining Parameters
    2. Defining Displays Generalities
    3. Defining 3D Displays
    4. Defining Charts
    5. Defining Monitors and Inspectors
    6. Defining Export files
    7. Defining User Interaction
  6. Exploring Models
    1. Run Several Simulations
    2. Batch Experiments
    3. Exploration Methods
  7. Optimizing Model Section
    1. Runtime Concepts
    2. Optimizing Models
  8. Multi-Paradigm Modeling
    1. Control Architecture
    2. Defining Differential Equations
  1. Manipulate OSM Data
  2. Diffusion
  3. Using Database
  4. Using FIPA ACL
  5. Using BDI with BEN
  6. Using Driving Skill
  7. Manipulate dates
  8. Manipulate lights
  9. Using comodel
  10. Save and restore Simulations
  11. Using network
  12. Headless mode
  13. Using Headless
  14. Writing Unit Tests
  15. Ensure model's reproducibility
  16. Going further with extensions
    1. Calling R
    2. Using Graphical Editor
    3. Using Git from GAMA
  1. Built-in Species
  2. Built-in Skills
  3. Built-in Architecture
  4. Statements
  5. Data Type
  6. File Type
  7. Expressions
    1. Literals
    2. Units and Constants
    3. Pseudo Variables
    4. Variables And Attributes
    5. Operators [A-A]
    6. Operators [B-C]
    7. Operators [D-H]
    8. Operators [I-M]
    9. Operators [N-R]
    10. Operators [S-Z]
  8. Exhaustive list of GAMA Keywords
  1. Installing the GIT version
  2. Developing Extensions
    1. Developing Plugins
    2. Developing Skills
    3. Developing Statements
    4. Developing Operators
    5. Developing Types
    6. Developing Species
    7. Developing Control Architectures
    8. Index of annotations
  3. Introduction to GAMA Java API
    1. Architecture of GAMA
    2. IScope
  4. Using GAMA flags
  5. Creating a release of GAMA
  6. Documentation generation

  1. Predator Prey
  2. Road Traffic
  3. 3D Tutorial
  4. Incremental Model
  5. Luneray's flu
  6. BDI Agents

  1. Team
  2. Projects using GAMA
  3. Scientific References
  4. Training Sessions

Resources

  1. Videos
  2. Conferences
  3. Code Examples
  4. Pedagogical materials
Clone this wiki locally