package testprocs;

import simulator.*;
import scripting.*;
import modules.color.*;
import modules.hierarchy.*;
import modules.neighborset.*;
import modules.gradient.*;
import utils.*;
import java.awt.*;
import java.util.*;
import java.io.*;

public class PNHExp implements ProcessorModule {
  static final String script = "modules.color.ColorModule\nmodules.gradient.ActiveGradient\nmodules.neighborset.NeighborSet\nmodules.broadcast.Broadcast\nmodules.region.RegionManager\nmodules.node.PersistentNodeB\nmodules.hierarchy.PNHierarchy\ntestprocs.PNHExp";

  public static void main (String argv[]) {

    try {
      out = new PrintWriter(new FileWriter("pnhtest.out"),true);
      out.println("Stability log for PNHierarchy");
    } catch(Exception e) {
      System.out.println("failure: "+e);
    }
    
    CommunicationsModel comm = new PerfectCommunicationsModel();
    ProcessorFactory pf = new ScriptProcFactory(script);
    Simulator sim = new Simulator(comm,pf,2000,0.04);
        
    // and... run!
    sim.setPaused(false);
    sim.run();
  }

  public Symbol getName() { return Symbol.GetSymbol("PNHExp"); }
  public void link(PortedProcessor p) {
    pp = p;
    pp.addClockListener(this);
    nh = (PNHierarchy)p.getModule("Hierarchy");
  }

  PortedProcessor pp; PNHierarchy nh;

  static double initiator = Double.NaN;
  public void init() {
    if(Double.isNaN(initiator)) {
      initiator=pp.getUID(); System.out.println("Starting...");
      out.println("Time\tProc ID\tOld ID\tNew ID");
    }
  }
  static PrintWriter out;

  Vector oldID = new Vector();
  public boolean signalEvent(Symbol name,Object data) {
    boolean dirty = false;
    if(name==PortedProcessor.kClock) {
      int t = ((Integer)data).intValue();
      
      Vector id = nh.getID();
      if(!oldID.equals(id)) {
        out.println(t+"\t"+pp.getUID()+"\t"+oldID+"\t"+id);
        oldID=id;
      }
    }
    return dirty;
  }

}


