Firstly, to create an internal action the easier way is by jedit where there is a button for it. It is near the buttons to create project, agent and environment. In the case of internal action a window ask for a package name, where we can give a group of actions meaning, like "format", and another field asks for an action name, I chose "formatCurrency".
After this, JEdit will create a folder called "format" with a java file called "formatCurrency.java" with a template content. Bellow the final code of the format method and some comments:
package format;
import jason.asSemantics.*;
import jason.asSyntax.*;
public class formatCurrency extends DefaultInternalAction {
private static final long serialVersionUID = 1L;
@Override
public Object execute(TransitionSystem ts, Unifier un,
Term[] args) throws Exception {
StringTerm str = new StringTermImpl(
String.format("%.2f", Float.valueOf(args[0].toString()))
);
un.unifies(str, args[1]);
return true;
}
}
As we can see, this class extends "DefaultInternalAction" overriding execute method which run when the internal action "formatCurrency" is invoked in an AgentSpeak code (a sample invoke code is shown bellow). For this example we are using the params Term[] and Unifier which represents the list o terms that the function accepts, which is here the original float number in args[0] and the result formatted string of input number with only two decimals in args[1]. The way to "write" this output is by unification as shown in the code "un.unifies(str, args[1])".
In the code bellow, the new internal action formatCurrency is being invoked. The input valuw in this example is a constant and output variable is "Price", the one that is being unified.
!test.
+!test : true <-
format.formatCurrency(3.66667,Price);
.print("$",Price).