Пример #1
0
  void replyNotUnderstood(ACLMessage msg) {
    // -----------------------------------------

    try {
      ContentElement content = getContentManager().extractContent(msg);
      ACLMessage reply = msg.createReply();
      reply.setPerformative(ACLMessage.NOT_UNDERSTOOD);
      getContentManager().fillContent(reply, content);
      send(reply);
      System.out.println("Not understood!");
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
Пример #2
0
    public void action() {

      try {
        ContentElement content = getContentManager().extractContent(query);
        Information info = (Information) ((Action) content).getAction();
        Object obj = processInformation(info);
        if (obj == null) replyNotUnderstood(query);
        else {
          ACLMessage reply = query.createReply();
          reply.setPerformative(ACLMessage.INFORM);
          Result result = new Result((Action) content, obj);
          getContentManager().fillContent(reply, result);
          send(reply);
          System.out.println("Information processed.");
        }
      } catch (Exception ex) {
        ex.printStackTrace();
      }
    }
Пример #3
0
    public void action() {

      try {
        ContentElement content = getContentManager().extractContent(request);
        CreateAccount ca = (CreateAccount) ((Action) content).getAction();
        Account acc = new Account();
        String id = generateId();
        acc.setId(id);
        acc.setName(ca.getName());
        Result result = new Result((Action) content, acc);
        ACLMessage reply = request.createReply();
        reply.setPerformative(ACLMessage.INFORM);
        getContentManager().fillContent(reply, result);
        send(reply);
        accounts.put(id, acc);
        operations.put(id, new ArrayList());
        System.out.println("Account [" + acc.getName() + " # " + acc.getId() + "] created!");
      } catch (Exception ex) {
        ex.printStackTrace();
      }
    }
Пример #4
0
    public void action() {

      ACLMessage msg = receive();
      if (msg == null) {
        block();
        return;
      }
      try {
        ContentElement content = getContentManager().extractContent(msg);
        Concept action = ((Action) content).getAction();

        switch (msg.getPerformative()) {
          case (ACLMessage.REQUEST):
            System.out.println("Request from " + msg.getSender().getLocalName());

            if (action instanceof CreateAccount)
              addBehaviour(new HandleCreateAccount(myAgent, msg));
            else if (action instanceof MakeOperation)
              addBehaviour(new HandleOperation(myAgent, msg));
            else replyNotUnderstood(msg);
            break;

          case (ACLMessage.QUERY_REF):
            System.out.println("Query from " + msg.getSender().getLocalName());

            if (action instanceof Information) addBehaviour(new HandleInformation(myAgent, msg));
            else replyNotUnderstood(msg);
            break;

          default:
            replyNotUnderstood(msg);
        }
      } catch (Exception ex) {
        ex.printStackTrace();
      }
    }
Пример #5
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);
  }
Пример #6
0
 protected void handleFailure(ACLMessage failure) {
   String text = failure.getContent();
   System.out.println(myAgent.getAID().getLocalName() + ": Curator failure (" + text + ")");
   exitCode = 1;
 }
Пример #7
0
 protected void handleInform(ACLMessage inform) {
   String text = inform.getContent();
   System.out.println(
       myAgent.getAID().getLocalName() + ": response from Curator (" + text + ")");
   exitCode = 0;
 }