A skill adds new features (attributes) and new capabilities (actions) to the instances of the species that use it.
A Skill is basically a singleton and stateless Java class that:
Skill
,@skill(name = "name_of_the_skill_in_gaml")
.Note: GAMA annotations are classes defined into the msi.gama.precompiler.GamlAnnotations
class.
To add new attributes to the species that declares this skill, developers have to define them before the class using the @vars and @var
annotations.
The @vars
annotation contains a set of @var
elements.
In a @var element, one has to define the name, the type and the default value of the attribute. For example in MovingSkill:
@vars({
@var(name = IKeyword.SPEED, type = IType.FLOAT, init = "1.0"),
@var(name = IKeyword.HEADING, type = IType.INT, init = "rnd(359)")
})
In order to detail how to access these new attributes (if needed), developers have to define a getter (using @getter
) and a setter (using @setter
) methods.
If no getter (and setter) is defined, the attribute can nevertheless be set and get, using implicit by default getter and setter. But as soon as a getter and/or a setter is defined, they replace the implicit default ones.
For example:
@getter(var = IKeyword.SPEED)
public double getSpeed(final IAgent agent) {
return (Double) agent.getAttribute(IKeyword.SPEED);
}
@setter(IKeyword.SPEED)
public void setSpeed(final IAgent agent, final double s) {
agent.setAttribute(IKeyword.SPEED, s);
}
An action (also called primitive
) is basically a Java method that can be called from the GAML language using the same syntax as the one used for calling actions defined in a model. The method should be annotated with @action
, supplying the name of the action as it will be available in GAML.
The developer can also define parameters for this action using the annotation @arg
will a set of parameters names. For example, the action goto
of the MovingSkill is defined as follows:
@action(name="goto", args={
@arg(name = "target", type = { IType.AGENT, IType.POINT,IType.GEOMETRY }, optional = false),
@arg(name = IKeyword.SPEED, type = IType.FLOAT, optional = true),
@arg(name = "on", type = { IType.GRAPH }, optional = true)
}
)
public IPath primGoto(final IScope scope) throws GamaRuntimeException {
...
}
It is called in GAMA models with:
do goto (target: the_target, on: the_graph);
or
path path_followed <- self goto (target: the_target, on: the_graph, return_path: true);
To get the value of the arguments passed in GAML to the Java code, two methods can be useful:
scope.hasArg("name_of_argument")
returns a boolean value testing whether the argument “name_of_argument” has been defined by the modeler, since all the arguments to actions should be considered as optional.getArg(name_arg,IType)
, getFloatArg(name_param_of_float)
, getIntArg(name_param_of_int)
and their variants return the value of the given parameter using a given (or predefined) type to cast it.Developers should notice that:
GamaRuntimeException
s. Other exceptions should be caught in the method and wrapped in a GamaRuntimeException
before being thrown.This annotations Allows to define a new skill (class grouping variables and actions that can be used by agents).
This annotation contains:
This annotations is used to describe a single variable or field.
This annotation contains:
It provides a unified way of attaching documentation to the various GAML elements tagged by the other annotations. The documentation is automatically assembled at compile time and also used at runtime in GAML editors.
This annotations is used to indicate that a method is to be used as a getter for a variable defined in the class. The variable must be defined on its own (in vars).
This annotation contains:
This annotations is used to indicate that a method is to be used as a setter for a variable defined in the class. The variable must be defined on its own (in vars).
This annotation contains:
This annotations is used to tag a method that will be considered as an action (or primitive) in GAML.
The method must have the following signature:
Object methodName(IScope) throws GamaRuntimeException
and be contained in a class annotated with @species or @skill (or a related class, like a subclass or an interface).
This annotation contains:
This annotations describes an argument passed to an action.
This annotation contains:
All these annotations are defined in the GamlAnnotations.java
file of the msi.gama.processor
plug-in.