예제 #1
0
  /** The plan body. */
  public void body() {
    Waste waste = (Waste) getBeliefbase().getBelief("carriedwaste").getFact();
    //		System.out.println("carriedwaste a ="+waste);
    //		if(waste==null)
    //			System.out.println("here");

    // Move to a not full waste-bin
    Wastebin wastebin = (Wastebin) getParameter("wastebin").getValue();
    if (wastebin == null) throw new PlanFailureException();

    Location location = wastebin.getLocation();
    IGoal moveto = createGoal("achievemoveto");
    moveto.getParameter("location").setValue(location);
    //		System.out.println("Created: "+location+" "+this);
    dispatchSubgoalAndWait(moveto);
    //		System.out.println("Reached: "+location+" "+this);

    // Drop waste to waste-bin.
    // IEnvironment env = (IEnvironment)getBeliefbase().getBelief("environment").getFact();
    // boolean success = env.dropWasteInWastebin(waste, wastebin);
    IGoal dg = createGoal("drop_waste_action");
    dg.getParameter("waste").setValue(waste);
    dg.getParameter("wastebin").setValue(wastebin);
    dispatchSubgoalAndWait(dg);

    // Update beliefs.
    //		getLogger().info("Dropping waste to wastebin!");
    wastebin.addWaste(waste);

    // Todo: Find out why atomic is needed.
    //		startAtomic();
    IBeliefSet wbs = getBeliefbase().getBeliefSet("wastebins");
    if (wbs.containsFact(wastebin)) {
      ((Wastebin) wbs.getFact(wastebin)).update(wastebin);
      //			wbs.updateFact(wastebin);
    } else {
      wbs.addFact(wastebin);
    }
    // getBeliefbase().getBeliefSet("wastebins").updateOrAddFact(wastebin);
    getBeliefbase().getBelief("carriedwaste").setFact(null);
    //		System.out.println("carriedwaste b =null");
    //		endAtomic();
  }
  private void updateAgentBeliefs() {
    ISpaceObject myself = (ISpaceObject) getBeliefbase().getBelief("myself").getFact();
    PerceptContainer localPerceptContainer =
        (PerceptContainer) myself.getProperty("perceptContainer");
    // Update Agent Beliefs
    Set<String> perceptIDSet = localPerceptContainer.perceptIDSet();
    // System.out.println("~~~~~Inside belief update :" + perceptIDSet.size() +"-"+this);
    // for (String perceptID : perceptIDSet)
    Object[] perceptIDSetA = perceptIDSet.toArray();
    for (int m = 0; m < perceptIDSetA.length; m++) {
      String perceptID = (String) perceptIDSetA[m];
      startAtomic();
      IGoal[] oldGoal = this.getGoalbase().getGoals();
      try {
        if (getBeliefbase().containsBelief(perceptID)) {
          Object oldFact = getBeliefbase().getBelief(perceptID).getFact();
          Object newFact = localPerceptContainer.read(perceptID);
          //					if (oldFact == null)
          //						System.out.println("NULLL OLD FACT CREATE EXCEPTION!");
          if (!newFact.equals(oldFact)) {
            getBeliefbase().getBelief(perceptID).setFact(newFact);
          }
          // System.out.println("------Inside belief update :" + perceptID +"-"+this);
        } else if (getBeliefbase().containsBeliefSet(perceptID)) {
          IBeliefSet beliefSet = getBeliefbase().getBeliefSet(perceptID);
          Object newFact = localPerceptContainer.read(perceptID);

          if (newFact.getClass().equals(beliefSet.getClazz())) {
            // Replace the old fact with the new one
            if (beliefSet.containsFact(newFact) == true) beliefSet.removeFact(newFact);
            beliefSet.addFact(newFact);
          } else {
            Object[] newFacts = (Object[]) newFact;
            for (int i = 0; i < newFacts.length; i++) {
              if (beliefSet.containsFact(newFacts[i]) == true) beliefSet.removeFact(newFacts[i]);
              beliefSet.addFact(newFacts[i]);
            }
            String nh = "";
            for (int i = 0; i < newFacts.length; i++) {
              nh = nh + newFacts[i].toString();
            }
            System.out.println(agentID + nh);
          }
        } else {
          // System.out.println("------Inside belief update");
          LOGGER.severe("perceptID is not recognized by Jadex's belief/beliefset");
          // throw new RuntimeException ("perceptID is not recognized by Jadex's belief/beliefset");
        }
      } catch (Exception e) {
        LOGGER.severe(e.getMessage());
      }

      // No need to track new number of goals, because the goal itself is created and the plan
      // are in queue to be launched even before this idleplan could dispatch another idleplan
      int noOfNewlyGeneratedGoal = this.getNoOfNewlyGeneratedGoalSince(oldGoal);
      endAtomic();
      if (noOfNewlyGeneratedGoal > 0) {
        SyncInteger noOfTopLevelGoal =
            (SyncInteger) getBeliefbase().getBelief("noOfTopLevelGoal").getFact();
        //				noOfTopLevelGoal = noOfTopLevelGoal + noOfNewlyGeneratedGoal;
        noOfTopLevelGoal.add(noOfNewlyGeneratedGoal);
        getBeliefbase().getBelief("noOfTopLevelGoal").setFact(noOfTopLevelGoal);
      }
      // System.out.println("------Inside belief update :" + perceptID +"-"+this);
    }
  }