Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //----------------------------------------------------------------------------
  // Copyright (C) 2003  Rafael H. Bordini and Jomi F. Hubner
  // 
  // 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.centralised;
 
 
 import java.io.File;
 import java.net.URL;
 import java.util.Map;
 import java.util.Set;
 
Runs MASProject using centralised infrastructure.
 
 public class RunCentralisedMAS {
 
     public final static String       logPropFile     = "logging.properties";
     public final static String       stopMASFileName = ".stop___MAS";
     public final static String       defaultProjectFileName = "default.mas2j";
 
     private   static Logger            logger        = Logger.getLogger(RunCentralisedMAS.class.getName());
     protected static RunCentralisedMAS runner        = null;
     private   static String            urlPrefix     = "";
     private   static boolean           readFromJAR   = false;
     private   static MAS2JProject      project;
     private   static boolean           debug         = false;
     
     private CentralisedEnvironment        env         = null;
     private CentralisedExecutionControl   control     = null;
     private Map<String,CentralisedAgArchags         = new ConcurrentHashMap<String,CentralisedAgArch>();
 
     public JButton                   btDebug;
     
     public RunCentralisedMAS() {
          = this;  
     }
     
     public static void main(String[] argsthrows JasonException {
          = new RunCentralisedMAS();
         .init(args);
         .create();
        .start();
        .waitEnd();
        .finish();
    }
        
    public int init(String[] args) {
        String projectFileName = null;
        if (args.length < 1) {
            if (RunCentralisedMAS.class.getResource("/"+) != null) {
                projectFileName = ;
                 = true;
                Config.get(false); // to void to call fix/store the configuration in this case everything is read from a jar/jnlp file
            } else {
                ..println("Jason "+Config.get().getJasonRunningVersion());
                ..println("You should inform the MAS project file.");
                JOptionPane.showMessageDialog(null,"Jason version "+Config.get().getJasonRunningVersion()+" library built on "+Config.get().getJasonBuiltDate(),"Jason".);
                System.exit(0);
            }
        } else {
            projectFileName = args[0];
        }
        setupLogger();
        if (args.length >= 2) {
            if (args[1].equals("-debug")) {
                 = true;
                Logger.getLogger("").setLevel(.);
            }
        }
        // discover the handler
        for (Handler h : Logger.getLogger("").getHandlers()) {
            // if there is a MASConsoleLogHandler, show it
            if (h.getClass().toString().equals(MASConsoleLogHandler.class.toString())) {
                MASConsoleGUI.get().getFrame().setVisible(true);
                MASConsoleGUI.get().setAsDefaultOut();
            }
        }
        int errorCode = 0;
        try {
            InputStream inProject;
            if () {
                inProject = RunCentralisedMAS.class.getResource("/"+).openStream();
                 = . + "/";
            } else {
                URL file;
                // test if the argument is an URL
                try {
                    file = new URL(projectFileName);
                    if (projectFileName.startsWith("jar")) {
                         = projectFileName.substring(0,projectFileName.indexOf("!")+1) + "/";
                    }
                } catch (Exception e) {
                    file = new URL("file:"+projectFileName);
                }
                inProject = file.openStream();
            }
            jason.mas2j.parser.mas2j parser = new jason.mas2j.parser.mas2j(inProject); 
             = parser.mas();
            .setupDefault();
            .registerDirectives();
            // set the aslSrcPath in the include
            ((Include)DirectiveProcessor.getDirective("include")).setSourcePath(.getSourcePaths());
            
            .fixAgentsSrc();
            if (MASConsoleGUI.hasConsole()) {
                MASConsoleGUI.get().setTitle("MAS Console - " + .getSocName());
                createButtons();
            }
            //runner.waitEnd();
            errorCode = 0;
        } catch (FileNotFoundException e1) {
            .log(."File " + projectFileName + " not found!");
            errorCode = 2;
        } catch (ParseException e) {
            .log(."Error parsing file " + projectFileName + "!"e);
            errorCode = 3;
        } catch (Exception e) {
            .log(."Error!?: "e);
            errorCode = 4;
        }
        
        ..flush();
        ..flush();
        if (!MASConsoleGUI.hasConsole() && errorCode != 0) {
            System.exit(errorCode);
        }
        return errorCode;
    }

    
create environment, agents, controller
    public void create() throws JasonException {
        createEnvironment();
        createAgs();
        createController();        
    }
    
    
start agents, ....
    public void start() {
        startAgs();
        startSyncMode();        
    }
    
    public static boolean isDebug() {
        return ;
    }
    public static synchronized void setupLogger() {
        if () {
            Handler[] hs = Logger.getLogger("").getHandlers(); 
            for (int i = 0; i < hs.lengthi++) { 
                Logger.getLogger("").removeHandler(hs[i]); 
            }
            Handler h = new MASConsoleLogHandler();
            h.setFormatter(new MASConsoleLogFormatter()); 
            Logger.getLogger("").addHandler(h);
            Logger.getLogger("").setLevel(.);
        } else {
            // checks a local log configuration file
            if (new File().exists()) {
                try {
                    LogManager.getLogManager().readConfiguration(new FileInputStream());
                } catch (Exception e) {
                    ..println("Error setting up logger:" + e);
                }
            } else {
                try {
                    LogManager.getLogManager().readConfiguration(RunCentralisedMAS.class.getResource("/templates/" + ).openStream());
                } catch (Exception e) {
                    ..println("Error setting up logger:" + e);
                    e.printStackTrace();
                }
            }
        }
    }
    
    public static void setupDefaultConsoleLogger() {
        Handler[] hs = Logger.getLogger("").getHandlers(); 
        for (int i = 0; i < hs.lengthi++) { 
            Logger.getLogger("").removeHandler(hs[i]); 
        }
        Handler h = new ConsoleHandler();
        h.setFormatter(new MASConsoleLogFormatter()); 
        Logger.getLogger("").addHandler(h);
        Logger.getLogger("").setLevel(.);        
    }
    protected void createButtons() {
        createStopButton();
        // add Button pause
        createPauseButton();
        // add Button debug
        . = new JButton("Debug"new ImageIcon(RunCentralisedMAS.class.getResource("/images/debug.gif")));
            public void actionPerformed(ActionEvent evt) {
                .changeToDebugMode();
                ..setEnabled(false);
                if (. != null) {
                    try {
                        ..getUserControl().setRunningCycle(false);
                    } catch (Exception e) { }
                }
            }
        });
        if () {
            ..setEnabled(false);
        }
        MASConsoleGUI.get().addButton(.);
        // add show sources button
        final JButton btShowSrc = new JButton("Sources"new ImageIcon(RunCentralisedMAS.class.getResource("/images/list.gif")));
        btShowSrc.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                showProjectSources();
            }
        });
        MASConsoleGUI.get().addButton(btShowSrc);
        // add Button start
        final JButton btStartAg = new JButton("New agent"new ImageIcon(RunCentralisedMAS.class.getResource("/images/newAgent.gif")));
        btStartAg.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                new StartNewAgentGUI(MASConsoleGUI.get().getFrame(), "Start a new agent to run in current MAS", System.getProperty("user.dir"));
            }
        });
        MASConsoleGUI.get().addButton(btStartAg);
        // add Button kill
        final JButton btKillAg = new JButton("Kill agent"new ImageIcon(RunCentralisedMAS.class.getResource("/images/killAgent.gif")));
        btKillAg.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                new KillAgentGUI(MASConsoleGUI.get().getFrame(), "Kill an agent of the current MAS");
            }
        });
        MASConsoleGUI.get().addButton(btKillAg);        
    }
    protected void createPauseButton() {
        final JButton btPause = new JButton("Pause"new ImageIcon(RunCentralisedMAS.class.getResource("/images/resume_co.gif")));
        btPause.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                if (MASConsoleGUI.get().isPause()) {
                    btPause.setText("Pause");
                    MASConsoleGUI.get().setPause(false);
                } else {
                    btPause.setText("Continue");
                    MASConsoleGUI.get().setPause(true);
                }
            }
        });
        MASConsoleGUI.get().addButton(btPause);
    }
    protected void createStopButton() {
        // add Button
        JButton btStop = new JButton("Stop"new ImageIcon(RunCentralisedMAS.class.getResource("/images/suspend.gif")));
        btStop.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                MASConsoleGUI.get().setPause(false);
                .finish();
            }
        });
        MASConsoleGUI.get().addButton(btStop);
    }
    
    public static RunCentralisedMAS getRunner() {
        return ;
    }
        return ;
    }
        return ;
    }
    
    public MAS2JProject getProject() {
        return ;
    }
    public void createEnvironment() throws JasonException {
        .fine("Creating environment " + .getEnvClass());
         = new CentralisedEnvironment(.getEnvClass(), this);
    }
    
    public void createAgs() throws JasonException {
        boolean isPool = .getInfrastructure().hasParameter("pool");
        if (isPool.info("Creating agents....");
        int nbAg = 0;
        Agent pag = null;
        
        // create the agents
        for (AgentParameters ap : .getAgents()) {
            try {
                
                String agName = ap.name;
                for (int cAg = 0; cAg < ap.qtycAg++) {
                    nbAg++;
                    
                    String numberedAg = agName;
                    if (ap.qty > 1) {
                        numberedAg += (cAg + 1);
                        // cannot add zeros before, it causes many compatibility problems and breaks dynamic creation 
                        // numberedAg += String.format("%0"+String.valueOf(ap.qty).length()+"d", cAg + 1);
                    }
                    .fine("Creating agent " + numberedAg + " (" + (cAg + 1) + "/" + ap.qty + ")");
                    CentralisedAgArch agArch;
                    if (isPool) {
                        agArch = new CentralisedAgArchForPool();
                    } else {
                        agArch = new CentralisedAgArch();
                    }
                    agArch.setAgName(numberedAg);
                    agArch.setEnvInfraTier();
                    if (isPool && cAg > 0) {
                        // creation by cloning previous agent (which is faster -- no parsing, for instance)
                        agArch.createArchs(ap.getAgArchClasses(), pagthis);
                    } else {
                        // normal creation
                        agArch.createArchs(ap.getAgArchClasses(), ap.agClass.getClassName(), ap.getBBClass(), ap.asSource.toString(), ap.getAsSetts(.getControlClass() != null), this);
                    }
                    addAg(agArch);
                    
                    pag = agArch.getTS().getAg();
                }
            } catch (Exception e) {
                .log(."Error creating agent " + ap.namee);
            }
        }
        
        if (isPool.info("Created "+nbAg+" agents.");
    }
    public void createController() throws JasonException {
        ClassParameters controlClass = .getControlClass();
        if ( && controlClass == null) {
            controlClass = new ClassParameters(ExecutionControlGUI.class.getName());
        }
        if (controlClass != null) {
            .fine("Creating controller " + controlClass);
             = new CentralisedExecutionControl(controlClassthis);
        }        
    }
    
    public void addAg(CentralisedAgArch ag) {
        .put(ag.getAgName(), ag);
    }
    public CentralisedAgArch delAg(String agName) {
        return .remove(agName);
    }
    
    public CentralisedAgArch getAg(String agName) {
        return .get(agName);
    }
    
    public Map<String,CentralisedAgArchgetAgs() {
        return ;
    }
    
    protected void startAgs() {
        // run the agents
        if (.getInfrastructure().hasParameter("pool")) {
            createThreadPool();
        } else {
            createAgsThreads();
        }
    }
    
    
creates one thread per agent
    private void createAgsThreads() {
        for (CentralisedAgArch ag : .values()) {
            ag.setControlInfraTier();
            
            // create the agent thread
            Thread agThread = new Thread(ag);
            ag.setThread(agThread);
            agThread.start();
        }        
    }
    
    private ExecutorService executor;
    
    
creates a pool of threads shared by all agents
    private void createThreadPool() {
         = Collections.synchronizedSet(new HashSet<CentralisedAgArch>());
        int maxthreads = 10;
        try {
            if (.getInfrastructure().hasParameters()) {
                maxthreads = Integer.parseInt(.getInfrastructure().getParameter(1));
                .info("Creating a thread pool with "+maxthreads+" thread(s).");
            }
        } catch (Exception e) {
            .warning("Error getting the number of thread for the pool.");
        }
        // define pool size
        int poolSize = .size();
        if (poolSize > maxthreads) {
            poolSize = maxthreads;
        }
        
        // create the pool
         = Executors.newFixedThreadPool(poolSize);
        // initially, add all agents in the tasks
        for (CentralisedAgArch ag : .values()) {
            .execute(ag);
        }
        /*new Thread("monitor") {
            public void run() {
                while (runner != null) {
                    try {
                        System.out.println("#ag:"+ags.size());
                        System.out.println("#slepping ags:"+mySleepAgs.size());
                        try {
                            ThreadPoolExecutor tp = (ThreadPoolExecutor)executor;
                            System.out.println("#queue:"+tp.getQueue().size());
                            System.out.println("#active:"+tp.getActiveCount());
                        } catch (Exception e) { }
                        sleep(3000);
                    } catch (InterruptedException e) { }
                }
            }            
        }.start();*/
        
        /*
        new Thread("wakeup") {
            public void run() {
                while (runner != null) {
                    try {
                        sleep(1000);
                        for (CentralisedAgArch ar: sleepingAgs) {
                            if (!ar.getTS().canSleep())
                                ar.wake();
                        }
                    } catch (Exception e) { 
                        System.out.println("**");
                    }
                }
            }            
        }.start();*/
    }
    
    
an agent architecture for the infra based on thread pool
    private final class CentralisedAgArchForPool extends CentralisedAgArch {
        private volatile boolean runWakeAfterTS = false;
        
        @Override
        public void sleep() {
            //if (sleepingAgs.contains(this))
            //    System.out.println("*** ops already slepping "+this);
            .add(this);     
        }
        @Override
        public void wake() {                
            if (.remove(this)) {
                /*try {
                    ThreadPoolExecutor tp = (ThreadPoolExecutor)executor;
                    if (tp.getQueue().contains(this)) {
                        System.out.println("ops... ading ag that is already in the pool "+this);
                    }
                } catch (Exception e) { }*/                
                .execute(this);
            } else {
                 = true;
            }
        }
        
        /*@Override
        public void receiveMsg(final Message m) {
            executor.execute(new Runnable() {                
                public void run() {
                    CentralisedAgArchForPool.super.receiveMsg(m);
                }
            });
        }*/
        
        @Override
        public void run() {
            if (isRunning()) { 
                 = false;
                if (getTS().reasoningCycle()) { // the agent run a cycle (did not enter in sleep)
                    /*try {
                        ThreadPoolExecutor tp = (ThreadPoolExecutor)executor;
                        if (tp.getQueue().contains(this)) {
                            System.out.println("*** ops... ading ag that is already in the pool "+this);
                        }
                    } catch (Exception e) { }*/
                    .execute(this);
                } else if () {
                    wake();
                }
            }
        }        
    }
    
    protected void stopAgs() {
        // stop the agents
        for (CentralisedAgArch ag : .values()) {
            ag.stopAg();
        }
    }

    
change the current running MAS to debug mode
    void changeToDebugMode() {
        try {
            if ( == null) {
                 = new CentralisedExecutionControl(new ClassParameters(ExecutionControlGUI.class.getName()), this);
                for (CentralisedAgArch ag : .values()) {
                    ag.setControlInfraTier();
                    Settings stts = ag.getTS().getSettings();
                    stts.setVerbose(2);
                    stts.setSync(true);
                    ag.getLogger().setLevel(.);
                    ag.getTS().getLogger().setLevel(.);
                    ag.getTS().getAg().getLogger().setLevel(.);
                }
            }
        } catch (Exception e) {
            .log(."Error entering in debug mode"e);
        }
    }
    protected void startSyncMode() {
        if ( != null) {
            // start the execution, if it is controlled
            try {
                Thread.sleep(500); // gives a time to agents enter in wait
                .informAllAgsToPerformCycle(0);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public void waitEnd() {
        try {
            // wait a file called .stop___MAS to be created!
            File stop = new File();
            if (stop.exists()) {
                stop.delete();
            }
            while (!stop.exists()) {
                Thread.sleep(1500);
                /*
                boolean allSleep = true;
                for (CentralisedAgArch ag : ags.values()) {
                    //System.out.println(ag.getAgName()+"="+ag.canSleep());
                    allSleep = allSleep && ag.canSleep();
                }
                if (allSleep)
                    break;
                */
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void finish() {
        
        try {
            // creates a thread that guarantees system.exit(0) in 5 seconds
            // (the stop of agents can  block)
            new Thread() {
                public void run() {
                    try {
                        sleep(5000);
                    } catch (InterruptedException e) {}
                    System.exit(0);
                }
            }.start();
            
            ..flush();
            ..flush();
            if (MASConsoleGUI.hasConsole()) { // should close first! (case where console is in pause)
                MASConsoleGUI.get().close();
            }
            if ( != null) {
                .stop();
                 = null;
            }
            if ( != null) {
                .stop();
                 = null;
            }
            
            stopAgs();
             = null;
            
            // remove the .stop___MAS file  (note that GUI console.close(), above, creates this file)
            File stop = new File();
            if (stop.exists()) {
                stop.delete();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.exit(0);
    }
    
    
show the sources of the project
    private static void showProjectSources(MAS2JProject project) {
        JFrame frame = new JFrame("Project "+project.getSocName()+" sources");
        JTabbedPane pane = new JTabbedPane();
        frame.getContentPane().add(pane);
        project.fixAgentsSrc();
        for (AgentParameters ap : project.getAgents()) {
            try {
                String tmpAsSrc = ap.asSource.toString();
                
                // read sources
                InputStream in = null;
                if (tmpAsSrc.startsWith(.)) {
                    in = RunCentralisedMAS.class.getResource(tmpAsSrc.substring(..length())).openStream();
                } else {
                    try {
                        in = new URL(tmpAsSrc).openStream(); 
                    } catch (MalformedURLException e) {
                        in = new FileInputStream(tmpAsSrc);
                    }
                }
                StringBuilder s = new StringBuilder();
                int c = in.read();
                while (c > 0) {
                    s.append((char)c);
                    c = in.read();
                }
                
                // show sources
                JTextArea ta = new JTextArea(40,50);
                ta.setEditable(false);
                ta.setText(s.toString());
                ta.setCaretPosition(0);
                JScrollPane sp = new JScrollPane(ta);
                pane.add(ap.namesp);
            } catch (Exception e) {
                .info("Error:"+e);
            }
        }
        frame.pack();
        frame.setVisible(true);
    }
New to GrepCode? Check out our FAQ X