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.infra.jade;
 
 import  jade.core.AID;
 import  jade.core.behaviours.CyclicBehaviour;
 import  jade.core.behaviours.OneShotBehaviour;
 import  jade.lang.acl.ACLMessage;
 
 
Concrete execution control implementation for Jade infrastructure.
 
 @SuppressWarnings("serial")
 public class JadeExecutionControl extends JadeAg implements ExecutionControlInfraTier {
 
     public static String controllerOntology = "AS-ExecControl";
 
     private ExecutionControl userControl;
     private ExecutorService executor// the thread pool used to execute actions
     
     @Override
     public void setup()  {
          = Logger.getLogger(JadeExecutionControl.class.getName());
         
         // create the user environment
         try {
             Object[] args = getArguments();
             if (args != null && args.length > 0) {
                 if (args[0] instanceof ClassParameters) { // it is an mas2j parameter
                     ClassParameters ecp = (ClassParameters)args[0];
                      = (ExecutionControl) Class.forName(ecp.getClassName()).newInstance();
                     .setExecutionControlInfraTier(this);
                     .init(ecp.getParametersArray());
                 } else {
                      = (ExecutionControl) Class.forName(args[0].toString()).newInstance();
                     .setExecutionControlInfraTier(this);
                     if (args.length > 1) {
                         .warning("Execution control arguments is not implemented yet (ask it to us if you need)!");
                     }
                 }
             } else {
                 .warning("Using default execution control.");
                  = new ExecutionControl();
                 .setExecutionControlInfraTier(this);
             }
         } catch (Exception e) {
             .log(."Error in setup Jade Environment"e);
         }
 
          = Executors.newFixedThreadPool(5);
         
         try {
             addBehaviour(new OneShotBehaviour() {
                 @Override
                 public void action() {
                     .updateNumberOfAgents();
                     informAllAgsToPerformCycle(0);
                     /*
                     executor.execute(new Runnable() {
                         public void run() {
                         }
                     });
                     */
                 }
             });
 
             addBehaviour(new CyclicBehaviour() {
                ACLMessage m;
                public void action() {
                     = receive();
                    if ( == null) {
                        block(1000);
                    } else {
                        try {
                            // check if the message is an agent state
                            @SuppressWarnings("unused")
                            Document o = (Document).getContentObject();
                            .warning("Received agState too late! in-reply-to:"+.getInReplyTo());
                        } catch (Exception _) {
                            try {
                                // check if the message is an end of cycle from some agent 
                                final String content = .getContent();
                                final int p = content.indexOf(",");
                                if (p > 0) {
                                    final String sender  = .getSender().getLocalName();
                                    final boolean breakpoint = Boolean.parseBoolean(content.substring(0,p));
                                    final int cycle = Integer.parseInt(content.substring(p+1));
                                    .execute(new Runnable() {
                                        public void run() {
                                            try {
                                                .receiveFinishedCycle(senderbreakpointcycle);
                                            } catch (Exception e) {
                                                .log(."Error processing end of cycle."e);                                              
                                            }
                                        }
                                    });
                                }
                            } catch (Exception e) {
                                .log(."Error in processing "+e);
                            }
                        }
                    }
                }
            });
            
        } catch (Exception e) {
            .log(."Error starting agent"e);
        }
    }
    @Override
    protected void takeDown() {
        if ( != null.stop();
    }
    
    public ExecutionControl getUserControl() {
        return ;
    }
    
    public void informAgToPerformCycle(final String agNamefinal int cycle) {
        addBehaviour(new OneShotBehaviour() {
            public void action() {
                ACLMessage m = new ACLMessage(ACLMessage.INFORM);
                m.setOntology();
                m.addReceiver(new AID(agName, AID.ISLOCALNAME));
                m.setContent("performCycle");
                m.addUserDefinedParameter("cycle", String.valueOf(cycle));
                send(m);
            }
        });
    }
    public void informAllAgsToPerformCycle(final int cycle) {
        addBehaviour(new OneShotBehaviour() {
            public void action() {
                try {
                    .fine("Sending performCycle "+cycle+" to all agents.");
                    ACLMessage m = new ACLMessage(ACLMessage.INFORM);
                    m.setOntology();
                    addAllAgsAsReceivers(m);
                    m.setContent("performCycle");
                    m.addUserDefinedParameter("cycle", String.valueOf(cycle));
                    send(m);
                } catch (Exception e) {
                    .log(."Error in informAllAgsToPerformCycle"e);
                }
            }
        });
    }
    public Document getAgState(final String agName) {
        if (agName == nullreturn null;
        
         = null;
        addBehaviour(new OneShotBehaviour() {
            public void action() {
                try {
                    ACLMessage m = new ACLMessage(ACLMessage.QUERY_REF);
                    m.setOntology();
                    m.addReceiver(new AID(agName, AID.ISLOCALNAME));
                    m.setContent("agState");
                    ACLMessage r = ask(m);
                    if (r == null) {
                        ..println("No agent state received! (possibly timeout in ask)");
                    } else {
                         = (Documentr.getContentObject();
                    }
                } catch (Exception e) {
                    .log(."Error in getAgState"e);
                } finally {
                    synchronized () {
                        .notifyAll();
                    }
                }
            }
        });
        return waitState();
    }
    
    private Document state = null;
    private Object syncWaitState = new Object();
    private Document waitState() {
        if ( == null) {
            synchronized () {
                try {
                    .wait();
                } catch (InterruptedException e) {}
            }
        }
        return ;
    }
        return new JadeRuntimeServices(getContainerController(), this);
    }
New to GrepCode? Check out our FAQ X