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.io.File;
 import java.util.List;
 import java.util.Map;
 
 
ArArch that displays the mind state of the agent
 
 public class MindInspectorAgArch extends AgArch {
     
     // variables for mind inspector
     protected boolean hasMindInspectorByCycle = false;
     protected int     updateInterval = 0;
     protected static JFrame         mindInspectorFrame = null;
     protected static JTabbedPane    mindInspectorTab = null;
     protected        JTextPane      mindInspectorPanel = null;
     protected        JSlider        mindInspectorHistorySlider = null;
     protected        JCheckBox      mindInspectorFreeze = null;
     protected        List<DocumentmindInspectorHistory = null;    
     protected        asl2xml        mindInspectorTransformer = null;
 
     protected        String         mindInspectorDirectory;
 
     // Which item is to be shown in HTML interface
     Map<String,Booleanshow = new HashMap<String,Boolean>();
 
     // what is currently shown
     Document agState = null;
     
     MindInspectorWeb webServer = null;
     boolean hasHistory = false;
     
     @Override
     public void init() {
         setupMindInspector(getTS().getSettings().getUserParameter("mindinspector"));
     }

    
A call-back method called by the infrastructure tier when the agent is about to be killed.
 
    @Override
    public void stop() {
        if ( != null)
            .dispose();
        super.stop();
    }
    @Override
    public void reasoningCycleStarting() {
        if ()
            addAgState();
        super.reasoningCycleStarting();
    }
    
    
    
    
process the mindinspector parameter used in the agent option in .mas2j project. E.g. agents bob x.asl [mindinspector="gui(cycle,html)"]; General syntax of the parameter: [gui|file|web] ( [ cycle|number ] , [xml,html] [, history | directory] )
    protected void setupMindInspector(String configuration) {
        Structure sConf = null;
        try {
            sConf = ASSyntax.parseStructure(configuration);
        } catch (Exception e) {
            getTS().getLogger().warning("The mindinspector argument does not parse as a predicate! "+configuration+" --  (see Jason FAQ) -- error: "+e);
            return;
        }
        
        // get the frequency of updates
         = sConf.getTerm(0).toString().equals("cycle");
        if (! ) {
            try {
                 = (int)((NumberTerm)sConf.getTerm(0)).solve();
            } catch (NoValueException e1) {
                e1.printStackTrace();
            }
            new Thread("update agent mind inspector") {
                public void run() {
                    try {
                        while (isRunning()) {
                            Thread.sleep();
                            addAgState();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                };
            }.start();
        }
                
        if (sConf.getFunctor().equals("gui")) {
            createGUIMindInspector(sConf);
        } else if (sConf.getFunctor().equals("file")) {
            createFileMindInspector(sConf);            
        } else if (sConf.getFunctor().equals("web")) {
            createWebMindInspector(sConf);
        }
    }
    
    private void createGUIMindInspector(Structure sConf) {
        // assume html output
        String format = "text/html";
        if ( == null) { // Initiate the common window
             = new JFrame(.);
               = new JTabbedPane(.);
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            .setBounds(100, 200, (int)((screenSize.width-100)*0.7), (int)((screenSize.height-100)*0.9));
            .setVisible(true);
        }
            
         = new JTextPane();
        .setEditable(false);
        .setContentType(format);
            public void hyperlinkUpdate(HyperlinkEvent evt) {
                hyperLink(evt);
            }
        });
        .put("bels"true);
        .put("annots", Config.get().getBoolean(.));
        .put("rules"false);
        .put("evt"true);
        .put("mb"false);
        .put("int"false);
        .put("int-details"false);
        .put("plan"false);
        .put("plan-details"false);
        // get history
        boolean hasHistory = sConf.getArity() == 3 && sConf.getTerm(2).toString().equals("history");
        if (! hasHistory) {
            .add(getAgName(), new JScrollPane());
        } else {
             = new ArrayList<Document>();
            JPanel pHistory = new JPanel(new BorderLayout());//new FlowLayout(FlowLayout.CENTER));
            pHistory.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Agent History"..));
             = new JSlider();
            .setMaximum(1);
            .setMinimum(0);
            .setValue(0);
            .setPaintTicks(true);
            .setPaintLabels(true);
            setupSlider();
                public void stateChanged(ChangeEvent e) {
                    try {
                        int c = (int).getValue();
                        showAgState(.get(c));
                    } catch (Exception e2) {                        }
                }
            });
            pHistory.add(.);
            
             = new JCheckBox();
            JPanel pf = new JPanel(new FlowLayout());
            pf.add();
            pf.add(new JLabel("freeze"));
            pHistory.add(.pf);
            
            JPanel pAg = new JPanel(new BorderLayout());
            pAg.add(.new JScrollPane());
            pAg.add(.pHistory);
            .add(getAgName(), pAg);
        }
        
        if (format.equals("text/html")) {
             = new asl2html("/xml/agInspection.xsl");
        }
    }
    
    private void hyperLink(HyperlinkEvent evt) {
        if (evt.getEventType() == ..) {
            String uri = "show?";
            int pos = evt.getDescription().indexOf(uri);
            if (pos >= 0) {
                String par = evt.getDescription().substring(pos+uri.length());
                .put(par,true);
            } else {
                uri = "hide?";
                pos = evt.getDescription().indexOf(uri);
                if (pos >= 0) {
                    String par = evt.getDescription().substring(pos+uri.length());
                    .put(par,false);
                }
            }
            showAgState();
        }
    }
    private void setupSlider() {
        int size = .size()-1;
        if (size < 0)
            return;
        
        Hashtable<Integer,ComponentlabelTable = new Hashtable<Integer,Component>();
        labelTable.put( 0, new JLabel("mind 0") );
        labelTable.putsizenew JLabel("mind "+size) );
        .setLabelTablelabelTable );
        //mindInspectorHistorySlider.setValue(size);
    }
    
    private void createFileMindInspector(Structure sConf) {
        if (sConf.getArity() <= 2)
             = "log";
        else
             = sConf.getTerm(2).toString();
        
        // assume xml output
         = new asl2xml();
        
        // create directories
         += "/"+getAgName();
        File dirmind = new File(); 
        if (!dirmind.exists()) // create agent dir
            dirmind.mkdirs();
        // create a directory for this execution
        int c = 0;
        String d = +"/run-"+c;
        while (new File(d).exists()) {
            d = +"/run-"+(c++);
        }
         = d;
        new File().mkdirs();
    }
    private void createWebMindInspector(Structure sConf) {
         = MindInspectorWeb.get();
         = sConf.getArity() == 3 && sConf.getTerm(2).toString().equals("history");
         = new asl2html("/xml/agInspection.xsl");
    }
    
    private String lastHistoryText = "";
    private int    fileCounter = 0;
    
    protected void addAgState() {
        try {
            Document state = getTS().getAg().getAgState(); // the XML representation of the agent's mind
            String sMind = getAgStateAsString(statetrue);
            if (sMind.equals()) 
                return;
             = sMind;
            if ( != null) { // output on GUI
                if ( == null || !.isSelected())
                    showAgState(state);
                if ( != null) {
                    .add(state);
                    setupSlider(); 
                    .setValue(.size()-1);
                }
            } else if ( != null) { // output on file
                String filename = String.format("%6d.xml",++).replaceAll(" ","0");
                FileWriter outmind = new FileWriter(new File(+"/"+filename));
                outmind.write(sMind);
                outmind.close();
            } else if ( != null) { // output on web
                .addAgState(getTS().getAg(), state);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private String previousShownText = "";    
    
show current agent state
    void showAgState(Document state) { // in GUI
        if (state != null) {
            try {
                String sMind = getAgStateAsString(statefalse);
                if (sMind.equals()) 
                    return// nothing to log
                 = sMind;
                 = state;
                .setText(sMind); // show the HTML in the screen
            } catch (Exception e) {
                .setText("Error in XML transformation!" + e);
                e.printStackTrace();
            }
        }        
    }
    
    String getAgStateAsString(Document agboolean full) { // full means with show all
        try {
            for (String p.keySet())
                if (full)
                    .setParameter("show-"+p"true");
                else
                    .setParameter("show-"+p.get(p)+"");
            return .transform(ag); // transform to HTML
        } catch (Exception e) {
            if ( != null) {
                .setText("Error in XML transformation!" + e);
            }
            e.printStackTrace();
            return "Error XML transformation (MindInspector)";
        }
    }
New to GrepCode? Check out our FAQ X