📄 dfservice.java
字号:
// Skip "agent-identifier"
parser.getElement();
while (parser.nextToken().startsWith(":")) {
String slotName = parser.getElement();
// Name
if (slotName.equals(SL0Vocabulary.AID_NAME)) {
id.setName(parser.getElement());
}
// Addresses
else if (slotName.equals(SL0Vocabulary.AID_ADDRESSES)) {
Iterator it = parseAggregate(parser).iterator();
while (it.hasNext()) {
id.addAddresses((String) it.next());
}
}
// Resolvers
else if (slotName.equals(SL0Vocabulary.AID_RESOLVERS)) {
Iterator it = parseAggregate(parser).iterator();
while (it.hasNext()) {
id.addResolvers((AID) it.next());
}
}
}
parser.consumeChar(')');
return id;
}
/**
The parser content has the form:
(sequence <val> <val> ......) <possibly something else>
or
(set <val> <val> ......) <possibly something else>
*/
private static List parseAggregate(SimpleSLTokenizer parser) throws Exception {
List l = new ArrayList();
// Skip first (
parser.consumeChar('(');
// Skip "sequence" or "set" (no matter)
parser.getElement();
String next = parser.nextToken();
while (!next.startsWith(")")) {
if (!next.startsWith("(")) {
l.add(parser.getElement());
}
else {
parser.consumeChar('(');
next = parser.nextToken();
if (next.equals(FIPAManagementVocabulary.DFAGENTDESCRIPTION)) {
l.add(parseDfd(parser));
}
if (next.equals(SL0Vocabulary.AID)) {
l.add(parseAID(parser));
}
else if (next.equals(FIPAManagementVocabulary.SERVICEDESCRIPTION)) {
l.add(parseServiceDescription(parser));
}
else if (next.equals(FIPAManagementVocabulary.PROPERTY)) {
l.add(parseProperty(parser));
}
}
next = parser.nextToken();
}
parser.consumeChar(')');
return l;
}
/**
S has the form:
(sequence (DFD...) (DFD...)) <possibly something else>
*/
private static DFAgentDescription[] decodeDfdSequence(String s) throws Exception {
List l = parseAggregate(new SimpleSLTokenizer(s));
// Convert the list into an array
DFAgentDescription[] items = new DFAgentDescription[l.size()];
for(int i = 0; i < l.size(); i++){
items[i] = (DFAgentDescription)l.get(i);
}
return items;
}
/**
Start indicates the index of the first char after the open parenthesis
*/
private static int countUntilEnclosing(String s, int start) {
int openCnt = 1;
boolean skipMode = false;
int cnt = start;
while (openCnt > 0) {
char c = s.charAt(cnt++);
if (!skipMode) {
if (c == '(') {
openCnt++;
}
else if (c == ')') {
openCnt--;
}
else if (c == '"') {
skipMode = true;
}
}
else {
if (c == '\\' && s.charAt(cnt) == '\"') {
cnt++;
}
else if (c == '"') {
skipMode = false;
}
}
}
return cnt-start;
}
///////////////////////////////////
// Encoding methods
///////////////////////////////////
/**
This is package scoped as it is used by DFUpdateBehaviour and
DFSearchBehaviour.
*/
static String encodeAction(AID df, String actionName, DFAgentDescription dfd, SearchConstraints sc) {
StringBuffer sb = new StringBuffer("((");
sb.append(SL0Vocabulary.ACTION);
sb.append(' ');
sb.append(df.toString());
sb.append(SPACE_BRACKET);
sb.append(actionName);
sb.append(' ');
encodeDfd(sb, dfd);
if (actionName.equals(FIPAManagementVocabulary.SEARCH) && sc == null) {
sc = new SearchConstraints();
sc.setMaxResults(MINUSONE);
}
if (sc != null) {
sb.append(SPACE_BRACKET);
sb.append(FIPAManagementVocabulary.SEARCHCONSTRAINTS);
encodeField(sb, sc.getMaxResults(), FIPAManagementVocabulary.SEARCHCONSTRAINTS_MAX_RESULTS);
encodeField(sb, sc.getMaxDepth(), FIPAManagementVocabulary.SEARCHCONSTRAINTS_MAX_DEPTH);
encodeField(sb, sc.getSearchId(), FIPAManagementVocabulary.SEARCHCONSTRAINTS_SEARCH_ID);
sb.append(')');
}
sb.append(")))"); // Close <actionName>, action and content
return sb.toString();
}
/**
This is package scoped as it is used by DFSearchBehaviour
*/
static String encodeIota(AID df, DFAgentDescription dfd, SearchConstraints sc) {
StringBuffer sb = new StringBuffer("((iota ?x (");
sb.append(SL0Vocabulary.RESULT);
sb.append(' ');
String tmp = encodeAction(df, FIPAManagementVocabulary.SEARCH, dfd, sc);
sb.append(tmp.substring(1, tmp.length()-1));
sb.append(" ?x)))"); // Close Result, iota and content
return sb.toString();
}
/**
This is package scoped as it is used by DFSearchBehaviour
*/
static String encodeCancel(AID df, ACLMessage msg) {
StringBuffer sb = new StringBuffer("((");
sb.append(SL0Vocabulary.ACTION);
sb.append(' ');
sb.append(df.toString());
sb.append(SPACE_BRACKET);
sb.append(ACLMessage.getPerformative(msg.getPerformative()));
encodeField(sb, msg.getSender(), SL0Vocabulary.ACLMSG_SENDER);
encodeAggregate(sb, msg.getAllReceiver(), SL0Vocabulary.SEQUENCE, SL0Vocabulary.ACLMSG_RECEIVERS);
encodeField(sb, msg.getProtocol(), SL0Vocabulary.ACLMSG_PROTOCOL);
encodeField(sb, msg.getLanguage(), SL0Vocabulary.ACLMSG_LANGUAGE);
encodeField(sb, msg.getOntology(), SL0Vocabulary.ACLMSG_ONTOLOGY);
encodeField(sb, msg.getReplyWith(), SL0Vocabulary.ACLMSG_REPLY_WITH);
encodeField(sb, msg.getConversationId(), SL0Vocabulary.ACLMSG_CONVERSATION_ID);
encodeField(sb, msg.getContent(), SL0Vocabulary.ACLMSG_CONTENT);
sb.append(")))"); // Close msg, action and content
return sb.toString();
}
private static void encodeDfd(StringBuffer sb, DFAgentDescription dfd) {
sb.append('(');
sb.append(FIPAManagementVocabulary.DFAGENTDESCRIPTION);
encodeField(sb, dfd.getName(), FIPAManagementVocabulary.DFAGENTDESCRIPTION_NAME);
encodeAggregate(sb, dfd.getAllProtocols(), SL0Vocabulary.SET, FIPAManagementVocabulary.DFAGENTDESCRIPTION_PROTOCOLS);
encodeAggregate(sb, dfd.getAllLanguages(), SL0Vocabulary.SET, FIPAManagementVocabulary.DFAGENTDESCRIPTION_LANGUAGES);
encodeAggregate(sb, dfd.getAllOntologies(), SL0Vocabulary.SET, FIPAManagementVocabulary.DFAGENTDESCRIPTION_ONTOLOGIES);
encodeAggregate(sb, dfd.getAllServices(), SL0Vocabulary.SET, FIPAManagementVocabulary.DFAGENTDESCRIPTION_SERVICES);
Date lease = dfd.getLeaseTime();
if (lease != null) {
sb.append(SPACE_COLON);
sb.append(FIPAManagementVocabulary.DFAGENTDESCRIPTION_LEASE_TIME);
sb.append(' ');
sb.append(ISO8601.toString(lease));
}
sb.append(')');
}
private static void encodeServiceDescription(StringBuffer sb, ServiceDescription sd) {
sb.append('(');
sb.append(FIPAManagementVocabulary.SERVICEDESCRIPTION);
encodeField(sb, sd.getName(), FIPAManagementVocabulary.SERVICEDESCRIPTION_NAME);
encodeField(sb, sd.getType(), FIPAManagementVocabulary.SERVICEDESCRIPTION_TYPE);
encodeField(sb, sd.getOwnership(), FIPAManagementVocabulary.SERVICEDESCRIPTION_OWNERSHIP);
encodeAggregate(sb, sd.getAllProtocols(), SL0Vocabulary.SET, FIPAManagementVocabulary.SERVICEDESCRIPTION_PROTOCOLS);
encodeAggregate(sb, sd.getAllLanguages(), SL0Vocabulary.SET, FIPAManagementVocabulary.SERVICEDESCRIPTION_LANGUAGES);
encodeAggregate(sb, sd.getAllOntologies(), SL0Vocabulary.SET, FIPAManagementVocabulary.SERVICEDESCRIPTION_ONTOLOGIES);
encodeAggregate(sb, sd.getAllProperties(), SL0Vocabulary.SET, FIPAManagementVocabulary.SERVICEDESCRIPTION_PROPERTIES);
sb.append(')');
}
private static void encodeProperty(StringBuffer sb, Property p) {
sb.append('(');
sb.append(FIPAManagementVocabulary.PROPERTY);
encodeField(sb, p.getName(), FIPAManagementVocabulary.PROPERTY_NAME);
encodeField(sb, p.getValue(), FIPAManagementVocabulary.PROPERTY_VALUE);
sb.append(')');
}
private static void encodeField(StringBuffer sb, Object val, String name) {
if (val != null) {
sb.append(SPACE_COLON);
sb.append(name);
sb.append(' ');
if (val instanceof String) {
encodeString(sb, (String) val);
}
else {
sb.append(val);
}
}
}
private static void encodeAggregate(StringBuffer sb, Iterator agg, String aggType, String name) {
if (agg != null && agg.hasNext()) {
sb.append(SPACE_COLON);
sb.append(name);
sb.append(SPACE_BRACKET);
sb.append(aggType);
while (agg.hasNext()) {
sb.append(' ');
Object val = agg.next();
if (val instanceof ServiceDescription) {
encodeServiceDescription(sb, (ServiceDescription) val);
}
else if (val instanceof Property) {
encodeProperty(sb, (Property) val);
}
else if (val instanceof String) {
encodeString(sb, (String) val);
}
else {
sb.append(val);
}
}
sb.append(')');
}
}
private static void encodeString(StringBuffer sb, String s) {
if (SimpleSLTokenizer.isAWord(s)) {
sb.append(s);
}
else {
sb.append(SimpleSLTokenizer.quoteString(s));
}
}
//#MIDP_EXCLUDE_BEGIN
/**
In some cases it is more convenient to execute this tasks in a non-blocking way.
This method returns a non-blocking behaviour that can be added to the queue of the agent behaviours, as usual, by using <code>Agent.addBehaviour()</code>.
<p>
Several ways are available to get the result of this behaviour and the programmer can select one according to his preferred programming style:
<ul>
<li>
call getLastMsg() and getSearchResults() where both throw a NotYetReadyException if the task has not yet finished;
<li>create a SequentialBehaviour composed of two sub-behaviours: the first subbehaviour is the returned RequestFIPAServiceBehaviour, while the second one is application-dependent and is executed only when the first is terminated;
<li>use directly the class RequestFIPAServiceBehaviour by extending it and overriding all the handleXXX methods that handle the states of the fipa-request interaction protocol.
</ul>
* @param a is the agent performing the task
* @param dfName is the AID of the DF that should perform the requested action
* @param actionName is the name of the action (one of the constants defined
* in FIPAManagementOntology: REGISTER / DEREGISTER / MODIFY / SEARCH).
* @param dfd is the agent description
* @param constraints are the search constraints (can be null if this is
* not a search operation)
* @return the behaviour to be added to the agent
@exception FIPAException A suitable exception can be thrown
to indicate some error condition
locally discovered (e.g.the agentdescription is not valid.)
@see jade.domain.FIPAAgentManagement.FIPAManagementOntology
@deprecated Use <code>AchieveREInitiator</code> instead
**/
public static RequestFIPAServiceBehaviour getNonBlockingBehaviour(Agent a, AID dfName, String actionName, DFAgentDescription dfd, SearchConstraints constraints) throws FIPAException {
return new RequestFIPAServiceBehaviour(a,dfName,actionName,dfd,constraints);
}
/**
* The default DF is used.
* @see #getNonBlockingBehaviour(Agent a, AID dfName, String actionName, DFAgentDescription dfd, SearchConstraints constraints)
@deprecated Use <code>AchieveREInitiator</code> instead
**/
public static RequestFIPAServiceBehaviour getNonBlockingBehaviour(Agent a, String actionName, DFAgentDescription dfd, SearchConstraints constraints) throws FIPAException {
return getNonBlockingBehaviour(a,a.getDefaultDF(),actionName,dfd,constraints);
}
/**
* The default DF is used.
the default SearchContraints are used.
a default AgentDescription is used, where only the agent AID is set.
* @see #getNonBlockingBehaviour(Agent a, AID dfName, String actionName, DFAgentDescription dfd, SearchConstraints constraints)
@deprecated Use <code>AchieveREInitiator</code> instead
**/
public static RequestFIPAServiceBehaviour getNonBlockingBehaviour(Agent a, String actionName) throws FIPAException {
DFAgentDescription dfd = new DFAgentDescription();
dfd.setName(a.getAID());
SearchConstraints constraints = new SearchConstraints();
constraints.setMaxResults(MINUSONE);
return getNonBlockingBehaviour(a,a.getDefaultDF(),actionName,dfd,constraints);
}
/**
the default SearchContraints are used.
a default AgentDescription is used, where only the agent AID is set.
* @see #getNonBlockingBehaviour(Agent a, AID dfName, String actionName, DFAgentDescription dfd, SearchConstraints constraints)
@deprecated Use <code>AchieveREInitiator</code> instead
**/
public static RequestFIPAServiceBehaviour getNonBlockingBehaviour(Agent a, AID dfName, String actionName) throws FIPAException {
DFAgentDescription dfd = new DFAgentDescription();
dfd.setName(a.getAID());
SearchConstraints constraints = new SearchConstraints();
constraints.setMaxResults(MINUSONE);
return getNonBlockingBehaviour(a,dfName,actionName,dfd,constraints);
}
/**
* The defautl DF is used.
the default SearchContraints are used.
* @see #getNonBlockingBehaviour(Agent a, AID dfName, String actionName, DFAgentDescription dfd, SearchConstraints constraints)
@deprecated Use <code>AchieveREInitiator</code> instead
**/
public static RequestFIPAServiceBehaviour getNonBlockingBehaviour(Agent a, String actionName, DFAgentDescription dfd) throws FIPAException {
SearchConstraints constraints = new SearchConstraints();
constraints.setMaxResults(MINUSONE);
return getNonBlockingBehaviour(a,a.getDefaultDF(),actionName,dfd,constraints);
}
/**
* the default SearchContraints are used.
* @see #getNonBlockingBehaviour(Agent a, AID dfName, String actionName, DFAgentDescription dfd, SearchConstraints constraints)
@deprecated Use <code>AchieveREInitiator</code> instead
**/
public static RequestFIPAServiceBehaviour getNonBlockingBehaviour(Agent a, AID dfName, String actionName, DFAgentDescription dfd) throws FIPAException {
SearchConstraints constraints = new SearchConstraints();
constraints.setMaxResults(MINUSONE);
return getNonBlockingBehaviour(a,dfName,actionName,dfd,constraints);
}
//#MIDP_EXCLUDE_END
/**
Default constructor.
*/
public DFService() {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -