예제 #1
0
 /**
  * create a new ACLMessage that is a reply to this message. In particular, it sets the following
  * parameters of the new message: receiver, language, ontology, protocol, conversation-id,
  * in-reply-to, reply-with. The programmer needs to set the communicative-act and the content. Of
  * course, if he wishes to do that, he can reset any of the fields.
  *
  * @return the ACLMessage to send as a reply
  */
 public ACLMessage createReply() {
   ACLMessage m = new ACLMessage(getPerformative());
   Iterator it = getAllReplyTo();
   while (it.hasNext()) m.addReceiver((AID) it.next());
   if ((reply_to == null) || reply_to.isEmpty()) m.addReceiver(getSender());
   m.setLanguage(getLanguage());
   m.setOntology(getOntology());
   m.setProtocol(getProtocol());
   m.setInReplyTo(getReplyWith());
   if (source != null) m.setReplyWith(source.getName() + java.lang.System.currentTimeMillis());
   else m.setReplyWith("X" + java.lang.System.currentTimeMillis());
   m.setConversationId(getConversationId());
   // Copy only well defined user-def-params
   String trace = getUserDefinedParameter(TRACE);
   if (trace != null) {
     m.addUserDefinedParameter(TRACE, trace);
   }
   // #CUSTOM_EXCLUDE_BEGIN
   // Set the Aclrepresentation of the reply message to the aclrepresentation of the sent message
   if (messageEnvelope != null) {
     m.setDefaultEnvelope();
     String aclCodec = messageEnvelope.getAclRepresentation();
     if (aclCodec != null) m.getEnvelope().setAclRepresentation(aclCodec);
   } else m.setEnvelope(null);
   // #CUSTOM_EXCLUDE_END
   return m;
 }
  @Override
  public void action() {
    block(15000);
    if (Biblioteca.podeTrocarDia()) {

      Biblioteca.quantidadeClientesAtendidos = 0;
      Biblioteca.quantidadeClientesInicioDia = Biblioteca.clienteIndex;

      DFAgentDescription template = new DFAgentDescription();
      ServiceDescription sd = new ServiceDescription();
      sd.setType(Cliente.TIPO_SERVICO_ACORDAR);
      sd.setName(Cliente.NOME_SERVICO_ACORDAR);
      template.addServices(sd);
      Ambiente.HOJE.add(Calendar.DATE, 1);
      System.out.println("Passou o dia, data atual: " + Ambiente.HOJE.getTime());

      Biblioteca.data.setText(new SimpleDateFormat("dd/MM/yyyy").format(Ambiente.HOJE.getTime()));
      try {

        DFAgentDescription[] result = DFService.search(myAgent, template);

        if (result != null && result.length > 0) {
          ACLMessage message = new ACLMessage(ACLMessage.REQUEST);
          message.setProtocol(FIPANames.InteractionProtocol.FIPA_REQUEST);
          message.setConversationId(ID_MENSAGEM_ACORDAR_CLIENTE);
          for (int i = 0; i < result.length; i++) {
            message.addReceiver(result[i].getName());
          }
          myAgent.addBehaviour(new IniciarAcordarCliente(myAgent, message));
        }
      } catch (FIPAException ex) {
        Logger.getLogger(Cliente.class.getName()).log(Level.SEVERE, null, ex);
      }
    }
  }
예제 #3
0
 void enviarMensaje(String mensaje) {
   ACLMessage msg = new ACLMessage(ACLMessage.INFORM);
   for (int i = 0; i < m_AidListaMensajeros.size(); i++) {
     msg.addReceiver(m_AidListaMensajeros.elementAt(i));
   }
   msg.setProtocol(FIPANames.InteractionProtocol.FIPA_REQUEST);
   msg.setConversationId("MS");
   msg.setContent(mensaje);
   send(msg);
   System.out.println(getLocalName() + ": Ha enviado un mensaje: " + mensaje);
 }
