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: .intend(I).

Description: checks if I is an intention: I is an intention if there is a triggering event +!I in any plan within an intention; just note that intentions can be suspended and appear in E, PA, and PI as well.

Example:

  • .intend(go(1,3)): is true if a plan with triggering event +!go(1,3) appears in an intention of the agent.

 
 public class intend 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() && !args[0].isVar())
             throw JasonException.createWrongArgument(this,"first argument must be a literal or variable");
     }
 
     @Override
     public Object execute(TransitionSystem tsUnifier unTerm[] argsthrows Exception {
         checkArguments(args);
         return allIntentions(ts.getC(),(Literal)args[0],un);
     }
 
     /*
     public boolean intends(Circumstance C, Literal l, Unifier un) {
         Trigger g = new Trigger(TEOperator.add, TEType.achieve, l);
 
         // we need to check the intention in the selected event in this cycle!!!
         // (as it was already removed from E)
         if (C.getSelectedEvent() != null) {
             // logger.log(Level.SEVERE,"Int: "+g+" unif "+ts.C.SE);
             if (C.getSelectedEvent().getIntention() != null)
                 if (C.getSelectedEvent().getIntention().hasTrigger(g, un))
                    return true;
        }
        // we need to check the selected intention in this cycle too!!!
        if (C.getSelectedIntention() != null) {
            // logger.log(Level.SEVERE,"Int: "+g+" unif "+ts.C.SI);
            if (C.getSelectedIntention().hasTrigger(g, un))
                return true;
        }
        // intention may be in E
        for (Event evt : C.getEvents()) {
            if (evt.getIntention() != null && evt.getIntention().hasTrigger(g, un))
                return true;
        }
        
        // intention may be suspended in PE
        for (Event evt : C.getPendingEvents().values()) {
            if (evt.getIntention() != null && evt.getIntention().hasTrigger(g, un))
                return true;
        }
        // intention may be suspended in PA! (in the new semantics)
        if (C.hasPendingAction()) {
            for (ActionExec ac: C.getPendingActions().values()) {
                if (ac.getIntention().hasTrigger(g, un))
                    return true;
            }
        }
        // intention may be suspended in PI! (in the new semantics)
        if (C.hasPendingIntention()) {
            for (Intention intention: C.getPendingIntentions().values()) {
                if (intention.hasTrigger(g, un))
                    return true;
            }
        }
        for (Intention i : C.getIntentions()) {
            if (i.hasTrigger(g, un))
                return true;
        }
        return false;
    }
     */
    enum Step { selEvt, selInt, evt, pendEvt, pendAct, pendInt, intentions, end }
    //private static Logger logger = Logger.getLogger(intend.class.getName());
    public static Iterator<UnifierallIntentions(final Circumstance Cfinal Literal lfinal Unifier un) {
        final Trigger g = new Trigger(..l);
        
        return new Iterator<Unifier>() {
            Step curStep = .;
            Unifier solution = null// the current response (which is an unifier)
            Iterator<Event>      evtIterator     = null;
            Iterator<Event>      pendEvtIterator = null;
            Iterator<ActionExecpendActIterator = null;
            Iterator<Intention>  pendIntIterator = null;
            Iterator<Intention>  intInterator    = null;
            
            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;
            }
            public void remove() {}
            
            void find() {
                switch () {
                case :
                     = .// set next step
                    // we need to check the intention in the selected event in this cycle!!!
                    // (as it was already removed from E)
                    if (C.getSelectedEvent() != null) {
                        // logger.log(Level.SEVERE,"Int: "+g+" unif "+ts.C.SE);
                        if (C.getSelectedEvent().getIntention() != null) {
                             = un.clone();
                            if (C.getSelectedEvent().getIntention().hasTrigger(g))
                                return;
                        }
                    }
                    find();
                    return;
                case :
                     = .// set next step
                    // we need to check the selected intention in this cycle too!!!
                    if (C.getSelectedIntention() != null) {
                        // logger.log(Level.SEVERE,"Int: "+g+" unif "+ts.C.SI);
                         = un.clone();
                        if (C.getSelectedIntention().hasTrigger(g))
                            return;
                    }
                    find();
                    return;
                    
                case :                    
                    if ( == null)
                         = C.getEventsPlusAtomic();
                    
                    if (.hasNext()) {
                         = un.clone();
                        Event e = .next();
                        if (e.getIntention() != null && e.getIntention().hasTrigger(g))
                            return;
                    } else {
                         = .// set next step
                    }
                    find();
                    return;
                    
                case :                    
                    if ( == null)
                         = C.getPendingEvents().values().iterator();
                    
                    if (.hasNext()) {
                         = un.clone();
                        Event e = .next();
                        if (e.getIntention() != null && e.getIntention().hasTrigger(g))
                            return;
                    } else {
                         = .// set next step
                    }
                    find();
                    return;
                    
                case :                    
                    // intention may be suspended in PA! (in the new semantics)
                    if (C.hasPendingAction()) {
                        if ( == null)
                             = C.getPendingActions().values().iterator();
                        
                        if (.hasNext()) {
                             = un.clone();
                            ActionExec ac = .next();
                            if (ac.getIntention().hasTrigger(g))
                                return;
                        } else {
                             = .// set next step                                                
                        }
                    } else {
                         = .// set next step                    
                    }
                    find();
                    return;
                case :                    
                    // intention may be suspended in PI! (in the new semantics)
                    if (C.hasPendingIntention()) {
                        if ( == null)
                             = C.getPendingIntentions().values().iterator();
                        
                        if (.hasNext()) {
                             = un.clone();
                            Intention i = .next();
                            //System.out.println("try "+i+" for "+g+" = "+i.hasTrigger(g, solution));
                            if (i.hasTrigger(g))
                                return;
                        } else {
                             = .// set next step                                                
                        }
                    } else {
                         = .// set next step                    
                    }
                    find();
                    return;
                case :                    
                    if ( == null)
                         = C.getIntentionsPlusAtomic();
                    
                    if (.hasNext()) {
                         = un.clone();
                        Intention i = .next();
                        //logger.info("* try "+i+"\n"+intInterator.hasNext());
                        if (i.hasTrigger(g))
                            return;
                    } else {
                         = .// set next step
                    }
                    find();
                    return;
                    
                case :
                    
                }
                 = null// nothing found
            }
        };        
    }
    
New to GrepCode? Check out our FAQ X