⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 introspector.java

📁 JADE(JAVA Agent开发框架)是一个完全由JAVA语言开发的软件,它简化了多Agent系统的实现。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************
 JADE - Java Agent DEvelopment Framework is a framework to develop
 multi-agent systems in compliance with the FIPA specifications.
 Copyright (C) 2000 CSELT S.p.A.
 
 GNU Lesser General Public License
 
 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,
 version 2.1 of the License.
 
 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.
 *****************************************************************/

package jade.tools.introspector;

import java.net.InetAddress;
import java.net.UnknownHostException;

import java.util.Collections;
import java.util.Enumeration;
import jade.util.leap.List;
import jade.util.leap.ArrayList;
import java.util.Map;
import java.util.TreeMap;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.HashSet;

import jade.core.*;
import jade.core.behaviours.*;

import jade.content.AgentAction;

import jade.domain.FIPANames;
import jade.domain.FIPAService;
import jade.domain.introspection.*;

import jade.domain.JADEAgentManagement.JADEManagementOntology;
import jade.domain.JADEAgentManagement.DebugOn;
import jade.domain.JADEAgentManagement.DebugOff;

import jade.gui.AgentTreeModel;

import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;

import jade.content.onto.basic.Action;
import jade.content.onto.basic.Done;

import jade.proto.SimpleAchieveREResponder;
import jade.proto.SimpleAchieveREInitiator;
import jade.proto.AchieveREInitiator;

import jade.tools.ToolAgent;
import jade.tools.introspector.gui.IntrospectorGUI;
import jade.tools.introspector.gui.MainWindow;

import jade.util.Logger;

/*
 This class represents the Introspector Agent. This agent registers
 with the AMS as a tool, to manage an AgentTree component, then
 activates its GUI. The agent listens for ACL messages containing
 introspection events and updates the display through the
 IntrospectorGUI class.
 
 @author Andrea Squeri, -  Universita' di Parma
 @author Giovanni Caire, -  TILAB
 */
public class Introspector extends ToolAgent {
	
	private Set allAgents = null;
	private Hashtable preload = null;
		
	private class AMSRequester extends SimpleAchieveREInitiator {
		
		private String actionName;
		
		
		public AMSRequester(String an, ACLMessage request) {
			super(Introspector.this, request);
			actionName = an;
		}
		
		protected void handleNotUnderstood(ACLMessage reply) {
			myGUI.showError("NOT-UNDERSTOOD received during " + actionName);
		}
		
		protected void handleRefuse(ACLMessage reply) {
			myGUI.showError("REFUSE received during " + actionName);
		}
		
		protected void handleAgree(ACLMessage reply) {
			if(logger.isLoggable(Logger.FINEST))
				logger.log(Logger.FINEST,"AGREE received");
		}
		
		protected void handleFailure(ACLMessage reply) {
			myGUI.showError("FAILURE received during " + actionName);
		}
		
		protected void handleInform(ACLMessage reply) {
			if(logger.isLoggable(Logger.FINEST))
				logger.log(Logger.FINEST,"INFORM received");
		}
		
	} // End of AMSRequester class
	
	// GUI events
	public static final int STEP_EVENT = 1;
	public static final int BREAK_EVENT = 2;
	public static final int SLOW_EVENT = 3;
	public static final int GO_EVENT = 4;
	public static final int KILL_EVENT = 5;
	public static final int SUSPEND_EVENT = 6;
	
	private IntrospectorGUI myGUI;
	private Sensor guiSensor = new Sensor();
	private String myContainerName;
	private Map windowMap = Collections.synchronizedMap(new TreeMap());
	
	// The set of agents that are observed in step-by-step mode
	private Set stepByStepAgents = new HashSet();
	// The set of agents that are observed in slow mode
	private Set slowAgents = new HashSet();
	// Maps an observed agent with the String used as reply-with in the
	// message that notified about an event that had to be observed synchronously
	private Map pendingReplies = new HashMap();
	// Maps an observed agent with the ToolNotifier that notifies events
	// about that agent to this Introspector
	private Map notifiers = new HashMap();
	
	private SequentialBehaviour AMSSubscribe = new SequentialBehaviour();
	
	class IntrospectorAMSListenerBehaviour extends AMSListenerBehaviour {
		