예제 #4
0
 private void job() {
   if (firstRound) {
     long time = System.currentTimeMillis();
     // Insert the start-time ion the protocol field
     msg.setProtocol(String.valueOf(time));
     firstRound = false;
   }
   send(msg);
   if (timeInterval == STEPBYSTEP_TIME_INTERVAL) {
     notifyReady();
   }
 }
예제 #5
0
  /**
   * Request for medicine.
   *
   * <p>This method sends a <b> FIPA REQUEST </b> message to all agents who offers the <tt>
   * m_sMedicService </tt> service.
   *
   * <p>The content of message is: <tt> ( x , y , z ) ( health ) </tt>.
   *
   * <p>Variable <tt> m_iMedicsCount </tt> is updated.
   *
   * <p><em> It's very useful to overload this method. </em>
   */
  protected void CallForMedic() {

    try {

      DFAgentDescription dfd = new DFAgentDescription();
      ServiceDescription sd = new ServiceDescription();
      sd.setType(m_sMedicService);
      dfd.addServices(sd);
      DFAgentDescription[] result = DFService.search(this, dfd);

      if (result.length > 0) {

        m_iMedicsCount = result.length;

        // Fill the REQUEST message
        ACLMessage msg = new ACLMessage(ACLMessage.REQUEST);

        for (int i = 0; i < result.length; i++) {

          DFAgentDescription dfdMedic = result[i];
          AID Medic = dfdMedic.getName();
          if (!Medic.equals(getName())) msg.addReceiver(dfdMedic.getName());
          else m_iMedicsCount--;
        }
        msg.setProtocol(FIPANames.InteractionProtocol.FIPA_REQUEST);
        msg.setConversationId("CFM");
        msg.setContent(
            " ( "
                + m_Movement.getPosition().x
                + " , "
                + m_Movement.getPosition().y
                + " , "
                + m_Movement.getPosition().z
                + " ) ( "
                + GetHealth()
                + " ) ");
        send(msg);
        System.out.println(getLocalName() + ": Need a Medic! (v21)");

      } else {
        m_iMedicsCount = 0;
      }
    } catch (FIPAException fe) {
      fe.printStackTrace();
    }
  }
예제 #6
0
  protected ACLMessage prepareRequest(ACLMessage request) {

    log.debug("WSIGBehaviour.prepareRequest");

    request = new ACLMessage(ACLMessage.REQUEST);
    request.setLanguage(codec.getName());
    request.setOntology(onto.getName());
    request.setProtocol(FIPANames.InteractionProtocol.FIPA_REQUEST);
    request.setReplyByDate(new Date(System.currentTimeMillis() + timeout));
    try {
      request.addReceiver(agentReceiver);
      myAgent.getContentManager().fillContent(request, new Action(agentReceiver, agentAction));

    } catch (Exception e) {
      status = FAILURE_STATUS;
      request = null;
      error = "Action encoding error: " + e.getMessage();
      log.error(e);
    }
    return request;
  }
예제 #7
0
  // generates and sends messages
  @Override
  void generate() {
    if (rand.nextDouble() < sprob) {
      try {
        int i = rand.nextInt(myUser.getReceivers().size());
        ACLMessage msgL3 = new ACLMessage(ACLMessage.INFORM);
        msgL3.setContentObject(new Integer(1));
        msgL3.addReceiver(myUser.getReceivers().get(i));
        msgL3.setSender(myUser.getAID());

        ACLMessage msgL2 = new ACLMessage(ACLMessage.INFORM);
        msgL2.setContentObject(msgL3);
        msgL2.setConversationId(Constants.NULL_CID);
        msgL2.addReceiver(myUser.getGateway());
        msgL2.setProtocol(Constants.INFORM_MESSAGE);

        log.log(
            Logger.INFO,
            myUser.getLocalName()
                + "# sended message to "
                + myUser.getReceivers().get(i).getLocalName());
        myUser.send(msgL2);
      } catch (IOException e) {
        log.log(Logger.SEVERE, "problems with output stream exception: ", e);
      } catch (IllegalArgumentException e) {
        log.log(
            Logger.WARNING,
            "User "
                + myUser.getLocalName()
                + " must have at least one destination user to generate traffic",
            e);
        block();
      } catch (Exception e) {
        log.log(Logger.SEVERE, "Exception", e);
      }
    }
  }
