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.architecture;
 
 
 import java.util.List;
Base agent architecture class that defines the overall agent architecture; the AS interpreter is the reasoner (a kind of mind) within this architecture (a kind of body).

The agent reasoning cycle (implemented in TransitionSystem class) calls these methods to get perception, action, and communication.

This class implements a Chain of Responsibilities design pattern. Each member of the chain is a subclass of AgArch. The last arch in the chain is the infrastructure tier (Centralised, JADE, Saci, ...). The getUserAgArch method returns the first arch in the chain. Users can customise the architecture by overriding some methods of this class.

 
 public class AgArch implements AgArchInfraTierComparable<AgArch> {
 
     private TransitionSystem ts = null;

    
Successor in the Chain of Responsibility
 
     private AgArch successor = null;    
     private AgArch firstArch = null;

    
the current cycle number, in case of sync execution mode
 
     private int cycleNumber = 0;
     
     public AgArch() {
          = this;
     }
    
    

Deprecated:
for arch initialisation you should override the init() method.
 
     public void initAg(String agClassClassParameters bbParsString asSrcSettings sttsthrows JasonException {
         //Agent.create(this, agClass, bbPars, asSrc, stts);
     }
         
     public void init() throws Exception {
     }
    
    
A call-back method called by the infrastructure tier when the agent is about to be killed.
 
     public void stop() {
         if ( != null)
             .stop();
     }
     
 
     // Management of the chain of responsibility
     
Returns the first architecture in the chain of responsibility pattern
 
     public AgArch getFirstAgArch() {
         return ;
     }
     public AgArch getNextAgArch() {
         return ;
    }
    public List<StringgetAgArchClassesChain() {
        List<Stringall = new ArrayList<String>();
        AgArch a = getFirstAgArch();
        while (a != null) {
            all.add(0,a.getClass().getName());
            a = a.getNextAgArch();
        }
        return all;
    }
 
    public void insertAgArch(AgArch arch) {
        if (arch != // to avoid loops
            arch.successor = ;
        if ( != null) {
            arch.ts = this.;
            .setAgArch(arch);
        }
        setFirstAgArch(arch);
    }
    private void setFirstAgArch(AgArch arch) {
         = arch;
        if ( != null)
            .setFirstAgArch(arch);
    }
    public void createCustomArchs(List<Stringarchsthrows Exception {
        if (archs == null)
            return;
        for (String agArchClassarchs) {
            // user custom arch
            if (!agArchClass.equals(AgArch.class.getName()) && !agArchClass.equals(CentralisedAgArch.class.getName())) {
                try {
                    AgArch a = (AgArch) Class.forName(agArchClass).newInstance();
                    a.setTS(); // so a.init() can use TS
                    a.initAg(nullnullnullnull); // for compatibility reasons
                    insertAgArch(a);                    
                    a.init();
                } catch (Exception e) {
                    ..println("Error creating custom agent aarchitecture."+e);
                    e.printStackTrace();
                    .getLogger().log(.,"Error creating custom agent aarchitecture.",e);
                }
            }
        }
    }

    
A call-back method called by TS when a new reasoning cycle is starting
    public void reasoningCycleStarting() {
        QueryProfiling q = getTS().getAg().getQueryProfiling();
        if (q != null)
            q.setNbReasoningCycles(getCycleNumber());
        if ( != null)
            .reasoningCycleStarting();
    }
    
   
    
returns the last arch in the chain, which is supposed to be the infra tier
 
    public AgArchInfraTier getArchInfraTier() {
        if (this. == null)
            return this;
        else
            return .getArchInfraTier();
    }
    
    public TransitionSystem getTS() {
        if ( != null)
            return ;
        if ( != null)
            return .getTS();
        return null;
    }
    
    public void setTS(TransitionSystem ts) {
        this. = ts;
        if ( != null)
            .setTS(ts);
    }

    
Gets the agent's perception as a list of Literals. The returned list will be modified by Jason.
    public List<Literalperceive() {
        if ( == null)
            return null;
        else
            return .perceive();
    }

    
Reads the agent's mailbox and adds messages into the agent's circumstance
    public void checkMail() {
        if ( != null)
            .checkMail();
    }

    
Executes the action action and, when finished, add it back in feedback actions.
    // TODO: remove feedback parameter and use getTS().addFeedbackAction in a new method of this class to be executed when the action is finished (as actionExecuted of centralised) 
    // for jason 2.0 to avoid backward compatibility problems
    public void act(ActionExec actionList<ActionExecfeedback) {
        if ( != null)
            .act(actionfeedback);
    }

    
Returns true if the agent can enter in sleep mode.
    public boolean canSleep() {
        return ( == null) || .canSleep();
    }

    
Puts the agent in sleep.
    public void sleep() {
        if ( != null)
            .sleep();
    }
    
    public void wake() {
        if ( != null)
            .wake();        
    }
        if ( == null)
            return null;
        else
            return .getRuntimeServices();        
    }
    
    
Gets the agent's name
    public String getAgName() {
        if ( == null)
            return "no-named";
        else
            return .getAgName();
    }

    
Sends a Jason message
    public void sendMsg(Message mthrows Exception {
        if ( != null)
            .sendMsg(m);
    }

    
Broadcasts a Jason message
    public void broadcast(Message mthrows Exception {
        if ( != null)
            .broadcast(m);
    }

    
Checks whether the agent is running
    public boolean isRunning() {
        return  == null || .isRunning();
    }

    
sets the number of the current cycle
    public void setCycleNumber(int cycle) {
         = cycle;
        if ( != null)
            .setCycleNumber(cycle);
    }
    
    public void incCycleNumber() {
        setCycleNumber(+1);
    }
    
    
gets the current cycle number
    public int getCycleNumber() {
        return ;
    }
    @Override
    public String toString() {
        return "arch-"+getAgName();
    }
    
    @Override
    public int hashCode() {
        return getAgName().hashCode();
    }
    
    @Override
    public boolean equals(Object obj) {
        if (obj == nullreturn false;
        if (obj == thisreturn true;
        if (obj instanceof AgArchreturn this.getAgName().equals(((AgArch)obj).getAgName());
        return false;
    }
    public int compareTo(AgArch o) {
        return getAgName().compareTo(o.getAgName());
    }
New to GrepCode? Check out our FAQ X