Start line:  
End line:  

Snippet Preview

Snippet HTML Code

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

Internal action: .prefix(P,L).

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

Parameters:

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

Examples:

  • .prefix([a],[a,b,c]): true.
  • .prefix([b,c],[a,b,c]): false.
  • .prefix(X,[a,b,c]): unifies X with any prefix of the list, i.e., [a,b,c], [a,b], [a], and [] in this order; note that this is different from what its usual implementation in logic programming would result, where the various prefixes are returned in increasing lengths instead.

 
 public class prefix extends DefaultInternalAction {
     
     private static final long serialVersionUID = -4736810884249871078L;
     private static InternalAction singleton = null;
     public static InternalAction create() {
         if ( == null
              = new prefix();
         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 unTerm[] argsthrows Exception {
 
         checkArguments(args);
 
         // execute the internal action
 
         final Term sublist = args[0];
         final List<Termlist = ((ListTerm)args[1]).getAsList(); // use a Java List for better performance in remove last
         
         return new Iterator<Unifier>() {
             Unifier c = null// the current response (which is an unifier)
             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 (!list.isEmpty()) {
                    ListTerm candidate = ASSyntax.createList(list);
                    list.remove(list.size()-1);
                     = un.clone();
                    if (.unifiesNoUndo(sublistcandidate)) {
                        return// found another sublist, c is the current response
                    }
                }
                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