예제 #8
0
  private ACLMessage prepareRequest(AID agent) {
    ACLMessage request = new ACLMessage(ACLMessage.REQUEST);
    request.addReceiver(getAMS());
    request.setLanguage(FIPANames.ContentLanguage.FIPA_SL0);
    request.setOntology(MobilityOntology.NAME);
    request.setProtocol(FIPANames.InteractionProtocol.FIPA_REQUEST);

    Action act = new Action();
    act.setActor(getAMS());

    WhereIsAgentAction action = new WhereIsAgentAction();
    action.setAgentIdentifier(agent);
    act.setAction(action);

    try {
      getContentManager().fillContent(request, act);
    } catch (CodecException e) {
      e.printStackTrace();
    } catch (OntologyException e) {
      e.printStackTrace();
    }

    return request;
  }
예제 #9
0
  /**
   * Prints a hello message and creates a sequential behavior.
   *
   * <p>First sub-behavior is a finite state machine, which tries to get tour information. It
   * contacts tour guide agent to get the tour guide. In case of failure, machine ends. If a
   * response is received, it contacts curator agent to get tour details.
   *
   * <p>Second sub-behavior prints goodbye message.
   */
  protected void setup() {
    /*
     * Hello message.
     */
    System.out.println(this.getAID().getLocalName() + ": begin operation");

    /*
     * Request sent to the tour guide. It uses TourGuideInitiator class to
     * make communication easier.
     */
    ACLMessage initTourRequest = new ACLMessage(ACLMessage.REQUEST);
    initTourRequest.setProtocol(FIPANames.InteractionProtocol.FIPA_REQUEST);
    initTourRequest.setContent("request-tour-guide");

    /*
     * The Profiler looks for the registered Tour Guides.
     */

    DFAgentDescription template = new DFAgentDescription();
    ServiceDescription sd = new ServiceDescription();
    sd.setType("tour-guide");
    template.addServices(sd);
    try {
      DFAgentDescription[] result = DFService.search(this, template);
      for (int i = 0; i < result.length; ++i) {
        initTourRequest.addReceiver(result[i].getName());
      }
    } catch (FIPAException fe) {
      fe.printStackTrace();
    }

    TourGuideInitiator initGuide = new TourGuideInitiator(this, initTourRequest);

    /*
     * Same as above, but with curator agent.
     */
    ACLMessage detailTourRequest = new ACLMessage(ACLMessage.REQUEST);
    detailTourRequest.setProtocol(FIPANames.InteractionProtocol.FIPA_REQUEST);
    detailTourRequest.setContent("request-tour-details");

    /*
     * The Profiler looks for the registered curators.
     */

    template = new DFAgentDescription();
    sd = new ServiceDescription();
    sd.setType("curator");
    template.addServices(sd);
    try {
      DFAgentDescription[] result = DFService.search(this, template);
      for (int i = 0; i < result.length; ++i) {
        detailTourRequest.addReceiver(result[i].getName());
      }
    } catch (FIPAException fe) {
      fe.printStackTrace();
    }

    CuratorInitiator initCurator = new CuratorInitiator(this, detailTourRequest);

    /*
     * State machine consists of two states:
     * 1. Communication with tour guide agent
     * 2. Communication with curator agent
     * 3. Final state
     *
     * It is constructed below.
     */
    OneShotBehaviour lastState =
        new OneShotBehaviour(this) {
          public void action() {}
        };

    FSMBehaviour fsm = new FSMBehaviour(this);
    fsm.registerFirstState(initGuide, STATE_GET_GUIDE);
    fsm.registerState(initCurator, STATE_GET_DETAILS);
    fsm.registerLastState(lastState, STATE_LAST);
    fsm.registerTransition(STATE_GET_GUIDE, STATE_GET_DETAILS, 0);
    fsm.registerTransition(STATE_GET_GUIDE, STATE_LAST, 1);
    fsm.registerDefaultTransition(STATE_GET_DETAILS, STATE_LAST);

    /*
     * Sequential behavior is constructed and added as default agent
     * behavior.
     */
    SequentialBehaviour seqBeh = new SequentialBehaviour(this);
    seqBeh.addSubBehaviour(fsm);
    seqBeh.addSubBehaviour(
        new OneShotBehaviour(this) {
          public void action() {
            System.out.println(myAgent.getAID().getLocalName() + ": closing");
          }

          public int onEnd() {
            myAgent.doDelete();
            return super.onEnd();
          }
        });
    addBehaviour(seqBeh);
  }
