protected ACLMessage prepareResultNotification(ACLMessage msg, ACLMessage response)
      throws FailureException {
    boolean performed = false;
    ContentElement content;
    Concept action = null;

    System.out.println("pRN");

    try {
      content = myAgent.getContentManager().extractContent(msg);
      action = ((Action) content).getAction();

      if (action instanceof AssignPowerRequest) {
        AssignPowerRequest apr = (AssignPowerRequest) action;
        System.out.println("Performing AssignPowerRequest");
        System.out.println("Requester assigned power: " + apr.getAssignedPower());
        if (windTurbine.setAssignedPower(apr.getAssignedPower())) {
          System.out.println("Check passed, power assigned");
          performed = true;
        } else {
          System.out.println("Out of bounds, power not assigned");
          performed = false;
        }
      }

      if (action instanceof BeginPowerTransitionRequest) {
        BeginPowerTransitionRequest bptr = (BeginPowerTransitionRequest) action;
        boolean isOk = true;
        System.out.println("Performing BeginPowerTransitionRequest");
        if (bptr.getAssignedPowerIsValid()) {
          System.out.println("Requester assigned power: " + bptr.getAssignedPower());
          if (windTurbine.setAssignedPower(bptr.getAssignedPower())) {
            System.out.println("Check passed, power assigned");
          } else {
            System.out.println("Out of bounds, power not assigned");
            isOk = false;
          }
        } else {
          System.out.println("Requester did not assigned new power");
        }

        if (isOk) {
          windTurbine.change_power();
        }

        performed = isOk;
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

    if (performed) {
      ACLMessage inform = msg.createReply();
      Message sm = new Message();
      TimeDelay td = new TimeDelay();
      td.setTime(windTurbine.time_delay());
      System.out.println(
          "Agent "
              + myAgent.getLocalName()
              + ": Action successfully performed, time delay is "
              + windTurbine.time_delay());
      sm.setMsg(td);
      try {
        myAgent.getContentManager().fillContent(inform, new Action(msg.getSender(), sm));
      } catch (CodecException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (OntologyException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      inform.setPerformative(ACLMessage.INFORM);
      return inform;
    } else {
      throw new FailureException("out of bounds");
    }
  }
Ejemplo n.º 2
0
  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));
    }
  }