Repository navigation
OperatorsDefinition
Operators in GAML compose expressions. An operator takes one or more operands and produces a value; see the documentation for the individual operator for its behavior and effects.
Most of them use a classical prefixed functional syntax (i.e. operator_name(operand1, operand2, operand3), see below), with the exception of arithmetic (e.g. +, /), logical (and, or), comparison (e.g. >, <), access (., [..]) and pair (::) operators, which require an infixed notation (i.e. operand1 operator_symbol operand1).
The ternary functional if-else operator, ? :, uses a special infixed syntax composed with two symbols (e.g. operand1 ? operand2 : operand3). Two unary operators (- and !) use a traditional prefixed syntax that does not require parentheses unless the operand is itself a complex expression (e.g. - 10, ! (operand1 or operand2)).
Finally, special constructor operators ({...} for constructing points, [...] for constructing lists and maps) will require their operands to be placed between their two symbols (e.g. {1,2,3}, [operand1, operand2, ..., operandn] or [key1::value1, key2::value2... keyn::valuen]).
With the exception of these special cases above, the following rules apply to the syntax of operators:
- if they only have one operand, the functional prefixed syntax is mandatory (e.g.
operator_name(operand1)) - if they have two arguments, either the functional prefixed syntax (e.g.
operator_name(operand1, operand2)) or the infixed syntax (e.g.operand1 operator_name operand2) can be used. - if they have more than two arguments, either the functional prefixed syntax (e.g.
operator_name(operand1, operand2, ..., operand)) or a special infixed syntax with the first operand on the left-hand side of the operator name (e.g.operand1 operator_name(operand2, ..., operand)) can be used.
All of these alternative syntaxes are completely equivalent.
Do not assume every operator is free of side effects. For example, shuffle(container) returns a new shuffled container, while delete_file(path) changes the filesystem. See the operator reference for the documented behavior of each operator. Actions used as operators can also have side effects, as described below.
The priority of operators determines, in the case of complex expressions composed of several operators, which one(s) will be evaluated first.
The grammar defines precedence from lower to higher: conditional ?:, or, and, casts with as, comparisons, addition and subtraction, multiplication and division, powers, custom infix operators, units, unary operators, and member/index access and function calls. Parentheses make the intended grouping explicit.
Pair construction with :: has a special rule: arithmetic expressions are grouped on its left, and its right side can contain a full expression. Add parentheses when combining pairs with comparisons or conditional expressions. List, map and point constructors use their own delimiters ([...] and {...}) rather than infix precedence.
Actions defined in species can be used as operators, provided they are called on the correct agent. The syntax is that of normal functional operators, but the agent that will perform the action must be added as the first operand.
For instance, if the following species is defined:
species spec1 {
int min(int x, int y) {
return x > y ? x : y;
}
}
Any agent instance of spec1 can use min as an operator (if the action conflicts with an existing operator, a warning will be emitted). For instance, in the same model, the following line is perfectly acceptable:
global {
init {
create spec1;
spec1 my_agent <- spec1[0];
int the_min <- my_agent min(10,20); // or min(my_agent, 10, 20);
}
}
If the action doesn't have any operands, the syntax to use is my_agent the_action(). Finally, if it does not return a value, it might still be used but is considering as returning a value of type unknown (e.g. unknown result <- my_agent the_action(op1, op2);).
Note that due to the fact that actions are written by modelers, the general functional contract is not respected in that case: actions might perfectly have side effects on their operands (including the agent).
- Installation and Launching
- Workspace, Projects and Models
- Editing Models
- Running Experiments
- Running Headless
- Exporting models and projects
- Preferences
- gama platform agent
- Troubleshooting
- Introduction
- Manipulate basic Species
- Global Species
- Defining Advanced Species
- Defining GUI Experiment
- Classes and Objects
- Exploring Models
- Optimizing Models
- Multi-Paradigm Modeling
- Manipulate OSM Data
- Cleaning OSM Data
- Diffusion
- Using Database
- Using Dataframes
- Using FIPA ACL
- Using BDI with BEN
- Using Driving Skill
- Manipulate dates
- Manipulate lights
- Using comodel
- Save and restore Simulations
- Using network
- Writing Unit Tests
- Ensure model's reproducibility
- Going further with extensions
- Built-in Species
- Built-in Skills
- Built-in Architecture
- Statements
- Data Type
- File Type
- Stats Extension
- Sound Extension
- BDI Extension
- FIPA Skill
- Expressions
- Exhaustive list of GAMA Keywords
- Installing the development version
- Introduction to GAMA Java API
- Developing Extensions
- Using GAMA flags
- Export plugin : Technical Documentation
- Creating a release of GAMA
- Documentation generation
- Desktop startup and deferred initialization