예제 #10
0
파일: ASU.java 프로젝트: agentlab/sg-sim
  public void setup() {
    System.out.println("ASU.setup()");

    getContentManager().registerLanguage(codec);
    getContentManager().registerOntology(ontology);

    ACLMessage aclmsg = new ACLMessage(ACLMessage.REQUEST);
    AssignPowerRequest apr = new AssignPowerRequest();
    apr.setAssignedPower(100000);

    aclmsg.setLanguage(codec.getName());
    aclmsg.setOntology(ontology.getName());
    aclmsg.setProtocol(FIPANames.InteractionProtocol.FIPA_REQUEST);
    aclmsg.setReplyByDate(new Date(System.currentTimeMillis() + 30000));

    AMSAgentDescription[] agents = null;
    try {
      SearchConstraints c = new SearchConstraints();
      c.setMaxResults(new Long(-1));

      agents = AMSService.search(this, new AMSAgentDescription(), c);
    } catch (Exception e) {
      System.out.println("Problem searching AMS: " + e);
      e.printStackTrace();
    }

    for (AMSAgentDescription agent : agents) {

      AID agentID = agent.getName();

      try {
        getContentManager().fillContent(aclmsg, new Action(agentID, apr));
      } catch (Exception e) {
        e.printStackTrace();
      }

      System.out.println(agentID.getLocalName());

      if (agentID.getLocalName().compareTo("TURBINE") != 0) continue;

      aclmsg.clearAllReceiver();
      aclmsg.addReceiver(agentID);

      System.out.println("Sending to " + agentID);

      addBehaviour(new ASURequestInitiatorBehaviour(this, aclmsg));

      try {
        Thread.yield();
        Thread.sleep(5000);
      } catch (Exception e) {
        System.out.println("INTERRUPT");
      }

      ACLMessage aclmsg2 = new ACLMessage(ACLMessage.REQUEST);
      BeginPowerTransitionRequest bptr = new BeginPowerTransitionRequest();
      bptr.setAssignedPowerValid(false);

      aclmsg2.setLanguage(codec.getName());
      aclmsg2.setOntology(ontology.getName());
      aclmsg2.setProtocol(FIPANames.InteractionProtocol.FIPA_REQUEST);
      aclmsg2.setReplyByDate(new Date(System.currentTimeMillis() + 30000));

      try {
        getContentManager().fillContent(aclmsg2, new Action(agentID, bptr));
      } catch (Exception e) {
        e.printStackTrace();
      }

      aclmsg2.clearAllReceiver();
      aclmsg2.addReceiver(agentID);

      System.out.println("Sending to " + agentID);

      addBehaviour(new ASURequestInitiatorBehaviour(this, aclmsg2));
    }
  }
