Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package jason.stdlib;
  
 
 import java.util.List;

Internal action: .sublist(S,L).

Description: checks if some list S is a sublist of list L. If S has free variables, this internal action backtracks all possible values for S. This is based on .prefix and .suffix (try prefixes first then prefixes of each suffix).

Parameters:

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

Examples:

  • .sublist([a],[a,b,c]): true.
  • .sublist([b],[a,b,c]): true.
  • .sublist([c],[a,b,c]): true.
  • .sublist([a,b],[a,b,c]): true.
  • .sublist([b,c],[a,b,c]): true.
  • .sublist([d],[a,b,c]): false.
  • .sublist([a,c],[a,b,c]): false.
  • .sublist(X,[a,b,c]): unifies X with any sublist of the list, i.e., [a,b,c], [a,b], [a], [b,c], [b], [c], and [] in this order; note that this is not the order in which its usual implementation would return in logic programming (see note on .prefix).

 
 public class sublist extends DefaultInternalAction {
     
     private static final long serialVersionUID = -1725808189703510112L;
     private static InternalAction singleton = null;
     public static InternalAction create() {
         if ( == null
              = new sublist();
         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");
     }
 
     @Override
     public Object execute(TransitionSystem tsfinal Unifier unfinal Term[] argsthrows Exception {
         checkArguments(args);
         final Term sublist = args[0];
         
         return new Iterator<Unifier>() {
             Unifier  c = null// the current response (which is an unifier)
             ListTerm listOutter = ((ListTerm)args[1]);
             List<Termlist = .getAsList(); // used in the inner loop, Java List is used for faster remove in the end 
             boolean  triedEmpty = false;
             
             public boolean hasNext() {
                 if ( == null// the first call of hasNext should find the first response 
                     find();
                 return  != null
             }
 
             public Unifier next() {
                if ( == nullfind();
                Unifier b = ;
                find(); // find next response
                return b;
            }
            
            void find() {
                while ( != null && !.isEmpty()) {
                    while (!.isEmpty()) {
                        ListTerm candidate = ASSyntax.createList();
                        .remove(.size()-1);
                         = un.clone();
                        if (.unifiesNoUndo(sublistcandidate)) {
                            return// found another sublist, c is the current response
                        }
                    }
                     = .getNext();
                    if ( == null || .isVar()) // the case of lists with tail
                        break;
                     = .getAsList(); 
                }
                if (!) {
                     = true;
                     = un.clone();
                    if (.unifiesNoUndo(sublist, ASSyntax.createList())) {
                        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