Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //----------------------------------------------------------------------------
  // Copyright (C) 2003  Rafael H. Bordini, Jomi F. Hubner, et al.
  // 
  // This library is free software; you can redistribute it and/or
  // modify it under the terms of the GNU Lesser General Public
  // License as published by the Free Software Foundation; either
  // version 2.1 of the License, or (at your option) any later version.
  // 
  // This library is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 // Lesser General Public License for more details.
 // 
 // You should have received a copy of the GNU Lesser General Public
 // License along with this library; if not, write to the Free Software
 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 // 
 // To contact the authors:
 // http://www.inf.ufrgs.br/~bordini
 // http://www.das.ufsc.br/~jomi
 //
 //----------------------------------------------------------------------------
 
 package jason.stdlib;
 

Internal action: .delete.

Description: delete elements of strings or lists.

Parameters:

  • + arg[0] (term, string, or number): if term, this arg is the element to be removed in the list (all occurrences of the element will be removed); if string, this arg is the substring to be removed (the second arg should be a string); if number, this arg is the position in the list/string of the element/character to be removed.
  • + arg[1] (list or string): the list/string where to delete.
  • +/- arg[2] (list or string): the list/string with the result of the deletion.

Examples:

  • .delete(a,[a,b,c,a],L): L unifies with [b,c].
  • .delete(a,[a,b,c,a],[c]): false.
  • .delete(0,[a,b,c,a],L): L unifies with [b,c,a].
  • .delete("a","banana",S): S unifies with "bnn".
  • .delete(0,"banana",S): S unifies with "anana".

 
 public class delete extends DefaultInternalAction {
 
     private static InternalAction singleton = null;
     public static InternalAction create() {
         if ( == null
              = new delete();
         return ;
     }
 
     @Override public int getMinArgs() { return 3; }
     @Override public int getMaxArgs() { return 3; }
 
     @Override
     public Object execute(TransitionSystem tsUnifier unTerm[] argsthrows Exception {
         checkArguments(args);
 
         if (args[0].isNumeric() && args[1].isString()) {
             return un.unifies(args[2], deleteFromString((int)((NumberTerm)args[0]).solve(),(StringTerm)args[1]));
         } 
         if (args[0].isNumeric() && args[1].isList()) {
             return un.unifies(args[2], deleteFromList((int)((NumberTerm)args[0]).solve(),(ListTerm)args[1]));
         }
         if (args[0].isString() && args[1].isString()) {
             return un.unifies(args[2], deleteFromString((StringTerm)args[0],(StringTerm)args[1]));
         }
         if (args[0].isString()) { // consider arg[1] as string
             return un.unifies(args[2], deleteFromString((StringTerm)args[0], new StringTermImpl(args[1].toString())));
        }
        
        
        // first element as term
        if (args[1].isList()) {
            return un.unifies(args[2], deleteFromList(args[0],(ListTerm)args[1], un.clone()));
        }
        throw new JasonException("Incorrect use of the internal action '.delete' (see documentation).");
    }
    
    ListTerm deleteFromList(Term elementListTerm lUnifier un) {
        Unifier bak = un;
        ListTerm r = new ListTermImpl();
        ListTerm last = r;
        for (Term tl) {
            boolean u = un.unifies(elementt); 
            if (u)
                un = bak.clone();
            else
                last = last.append(t.clone());
        }
        return r;
    }
    ListTerm deleteFromList(int indexListTerm l) {
        ListTerm r = new ListTermImpl();
        ListTerm last = r;
        int i = 0;
        for (Term tl) {
            if ((i++) != index)
                last = last.append(t.clone());
        }
        return r;
    }
    
    StringTerm deleteFromString(int indexStringTerm st) {
        try {
            String s = st.getString();
            s = s.substring(0,index) + s.substring(index+1);
            return new StringTermImpl(s);
        } catch (StringIndexOutOfBoundsException e) {
            return st;
        }
    }
        try {
            String s1 = st1.getString();
            String s2 = st2.getString();
            return new StringTermImpl(s2.replaceAll(s1""));
        } catch (StringIndexOutOfBoundsException e) {
            ..println(e);
            return st1;
        }
    }
New to GrepCode? Check out our FAQ X