Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package jason.functions;
Wraps a rule into a function. For example the rule sum(X,Y,Z) :- Z = X+Y. is wrapped in a function sum(X,Y).

To define sum as a function the user should register it using a directive in the ASL code: { register_function("myf.sum",2,"sum") } where myf.sum is the name of the function, sum is the name of the rule (or literal, or internal action) and 2 is the function's arity.

public class RuleToFunction extends DefaultArithFunction  {
    private final String literal;
    private final int    arity;
    public RuleToFunction(String literalint arity) {
        this. = literal;
        this. = arity;
    public String getName() {
        return super.getName()+"_{"++"}";
    public boolean checkArity(int a) {
        return a == ;
    public boolean allowUngroundTerms() {
        return true;
    public double evaluate(TransitionSystem tsTerm[] argsthrows Exception {
        // create a literal to perform the query
        Literal r;
        if (.indexOf(".") > 0) // is internal action
            r = new InternalActionLiteral();
            r = new LiteralImpl();
        VarTerm answer = new VarTerm("__RuleToFunctionResult");
        // query the BB
        Iterator<Unifieri = r.logicalConsequence( (ts == null ? null : ts.getAg()), new Unifier());
        if (i.hasNext()) {
            Term value =;
            if (value.isNumeric())
                return ((NumberTerm)value).solve();
                throw new JasonException("The result of "+r+" (="+value+") is not numeric!");             
        } else 
            throw new JasonException("No solution was found for rule "+r);
    public String toString() { 
        return "function wrapper for "++"/"+;
New to GrepCode? Check out our FAQ X