Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package jason.stdlib;
  
 

Internal action: .member(T,L).

Description: checks if some term T is in a list L. If T is a free variable, this internal action backtracks all possible values for T.

Parameters:

  • +/- member (term): the term to be checked.
  • + list (list): the list where the term should be in.

Examples:

  • .member(c,[a,b,c]): true.
  • .member(3,[a,b,c]): false.
  • .member(X,[a,b,c]): unifies X with any member of the list.

 
 public class member extends DefaultInternalAction {
     
     private static InternalAction singleton = null;
     public static InternalAction create() {
         if ( == null
              = new member();
         return ;
     }
     
     @Override public int getMinArgs() { return 2; }
     @Override public int getMaxArgs() { return 2; }
 
     @Override protected void checkArguments(Term[] argsthrows JasonException {
         super.checkArguments(args); // check number of arguments
         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);
 
         final Term member = args[0];
         final Iterator<Termi = ((ListTerm)args[1]).iterator();
 
         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 
                     find();
                 return  != null
             }
 
             public Unifier next() {
                 if ( == nullfind();
                 Unifier b = ;
                 find(); // find next response
                 return b;
             }
             
             void find() {
                 while (i.hasNext()) {
                      = un.clone();
                     if (.unifiesNoUndo(memberi.next()))
                         return// member found in the list, c is the current response
                 }
                  = null// no member is found, 
             }
            public void remove() {}
        };
        
        /* -- old version of the implementation
         * -- problem: even if the user wants only the first member, if search all
        List<Unifier> answers = new ArrayList<Unifier>();
        Unifier newUn = (Unifier)un.clone(); // clone un so as not to change it
        for (Term t: lt) {
            if (newUn.unifies(member, t)) {
                // add this unification to the  answers
                answers.add(newUn);
                newUn = (Unifier)un.clone(); // creates a new clone of un
            }
        }                
        return answers.iterator();
        */
    }
New to GrepCode? Check out our FAQ X