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: .succeed_goal(G).

Description: remove goals G from the agent circumstance as if a plan for such goal had successfully finished. G is a goal if there is a triggering event +!G in any plan within any intention; also note that intentions can be suspended hence appearing in E, PA, or PI as well.

Example:

  • .succeed_goal(go(X,3)): stops any attempt to achieve goals such as !go(1,3) as if it had already been achieved.
(Note: this internal action was introduced in a DALT 2006 paper, where it was called .dropGoal(G,true).)

 
 public class succeed_goal extends DefaultInternalAction {
 
     @Override public int getMinArgs() { return 1; }
     @Override public int getMaxArgs() { return 1; }
 
     @Override protected void checkArguments(Term[] argsthrows JasonException {
         super.checkArguments(args); // check number of arguments
         if (!args[0].isLiteral())
             throw JasonException.createWrongArgument(this,"first argument must be a literal");
     }
     
 
     @Override
     public Object execute(TransitionSystem tsUnifier unTerm[] argsthrows Exception {
         checkArguments(args);
         drop(ts, (Literal)args[0], un);
         return true;
     }
     
     public void drop(TransitionSystem tsLiteral lUnifier unthrows Exception {
         Trigger g = new Trigger(..l);
         Circumstance C = ts.getC();
         Unifier bak = un.clone();
         
         for (Intention iC.getIntentions()) {
             if (dropIntention(igtsun) > 1) {
                C.dropIntention(i);
                un = bak.clone();
            }
        }
        
        // dropping the current intention?
        dropIntention(C.getSelectedIntention(), gtsun);
        un = bak.clone();
            
        // dropping G in Events
        for (Event eC.getEvents()) {
            // test in the intention
            Intention i = e.getIntention();
            int r = dropIntention(igtsun);
            if (r > 0) {
                C.removeEvent(e);
                if (r == 1) {
                    C.resumeIntention(i);
                }
                un = bak.clone();
            } else {
                // test in the event
                Trigger t = e.getTrigger();
                if (i != . && !i.isFinished()) { //i.size() > 0) {
                    t = t.clone();
                    t.apply(i.peek().getUnif());
                }
                if (un.unifies(gt)) {                    
                    dropInEvent(ts,e,i);
                    un = bak.clone();
                }
            }
        }
        
        // dropping G in Pending Events
        for (String ekC.getPendingEvents().keySet()) {
            // test in the intention
            Event e = C.getPendingEvents().get(ek);
            Intention i = e.getIntention();
            int r = dropIntention(igtsun);
            if (r > 0) {
                C.removePendingEvent(ek);
                if (r == 1) {
                    C.resumeIntention(i);
                }
                un = bak.clone();
            } else {
                // test in the event
                Trigger t = e.getTrigger();
                if (i != . && !i.isFinished()) { //i.size() > 0) {
                    t = t.clone();
                    t.apply(i.peek().getUnif());
                }
                if (un.unifies(gt)) {
                    dropInEvent(ts,e,i);
                    un = bak.clone();
                }
            }
        }
        // dropping from Pending Actions
        for (ActionExec aC.getPendingActions().values()) {
            Intention i = a.getIntention();
            int r = dropIntention(igtsun);
            if (r > 0) { // i was changed
                C.removePendingAction(i.getId());  // remove i from PA
                if (r == 1) {                      // i must continue running
                    C.resumeIntention(i);          // and put the intention back in I
                }                                  // if r > 1, the event was generated and i will be back soon
                un = bak.clone();
            }
        }
        
        // dropping from Pending Intentions
        for (Intention iC.getPendingIntentions().values()) {
            int r = dropIntention(igtsun);
            if (r > 0) { 
                C.removePendingIntention(i.getId()); 
                if (r == 1) { 
                    C.resumeIntention(i); 
                }
                un = bak.clone();
            }
        }
    }
    
    /* returns: >0 the intention was changed
     *           1 = intention must continue running
     *           2 = fail event was generated and added in C.E
     *           3 = simply removed without event
     */
    int dropIntention(Intention iTrigger gTransitionSystem tsUnifier unthrows JasonException {
        if (i != null && i.dropGoal(gun)) {
            if (ts.hasGoalListener())
                for (GoalListener glts.getGoalListeners())
                    gl.goalFinished(g);
            // continue the intention
            if (!i.isFinished()) { // could be finished after i.dropGoal() !!
                if (ts.getC().getSelectedIntention() != i// if i is not the current intention, remove
                    i.peek().removeCurrentStep();
                ts.applyClrInt(i);
                return 1;
            } else {
                ts.applyClrInt(i);
                return 3;
            }
        }
        return 0;        
    }
    
    void dropInEvent(TransitionSystem tsEvent eIntention ithrows Exception {
        Circumstance C = ts.getC();
        C.removeEvent(e);
        if (i != null) {
            if (ts.hasGoalListener())
                for (GoalListener glts.getGoalListeners())
                    gl.goalFinished(e.getTrigger());
            i.peek().removeCurrentStep();
            ts.applyClrInt(i);
            C.addIntention(i);
        }
    }
New to GrepCode? Check out our FAQ X