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.

Author(s):
Jomi
public class RuleToFunction extends DefaultArithFunction  {
    private final String literal;
    private final int    arity;
    
    public RuleToFunction(String literalint arity) {
        this. = literal;
        this. = arity;
    }
    
    @Override
    public String getName() {
        return super.getName()+"_{"++"}";
    }
    
    @Override
    public boolean checkArity(int a) {
        return a == ;
    }
    
    @Override
    public boolean allowUngroundTerms() {
        return true;
    }
    
    @Override
    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();
        else
            r = new LiteralImpl();
        
        r.addTerms(args);
        VarTerm answer = new VarTerm("__RuleToFunctionResult");
        r.addTerm(answer);
        
        // query the BB
        Iterator<Unifieri = r.logicalConsequence( (ts == null ? null : ts.getAg()), new Unifier());
        if (i.hasNext()) {
            Term value = i.next().get(answer);
            if (value.isNumeric())
                return ((NumberTerm)value).solve();
            else
                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