@Override
 public void mouseClicked(MouseEvent me) {
   Element el = doc.getCharacterElement(viewToModel(me.getPoint()));
   if (el == null) return;
   AttributeSet as = el.getAttributes();
   if (as.isDefined("ip")) {
     String ip = (String) as.getAttribute("ip");
     ScriptException se = (ScriptException) as.getAttribute("exception");
     Node node = net.getAtIP(ip);
     if (node == null) {
       Utility.displayError("Error", "Computer does not exist");
       return;
     }
     String errorString =
         "--ERROR--\n"
             + "Error at line number "
             + se.getLineNumber()
             + " and column number "
             + se.getColumnNumber()
             + "\n"
             + se.getMessage();
     new ScriptDialog(
             parentFrame,
             str -> {
               if (str != null) {
                 node.setScript(str);
               }
             },
             node.getScript(),
             errorString)
         .setVisible(true);
   }
 }
  private void executeSecondLife() {
    VirtualEntity entity;
    VirtualEntityShell shell;
    VirtualWorldDistributionShell firstStep;
    Iterator it;
    SolipsisProtocol solipsis;
    int i, toPercentCoef, percent, userNb;
    VirtualWorldDistribution vwd;

    Globals.mapSize = 25600;
    try {
      Globals.slTrace = new SecondLifeTraceDistribution("data/SecondLifeRecord2.txt");
      this.trace = Globals.slTrace;
    } catch (IOException e) {
      System.err.println("File error");
      System.exit(1);
    }

    vwd = new VirtualWorldDistribution();
    Globals.distribution = vwd;
    if (this.trace.getTrace().isEmpty()) {
      System.err.println("The file is corrupted");
      System.exit(1);
    }
    System.err.println("Joining nodes...");
    userNb = this.trace.getTrace().get(0).countAvatars();
    percent = 0;
    firstStep = this.trace.getTrace().get(0);
    it = firstStep.getDistribution().entrySet().iterator();
    userNb = firstStep.countAvatars();
    toPercentCoef = (userNb > 100) ? userNb / 100 : 1;
    i = 0;
    Globals.stepCount = 1;
    while (it.hasNext()) {
      shell = (VirtualEntityShell) ((Map.Entry) it.next()).getValue();
      entity = new VirtualEntity(shell.getCoord());
      solipsis = (SolipsisProtocol) Network.get(i).getProtocol(applicativeLayerId);
      solipsis.setPeersimNodeId(i);
      solipsis.setVirtualEntity(entity);
      entity.setProtocol(solipsis);
      entity.setOrder(i + 1);
      entity.setId(shell.getId());
      vwd.addToDistribution(entity);
      if (i > 0) {
        solipsis.join((SolipsisProtocol) Network.get(i - 1).getProtocol(applicativeLayerId));
      }
      i++;
      if (i % toPercentCoef == 0) System.err.println(++percent + "%");
    }
    Network.setCapacity(userNb);
    System.err.println("Done.");
    System.err.println("Normalizing Solipsis topology...");
    percent = 0;
    it = vwd.getDistribution().entrySet().iterator();
    for (i = 0; i < userNb; i++) {
      entity = (VirtualEntity) ((Map.Entry) it.next()).getValue();
      //        	System.out.println("peer "+i+": "+entity.getNeighbors().size());
      entity.getProtocol().finalizeKnowledgeZone();
      //        	System.out.println("peer "+i+": "+entity.getNeighbors().size());
      entity.getProtocol().simplifyTopology();
      //        	System.out.println("peer "+i+": "+entity.getNeighbors().size());
      if (i % (3 * toPercentCoef) == 0) System.err.println(++percent + "%");
    }

    it = vwd.getDistribution().entrySet().iterator();
    for (i = 0; i < userNb; i++) {
      entity = (VirtualEntity) ((Map.Entry) it.next()).getValue();
      entity.refreshState();
      if (i % (3 * toPercentCoef) == 0) System.err.println(++percent + "%");
    }

    it = vwd.getDistribution().entrySet().iterator();
    for (i = 0; i < userNb; i++) {
      entity = (VirtualEntity) ((Map.Entry) it.next()).getValue();
      entity.getProtocol().maintainKnowledgeZone();
      entity.getProtocol().finalizeKnowledgeZone();
      entity.getProtocol().removeUnwantedNeighbors();
      //        	System.out.println("peer "+i+": "+entity.getNeighbors().size());
      if (i % (3 * toPercentCoef) == 0) System.err.println(++percent + "%");
    }

    System.err.println("Done.");
    //    	for (int i = 0; i < 15; i++) {
    //    		System.out.println("cycle...");
    //    		Globals.distribution.animate();
    //
    //    	}
    Globals.topologyIsReady = true;
    System.err.println("Populated");
    //		System.err.println("Collisions:" + this.collisionCount());
    //		System.exit(2);
    if (Globals.realTime) {
      //    		VirtualWorldMonitor map = new VirtualWorldMonitor(vwd);
      //    		Globals.monitor = map;
      //        	map.showVirtualWorld();
      Globals.recorder = new VirtualWorldRecorder();
    } else {
      Globals.evaluator = new StatisticsGatherer();
    }
  }
  private void executeSynthetic() {

    int toPercentCoef, percent = 0;
    int userNb = Network.size();
    HashMap<Integer, VirtualEntity> coords;
    SolipsisProtocol solipsis;
    VirtualEntity entity;
    System.err.println("Populating Virtual World with " + userNb + " participants...");
    VirtualWorldDistribution vwd =
        new VirtualWorldDistribution(
            this.distribution,
            userNb,
            Globals.zoneNb,
            Globals.outOfZoneNb,
            Globals.mapSize,
            Globals.zoneSize,
            Globals.smallZoneSize,
            Globals.smallZoneNb,
            this.applicativeLayerId);
    Globals.distribution = vwd;
    coords = vwd.getDistribution();
    Iterator it = coords.entrySet().iterator();
    toPercentCoef = userNb / 100;
    System.err.println("Joining nodes...");
    for (int i = 0; i < userNb; i++) {
      entity = (VirtualEntity) ((Map.Entry) it.next()).getValue();
      solipsis = (SolipsisProtocol) Network.get(i).getProtocol(applicativeLayerId);
      solipsis.setVirtualEntity(entity);
      solipsis.setPeersimNodeId(i);
      entity.setProtocol(solipsis);
      entity.setOrder(i + 1);
      if (i > 0) {
        solipsis.join((SolipsisProtocol) Network.get(i - 1).getProtocol(applicativeLayerId));
      }
      if (i % toPercentCoef == 0) System.err.println(++percent + "%");
      //        	System.out.println("peer "+i+":
      // "+solipsis.getVirtualEntity().getNeighbors().size());
    }
    System.err.println("Done.");
    //    	System.out.println("uci2");
    System.err.println("Normalizing Solipsis topology...");
    percent = 0;
    it = coords.entrySet().iterator();
    for (int i = 0; i < userNb; i++) {
      entity = (VirtualEntity) ((Map.Entry) it.next()).getValue();
      //        	System.out.println("peer "+i+": "+entity.getNeighbors().size());
      entity.getProtocol().finalizeKnowledgeZone();
      //        	System.out.println("peer "+i+": "+entity.getNeighbors().size());
      entity.getProtocol().simplifyTopology();
      //        	System.out.println("peer "+i+": "+entity.getNeighbors().size());
      if (i % (3 * toPercentCoef) == 0) System.err.println(++percent + "%");
    }

    it = coords.entrySet().iterator();
    for (int i = 0; i < userNb; i++) {
      entity = (VirtualEntity) ((Map.Entry) it.next()).getValue();
      entity.refreshState();
      if (i % (3 * toPercentCoef) == 0) System.err.println(++percent + "%");
    }

    it = coords.entrySet().iterator();
    for (int i = 0; i < userNb; i++) {
      entity = (VirtualEntity) ((Map.Entry) it.next()).getValue();
      entity.getProtocol().maintainKnowledgeZone();
      entity.getProtocol().finalizeKnowledgeZone();
      entity.getProtocol().removeUnwantedNeighbors();
      //        	System.out.println("peer "+i+": "+entity.getNeighbors().size());
      if (i % (3 * toPercentCoef) == 0) System.err.println(++percent + "%");
    }

    System.out.println("Done.");
    //    	for (int i = 0; i < 15; i++) {
    //    		System.out.println("cycle...");
    //    		Globals.distribution.animate();
    //
    //    	}
    Globals.topologyIsReady = true;
    System.err.println("Populated");
    //		System.err.println("Collisions:" + this.collisionCount());
    //		System.exit(2);
    if (Globals.realTime) {
      //    		VirtualWorldMonitor map = new VirtualWorldMonitor(vwd);
      //    		Globals.monitor = map;
      //        	map.showVirtualWorld();
      Globals.recorder = new VirtualWorldRecorder();
    } else {
      Globals.evaluator = new StatisticsGatherer();
    }
  }
  public boolean execute() {
    if (CDState.getCycle() % period != 0) return false;

    MycoCast mycocast = (MycoCast) Network.get(0).getProtocol(mycocastPid);

    int bio = mycocast.countBiomass();
    int ext = mycocast.countExtending();
    int bra = mycocast.countBranching();
    int imm = mycocast.countImmobile();

    // Update vertices
    Set<MycoNode> activeNodes = new HashSet<MycoNode>();
    for (int i = 0; i < Network.size(); i++) {
      MycoNode n = (MycoNode) Network.get(i);
      activeNodes.add(n);
      HyphaData data = n.getHyphaData();
      // if (data.isBiomass()) { continue; }
      if (graph.containsVertex(n)) {
        graph.removeVertex(n);
      }
      if (!graph.containsVertex(n)) {
        graph.addVertex(n);
      }
    }
    Set<MycoNode> jungNodes = new HashSet<MycoNode>(graph.getVertices());
    jungNodes.removeAll(activeNodes);

    for (MycoNode n : jungNodes) {
      graph.removeVertex(n);
    }

    // Update edges
    for (int i = 0; i < Network.size(); i++) {
      MycoNode n = (MycoNode) Network.get(i);
      HyphaData data = n.getHyphaData();
      HyphaLink link = n.getHyphaLink();

      synchronized (graph) {

        // We now add in all links and tune out display in Visualizer
        java.util.List<MycoNode> neighbors = (java.util.List<MycoNode>) link.getNeighbors();

        //// Adding only links to hypha thins out links to biomass
        //    (java.util.List<MycoNode>) link.getHyphae();

        Collection<MycoNode> jungNeighbors = graph.getNeighbors(n);

        // Remove edges from Jung graph that are not in peersim graph
        for (MycoNode o : jungNeighbors) {
          if (!neighbors.contains(o)) {
            MycoEdge edge = graph.findEdge(n, o);
            while (edge != null) {
              graph.removeEdge(edge);
              edge = graph.findEdge(n, o);
            }
          }
        }

        // Add missing edges to Jung graph that are in peersim graph
        for (MycoNode o : neighbors) {
          if (graph.findEdge(n, o) == null) {
            MycoEdge edge = new MycoEdge();
            graph.addEdge(edge, n, o, EdgeType.DIRECTED);
          }
        }
      }

      // log.finest("VERTICES: " + graph.getVertices());
      // log.finest("EDGES: " + graph.getEdges());
    }

    for (ChangeListener cl : changeListeners) {
      cl.stateChanged(new ChangeEvent(graph));
    }
    if (walking) {
      try {
        Thread.sleep(walkDelay);
      } catch (InterruptedException e) {
      }
      stepBlocked = false;
    }

    try {
      while (stepBlocked && !noBlock) {
        synchronized (JungGraphObserver.class) {
          JungGraphObserver.class.wait();
        }
      }
    } catch (InterruptedException e) {
      stepBlocked = true;
    }
    stepBlocked = true;
    // System.out.println(graph.toString());
    return false;
  }