DOCUMENTATION TUTORIALS DOWNLOAD NEWS CONTRIBUTE

Developing a New Skill

A skill adds new features (attributes) and new capabilities (actions) to the instances of the species that use it.

Defining the class

A Skill is basically a singleton and stateless Java class that:

Note: GAMA annotations are classes defined into the msi.gama.precompiler.GamlAnnotations class.

Defining new attributes

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);
}

Defining new actions

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);

Access to parameters in actions

To get the value of the arguments passed in GAML to the Java code, two methods can be useful:

Warnings

Developers should notice that:

Annotations

@skill

This annotations Allows to define a new skill (class grouping variables and actions that can be used by agents).

This annotation contains:

@var

This annotations is used to describe a single variable or field.

This annotation contains:

@doc

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.

@getter

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:

@setter

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:

@action

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:

@arg

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.