protected long getLongAttribute(String id, long defaultValue) {
   if (attributeDefined(id)) {
     return model.getLongAttribute(id);
   } else {
     return defaultValue;
   }
 }
 /**
  * Get all killed nodes in the community model.
  *
  * @return List
  */
 protected List getDeadNodes() {
   List deadNodes = new ArrayList();
   String nodes[] = model.listEntries(CommunityStatusModel.NODE, DefaultRobustnessController.DEAD);
   for (int i = 0; i < nodes.length; i++) {
     deadNodes.add(nodes[i]);
   }
   return deadNodes;
 }
 /**
  * Get all new nodes in the community model.
  *
  * @return List
  */
 protected List getVacantNodes() {
   List vacantNodes = new ArrayList();
   String nodes[] =
       model.listEntries(CommunityStatusModel.NODE, DefaultRobustnessController.ACTIVE);
   for (int i = 0; i < nodes.length; i++) {
     if (isVacantNode(nodes[i])) {
       vacantNodes.add(nodes[i]);
     }
   }
   return vacantNodes;
 }
 protected boolean attributeDefined(String id) {
   return model.hasAttribute(id);
 }
  public void moveAgents(Map layout) {

    String communityName = model.getCommunityName();

    Map oldnodes = new HashMap(); // save all nodes and the number of agents in each current node
    Map newnodes =
        new HashMap(); // save all nodes and the number of agents in each node after balancing

    Map temp = new HashMap();
    synchronized (layout) {
      temp.putAll(layout);
    }

    // remove all agents who don't need move.
    for (Iterator it = temp.entrySet().iterator(); it.hasNext(); ) {
      Map.Entry me = (Map.Entry) it.next();
      String agent = (String) me.getKey();
      String newNode = (String) me.getValue();
      increaseCounts(newnodes, newNode);
      String currentNode = model.getLocation(agent);
      if (model.getType(agent) == CommunityStatusModel.AGENT && currentNode != null) {
        increaseCounts(oldnodes, currentNode);
        if (newNode.equals(currentNode)) {
          it.remove();
        }
      } else {
        it.remove();
      }
    }

    boolean needChange = true; // if the result of compare of two sets need be changed?
    String lastChangeNode = ""; // what is the last result of the comparison?
    int checkDiffCount = 0; // how many times we get a "useless" result?
    int nodeSize = oldnodes.size() > newnodes.size() ? oldnodes.size() : newnodes.size();
    /**
     * This loop does the following jobs to keep node balancing during agents moving: 1. Find the
     * node who has the most number of agents need to be moved out. 2. Move five agents from the
     * node to destinations. 3. Repeat step1 to step2 until all agents get destinations.
     */
    while (!temp.isEmpty()) {
      String node = getBiggestDifference(oldnodes, newnodes, needChange, lastChangeNode);
      if (needChange) checkDiffCount = checkDiffCount + 1;
      else checkDiffCount = 0;
      int count = 0;
      lastChangeNode = node;
      needChange = true;
      for (Iterator it = temp.keySet().iterator(); it.hasNext(); ) {
        if (count == 5) {
          break;
        }
        String agent = (String) it.next();
        String newNode = (String) temp.get(agent);
        String currentNode = model.getLocation(agent);
        if ((currentNode.equals(node) && !(newNode.equals(currentNode)))
            || node.equals("")
            || checkDiffCount >= nodeSize) {
          if (logger.isInfoEnabled()) {
            logger.info(
                "move agent "
                    + agent
                    + " from "
                    + currentNode
                    + " to "
                    + newNode
                    + " in community "
                    + communityName);
          }
          moveHelper.moveAgent(agent, currentNode, newNode, communityName);
          count++;
          needChange = false;
          int num = ((Integer) oldnodes.get(currentNode)).intValue();
          oldnodes.put(currentNode, new Integer(num - 1));
          if (oldnodes.containsKey(newNode)) {
            num = ((Integer) oldnodes.get(newNode)).intValue();
            oldnodes.put(newNode, new Integer(num + 1));
          } else {
            oldnodes.put(newNode, new Integer(1));
          }
          it.remove();
        }
      }
    }
    if (logger.isInfoEnabled()) {
      logger.info("LoadBalance finished.");
    }
  }
 protected List getExcludedNodes() {
   return model.search("(UseForRestarts=False)");
 }
 protected boolean isVacantNode(String name) {
   return model.entitiesAtLocation(name, CommunityStatusModel.AGENT).length == 0;
 }