		protected void installHandlers(Map handlersTable) {
			
			handlersTable.put(IntrospectionVocabulary.META_RESETEVENTS, new EventHandler() {
				public void handle(Event ev) {
					ResetEvents re = (ResetEvents)ev;
					myGUI.resetTree();
				}
			});
			
			handlersTable.put(IntrospectionVocabulary.ADDEDCONTAINER, new EventHandler() {
				public void handle(Event ev) {
					AddedContainer ac = (AddedContainer)ev;
					ContainerID cid = ac.getContainer();
					String name = cid.getName();
					String address = cid.getAddress();
					try {
						InetAddress addr = InetAddress.getByName(address);
						myGUI.addContainer(name, addr);
					}
					catch(UnknownHostException uhe) {
						myGUI.addContainer(name, null);
					}
				}
			});
			
			handlersTable.put(IntrospectionVocabulary.REMOVEDCONTAINER, new EventHandler() {
				public void handle(Event ev) {
					RemovedContainer rc = (RemovedContainer)ev;
					ContainerID cid = rc.getContainer();
					String name = cid.getName();
					myGUI.removeContainer(name);
				}
			});
			
			handlersTable.put(IntrospectionVocabulary.BORNAGENT, new EventHandler() {
				public void handle(Event ev) {
					BornAgent ba = (BornAgent)ev;
					ContainerID cid = ba.getWhere();
					String container = cid.getName();
					AID agent = ba.getAgent();
					allAgents.add(agent);
					myGUI.addAgent(container, agent);
					if( preloadContains( agent.getName() ) != null )
						Introspector.this.addAgent( agent );
					if(agent.equals(getAID()))
						myContainerName = container;
				}
			});
			
			handlersTable.put(IntrospectionVocabulary.DEADAGENT, new EventHandler() {
				public void handle(Event ev) {
					DeadAgent da = (DeadAgent)ev;
					ContainerID cid = da.getWhere();
					String container = cid.getName();
					AID agent = da.getAgent();
					allAgents.remove(agent);
					MainWindow m = (MainWindow)windowMap.get(agent);
					if(m != null) {
						myGUI.closeInternal(m);
						windowMap.remove(agent);
					}
					myGUI.removeAgent(container, agent);
				}
			});
			
			handlersTable.put(IntrospectionVocabulary.MOVEDAGENT, new EventHandler() {
				public void handle(Event ev) {
					MovedAgent ma = (MovedAgent)ev;
					AID agent = ma.getAgent();
					ContainerID from = ma.getFrom();
					myGUI.removeAgent(from.getName(), agent);
					ContainerID to = ma.getTo();
					myGUI.addAgent(to.getName(), agent);
					
					if (windowMap.containsKey(agent)) {
						MainWindow m = (MainWindow)windowMap.get(agent);
						// FIXME: We should clean behaviours and pending messages here
						requestDebugOn(agent);
					}
				}
			});
			
		} // End of installHandlers() method
	}
	
	public void toolSetup() {
		
		ACLMessage msg = getRequest();
		msg.setOntology(JADEManagementOntology.NAME);
		
		// Send 'subscribe' message to the AMS
		AMSSubscribe.addSubBehaviour(new SenderBehaviour(this, getSubscribe()));
		
		// Handle incoming 'inform' messages about Platform events from the AMS
		AMSSubscribe.addSubBehaviour(new IntrospectorAMSListenerBehaviour());
		
		addBehaviour(AMSSubscribe);
		
		// Handle incoming INFORM messages about Agent and Message events from the
		// ToolNotifiers
		addBehaviour(new IntrospectionListenerBehaviour());
		
		// Handle incoming INFORM messages about observation start/stop from
		// ToolNotifiers
		addBehaviour(new ControlListenerBehaviour(this));
		
		// Handle incoming REQUEST to start/stop debugging agents
		addBehaviour(new RequestListenerBehaviour());
		
		// Manages GUI events
		addBehaviour(new SensorManager(this, guiSensor) {
			public void onEvent(jade.util.Event ev) {
				AID id = ((MainWindow) ev.getSource()).getDebugged();
				switch (ev.getType()) {
				case STEP_EVENT:
					proceed(id);
					break;
				case BREAK_EVENT:
					stepByStepAgents.add(id);
					slowAgents.remove(id);
					break;
				case SLOW_EVENT:
					stepByStepAgents.remove(id);
					slowAgents.add(id);
					proceed(id);
					break;
				case GO_EVENT:
					stepByStepAgents.remove(id);
					slowAgents.remove(id);
					proceed(id);
				}
			}
		}	);
		
		allAgents = new HashSet();
		preload = new Hashtable();
		
		/*
		 * preload agents from argument array if arguments present
		 */
		Object[] arguments = getArguments();
		if ( arguments != null )
		{
			for( int i=0; i < arguments.length; ++i )
			{
				parsePreloadDescription( (String)arguments[i] );
			}
		}
		
		// Show Graphical User Interface
		myGUI = new IntrospectorGUI(this);
		myGUI.setVisible(true);
		
	}
	