예제 #11
0
  /**
   * @param out
   * @param parser
   * @param sender the sender of the message
   * @param receiver the receiver of the message
   * @param actionName the action requested
   * @param parentDF the df to wich request an action (used for federate action)
   */
  JADEAppletRequestProto(
      DFAppletCommunicator communicator,
      AID receiver,
      String actionName,
      Object description,
      Object parentDF,
      SearchConstraints constraints)
      throws FIPAException {
    super(communicator.getStream(), communicator.getParser(), new ACLMessage(ACLMessage.REQUEST));

    this.gui = communicator.getGUI();
    this.dfApplet = communicator;
    ACLMessage request = new ACLMessage(ACLMessage.REQUEST);
    // request.setSender(sender);
    request.addReceiver(receiver);
    request.setProtocol(FIPANames.InteractionProtocol.FIPA_REQUEST);
    request.setLanguage(FIPANames.ContentLanguage.FIPA_SL);
    request.setOntology(DFAppletOntology.NAME);
    request.setReplyWith("rw" + (new Date()).getTime());
    request.setConversationId("conv" + (new Date()).getTime());

    this.reqMsg = (ACLMessage) request.clone();
    this.action = actionName;
    this.receiver = receiver;
    this.parent = (AID) parentDF;

    Action act = new Action();
    act.setActor(receiver);
    if (actionName.equalsIgnoreCase(DFAppletVocabulary.FEDERATE)) {

      Federate action = new Federate();
      action.setDf((AID) parentDF);
      action.setDescription((DFAgentDescription) description);

      act.setAction(action);

    } else if (actionName.equalsIgnoreCase(DFAppletVocabulary.GETDESCRIPTION))
      act.setAction(new GetDescription());
    else if (actionName.equalsIgnoreCase(DFAppletVocabulary.GETPARENTS))
      act.setAction(new GetParents());
    else if (actionName.equalsIgnoreCase(DFAppletVocabulary.GETDESCRIPTIONUSED)) {
      GetDescriptionUsed action = new GetDescriptionUsed();
      action.setParentDF((AID) parentDF);
      act.setAction(action);
    } else if (actionName.equalsIgnoreCase(DFAppletVocabulary.DEREGISTERFROM)) {
      DeregisterFrom action = new DeregisterFrom();
      action.setDf((AID) parentDF);
      action.setDescription((DFAgentDescription) description);

      act.setAction(action);
    } else if (actionName.equalsIgnoreCase(DFAppletVocabulary.REGISTERWITH)) {
      RegisterWith action = new RegisterWith();
      action.setDf((AID) parentDF);
      action.setDescription((DFAgentDescription) description);

      act.setAction(action);
    } else if (actionName.equalsIgnoreCase(DFAppletVocabulary.SEARCHON)) {
      SearchOn action = new SearchOn();
      action.setDf((AID) parentDF);
      action.setDescription((DFAgentDescription) description);
      action.setConstraints(constraints);

      act.setAction(action);
    } else if (actionName.equalsIgnoreCase(DFAppletVocabulary.MODIFYON)) {
      ModifyOn action = new ModifyOn();
      action.setDf((AID) parentDF);
      action.setDescription((DFAgentDescription) description);

      act.setAction(action);
    } else throw new UnsupportedFunction();

    // initialize SL0 Codec and FIPAAgentManagementOntology
    // FIXME for applet I have not the agent c = sender.lookupLanguage(SL0Codec.NAME);
    // if (c == null)
    c = new SLCodec();

    // Write the action in the :content slot of the request
    List content = new ArrayList();
    content.add(act);

    try {
      String s = ((SLCodec) c).encode(o, (AbsContentElement) o.fromObject(act));
      this.reqMsg.setContent(s);
    } catch (OntologyException oe) {
      oe.printStackTrace();
      throw new FIPAException("Ontology error: " + oe.getMessage());
    } catch (Exception e) {
      e.printStackTrace();
    }
  }