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

📄 amsservice.java

📁 java实现的P2P多agent中间件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			amsd.setName(a.getAID());
		checkIsValid(amsd);
		// Build a AMS action object for the request
		Modify m = new Modify();
		m.setDescription(amsd);
		
		Action act = new Action();
		act.setActor(AMSName);
		act.setAction(m);
		
		synchronized (cm) {
			try{    
				cm.fillContent(request, act);
			}
			catch(Exception e){
				throw new FIPAException("Error encoding REQUEST content. "+e);
			}
		}
		
		// Send message and collect reply
		doFipaRequestClient(a,request);
	}
	
	/**
	 The AID of the AMS is defaulted to the AMS of this platform.
	 @see #modify(Agent a, AID AMSName, AMSAgentDescription amsd)
	 **/
	public static void modify(Agent a, AMSAgentDescription amsd) throws FIPAException {
		modify(a,a.getAMS(),amsd);
	}
	
	/**
	 Searches for data contained within a <b>AMS</b> agent. 
	 @param a is the Agent performing the search 
	 @param AMSName The GUID of the <b>AMS</b> agent to start search from.
	 @param amsd A <code>AMSAgentDescriptor</code> object containing
	 data to search for; this parameter is used as a template to match
	 data against.
	 @param constraints of the search 
	 @return An array of <code>AMSAgentDescription</code> 
	 containing all found
	 items matching the given
	 descriptor, subject to given search constraints for search depth
	 and result size.
	 @exception FIPAException A suitable exception can be thrown when
	 a <code>refuse</code> or <code>failure</code> messages are
	 received from the AMS to indicate some error condition.
	 */
	public static AMSAgentDescription[] search(Agent a, AID AMSName, AMSAgentDescription amsd, SearchConstraints constraints) throws FIPAException {
		ACLMessage request = createRequestMessage(a, AMSName);
		
		// Build a AMS action object for the request
		Search s = new Search();
		s.setDescription(amsd);
		s.setConstraints(constraints);
		
		Action act = new Action();
		act.setActor(AMSName);
		act.setAction(s);
		
		synchronized (cm) {
			try{    
				cm.fillContent(request, act);
			}
			catch(Exception e){
				throw new FIPAException("Error encoding REQUEST content. "+e);
			}
		}
		
		// Send message and collect reply
		ACLMessage inform = doFipaRequestClient(a,request);
		
		Result r = null;
		synchronized (cm) {
			try{
				r = (Result) cm.extractContent( inform );
			}
			catch(Exception e){
				throw new FIPAException("Error decoding INFORM content. "+e);
			}
		}
		
		return toArray(r.getItems());
	}
	
	private static AMSAgentDescription[] toArray(List l) throws FIPAException {
		try {
			AMSAgentDescription[] items = new AMSAgentDescription[l.size()];
			for(int i = 0; i < l.size(); i++){
				items[i] = (AMSAgentDescription)l.get(i);
			}
			return items;
		}
		catch (ClassCastException cce) {
			throw new FIPAException("Found items are not AMSAgentDescriptions. "+cce);
		}
	}
	
	/**
	 * searches with the default AMS
	 * @see #search(Agent,AID,AMSAgentDescription,SearchConstraints)
	 **/
	public static AMSAgentDescription[] search(Agent a, AMSAgentDescription amsd, SearchConstraints constraints) throws FIPAException {
		return search(a,a.getAMS(),amsd,constraints);
	}
	
	/**
	 * searches with the default AMS and the default SearchConstraints.
	 * The default constraints specified by FIPA are max_results and max_depth
	 * both unspecified and left to the choice of the responder AMS.
	 * @see #search(Agent,AID,AMSAgentDescription,SearchConstraints)
	 **/
	public static AMSAgentDescription[] search(Agent a, AMSAgentDescription amsd) throws FIPAException {
		SearchConstraints constraints = new SearchConstraints();
		return search(a,a.getAMS(),amsd,constraints);
	}
	
	/**
	 * searches with the passed AMS by using the default SearchConstraints.
	 * The default constraints specified by FIPA are max_results and max_depth
	 * both unspecified and left to the choice of the responder AMS.
	 * @see #search(Agent,AID,AMSAgentDescription,SearchConstraints)
	 **/
	public static AMSAgentDescription[] search(Agent a, AID AMSName, AMSAgentDescription amsd) throws FIPAException {
		SearchConstraints constraints = new SearchConstraints();
		return search(a,AMSName,amsd,constraints);
	}
	
	
	/**
	 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 AMSName is the AID 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 amsd 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 amsdescription is not valid.)
	 @see jade.domain.FIPAAgentManagement.FIPAManagementOntology
	 **/
	public static RequestFIPAServiceBehaviour getNonBlockingBehaviour(Agent a, AID AMSName, String actionName, AMSAgentDescription amsd, SearchConstraints constraints) throws FIPAException {
		return new RequestFIPAServiceBehaviour(a,AMSName,actionName,amsd,constraints);
	}
	
	/**
	 the default AMS is used.
	 @see #getNonBlockingBehaviour(Agent a, AID AMSName, String actionName, AMSAgentDescription amsd, SearchConstraints constraints)
	 **/
	public static RequestFIPAServiceBehaviour getNonBlockingBehaviour(Agent a, String actionName, AMSAgentDescription amsd, SearchConstraints constraints) throws FIPAException {
		return getNonBlockingBehaviour(a,a.getAMS(),actionName,amsd,constraints);
	}
	
	/**
	 the default AMS is used.
	 the default SearchContraints are used.
	 a default AgentDescription is used, where only the agent AID is set.
	 @see #getNonBlockingBehaviour(Agent a, AID AMSName, String actionName, AMSAgentDescription amsd, SearchConstraints constraints)
	 * @see #search(Agent,AID,AMSAgentDescription)
	 **/
	public static RequestFIPAServiceBehaviour getNonBlockingBehaviour(Agent a, String actionName) throws FIPAException {
		AMSAgentDescription amsd = new AMSAgentDescription();
		amsd.setName(a.getAID());
		SearchConstraints constraints = new SearchConstraints();
		return getNonBlockingBehaviour(a,a.getAMS(),actionName,amsd,constraints);
	}
	
	/**
	 the default SearchContraints are used.
	 a default AgentDescription is used, where only the agent AID is set.
	 @see #getNonBlockingBehaviour(Agent a, AID AMSName, String actionName, AMSAgentDescription amsd, SearchConstraints constraints)
	 * @see #search(Agent,AID,AMSAgentDescription)
	 **/
	public static RequestFIPAServiceBehaviour getNonBlockingBehaviour(Agent a, AID amsName, String actionName) throws FIPAException {
		AMSAgentDescription amsd = new AMSAgentDescription();
		amsd.setName(a.getAID());
		SearchConstraints constraints = new SearchConstraints();
		return getNonBlockingBehaviour(a,amsName,actionName,amsd,constraints);
	}
	
	/**
	 the default AMS is used.
	 the default SearchContraints are used.
	 a default AgentDescription is used, where only the agent AID is set.
	 @see #getNonBlockingBehaviour(Agent a, AID AMSName, String actionName, AMSAgentDescription amsd, SearchConstraints constraints)
	 * @see #search(Agent,AID,AMSAgentDescription)
	 **/
	public static RequestFIPAServiceBehaviour getNonBlockingBehaviour(Agent a, String actionName, AMSAgentDescription amsd) throws FIPAException {
		SearchConstraints constraints = new SearchConstraints();
		return getNonBlockingBehaviour(a,a.getAMS(),actionName,amsd,constraints);
	}
	
	/**
	 the default AMS is used.
	 the default SearchContraints are used.
	 @see #getNonBlockingBehaviour(Agent a, AID AMSName, String actionName, AMSAgentDescription amsd, SearchConstraints constraints)
	 * @see #search(Agent,AID,AMSAgentDescription)
	 **/
	public static RequestFIPAServiceBehaviour getNonBlockingBehaviour(Agent a, AID amsName, String actionName, AMSAgentDescription amsd) throws FIPAException {
		SearchConstraints constraints = new SearchConstraints();
		return getNonBlockingBehaviour(a,amsName,actionName,amsd,constraints);
	}
	//#MIDP_EXCLUDE_END
	
	
	/**
	 Extracts the receiver a message could not be delivered to from
	 a FAILURE message received by the AMS.
	 @param a The agent that is calling this method.
	 @param failure The FAILURE message received by thye AMS.
	 @return the receiver a message could not be delivered to.
	 */
	public static AID getFailedReceiver(Agent a, ACLMessage failure) throws FIPAException {
		if (failure.getPerformative() != ACLMessage.FAILURE || !failure.getSender().equals(a.getAMS())) {
			throw new FIPAException("Invalid AMS FAILURE message");
		}
		try {
			String content = failure.getContent();
			int start = content.indexOf("MTS-error");
			start = content.indexOf(SL0Vocabulary.AID, start);
			SimpleSLTokenizer parser = new SimpleSLTokenizer(content.substring(start));
			return parseAID(parser);
		}
		catch (Exception e) {
			throw new FIPAException("Invalid content. "+e);
		}
	}
	/**
	 Default constructor.
	 */
	public AMSService() {
	}
	
}

⌨️ 快捷键说明

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