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

Description: suspend goals G, i.e., all intentions trying to achieve G will stop running until the internal action .resume change the state of those intentions. A literal G is a goal if there is a triggering event +!G in any plan within any intention in I, E, PI, or PA.

Examples:

  • .suspend(go(1,3)): suspends intentions to go to the location 1,3.
  • .suspend: suspends the current intention.

 
 public class suspend extends DefaultInternalAction {
     
     boolean suspendIntention = false;
     public static final String SUSPENDED_INT      = "suspended-";
     public static final String SELF_SUSPENDED_INT = +"self-";
 
     @Override public int getMinArgs() { return 0; }
     @Override public int getMaxArgs() { return 1; }
 
     @Override protected void checkArguments(Term[] argsthrows JasonException {
         super.checkArguments(args); // check number of arguments
         if (args.length == 1 && !args[0].isLiteral())
             throw JasonException.createWrongArgument(this,"first argument must be a literal");
     }
 
     @Override
     public Object execute(TransitionSystem tsUnifier unTerm[] argsthrows Exception {
         checkArguments(args);
 
          = false;
         
         Circumstance C = ts.getC();
 
         if (args.length == 0) {
             // suspend the current intention
             Intention i = C.getSelectedIntention();
              = true;
             i.setSuspended(true);
             C.addPendingIntention(i);
            return true;
        }
        
        // use the argument to select the intention to suspend.
        
        Trigger      g = new Trigger(.., (Literal)args[0]);
        // ** Must test in PA/PI first since some actions (as .suspend) put intention in PI
        
        // suspending from Pending Actions
        for (ActionExec aC.getPendingActions().values()) {
            Intention i = a.getIntention();
            if (i.hasTrigger(gun)) {
                i.setSuspended(true);
                C.addPendingIntention(+i.getId(), i);
            }
        }
        
        // suspending from Pending Intentions
        for (Intention iC.getPendingIntentions().values()) {
            if (i.hasTrigger(gun)) { 
                i.setSuspended(true);
            }
        }
        for (Intention iC.getIntentions()) {
            if (i.hasTrigger(gun)) {
                i.setSuspended(true);
                C.removeIntention(i);
                C.addPendingIntention(+i.getId(), i);
                //System.out.println("sus "+g+" from I "+i.getId()+" #"+C.getPendingIntentions().size());
            }
        }
        
        // suspending the current intention?
        Intention i = C.getSelectedIntention();
        if (i.hasTrigger(gun)) {
             = true;
            i.setSuspended(true);
            C.addPendingIntention(+i.getId(), i);
        }
            
        // suspending G in Events
        int c = 0;
        for (Event eC.getEvents()) {
            i = e.getIntention();
            if (un.unifies(ge.getTrigger()) || (i != null && i.hasTrigger(gun))) {
                C.removeEvent(e);
                C.addPendingEvent(+e.getTrigger()+(c++), e);
                if (i != null)
                    i.setSuspended(true);                
                //System.out.println("sus "+g+" from E "+e.getTrigger());
            }
            
            /*
            if ( i != null && 
                    (i.hasTrigger(g, un) ||       // the goal is in the i's stack of IM
                     un.unifies(g, e.getTrigger())  // the goal is the trigger of the event
                    )
                ) {
                i.setSuspended(true);
                C.removeEvent(e);                    
                C.addPendingIntention(k, i);
            } else if (i == Intention.EmptyInt && un.unifies(g, e.getTrigger())) { // the case of !!
                // creates an intention to suspend the "event"
                i = new Intention();
                i.push(new IntendedMeans(
                        new Option(
                                new Plan(null, e.getTrigger(), Literal.LTrue, 
                                        new PlanBodyImpl(BodyType.achieveNF, e.getTrigger().getLiteral())), 
                                new Unifier()), 
                        e.getTrigger()));
                e.setIntention(i);
                i.setSuspended(true);
                C.removeEvent(e);                    
                C.addPendingIntention(k, i);
            }
            */                
        }
        
        // TODO: consider the case of suspending something that is already suspended. 
        return true;
    }
    @Override
    public boolean suspendIntention() {
        return ;
    }
New to GrepCode? Check out our FAQ X