Example #1
0
  Main init() {

    // Create a chart to monitor the infection progress rate
    final XYLineChart chart =
        new XYLineChart("Infection Rate", 5.0, "Infected Nodes (%)", "time(s)");
    chart.setYRange(false, 0, 100);
    chart.setSeriesLinesAndShapes("s0", true, true);

    Gui.setFrameRectangle("MainFrame", 0, 0, 480, 480);
    Gui.setFrameRectangle("Infection Rate", 484, 0, 480, 480);

    // Create the simulation nodes
    for (int i = 0; i < TOTAL_NODES; i++) new Node();

    // Initialize the simulation nodes
    for (Node i : NodeDB.nodes()) i.init();

    NodeDB.randomNode().infect();

    // Sets up a periodic task that, at one second intervals, computes and shows the percentage of
    // infected nodes in the system
    // Stops the simulation when it detects that every node is infected...
    new PeriodicTask(1.0) {
      public void run() {
        double T = 0, N = 0;
        for (Node n : NodeDB.nodes()) {
          if (n.infected) T++;
          N++;
        }
        chart.getSeries("s0").add(Simulation.currentTime(), 100.0 * T / N);
        if (N == T) stop();
      };
    };

    // From time to time, select a random node to go fail and go offline...
    new Task(0) {
      public void run() {
        NodeDB.randomNode().crash();
        reSchedule(0.5 + 0.5 * rg.nextDouble()); // schedules a new execution of this task...
      }
    };

    // From time to time, create a new node. If the rate of births and deaths is the same,
    // the size of the system should stay constant on average.
    new Task(0) {
      public void run() {
        new Node().init();
        reSchedule(0.5 + 0.5 * rg.nextDouble()); // schedules a new execution of this task...
      }
    };

    super.setSimulationMaxTimeWarp(2.0);

    return this;
  }
  public void start() {
    nodes.addAll(readNodes);
    nodes.addAll(taskNodes);
    nodes.addAll(writerNodes);

    for (Node node : nodes) {
      node.init();
      node.pre();
    }

    for (ReadNode readNode : readNodes) {
      readNode.start();
    }
  }
 /**
  * Obtain a new Node randomly chosen from the given list. The Node is cloned and initialized, so
  * it can be used separatedly
  *
  * @param l The list of Nodes from which to choose
  * @param currentGlobalDepth The depth of the requested Node in the Tree
  */
 public Node newRandomNode(List<Node> l, int currentGlobalDepth) {
   int which = Common.globalRandom.nextInt(l.size());
   Node outNode = null;
   try {
     outNode = (Node) l.get(which).clone();
   }
   /* This should never happen, as nodes do support cloning, so it's ok
    * to catch this exception here
    */
   catch (CloneNotSupportedException e) {
     Logger.log(e);
   }
   outNode.init(config, problemData);
   outNode.setCurrentDepth(currentGlobalDepth);
   return outNode;
 }
 static Node alloc() {
   final int threshold = 2;
   int tryCnt = 0;
   while (true) {
     tryCnt++;
     Node n = pool.poll();
     if (n == null) {
       return new Node();
     } else {
       if (n.getRefCnt() > 0) {
         pool.add(n);
         if (tryCnt <= threshold) continue;
         else return new Node();
       } else {
         if (n.next != null) n.next.decRefCnt();
         n.init();
         return n;
       }
     }
   }
 }