📄 mailboxbaseagent.java
字号:
import SOMA.agent.*;
import SOMA.naming.*;
import java.util.*;
/**
* An example of a base agent using its mailbox to coordinate its work
* with other lurker agents.
*
* @author Giulio Piancastelli
* @version 0.1 - Wednesday 13th February, 2002
*/
public class MailboxBaseAgent extends Agent {
private int numAgents = 0;
private int numMessages = 0;
public void run() {
Vector placesToVisit = new Vector();
PlaceID home = agentSystem.getPlaceID();
SOMA.Environment env = agentSystem.getEnvironment();
if (home.isDomain()) {
// Get children domains
Vector children = env.domainNameService.getChildrenDNS(); // a Vector of PlaceIDs
for (int index = children.size() - 1; index >= 0; index--) {
PlaceID place = (PlaceID) children.get(index);
// update the list of places to visit
placesToVisit.add(place);
}
// Get places in this domain
Vector places = new Vector(Arrays.asList(agentSystem.getPlaces()));
places.remove(home);
for (int index = places.size() - 1; index >= 0; index--) {
PlaceID place = (PlaceID) places.get(index);
// update the list of places to visit
placesToVisit.add(place);
}
}
// Initialize the shared objects relative to all children domains and places
for (int index = placesToVisit.size() - 1; index >= 0; index--) {
PlaceID place = (PlaceID) placesToVisit.get(index);
agentSystem.sharedObjects.put(index, place);
}
// Create lurkers which will move on first level domains and places, access shared
// objects and somehow manipulate them
for (int index = placesToVisit.size() - 1; index >= 0; index--) {
Vector v = new Vector();
v.add(home);
v.add(new Integer(index));
v.add(getID()); // the base agent ID
AgentWorker lurker = env.agentManager.createAgent("MailboxLurkerAgent", v, false, true);
try {
lurker.start();
} catch (AgentWorker.AgentWorkerException awe) {
awe.printStackTrace();
}
numAgents++;
}
// if the agent is not traceable, there is no mailbox
if (mailbox != null)
mailbox.mailListener = new MyMailListener(this);
agentSystem.getOut().println("" + getID() + ": I'm going idle...");
idle("idleCycle"); // wait
}
public void idleCycle() {
if (numMessages != numAgents)
idle("idleCycle");
else
printSharedObjects();
}
public void printSharedObjects() {
for (int index = agentSystem.sharedObjects.size() - 1; index >= 0; index--)
agentSystem.getOut().println(agentSystem.sharedObjects.get(index).toString());
}
public int getNumAgents() {
return numAgents;
}
public void setNumMessages(int num) {
numMessages = num;
}
} // end MailboxBaseAgent
class MyMailListener implements Mailbox.MailListener {
private MailboxBaseAgent theAgent;
private int numMessages;
public MyMailListener(MailboxBaseAgent a) {
theAgent = a;
numMessages = 0;
}
public void run() {
// The agent look in the mailbox for a message, but if there are no
// messages, it suspends itself on the mailbox
Message mex = theAgent.mailbox.getMessage();
theAgent.agentSystem.getOut().println("Message received from " + mex.from);
// The messages sent by lurker agents have a particular format
if (mex.message.toString().equals("lurker_agent_done"))
theAgent.setNumMessages(++numMessages);
theAgent.agentSystem.getOut().println("" + numMessages + " message(s) arrived");
}
} // end MyMailListener
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -