My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
CreationSkill14  
How to develop a new skill.
Updated Jan 30, 2012 by benoit.g...@gmail.com



Introduction

A new skill adds new features (attributes) and new capabilities (actions) to an agent.

How to define a new skill

A Skill is basically a Java class that:

  • extends the abstract class Skill,
  • begins by the annotation @skill("name_of_the_skill_in_gaml").

How to define new attributes

To add new attributes to agents with this skill, developpers have to define them before the class thanks to @vars and @var annotations. The @vars annotation contains a set of @var elements.

In a @var element will define the name, the type and the init by default value of the parameter. For example in the MovingSkill:

@vars({
  @var(name = IKeyword.SPEED, type = IType.FLOAT_STR, init = "1.0"),
  @var(name = IKeyword.HEADING, type = IType.INT_STR, init = "rnd 359")
})

In order to access to these new attributes a an easy way in the GAML model, developpers have to define a getter (using @getter) and a setter (using @setter) method. 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);
}

How to define a new action

An action is basically a Java method that can be used from the GAML language. The method should by annotated by @action and the name of the action as it will be available in GAML.

The developper can also define parameters for this action using the annotation @args will a set of parameters names. For example, the action goto from the MovingSkill is defined as follows:

@action("goto")
@args({ "target", "speed", "on" })
public IPath primGoto(final IScope scope) throws GamaRuntimeException {
...
}

It is called in GAMA models with:

do action: goto {
   arg target value: the_target ;
   arg on value: the_graph;
}

or

let path_followed type: path value: self.goto [target::the_target, on::the_graph];

Access to parameters

To get parameters value in the Java code, two methods can be usefull:

  • scope.hasArg("name_of_parametre") returns a boolean value testing whether the attribute "name_of_parameter" has been used by the modeler,
  • getArg("name_param", IType), getFloatArg("name_param_of_float") or getIntArg("name_param_of_int") return the value of the given parameter.

Warnings

Developpers should notice that:

  • the method associated with an action has to return a non-void object.
  • such a method can only throws GamaRuntimeException. Other exceptions should be catch in the method.

Annotations

@skill

@vars and @var

@action

@args

@getter

@setter


Sign in to add a comment
Powered by Google Project Hosting