From: <jom...@us...> - 2014-06-06 19:20:22
|
Revision: 1788 http://sourceforge.net/p/jason/svn/1788 Author: jomifred Date: 2014-06-06 19:20:11 +0000 (Fri, 06 Jun 2014) Log Message: ----------- improve web mind inspector Modified Paths: -------------- trunk/examples/auction/auction.mas2j trunk/src/jason/architecture/MindInspectorAgArch.java trunk/src/jason/architecture/MindInspectorWeb.java trunk/src/jason/asSemantics/Agent.java trunk/src/jason/infra/centralised/RunCentralisedMAS.java trunk/src/jason/infra/jade/JadeMASLauncherAnt.java trunk/src/jason/jeditplugin/Config.java Modified: trunk/examples/auction/auction.mas2j =================================================================== --- trunk/examples/auction/auction.mas2j 2014-06-03 20:05:43 UTC (rev 1787) +++ trunk/examples/auction/auction.mas2j 2014-06-06 19:20:11 UTC (rev 1788) @@ -6,7 +6,6 @@ agents: ag1; ag2; - ag3 [mindinspector="web(cycle,html,history)"]; // two agents with web view of their minds - auctioneer [mindinspector="web(cycle,html,history)"] - agentArchClass AuctioneerGUI; + ag3 [mindinspector="gui(cycle,html,history)"]; // this agent has a GUI mind inspector + auctioneer agentArchClass AuctioneerGUI; } Modified: trunk/src/jason/architecture/MindInspectorAgArch.java =================================================================== --- trunk/src/jason/architecture/MindInspectorAgArch.java 2014-06-03 20:05:43 UTC (rev 1787) +++ trunk/src/jason/architecture/MindInspectorAgArch.java 2014-06-06 19:20:11 UTC (rev 1788) @@ -118,7 +118,7 @@ * E.g. agents bob x.asl [mindinspector="gui(cycle,html)"]; * * General syntax of the parameter: - * [gui|file] ( [ cycle|number ] , [xml,html] [, history | directory] ) + * [gui|file|web] ( [ cycle|number ] , [xml,html] [, history | directory] ) */ protected void setupMindInspector(String configuration) { Structure sConf = null; Modified: trunk/src/jason/architecture/MindInspectorWeb.java =================================================================== --- trunk/src/jason/architecture/MindInspectorWeb.java 2014-06-03 20:05:43 UTC (rev 1787) +++ trunk/src/jason/architecture/MindInspectorWeb.java 2014-06-06 19:20:11 UTC (rev 1788) @@ -37,12 +37,15 @@ //private Map<String,Boolean> agHasHistory = new HashMap<String, Boolean>(); private Map<String,List<Document>> histories = new TreeMap<String,List<Document>>(); private Map<String,Integer> lastStepSeenByUser = new HashMap<String, Integer>(); + private Map<String,Agent> registeredAgents = new HashMap<String, Agent>(); public static synchronized MindInspectorWeb get() { if (singleton == null) { singleton = new MindInspectorWeb(); singleton.startHttpServer(); singleton.registerRootBrowserView(); + singleton.registerAgentsBrowserView(); + singleton.registerAgView("no_ag"); } return singleton; } @@ -83,15 +86,81 @@ OutputStream responseBody = exchange.getResponseBody(); if (requestMethod.equalsIgnoreCase("GET")) { - responseBody.write(("<html><head><title>Jason Mind Inspector -- Web View</title><meta http-equiv=\"refresh\" content=\""+refreshInterval+"\" ></head><body>").getBytes()); - responseBody.write(("<h2>Agents</h2><ul>").getBytes()); + String path = exchange.getRequestURI().getPath(); + StringWriter so = new StringWriter(); + + if (path.length() < 2) { // it is the root + so.append("<html><head><title>Jason Mind Inspector -- Web View</title></head><body>"); + so.append("<iframe width=\"20%\" height=\"100%\" align=left src=\"/agents\" border=5 frameborder=0 ></iframe>"); + so.append("<iframe width=\"78%\" height=\"100%\" align=left src=\"/agent-mind/no_ag\" name=\"am\" border=5 frameborder=0></iframe>"); + so.append("</body></html>"); + } else if (path.indexOf("agent-mind") >= 0) { + if (tryToIncludeMindInspectorForAg(path)) + so.append("<meta http-equiv=\"refresh\" content=0>"); + else + so.append("unkown agent!"); + } + responseBody.write(so.toString().getBytes()); + } + responseBody.close(); + } + }); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private String getAgNameFromPath(String path) { + int nameStart = path.indexOf("agent-mind")+11; + int nameEnd = path.indexOf("/",nameStart+1); + if (nameEnd >= 0) + return path.substring(nameStart,nameEnd).trim(); + else + return path.substring(nameStart).trim(); + } + + private boolean tryToIncludeMindInspectorForAg(String path) { + try { + Agent ag = registeredAgents.get(getAgNameFromPath(path)); + if (ag != null) { + AgArch arch = ag.getTS().getUserAgArch(); + if (arch != null) { + // should add a new conf for mindinspector, otherwise will start a new gui for the agent + arch.getTS().getSettings().addOption("mindinspector","web(cycle,html,history)"); + MindInspectorAgArch miArch = new MindInspectorAgArch(); + arch.insertAgArch(miArch); + miArch.init(); + miArch.addAgState(); + return true; + } + } + } catch (Exception e) { + e.printStackTrace(); + } + return false; + } + + private void registerAgentsBrowserView() { + if (httpServer == null) + return; + try { + httpServer.createContext("/agents", new HttpHandler() { + public void handle(HttpExchange exchange) throws IOException { + String requestMethod = exchange.getRequestMethod(); + Headers responseHeaders = exchange.getResponseHeaders(); + responseHeaders.set("Content-Type", "text/html"); + exchange.sendResponseHeaders(200, 0); + OutputStream responseBody = exchange.getResponseBody(); + + if (requestMethod.equalsIgnoreCase("GET")) { + responseBody.write(("<html><head><title>Jason (list of agents)</title><meta http-equiv=\"refresh\" content=\""+refreshInterval+"\" ></head><body>").getBytes()); + responseBody.write(("<font size=\"+2\"><p style='color: red; font-family: arial;'>Agents</p></font>").getBytes()); for (String a: histories.keySet()) { - responseBody.write( ("<li><a href=\"/agent-mind/"+a+"/latest\">"+a+"</a>").getBytes()); - //responseBody.write( (" <a href=\"/agent-code/"+a+"\">(code)</a></li>").getBytes()); - responseBody.write( ("</li>").getBytes()); + responseBody.write( ("- <a href=\"/agent-mind/"+a+"/latest\" target=\"am\" style=\"font-family: arial; text-decoration: none\">"+a+"</a><br/>").getBytes()); } - responseBody.write("</ul><hr/><a href=\"http://jason.sf.net\">Jason</a></body></html>".getBytes()); } + responseBody.write("<hr/>by <a href=\"http://jason.sf.net\" target=\"_blank\">Jason</a>".getBytes()); + responseBody.write("</body></html>".getBytes()); responseBody.close(); } }); @@ -100,15 +169,22 @@ } } + /** add the agent in the list of available agent for mind inspection */ + public synchronized void registerAg(Agent ag) { + String agName = ag.getTS().getUserAgArch().getAgName(); + registeredAgents.put(agName, ag); + histories.put(agName, new ArrayList<Document>()); // just for the agent name to appear in the list of agents + } + public synchronized void addAgState(Agent ag, Document mind, boolean hasHistory) { String agName = ag.getTS().getUserAgArch().getAgName(); List<Document> h = histories.get(agName); if (h == null) { h = new ArrayList<Document>(); - registerAgView(agName); - //registerAgCodeBrowserView(agName, "not implemented"); histories.put(agName, h); } + if (h.isEmpty()) + registerAgView(agName); // the first time a state is added for the agent, register in the browser if (hasHistory || h.isEmpty()) h.add(mind); else @@ -132,63 +208,73 @@ try { StringWriter so = new StringWriter(); so.append("<html><head><title>"+agName+"</title>"); - List<Document> h = histories.get(agName); - if (h != null) { - Document agState; - int i = -1; - exchange.getRemoteAddress(); - - String path = exchange.getRequestURI().getPath(); - String query = exchange.getRequestURI().getRawQuery(); // what follows ? - String remote = exchange.getRemoteAddress().toString(); - - if (path.endsWith("hide")) { - show.put(query,false); - Integer ii = lastStepSeenByUser.get(remote); - if (ii != null) - i = ii; - } else if (path.endsWith("show")) { - show.put(query,true); - Integer ii = lastStepSeenByUser.get(remote); - if (ii != null) - i = ii; - } else if (path.endsWith("clear")) { - agState = h.get(h.size()-1); - h.clear(); - h.add(agState); - } else { - // see if ends with a number + + // test if the url is for this agent + String path = exchange.getRequestURI().getPath(); + if (!getAgNameFromPath(path).equals(agName)) { + if (tryToIncludeMindInspectorForAg(path)) + so.append("<meta http-equiv=\"refresh\" content=0>"); + else + so.append("unkown agent!"); + } else { + + List<Document> h = histories.get(agName); + if (h != null && h.size() > 0) { + Document agState; + int i = -1; + exchange.getRemoteAddress(); + + String query = exchange.getRequestURI().getRawQuery(); // what follows ? + String remote = exchange.getRemoteAddress().toString(); + + if (path.endsWith("hide")) { + show.put(query,false); + Integer ii = lastStepSeenByUser.get(remote); + if (ii != null) + i = ii; + } else if (path.endsWith("show")) { + show.put(query,true); + Integer ii = lastStepSeenByUser.get(remote); + if (ii != null) + i = ii; + } else if (path.endsWith("clear")) { + agState = h.get(h.size()-1); + h.clear(); + h.add(agState); + } else { + // see if ends with a number + try { + int pos = path.lastIndexOf("/"); + String n = path.substring(pos+1).trim(); + i = new Integer(n); + } catch (Exception e) {} + } + if (i == -1) { + so.append("<meta http-equiv=\"refresh\" content=\""+refreshInterval+"\">"); + agState = h.get(h.size()-1); + } else { + agState = h.get(i-1); + } try { - int pos = path.lastIndexOf("/"); - String n = path.substring(pos+1).trim(); - i = new Integer(n); - } catch (Exception e) {} - } - if (i == -1) { - so.append("<meta http-equiv=\"refresh\" content=\""+refreshInterval+"\">"); - agState = h.get(h.size()-1); - } else { - agState = h.get(i-1); - } - try { - lastStepSeenByUser.put(remote, i); - } catch (Exception e) { - e.printStackTrace(); - } - so.append("</head><body>"); - if (h.size() > 1) { - so.append("history: "); - so.append("<a href=/agent-mind/"+agName+"/latest>latest state</a> "); - for (i=h.size()-1; i>0; i--) { - so.append("<a href=\"/agent-mind/"+agName+"/"+i+"\" style=\"text-decoration: none\">"+i+"</a> "); + lastStepSeenByUser.put(remote, i); + } catch (Exception e) { + e.printStackTrace(); } - so.append("<a href=\"/agent-mind/"+agName+"/clear\">clear history</a> "); - so.append("<hr/>"); + so.append("</head><body>"); + if (h.size() > 1) { + //so.append("history: "); + so.append("<a href=/agent-mind/"+agName+"/latest>latest state</a> "); + for (i=h.size()-1; i>0; i--) { + so.append("<a href=\"/agent-mind/"+agName+"/"+i+"\" style=\"text-decoration: none\">"+i+"</a> "); + } + so.append("<a href=\"/agent-mind/"+agName+"/clear\">clear history</a> "); + so.append("<hr/>"); + } + so.append(getAgStateAsString(agState, false)); + //so.append("<hr/><a href=\"/\"> list of agents</a> "); + } else { + so.append("select an agent"); } - so.append(getAgStateAsString(agState, false)); - so.append("<hr/><a href=\"/\"> list of agents</a> "); - } else { - so.append("no register for this agent!"); } responseBody.write(so.toString().getBytes()); Modified: trunk/src/jason/asSemantics/Agent.java =================================================================== --- trunk/src/jason/asSemantics/Agent.java 2014-06-03 20:05:43 UTC (rev 1787) +++ trunk/src/jason/asSemantics/Agent.java 2014-06-06 19:20:11 UTC (rev 1788) @@ -26,6 +26,7 @@ import jason.JasonException; import jason.RevisionFailedException; import jason.architecture.AgArch; +import jason.architecture.MindInspectorWeb; import jason.asSyntax.ASSyntax; import jason.asSyntax.ArithFunctionTerm; import jason.asSyntax.InternalActionLiteral; @@ -46,6 +47,7 @@ import jason.bb.DefaultBeliefBase; import jason.functions.Count; import jason.functions.RuleToFunction; +import jason.jeditplugin.Config; import jason.mas2j.ClassParameters; import jason.profiling.QueryProfiling; import jason.runtime.Settings; @@ -155,6 +157,8 @@ //if (ts.getSettings().hasQueryCache()) qCache = new QueryCache(this); if (ts.getSettings().hasQueryProfiling()) qProfiling = new QueryProfiling(this); if (ts.getSettings().hasQueryCache()) qCache = new QueryCacheSimple(this, qProfiling); + + if (! "false".equals(Config.get().getProperty(Config.START_WEB_MI))) MindInspectorWeb.get().registerAg(this); } Modified: trunk/src/jason/infra/centralised/RunCentralisedMAS.java =================================================================== --- trunk/src/jason/infra/centralised/RunCentralisedMAS.java 2014-06-03 20:05:43 UTC (rev 1787) +++ trunk/src/jason/infra/centralised/RunCentralisedMAS.java 2014-06-06 19:20:11 UTC (rev 1788) @@ -206,7 +206,7 @@ /** start agents, .... */ public void start() { startAgs(); - startSyncMode(); + startSyncMode(); } public static boolean isDebug() { Modified: trunk/src/jason/infra/jade/JadeMASLauncherAnt.java =================================================================== --- trunk/src/jason/infra/jade/JadeMASLauncherAnt.java 2014-06-03 20:05:43 UTC (rev 1787) +++ trunk/src/jason/infra/jade/JadeMASLauncherAnt.java 2014-06-06 19:20:11 UTC (rev 1788) @@ -167,7 +167,7 @@ } args = Config.get().getJadeArgs(); if (mainHostPort > 0) - args += "-port "+mainHostPort; + args += " -port "+mainHostPort; if (Config.get().getBoolean(Config.JADE_RMA)) args += " -gui "; } Modified: trunk/src/jason/jeditplugin/Config.java =================================================================== --- trunk/src/jason/jeditplugin/Config.java 2014-06-03 20:05:43 UTC (rev 1787) +++ trunk/src/jason/jeditplugin/Config.java 2014-06-06 19:20:11 UTC (rev 1788) @@ -83,6 +83,7 @@ public static final String jacamoHomeProp = "JaCaMoHome"; public static final String SHORT_UNNAMED_VARS = "shortUnnamedVars"; + public static final String START_WEB_MI = "startWebMindInspector"; private static Config singleton = null; @@ -313,6 +314,10 @@ if (getProperty(SHOW_ANNOTS) == null) { put(SHOW_ANNOTS, "true"); } + + if (getProperty(START_WEB_MI) == null) { + put(START_WEB_MI, "true"); + } // Default infrastructures put("infrastructure.Centralised", CentralisedFactory.class.getName()); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |