/**
  * Calculate the number of roots, which could caused a reasoning process in Jadex.
  *
  * @return
  */
 private int calculateNoOfRoot() {
   SyncInteger noOfTopLevelGoal =
       (SyncInteger) getBeliefbase().getBelief("noOfTopLevelGoal").getFact();
   SyncInteger noOfMessageEvent =
       (SyncInteger) getBeliefbase().getBelief("noOfMessageEvent").getFact();
   int noOfRoot = noOfTopLevelGoal.read() + noOfMessageEvent.read();
   return noOfRoot;
 }
  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);
    }
  }