// you’re reading...

Java

JADE: Start agents function code

Code zu schreiben, der in Jade neue Agents erstellt erfordert einiges an Aufwand und kann an Kleinigkeiten scheitern. Daher stelle ich hier meine Version zur Verfügung, in der Hoffnung, dass sie anderen entwicklern einiges an Nerven erspart. Es handelt sich um eine komplette Funktion, die in eine beliebige Klasse eingebunden werden kann. Zusätzlich müssen natürlich die passenden Imports eingefügt werden.

Implementing code in JADE to be able to create new agents using java code can be really annoying. To prevent you to spent several hours to figure out how to realize such code, find my version here. You can put this function to any existing class. Remember to organize your imports.

 
/*****************************************************************
	* Very interesting function to start any agent of any class on any container
	* agent is the agent, from where you want to create another agent. The other variables are
	* should be self-explanatory.
	*****************************************************************/
	static void startAgent(Agent agent, String agentName, String containerName, String className, String arg[], String ownerName) {
		CreateAgent ca = new CreateAgent();
 
		if(containerName.equals(""))
			containerName = AgentContainer.MAIN_CONTAINER_NAME;
 
		// fill the create action with the intended agent owner
		jade.security.JADEPrincipal intendedOwner = null;
		jade.security.Credentials initialCredentials = null;
 
		if ((ownerName==null) || (ownerName.trim().length()==0)) {
			// it is requested the creation of an agent
			// with the same owner of the RMA
			try {
				jade.security.JADEPrincipal rmaOwner = null;
				jade.security.Credentials rmaCredentials = null;
				jade.security.CredentialsHelper ch = (jade.security.CredentialsHelper) agent.getHelper("jade.core.security.Security");
				// get RMA's owner
				if (ch!=null) {  rmaCredentials = ch.getCredentials();    }
				if (rmaCredentials!=null) {  rmaOwner = rmaCredentials.getOwner();    }
				intendedOwner = rmaOwner;
			}
			catch (ServiceException se) { // Security service not present. Owner is null.
				intendedOwner=null;
				initialCredentials=null;
			}
 
		} else {
			// it is requested the creation of an agent
			// with a specified owner name
			try
			{
				Class c = Class.forName("jade.security.impl.JADEPrincipalImpl");
				intendedOwner = (JADEPrincipal) c.newInstance();
				java.lang.reflect.Method setName = c.getDeclaredMethod("setName", new Class[]{ String.class });
				setName.invoke(intendedOwner, new Object[] {ownerName});
			} catch (Exception e)
			{
				//e.printStackTrace();
				// Security service not present. Owner is null.
				intendedOwner=null;
				initialCredentials=null;
			}
		}
 
		ca.setOwner( intendedOwner );
		ca.setInitialCredentials( initialCredentials );
 
		ca.setAgentName(agentName);
		ca.setClassName(className);
		ca.setContainer(new ContainerID(containerName, null));
		if (arg != null) {
			for(int i = 0; i<arg.length ; i++) {
				ca.addArguments((Object)arg[i]);
			}
		}
 
		try {
 
			Action a = new Action();
			a.setActor(agent.getAMS());
			a.setAction(ca);
 
			ACLMessage requestMsg = new ACLMessage(ACLMessage.REQUEST);
			requestMsg.setSender(agent.getAID());
			requestMsg.addReceiver(agent.getAMS());
			requestMsg.setProtocol(FIPANames.InteractionProtocol.FIPA_REQUEST);
			requestMsg.setLanguage(FIPANames.ContentLanguage.FIPA_SL);
 
			requestMsg.setOntology(JADEManagementOntology.NAME);
			agent.getContentManager().fillContent(requestMsg, a);
			SimpleAchieveREInitiator initiator = new SimpleAchieveREInitiator(agent, requestMsg);
			agent.addBehaviour(initiator);
 
		}
		catch(Exception fe) {
			fe.printStackTrace();
		}
 
	}
 

Good luck!

Ähnliche Beiträge

Discussion

Ein Kommentar zu “JADE: Start agents function code”

  1. Thanks for the code. There is another example in the jade-doc folder (“HostAgent.java” in the party example).

    Posted by Susanne | 25.10.11, 18:36

Kommentieren

Neueste Kommentare