|
CreationSkill14
How to develop a new skill.
IntroductionA new skill adds new features (attributes) and new capabilities (actions) to an agent. How to define a new skillA Skill is basically a Java class that:
How to define new attributesTo 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 actionAn 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 parametersTo get parameters value in the Java code, two methods can be usefull:
WarningsDeveloppers should notice that:
Annotations@skill@vars and @var@action@args@getter@setter | |