Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package jason.stdlib;
 

Internal action: .range(Var,Start,End).

Description: backtrack all values for Var starting at Start and finishing at End.

Parameters:

  • +/- var (Variable): the variable that unifies with all values.
  • + start (number): initial value.
  • + end (number): last value.

Examples:

  • .range(3,1,5): true.
  • .range(6,1,5): false.
  • .range(X,1,5): unifies X with 1, 2, 3, 4, and 5.

See also:
foreach for
public class range extends DefaultInternalAction {
    
    private static InternalAction singleton = null;
    public static InternalAction create() {
        if ( == null
             = new range();
        return ;
    }
    @Override public int getMinArgs() { return 3; }
    @Override public int getMaxArgs() { return 3; }
    @Override protected void checkArguments(Term[] argsthrows JasonException {
        super.checkArguments(args); // check number of arguments
        if (!args[1].isNumeric())
            throw JasonException.createWrongArgument(this,"second parameter ('" + args[1] + "') must be a number!");
        if (!args[2].isNumeric())
            throw JasonException.createWrongArgument(this,"third parameter ('" + args[2] + "') must be a number!");
    }
    
    @Override
    public Object execute(TransitionSystem tsfinal Unifier unTerm[] argsthrows Exception {
        checkArguments(args);
        final int start = (int)((NumberTerm)args[1]).solve();
        final int end   = (int)((NumberTerm)args[2]).solve();
        if (!args[0].isVar()) {
            // first arg is not a var
            int vl = (int)((NumberTerm)args[0]).solve();
            return vl >= start && vl <= end;
        } else {
            // first arg is a var, backtrack
            final Term var = args[0];
            return new Iterator<Unifier>() {
                int vl = start-1;
                
                public boolean hasNext() {
                    return  < end;
                }
                public Unifier next() {
                    ++;
                    Unifier c = un.clone();
                    c.unifiesNoUndo(var,new NumberTermImpl());
                    return c;
                }
                
                public void remove() {}
            };
        }
    }
New to GrepCode? Check out our FAQ X