	/*
	 Adds an agent to the debugged agents map, and asks the AMS to
	 start debugging mode on that agent.
	 */
	public boolean addAgent(AID name) {
		if(!windowMap.containsKey(name)) {
			MainWindow m = new MainWindow(guiSensor, name);
			myGUI.addWindow(m);
			windowMap.put(name, m);
			
			// Enable the following instruction if you want STEP_BY_STEP to
			// be the default debug mode
			//stepByStepAgents.add(name);
			
			requestDebugOn(name);
			return true;
		}
		else {
			return false;
		}    
	}
	
	private void requestDebugOn(AID name) {
		try {
			ACLMessage msg = getRequest();
			msg.setOntology(JADEManagementOntology.NAME);
			DebugOn dbgOn = new DebugOn();
			dbgOn.setDebugger(getAID());
			dbgOn.addDebuggedAgents(name);
			Action a = new Action();
			a.setActor(getAMS());
			a.setAction(dbgOn);
			
			getContentManager().fillContent(msg, a);
			
			addBehaviour(new AMSRequester("DebugOn", msg));
		}
		catch(Exception fe) {
			fe.printStackTrace();
		}
	}
	
	/*
	 Removes an agent from the debugged agents map, and closes its
	 window. Moreover,it and asks the AMS to stop debugging mode on
	 that agent.
	 */
	public void removeAgent(final AID name) {
		if(windowMap.containsKey(name)) {
			try {
				final MainWindow m = (MainWindow)windowMap.get(name);
				myGUI.closeInternal(m);
				windowMap.remove(name);
				
				stepByStepAgents.remove(name);
				slowAgents.remove(name);
				proceed(name);
				
				ACLMessage msg = getRequest();
				msg.setOntology(JADEManagementOntology.NAME);
				DebugOff dbgOff = new DebugOff();
				dbgOff.setDebugger(getAID());
				dbgOff.addDebuggedAgents(name);
				Action a = new Action();
				a.setActor(getAMS());
				a.setAction(dbgOff);
				
				getContentManager().fillContent(msg, a);
				
				addBehaviour(new AMSRequester("DebugOff", msg));
			}
			catch(Exception fe) {
				fe.printStackTrace();
			}
		}
	}
	
	
	/**
	 Cleanup during agent shutdown. This method cleans things up when
	 <em>RMA</em> agent is destroyed, disconnecting from <em>AMS</em>
	 agent and closing down the platform administration <em>GUI</em>.
	 */
	public void toolTakeDown() {
		// Stop debugging all the agents
		if(!windowMap.isEmpty()) {
			ACLMessage msg = getRequest();
			msg.setOntology(JADEManagementOntology.NAME);
			DebugOff dbgOff = new DebugOff();
			dbgOff.setDebugger(getAID());
			Iterator it = windowMap.keySet().iterator();
			while(it.hasNext()) {
				AID id = (AID)it.next();
				dbgOff.addDebuggedAgents(id);
			}
			
			Action a = new Action();
			a.setActor(getAMS());
			a.setAction(dbgOff);
			
			try {
				getContentManager().fillContent(msg, a);
				FIPAService.doFipaRequestClient(this, msg);
			}
			catch(Exception fe) {
				// When the AMS replies the tool notifier is no longer registered.
				// But we don't care as we are exiting
				if(logger.isLoggable(Logger.WARNING))
					logger.log(Logger.WARNING,fe.getMessage());
			}
		}
		
		send(getCancel());
		// myGUI.setVisible(false);  Not needed and can cause thread deadlock on join.
		myGUI.disposeAsync();
	}
	
	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -