Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package jason.stdlib;

Internal action: .suffix(S,L).

Description: checks if some list S is a suffix of list L. If S has free variables, this internal action backtracks all possible values for S.


  • +/- suffix (list): the suffix to be checked.
  • + list (list): the list where the suffix is from.


  • .suffix([c],[a,b,c]): true.
  • .suffix([a,b],[a,b,c]): false.
  • .suffix(X,[a,b,c]): unifies X with any suffix of the list, i.e., [a,b,c], [b,c], [c], and [] in this order.

 public class suffix extends DefaultInternalAction {
     private static final long serialVersionUID = 2463927564326061873L;
     private static InternalAction singleton = null;
     public static InternalAction create() {
         if ( == null
              = new suffix();
         return ;
     // Needs exactly 2 arguments
     @Override public int getMinArgs() { return 2; }
     @Override public int getMaxArgs() { return 2; } 
     // improve the check of the arguments to also check the type of the arguments
     @Override protected void checkArguments(Term[] argsthrows JasonException {
         super.checkArguments(args); // check number of arguments
         if (!args[0].isList() && !args[0].isVar())
             throw JasonException.createWrongArgument(this,"first argument must be a list or a variable");
         if (!args[1].isList()) 
             throw JasonException.createWrongArgument(this,"second argument must be a list");
     public Object execute(TransitionSystem tsfinal Unifier unTerm[] argsthrows Exception {
         // execute the internal action
         final Term sublist = args[0];
         final Iterator<ListTermlist = ((ListTerm)args[1]).listTermIterator();
         return new Iterator<Unifier>() {
             Unifier c = null// the current response (which is an unifier)
             public boolean hasNext() {
                 if ( == null// the first call of hasNext should find the first response 
                 return  != null
             public Unifier next() {
                 if ( == null
                 Unifier b = ;
                 find(); // find next response
                 return b;
            void find() {
                while (list.hasNext()) { 
                    ListTerm l =;
                    if (l.isVar()) // the case of the tail of the list
                     = un.clone();
                    if (.unifiesNoUndo(sublist, ASSyntax.createList(l))) {
                        return// found another sublist, c is the current response
                 = null// no more sublists found 
            public void remove() {}
New to GrepCode? Check out our FAQ X