simpleachievereinitiator.java

来自「JADE(JAVA Agent开发框架)是一个完全由JAVA语言开发的软件,它简」· Java 代码 · 共 511 行 · 第 1/2 页

JAVA
511
字号
		    state = ALL_RESULT_NOTIFICATION_RECEIVED_STATE;		 
		    handleFailure(firstReply);
		    break;
		}
		case ACLMessage.INFORM:{
		    Vector allResNot= (Vector) ds.get(ALL_RESULT_NOTIFICATIONS_KEY);
		    allResNot.addElement(firstReply);
		    state = ALL_RESULT_NOTIFICATION_RECEIVED_STATE;		    
		    handleInform(firstReply);
		    break;
		}
		default:{
		    state = RECEIVE_REPLY_STATE;		 
		    handleOutOfSequence(firstReply);
		    break;
		}
		}
		break;
	    }else{
	
		if(timeout > 0){
		    long blockTime = endingTime - System.currentTimeMillis();
		  
		    if(blockTime <=0 )
			//timeout Expired
			state = ALL_REPLIES_RECEIVED_STATE;
		    else //timeout not yet expired.
			block(blockTime); 
		   
	          
		}else //request without timeout.
		    block();
		
		break;
	    }
	   
	}
	case RECEIVE_2ND_REPLY_STATE:{
	    //after received an AGREE message. Wait for the second message.
	 
	    ACLMessage secondReply = myAgent.receive(mt);
	    if(secondReply != null){
		DataStore ds = getDataStore();
		switch (secondReply.getPerformative()){
		case ACLMessage.INFORM:{
		    //call the method handleAllResponses since if an agree was arrived it was not called.
		    state = ALL_RESULT_NOTIFICATION_RECEIVED_STATE;
		   
		    Vector allResNot = (Vector) ds.get(ALL_RESULT_NOTIFICATIONS_KEY);
		    allResNot.addElement(secondReply);
		    handleInform(secondReply);
		    break;
		}
		case ACLMessage.FAILURE:{
		    state = ALL_RESULT_NOTIFICATION_RECEIVED_STATE;		   
		   
		    Vector allResNot = (Vector) ds.get(ALL_RESULT_NOTIFICATIONS_KEY);
		    allResNot.addElement(secondReply);
		    handleFailure(secondReply);		    
		    break;
		}
		default:{
		    state = RECEIVE_REPLY_STATE;		   
		    handleOutOfSequence(secondReply);
		    break;
		}
		}
		break;
	    }else{
		block();
		break;
	    }
	}
	case ALL_REPLIES_RECEIVED_STATE:{
	    //after received a NOT-UNDERSTOOD and REFUSE message.
	    //call the handleAllResponses and then the handleAllResultNotification without any message.
	    state = ALL_RESULT_NOTIFICATION_RECEIVED_STATE;
	    handleAllResponses((Vector)(getDataStore().get(ALL_RESPONSES_KEY)));
	    break;
	}
	case ALL_RESULT_NOTIFICATION_RECEIVED_STATE:{
	    //after an INFORM or FAILURE message arrived.
	    finished = true;	  
	    handleAllResultNotifications((Vector)(getDataStore().get(ALL_RESULT_NOTIFICATIONS_KEY)));
	    break;
	}
	default: break;
	}
    }

    public void onStart(){
	initializeDataStore();
    }

    public boolean done(){
  	return finished;
    }

    /**
     * This method must return the ACLMessage to be sent.
     * This default implementation just return the ACLMessage object passed in the constructor.
     * Programmer might override the method in order to return a different ACLMessage.
     * Note that for this simple version of protocol, the message will be just send to the first receiver set.
     * @param msg the ACLMessage object passed in the constructor.
     * @return a ACLMessage.
     **/
    protected ACLMessage prepareRequest(ACLMessage msg){
	return msg;
    }

    /**
     * This method is called every time an <code>agree</code>
     * message is received, which is not out-of-sequence according
     * to the protocol rules.
     * This default implementation does nothing; programmers might
     * wish to override the method in case they need to react to this event.
     * @param agree the received agree message
     **/
    protected void handleAgree(ACLMessage msg){
		if(logger.isLoggable(Logger.FINE))
			logger.log(Logger.FINE,"in HandleAgree: " + msg.toString());

    }   

    /**
     * This method is called every time a <code>refuse</code>
     * message is received, which is not out-of-sequence according
     * to the protocol rules.
     * This default implementation does nothing; programmers might
     * wish to override the method in case they need to react to this event.
     * @param refuse the received refuse message
     **/
    protected void handleRefuse(ACLMessage msg){
		if(logger.isLoggable(Logger.FINE))
			logger.log(Logger.FINE,"in HandleRefuse: " + msg.toString());
    }

    /**
     * This method is called every time a <code>not-understood</code>
     * message is received, which is not out-of-sequence according
     * to the protocol rules.
     * This default implementation does nothing; programmers might
     * wish to override the method in case they need to react to this event.
     * @param notUnderstood the received not-understood message
     **/
    protected void handleNotUnderstood(ACLMessage msg){
		if(logger.isLoggable(Logger.FINE))
			logger.log(Logger.FINE,"in HandleNotUnderstood: " + msg.toString());
    }

    /**
     * This method is called every time a <code>inform</code>
     * message is received, which is not out-of-sequence according
     * to the protocol rules.
     * This default implementation does nothing; programmers might
     * wish to override the method in case they need to react to this event.
     * @param inform the received inform message
     **/
    protected void handleInform(ACLMessage msg){
	if(logger.isLoggable(Logger.FINE))
		logger.log(Logger.FINE,"in HandleInform: " + msg.toString());
    }

    /**
     * This method is called every time a <code>failure</code>
     * message is received, which is not out-of-sequence according
     * to the protocol rules.
     * This default implementation does nothing; programmers might
     * wish to override the method in case they need to react to this event.
     * @param failure the received failure message
     **/
    protected void handleFailure(ACLMessage msg){
	if(logger.isLoggable(Logger.FINEST))
		logger.log(Logger.FINEST,"in HandleFailure: " + msg.toString());
    }

    /**
     * This method is called every time a 
     * message is received, which is out-of-sequence according
     * to the protocol rules.
     * This default implementation does nothing; programmers might
     * wish to override the method in case they need to react to this event.
     * @param msg the received message
     **/
    protected void handleOutOfSequence(ACLMessage msg){
	if(logger.isLoggable(Logger.FINEST))
		logger.log(Logger.FINEST,"in HandleOutOfSequence: " + msg.toString());
    }

    /**
     * This method is called when all the responses have been
     * collected or when the timeout is expired.
     * By response message we intend here all the <code>agree, not-understood,
     * refuse</code> received messages, which are not
     * not out-of-sequence according
     * to the protocol rules.
     * This default implementation does nothing; programmers might
     * wish to override the method in case they need to react to this event
     * by analysing all the messages in just one call.
     * @param responses the Vector of ACLMessage objects that have been received 
     **/
    protected void handleAllResponses(Vector msgs){
	if(logger.isLoggable(Logger.FINEST))
		logger.log(Logger.FINEST,myAgent.getName()+"in handleAllResponses: ");    
    }

    /**
     * This method is called when all the result notification messages 
     * have been collected. 
     * By result notification message we intend here all the <code>inform, 
     * failure</code> received messages, which are not
     * not out-of-sequence according to the protocol rules.
     * This default implementation does nothing; programmers might
     * wish to override the method in case they need to react to this event
     * by analysing all the messages in just one call.
     * @param resultNodifications the Vector of ACLMessage object received 
     **/
    protected void handleAllResultNotifications(Vector msgs){
	if(logger.isLoggable(Logger.FINEST))
		logger.log(Logger.FINEST,myAgent.getName()+ "in HandleAllResultNotification: ");
    }

    /**
       This method resets this behaviour so that it restarts from the initial 
       state of the protocol with a null message.   
    */
    public void reset(){
	reset(null);
    }

    /**
       This method resets this behaviour so that it restarts the protocol with 
       another request message.
       @param msg updates message to be sent.
    */
    public void reset(ACLMessage msg){

	finished = false;
	state = PREPARE_MSG_STATE;
	getDataStore().put(REQUEST_KEY,msg);
	initializeDataStore();
	super.reset();
    }

    private void initializeDataStore(){

	Vector l = new Vector();
	getDataStore().put(ALL_RESPONSES_KEY,l);
	l = new Vector();
	getDataStore().put(ALL_RESULT_NOTIFICATIONS_KEY,l);
    }

}//end class SimpleAchieveREInitiator
 

⌨️ 快捷键说明

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