📄 df.java
字号:
else {
gui.showStatusMsg("Error processing request. "+((InternalError) result).getMessage());
}
return 0;
}
};
addBehaviour(b);
}
break;
}
case DFGUIAdapter.MODIFY: {
AID df = (AID) ev.getParameter(0);
DFAgentDescription dfd = (DFAgentDescription) ev.getParameter(1);
DFService.checkIsValid(dfd, true);
if (getAID().equals(df)) {
// Modify the description of an agent with this DF
DFModify(dfd);
}
else {
// Modify the description of an agent with another DF
gui.showStatusMsg("Processing your request & waiting for result...");
Modify m = new Modify();
m.setDescription(dfd);
Behaviour b = new RemoteDFRequester(df, m) {
public int onEnd() {
Object result = getResult();
if (!(result instanceof InternalError)) {
gui.showStatusMsg("Modification request processed. Ready for new request");
}
else {
gui.showStatusMsg("Error processing request. "+((InternalError) result).getMessage());
}
return 0;
}
};
addBehaviour(b);
}
break;
}
case DFGUIAdapter.SEARCH: {
AID df = (AID) ev.getParameter(0);
DFAgentDescription dfd = (DFAgentDescription) ev.getParameter(1);
SearchConstraints sc = (SearchConstraints)ev.getParameter(2);
// Note that we activate a RemoteDFBehaviour even if the DF to perform
// the search on is the local DF. This allows handling recursive
// search (if needed) correctly
gui.showStatusMsg("Processing your request & waiting for result...");
Search s = new Search();
s.setDescription(dfd);
s.setConstraints(sc);
Behaviour b = new RemoteDFRequester(df, s) {
public int onEnd() {
Object result = getResult();
if (!(result instanceof InternalError)) {
gui.showStatusMsg("Search request processed. Ready for new request");
gui.refreshLastSearchResults((List) result, getRemoteDF());
}
else {
gui.showStatusMsg("Error processing request. "+((InternalError) result).getMessage());
}
return 0;
}
};
addBehaviour(b);
break;
}
case DFGUIAdapter.FEDERATE: {
AID df = (AID) ev.getParameter(0);
final DFAgentDescription dfd = (DFAgentDescription) ev.getParameter(1);
gui.showStatusMsg("Processing your request & waiting for result...");
Register r = new Register();
r.setDescription(dfd);
Behaviour b = new RemoteDFRequester(df, r) {
public int onEnd() {
Object result = getResult();
if (!(result instanceof InternalError)) {
gui.showStatusMsg("Federation request processed. Ready for new request");
addParent(getRemoteDF(), dfd);
}
else {
gui.showStatusMsg("Error processing request. "+((InternalError) result).getMessage());
}
return 0;
}
};
addBehaviour(b);
break;
}
} // END of switch
} // END of try
catch(FIPAException fe) {
gui.showStatusMsg("Error processing request. "+fe.getMessage());
fe.printStackTrace();
}
}
//#APIDOC_EXCLUDE_END
/**
This method returns the description of an agent registered with the DF.
*/
public DFAgentDescription getDFAgentDsc(AID name) throws FIPAException
{
DFAgentDescription template = new DFAgentDescription();
template.setName(name);
List l = agentDescriptions.search(template, 1);
if(l.isEmpty())
return null;
else
return (DFAgentDescription)l.get(0);
}
/**
* This method returns the current description of this DF
*/
public DFAgentDescription getDescriptionOfThisDF() {
return myDescription;
}
/**
* This method returns the description of this df used to federate with the given parent
*/
public DFAgentDescription getDescriptionOfThisDF(AID parent) {
return (DFAgentDescription)dscDFParentMap.get(parent);
}
/////////////////////////////////////
// Utility methods
/////////////////////////////////////
/**
This method make visible the GUI of the DF.
@return true if the GUI was not visible already, false otherwise.
*/
protected boolean showGui() {
if (gui == null)
{
try{
Class c = Class.forName("jade.tools.dfgui.DFGUI");
gui = (DFGUIInterface)c.newInstance();
gui.setAdapter(df.this); //this method must be called to avoid reflection (the constructor of the df gui has no parameters).
DFAgentDescription matchEverything = new DFAgentDescription();
// FIXME: Getting a list with all DFAgentDescriptions may cause out of memory
// The DFGui should probably be paged and we should use an iterated search.
List agents = agentDescriptions.search(matchEverything, -1);
List AIDList = new ArrayList();
Iterator it = agents.iterator();
while(it.hasNext())
AIDList.add(((DFAgentDescription)it.next()).getName());
gui.refresh(AIDList.iterator(), parents.iterator(), children.iterator());
gui.setVisible(true);
return true;
}catch(Exception e){e.printStackTrace();}
}
return false;
}
/**
* This method creates the DFAgent descriptor for this df used to federate with other df.
*/
private DFAgentDescription getDefaultDescription() {
DFAgentDescription out = new DFAgentDescription();
out.setName(getAID());
out.addOntologies(FIPAManagementOntology.getInstance().getName());
out.addLanguages(FIPANames.ContentLanguage.FIPA_SL0);
out.addProtocols(FIPANames.InteractionProtocol.FIPA_REQUEST);
ServiceDescription sd = new ServiceDescription();
sd.setName("df-service");
sd.setType("fipa-df");
sd.addOntologies(FIPAManagementOntology.getInstance().getName());
sd.addLanguages(FIPANames.ContentLanguage.FIPA_SL0);
sd.addProtocols(FIPANames.InteractionProtocol.FIPA_REQUEST);
try{
sd.setOwnership(InetAddress.getLocalHost().getHostName());
}catch (java.net.UnknownHostException uhe){
sd.setOwnership("unknown");
}
out.addServices(sd);
return out;
}
/**
* This method set the description of the df according to the DFAgentDescription passed.
* The programmers can call this method to provide a different initialization of the description of the df they are implementing.
* The method is called inside the setup of the agent and set the df description using a default description.
*/
protected void setDescriptionOfThisDF(DFAgentDescription dfd) {
myDescription = dfd;
myDescription.setName(getAID());
if (!isADF(myDescription)) {
if(logger.isLoggable(Logger.WARNING))
logger.log(Logger.WARNING,"The description set for this DF does not include a \"fipa-df\" service.");
}
}
/**
* This method can be used to add a parent (a DF this DF is federated with).
* @param dfName the parent df (the df with which this df has been registered)
* @param dfd the description used by this df to register with the parent.
*/
protected void addParent(AID dfName, DFAgentDescription dfd)
{
parents.add(dfName);
if(gui != null) // the gui can be null if this method is called in order to manage a request made by the df-applet.
gui.addParent(dfName);
dscDFParentMap.put(dfName,dfd); //update the table of corrispondence between parents and description of this df used to federate.
}
/**
this method can be used to remove a parent (a DF with which this DF is federated).
*/
protected void removeParent(AID dfName)
{
parents.remove(dfName);
if(gui != null) //the gui can be null is this method is called in order to manage a request from the df applet
gui.removeParent(dfName);
dscDFParentMap.remove(dfName);
}
/**
Store the request message
related to an action that is being processed by a Behaviour.
This information will be used by the Behaviour to send back the
notification to the requester.
*/
void storePendingRequest(Object key, ACLMessage request) {
pendingRequests.put(key, request);
}
void removePendingRequest(Object key) {
pendingRequests.remove(key);
}
/**
Send the notification related to an action that has been processed
by a Behaviour.
*/
private void sendPendingNotification(Concept action, Object result) {
ACLMessage request = (ACLMessage) pendingRequests.remove(action);
if (request != null) {
ACLMessage notification = request.createReply();
ContentElement ce = null;
Action act = new Action(getAID(), action);
if (result instanceof InternalError) {
// Some error occurred during action processing
notification.setPerformative(ACLMessage.FAILURE);
ContentElementList cel = new ContentElementList();
cel.add(act);
cel.add((Predicate) result);
ce = cel;
}
else {
// Action processing was OK
notification.setPerformative(ACLMessage.INFORM);
if (result != null) {
ce = new Result(act, result);
}
else {
ce = new Done(act);
}
}
try {
getContentManager().fillContent(notification, ce);
send(notification);
AID receiver = (AID) notification.getAllReceiver().next();
if(logger.isLoggable(Logger.FINE))
logger.log(Logger.FINE,"Notification sent back to "+receiver.getName());
}
catch (Exception e) {
// Should never happen
if(logger.isLoggable(Logger.SEVERE))
logger.log(Logger.SEVERE,"Error encoding pending notification content.");
e.printStackTrace();
}
}
else {
if(logger.isLoggable(Logger.WARNING))
logger.log(Logger.WARNING,"Processed action request not found.");
}
}
/**
* @return
* <ul>
* <li> 1 if constraints.maxResults == null (according to FIPA specs)
* <li> maxResultLimit if constraints.maxResults is infinite (i.e. < 0) or
* greater than maxResultLimit
* <li> constraints.maxResults otherwise
* </ul>
* This is package-scoped since it is also used by the DFIteratedSearchManagementBehaviour
**/
int getActualMaxResults(SearchConstraints constraints) {
int maxResult = (constraints.getMaxResults() == null ? 1 : constraints.getMaxResults().intValue());
maxResult = ((maxResult < 0 || maxResult > maxResultLimit) ? maxResultLimit : maxResult); // limit the max num of results
return maxResult;
}
/**
Check if this search must be served, i.e. if it has not yet been received.
In particular the value of search_id must be different from any prior value that was received.
If search_id is not null and it has not yet been received, search_id is
added into the cache.
@exception FIPAException if the search id is already in the cache.
*/
private void checkSearchId(String searchId) throws FIPAException {
if (searchId != null) {
if (searchIdCache.contains(searchId)) {
throw new InternalError("search-id already served");
}
else {
searchIdCache.add(searchId);
}
}
}
private boolean isADF(DFAgentDescription dfd) {
try {
return ((ServiceDescription)dfd.getAllServices().next()).getType().equalsIgnoreCase("fipa-df");
} catch (Exception e) {
return false;
}
}
//#APIDOC_EXCLUDE_END
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -