Package jade.core.behaviours

Examples of jade.core.behaviours.DataStore


import jade.lang.acl.ACLMessage;

public class SSIteratedContractNetResponder extends SSContractNetResponder {
 
  public SSIteratedContractNetResponder(Agent a, ACLMessage cfp) {
    this(a, cfp, new DataStore());
  }
View Full Code Here


    /**
     * Construct for the class by creating a new empty DataStore
     * @see #SimpleAchieveREInitiator(Agent, ACLMessage, DataStore)
     **/
    public SimpleAchieveREInitiator(Agent a,ACLMessage msg){
  this(a,msg,new DataStore());
    }
View Full Code Here

      state = SEND_MSG_STATE;
      break;
  }
  case SEND_MSG_STATE:{
      //send the message. If there is more than one receiver only the first will be taken into account.
      DataStore ds = getDataStore();
      String conversationID = null;
      ACLMessage request = (ACLMessage)ds.get(REQUEST_SENT_KEY);
      if (request == null){
    //no message to send --> protocol finished;
    //state = FINAL_STATE;
    finished = true;
    break;
      }else{
    if(request.getConversationId()== null){
        conversationID = "C" +hashCode()+"_" + System.currentTimeMillis();
        request.setConversationId(conversationID);
    }
    else
        conversationID = request.getConversationId();

    mt = MessageTemplate.MatchConversationId(conversationID);

    //send the message only to the first receiver.
    Iterator receivers = request.getAllReceiver();
    AID r = (AID)receivers.next();
    request.clearAllReceiver();
    request.addReceiver(r);
     if(receivers.hasNext())
        if(logger.isLoggable(Logger.WARNING))
          logger.log(Logger.WARNING,"The message you are sending has more than one receivers. The message will be sent only to the first one !!");
    if(r.equals(myAgent.getAID())){
        //if myAgent is the receiver then modify the messageTemplate
        //to avoid intercepting the request as it was a reply.
        mt = MessageTemplate.and(mt,MessageTemplate.not(MessageTemplate.MatchCustom(request,true)));
    }

    //set the timeout
    //FIXME: if the Timeout is already expired before the message will be sent, it will be considered a infinite timeout
    Date d = request.getReplyByDate();
    if(d!=null)
        timeout = d.getTime() - (new Date()).getTime();
    else
        timeout = -1;
    endingTime = System.currentTimeMillis()+ timeout;
 
    myAgent.send(request);
    state = RECEIVE_REPLY_STATE;
      }
      break;
  }
  case RECEIVE_REPLY_STATE:{
      ACLMessage firstReply = myAgent.receive(mt);
      if(firstReply != null){
    DataStore ds = getDataStore();
    switch(firstReply.getPerformative()){
    case ACLMessage.AGREE:{
        state = RECEIVE_2ND_REPLY_STATE;
        Vector allResp = (Vector)ds.get(ALL_RESPONSES_KEY);
        allResp.addElement(firstReply);
        handleAgree(firstReply);
        //all the responses have been collected.
        handleAllResponses((Vector)getDataStore().get(ALL_RESPONSES_KEY));
        break;
    }
    case ACLMessage.REFUSE:{
        Vector allResp = (Vector) ds.get(ALL_RESPONSES_KEY);
        allResp.addElement(firstReply);
        state = ALL_REPLIES_RECEIVED_STATE;    
        handleRefuse(firstReply);
        break;
    }
    case ACLMessage.NOT_UNDERSTOOD:{
        Vector allResp = (Vector) ds.get(ALL_RESPONSES_KEY);
        allResp.addElement(firstReply);
        state = ALL_REPLIES_RECEIVED_STATE;     
        handleNotUnderstood(firstReply);
        break;
    }
    case ACLMessage.FAILURE:{
        Vector allResNot = (Vector) ds.get(ALL_RESULT_NOTIFICATIONS_KEY);
        allResNot.addElement(firstReply);
        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:{
View Full Code Here

    /**
     * Constructor of the behaviour that creates a new empty DataStore
     **/
    public SimpleAchieveREResponder(Agent a,MessageTemplate mt){
  this(a,mt,new DataStore());
    }
View Full Code Here

        }else
      block();
        break;
  }
  case PREPARE_RESPONSE_STATE:{
      DataStore ds = getDataStore();
      ACLMessage request = (ACLMessage)ds.get(REQUEST_KEY);
      ACLMessage response = null;
      state = SEND_RESPONSE_STATE;
      try{
    response = prepareResponse(request);
      }catch(NotUnderstoodException nue){
    response = request.createReply();
    response.setContent(nue.getMessage());
    response.setPerformative(ACLMessage.NOT_UNDERSTOOD);
      }catch(RefuseException re){
    response = request.createReply();
    response.setContent(re.getMessage());
    response.setPerformative(ACLMessage.REFUSE);
      }
      ds.put(RESPONSE_KEY,response);
      break;
  }
  case SEND_RESPONSE_STATE:{
      DataStore ds = getDataStore();
      ACLMessage response = (ACLMessage)ds.get(RESPONSE_KEY);
      if(response != null){
    ACLMessage receivedMsg = (ACLMessage)ds.get(REQUEST_KEY);
 
    response = arrangeMessage(receivedMsg,response);
    myAgent.send(response);
    if(response.getPerformative() == ACLMessage.AGREE)
        state = PREPARE_RES_NOT_STATE;
    else
        state = RESET_STATE;

      }else{
    //could directly send a resultNotification message.
    state = PREPARE_RES_NOT_STATE;
      }
      break;
  }
  case PREPARE_RES_NOT_STATE:{
      state = SEND_RESULT_NOTIFICATION_STATE;
      DataStore ds = getDataStore();
      ACLMessage request = (ACLMessage)ds.get(REQUEST_KEY);
      ACLMessage response = (ACLMessage)ds.get(RESPONSE_KEY);
      ACLMessage resNotification = null;
      try{
    resNotification = prepareResultNotification(request,response);
      }catch(FailureException fe){
    resNotification = request.createReply();
    resNotification.setContent(fe.getMessage());
    resNotification.setPerformative(ACLMessage.FAILURE);
      }
      ds.put(RESULT_NOTIFICATION_KEY,resNotification);
      break;
  }
  case SEND_RESULT_NOTIFICATION_STATE:{
      state = RESET_STATE;
      DataStore ds = getDataStore();
      ACLMessage resNotification = (ACLMessage)ds.get(RESULT_NOTIFICATION_KEY);
      if(resNotification != null){
    ACLMessage receivedMsg = (ACLMessage)ds.get(REQUEST_KEY);

    myAgent.send(arrangeMessage(receivedMsg,resNotification));
      }
      break;
  }
View Full Code Here

       Reset this behaviour using the same MessageTemplate.
     */
    public void reset(){
  finished = false;
  state = WAITING_MSG_STATE;
  DataStore ds = getDataStore();
  ds.remove(REQUEST_KEY);
  ds.remove(RESPONSE_KEY);
  ds.remove(RESULT_NOTIFICATION_KEY);
  super.reset();
    }
View Full Code Here

TOP

Related Classes of jade.core.behaviours.DataStore

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.