package modules.hierarchy;

import utils.*;
import simulator.*;
import modules.gradient.*;
import modules.node.*;
import java.util.*;

public class NodeLevelExclusionEstimator implements ProcessorLoadEstimator {
  Pair nid; Symbol lid;
  public NodeLevelExclusionEstimator(Pair nid, Symbol lid) 
  { this.nid=nid; this.lid=lid; }

  public double estimate(PortedProcessor pp) {
    // get the PN and AG modules
    Vector pnr = getLevelReflectors(pp);
    if(pnr.size()>1) return -(pnr.size()-1); 
    else if(pnr.size()>0 && nid.equals(pnr.get(0))) return 1;
    else return -1;
  }

  private Vector getLevelReflectors(PortedProcessor pp) {
    PersistentNodeB pn = (PersistentNodeB)pp.getModule("PersistentNode");
    Vector nodes = pn.getNodeReflectors();
    Iterator i=nodes.iterator(); 
    while(i.hasNext()) { 
      Object id=i.next();
      if(!(id instanceof Pair) || (lid!=((Pair)id).car())) i.remove();
    }
    return nodes;
  }
}
