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: .wait(E,T).

Description: suspend the intention for the time specified by T (in milliseconds) or until some event E happens. The events follow the AgentSpeak syntax but are enclosed by { and }, e.g. {+bel(33)}, {+!go(X,Y)}.

Parameters:

  • + event (trigger term): the event to wait for.
  • + timeout (number).

Examples:

  • .wait(1000): suspend the intention for 1 second.
  • .wait({+b(1)}): suspend the intention until the belief b(1) is added in the belief base.
  • .wait({+!g}, 2000): suspend the intention until the goal g is triggered or 2 seconds have passed, whatever happens first. In case the event does not happens in two seconds, the internal action fails.
  • .wait({+!g}, 2000, EventTime): suspend the intention until the goal g is triggered or 2 seconds have passed, whatever happens first. As this use of .wait has three arguments, in case the event does not happen in two seconds, the internal action does not fail (as in the previous example). The third argument will be unified to the elapsed time (in miliseconds) from the start of .wait until the event or timeout.

See also:
at
 
 public class wait extends DefaultInternalAction {
 
     public static final String waitAtom = ".wait"
 
     @Override public boolean canBeUsedInContext() {  return false;  }
     @Override public boolean suspendIntention() { return true;  } 
     
     @Override public int getMinArgs() { return 1; }
     @Override public int getMaxArgs() { return 3; }
 
     @Override
     public Object execute(final TransitionSystem tsUnifier unTerm[] argsthrows Exception {
         checkArguments(args);
         
         long timeout = -1;
         Trigger te = null;
         Term elapsedTime = null;
         
        if (args[0].isNumeric()) {
            // time in milliseconds
            NumberTerm time = (NumberTerm)args[0];
            timeout = (longtime.solve();
        } else { // if (args[0].isString()) 
            // wait for event
            te = Trigger.tryToGetTrigger(args[0]); //ASSyntax.parseTrigger( ((StringTerm) args[0]).getString());
                                                   //te.getLiteral().apply(un);
            
            if (args.length >= 2)
                timeout = (long) ((NumberTermargs[1]).solve();
            if (args.length == 3)
                elapsedTime = args[2];
        }
        new WaitEvent(teuntstimeoutelapsedTime);
        return true;
    }    
    class WaitEvent implements CircumstanceListener { 
        private Trigger          te;
        private String           sTE// a string version of TE
        private Unifier          un;
        private Intention        si;
        private TransitionSystem ts;
        private Circumstance     c;
        private boolean          dropped = false;
        private Term             elapsedTimeTerm;
        private long             startTime;
        
        WaitEvent(Trigger teUnifier unTransitionSystem tslong timeoutTerm elapsedTimeTerm) {
            this. = te;
            this. = un;
            this. = ts;
             = ts.getC();
             = .getSelectedIntention();
            this. = elapsedTimeTerm;
            // register listener
            .addEventListener(this);
            
            if (te != null) {
                 = te.toString();
            } else {
                 = "time"+(timeout);
            }
             = .getId()+"/"+;
            .addPendingIntention();
            
             = System.currentTimeMillis();
            if (timeout > 0) {
                ts.getAg().getScheduler().schedule(new Runnable() {
                    public void run() {
                        resume(true);
                    }
                }, timeout.);
            }
        }
        void resume(final boolean stopByTimeout) {
            // unregister (to not receive intentionAdded again)
            .removeEventListener(this);
            // invoke changes in C latter, so to avoid concurrent changes in C
            .runAtBeginOfNextCycle(new Runnable() {
                public void run() {
                    try {
                        // add SI again in C.I if it was not removed and this wait was not dropped
                        if (.removePendingIntention() ==  && !.getIntentions().contains() && !) {
                            if (stopByTimeout &&  != null &&  == null) {
                                // fail the .wait by timeout
                                if (.isSuspended()) { // if the intention was suspended by .suspend
                                    PlanBody body = .peek().getPlan().getBody();
                                    body.add(1, new PlanBodyImpl(.new InternalActionLiteral(".fail")));
                                    .addPendingIntention(.+.getId(), );
                                } else {
                                    .generateGoalDeletion(, JasonException.createBasicErrorAnnots("wait_timeout""timeout in .wait"));
                                }
                            } else {
                                .peek().removeCurrentStep();
                                
                                if ( != null) {
                                    long elapsedTime = System.currentTimeMillis() - ;
                                    .unifies(new NumberTermImpl(elapsedTime));
                                }
                                if (.isSuspended()) { // if the intention was suspended by .suspend
                                    .addPendingIntention(.+.getId(), );
                                } else {
                                    .resumeIntention();
                                }
                            }
                        }    
                    } catch (Exception e) {
                        .getLogger().log(."Error at .wait thread"e);
                    }
                }
            });
            .getUserAgArch().wake();
        }
        public void eventAdded(Event e) {
            if ( != null && ! && .unifies(e.getTrigger())) {
                resume(false);
            }
        }
        public void intentionDropped(Intention i) {
            if (i.equals()) {
                 = true;
                resume(false);
            }
        }
        public void intentionAdded(Intention i) { }
        public void intentionResumed(Intention i) { }
        public void intentionSuspended(Intention iString reason) { }
        public String toString() {
            return ;
        }
    }
New to GrepCode? Check out our FAQ X