コード例 #1
0
  @Override
  public boolean write(String filename, String key) {
    Filewriter fw = new Filewriter(filename);

    // CLASS
    fw.writeComment(Config.get("GRAPH_PROPERTY_CLASS"));
    fw.writeln(this.getClass().getCanonicalName().toString());

    // KEY
    fw.writeComment(Config.get("GRAPH_PROPERTY_KEY"));
    fw.writeln(key);

    // # MODULUS
    fw.writeComment("Modulus");
    fw.writeln(this.modulus);

    // # WRAP-AROUND
    fw.writeComment("Wrap-around");
    fw.writeln(this.wrapAround + "");

    // # PARTITIONS
    fw.writeComment("Partitions");
    fw.writeln(this.partitions.length);

    fw.writeln();

    // PARTITIONS
    int index = 0;
    for (RingPartition p : this.partitions) {
      fw.writeln(index++ + ":" + p.toString());
    }

    return fw.close();
  }
コード例 #2
0
  /*
   * (non-Javadoc)
   *
   * @see gtna.io.graphReader.GraphReader#read(java.lang.String)
   */
  @Override
  public Graph read(String filename) {
    String sep1 = Config.get("SNAP_SEPARATOR_1");
    String ffd = Config.get("FILESYSTEM_FOLDER_DELIMITER");
    SNAPFileReader fr = new SNAPFileReader(filename);
    try {
      String line = null;
      String name = filename.substring(filename.lastIndexOf(ffd) + 1);

      Graph graph = new Graph(name);

      Map<Integer, Integer> idMapping = new HashMap<Integer, Integer>();
      int nodecounter = 0;

      LinkedList<String> edges = new LinkedList<String>();

      graphtype type = null;

      line = fr.readLine();
      type = (line.contains("Directed graph")) ? graphtype.DIRECTED : graphtype.UNDIRECTED;

      while (!line.contains("FromNodeId") && !line.contains("ToNodeId")) {
        line = fr.readLine();
      }
      nodecounter = parseEdges(sep1, fr, idMapping, nodecounter, edges, type);

      Node[] nodes = Node.init(idMapping.size(), graph);
      Edges e = new Edges(nodes, edges.size());
      ListIterator<String> edgeIterator = edges.listIterator();
      while (edgeIterator.hasNext()) {
        String[] pair = edgeIterator.next().split("-");
        e.add(Integer.parseInt(pair[0]), Integer.parseInt(pair[1]));
      }

      e.fill();
      graph.setNodes(nodes);
      return graph;
    } catch (Exception e) {
      return null;
    } finally {
      fr.close();
    }
  }
コード例 #3
0
 public void computeData(Graph g, Network n, Hashtable<String, Metric> m) {
   this.timer = new Timer();
   boolean onlyOut =
       GraphProperties.bidirectional(g)
           || Config.getBoolean("CC_NEIGHBORHOOD_DEFINED_BY_OUTGOING_EDGES");
   this.lcc = this.computeLCC(g.nodes, onlyOut);
   Arrays.sort(this.lcc);
   this.lccShort = this.computeLCCShort(this.lcc);
   this.cc = this.computeCC(this.lcc);
   this.ccw = this.computeCCW(this.lcc, g.nodes);
   this.timer.end();
 }
コード例 #4
0
  /** Returns number of nodes in the graph, expects a line: # Nodes: nodecount Edges: edgecount */
  @Override
  public int nodes(String filename) {
    String sep1 = Config.get("SNAP_SEPARATOR_1");

    SNAPFileReader fr = new SNAPFileReader(filename);

    String line = fr.readLine();
    while (!line.contains("Nodes:")) {
      line = fr.readLine();
    }

    String[] sl = line.split(sep1);
    return Integer.parseInt(sl[1]);
  }
コード例 #5
0
ファイル: GtnaV1GraphReader.java プロジェクト: NicoHaase/GTNA
  @Override
  public Graph read(String filename) {
    String delimiter = Config.get("GRAPH_WRITER_DELIMITER");
    Filereader fr = new Filereader(filename);

    String name = fr.readLine();
    int N = Integer.parseInt(fr.readLine());
    int E = Integer.parseInt(fr.readLine());

    Graph graph = new Graph(name);
    Node[] nodes = Node.init(N, graph);
    Edges edges = new Edges(nodes, E);

    String line = null;
    while ((line = fr.readLine()) != null) {
      String[] temp = line.split(delimiter);
      edges.add(Integer.parseInt(temp[0]), Integer.parseInt(temp[1]));
    }
    edges.fill();
    graph.setNodes(nodes);
    fr.close();
    return graph;
  }
コード例 #6
0
 private double[] computeLCCShort(double[] lcc) {
   return Util.avgArray(lcc, Config.getInt("CC_LCC_SHORT_MAX_VALUES"));
 }
コード例 #7
0
ファイル: ODRI.java プロジェクト: zoranzaric/GTNA
 private ODRINode getBootstrap(Random rand, ODRINode[] nodes, int index) {
   int k = Config.getInt("ODRI_SAMPLES");
   ODRINode bootstrap = nodes[rand.nextInt(index)];
   return getMax(bootstrap, this.WALK_LENGTH, k);
 }
コード例 #8
0
ファイル: RoutingTest.java プロジェクト: patrickTUD/GTNA
  /** @param args */
  public static void main(String[] args) {

    Config.overwrite("MAIN_DATA_FOLDER", "data/routing-test/");
    Config.overwrite("MAIN_PLOT_FOLDER", "plots/routing-test/");
    Config.overwrite("SKIP_EXISTING_DATA_FOLDERS", "false");

    int ttl = 50;
    int rw = 6;
    int[] R_STORAGE = new int[] {1, 5, 10, 20, 30, 40, 50};
    R_STORAGE = new int[] {1, 50};
    int rStorage = 10;
    int rRouting = 1;

    int times = 1;
    int nodes = 500;
    boolean get = true;

    Transformation rrids = new RandomRingIDSpaceSimple();
    Transformation lmc = new LMC(1000, LMC.MODE_UNRESTRICTED, 0, LMC.DELTA_1_N, 0);
    Transformation sw = new Swapping(1000);

    Map<Transformation[], String> names = new HashMap<Transformation[], String>();

    Transformation[][] t1 = new Transformation[R_STORAGE.length][];
    for (int i = 0; i < R_STORAGE.length; i++) {
      Transformation mpds =
          new MultiPhaseDataStorage(
              R_STORAGE[i],
              new RoutingAlgorithm[] {
                new RandomWalk(rw), new HighestDegreeNeighbor(), new Greedy()
              });
      t1[i] = new Transformation[] {rrids, lmc, mpds};
      names.put(t1[i], "MultiPhase-R-D-G " + R_STORAGE[i]);
    }

    Transformation[][] t2 = new Transformation[R_STORAGE.length][];
    for (int i = 0; i < R_STORAGE.length; i++) {
      Transformation mpds =
          new MultiPhaseDataStorage(
              R_STORAGE[i],
              new RoutingAlgorithm[] {
                new RandomWalk(rw), new HighestDegreeNeighbor(), new LookaheadSimple(ttl)
              });
      t2[i] = new Transformation[] {rrids, lmc, mpds};
      names.put(t2[i], "MultiPhase-R-D-L " + R_STORAGE[i]);
    }

    Transformation[][][] t = new Transformation[][][] {t1, t2};

    Metric dd = new DegreeDistribution();
    Metric dsm = new DataStorageMetric();
    Routing r1 = new Routing(new Greedy(ttl));
    Routing r2 =
        new Routing(
            new MultiPhaseRouting(
                rRouting,
                new RoutingAlgorithm[] {
                  new RandomWalk(rw), new HighestDegreeNeighbor(), new Greedy(ttl)
                }));
    Routing r3 =
        new Routing(
            new MultiPhaseRouting(
                rRouting,
                new RoutingAlgorithm[] {
                  new RandomWalk(rw), new HighestDegreeNeighbor(), new LookaheadSimple(ttl)
                }));
    Routing r4 = new Routing(new RandomWalk(ttl));
    Metric[] metrics = new Metric[] {dd, dsm, r1, r2, r3, r4};

    Network[][] nw = RoutingTest.getNW(nodes, t, names);

    Series[][] s = get ? Series.get(nw, metrics) : Series.generate(nw, metrics, times);

    Plotting.multi(s, metrics, "multi/");
    Plotting.single(s, metrics, "single/");
  }
コード例 #9
0
ファイル: WOTReader.java プロジェクト: GustavoNiewoehner/GTNA
/**
 * This is a reader for the Web of Trust file format version 0.2.
 *
 * <p>The Web of Trust file format specification is available at:
 * http://www.lysator.liu.se/~jc/wotsap/wotfileformat.txt
 *
 * <p>*.wot files are compressed with bzip2, this reader assumes a uncompressed version of the file.
 *
 * @author Zoran Zaric <*****@*****.**>
 */
public class WOTReader {
  // private final static int SIGNATURE_TYPE_MASK = 0xF0000000;
  private static final int SIGNATURE_INDEX_MASK = 0x0FFFFFFF;

  public static Graph read(String filename) throws Exception {
    Timer timer = new Timer();

    RandomAccessFile file = new RandomAccessFile(new File(filename), "r");

    // seek to names
    while (!file.readLine().startsWith("names/")) {}

    // read names
    Vector<WOTNode> wotNodes = new Vector<WOTNode>();
    int index = 0;
    String line;
    while (!(line = file.readLine()).startsWith("keys/")) {
      if (!line.equals("")) {
        WOTNode node = new WOTNode(index, line);
        wotNodes.add(node);
        index += 1;
      }
    }

    // read keys
    for (index = 0; index < wotNodes.size(); index++) {
      byte[] key = new byte[4];
      file.read(key);
      WOTNode node = wotNodes.get(index);
      node.key = Util.toInt(key);
    }

    // skip the "signatures/" line
    String signaturesLine = file.readLine();
    if (!signaturesLine.startsWith("signatures/")) {
      // FIXME an IllegalArgumentException isn't really semantically
      // correct
      throw new IllegalArgumentException("A line starting with \"signatures/\" was expected.");
    }

    // read signatures
    for (index = 0; index < wotNodes.size(); index++) {
      WOTNode node = wotNodes.get(index);

      // read how many keys are signed by this key
      byte[] buffer = new byte[4];
      file.read(buffer);
      int signatureCount = Util.toInt(buffer);

      // inspect all signatures
      for (int signatureIndex = 0; signatureIndex < signatureCount; signatureIndex++) {
        byte[] signatureBuffer = new byte[4];
        file.read(signatureBuffer);

        int signature = Util.toInt(signatureBuffer);

        // get the signature type
        // TODO do something with the signature type
        // int signatureType = signature & SIGNATURE_TYPE_MASK;

        // get the target key
        int targetIndex = signature & SIGNATURE_INDEX_MASK;
        if (targetIndex >= wotNodes.size()) {
          // FIXME an IllegalArgumentException isn't really
          // semantically correct
          throw new IllegalArgumentException("A target index that matches a node was expected.");
        }

        // retrieve the target node
        WOTNode target = wotNodes.get(targetIndex);
        node.signatures.add(target);
      }
    }
    // skip the "debug/" line
    String debugLine = file.readLine();
    if (!debugLine.startsWith("debug/")) {
      // FIXME an IllegalArgumentException isn't really semantically
      // correct
      throw new IllegalArgumentException("A line starting with \"debug/was expected.");
    }

    file.close();
    timer.end();

    // prepare return graph
    Graph graph = new Graph("WOT - read from " + filename);
    Node[] nodes = Node.init(wotNodes.size(), graph);
    Edges edges = new Edges(nodes, nodes.length);
    for (WOTNode source : wotNodes) {
      for (WOTNode target : source.signatures) {
        edges.add(source.index, target.index);
      }
    }
    edges.fill();
    graph.setNodes(nodes);
    return graph;
  }

  private static class WOTNode {
    public int index;

    public String name;

    public int key;

    public Vector<WOTNode> signatures;

    public WOTNode(int index, String name) {
      this.index = index;
      this.name = name;
      this.key = -1;
      this.signatures = new Vector<WOTReader.WOTNode>();
    }

    public String toString() {
      return this.name + " [" + this.key + "]";
    }
  }

  private static final boolean PRINT_ERRORS = Config.getBoolean("WOT_READER_PRINT_ERRORS");

  public static void getWOTBetween(int Y1, int M1, int D1, int Y2, int M2, int D2) {
    int millisInDay = 1000 * 60 * 60 * 24;
    Calendar c = Calendar.getInstance();
    c.set(Calendar.YEAR, Y2);
    c.set(Calendar.MONTH, M2 - 1);
    c.set(Calendar.DAY_OF_MONTH, D2);
    long end = c.getTimeInMillis();
    c.set(Calendar.YEAR, Y1);
    c.set(Calendar.MONTH, M1 - 1);
    c.set(Calendar.DAY_OF_MONTH, D1);
    while (c.getTimeInMillis() <= end) {
      int Y = c.get(Calendar.YEAR);
      int M = c.get(Calendar.MONTH) + 1;
      int D = c.get(Calendar.DAY_OF_MONTH);
      System.out.println(Y + "-" + M + "-" + D);
      System.out.println("   " + getWOT(Y, M, D));
      c.setTimeInMillis(c.getTimeInMillis() + millisInDay);
    }
  }

  public static boolean getWOT(int y, int m, int d) {
    String Y = "" + y;
    String M = m < 10 ? "0" + m : "" + m;
    String D = d < 10 ? "0" + d : "" + d;

    String fetchOut = Config.get("WOT_READER_FETCH_OUT");
    String fetchFolder = Config.get("WOT_READER_FETCH_FOLDER");
    String fetchCmd = Config.get("WOT_READER_FETCH_CMD");
    fetchOut = replace(fetchOut, Y, M, D);
    fetchFolder = replace(fetchFolder, Y, M, D);
    fetchCmd = replace(fetchCmd, Y, M, D);
    fetchCmd = fetchCmd.replace("%OUT", fetchOut);

    String unzipOut = Config.get("WOT_READER_UNZIP_OUT");
    String unzipFolder = Config.get("WOT_READER_UNZIP_FOLDER");
    String unzipCmd = Config.get("WOT_READER_UNZIP_CMD");
    unzipOut = replace(unzipOut, Y, M, D);
    unzipFolder = replace(unzipFolder, Y, M, D);
    unzipCmd = replace(unzipCmd, Y, M, D);
    unzipCmd = unzipCmd.replace("%WOT", fetchOut);
    unzipCmd = unzipCmd.replace("%OUT", unzipOut);

    String wot = fetchFolder + fetchOut;
    String unzipped = unzipFolder + unzipOut;
    String filename = Config.get("WOT_READER_GRAPH_OUT");
    filename = replace(filename, Y, M, D);

    execute(fetchCmd, fetchFolder);
    if (!(new File(wot)).exists()) {
      return false;
    }

    execute(unzipCmd, unzipFolder);
    if (!(new File(unzipped)).exists()) {
      return false;
    }

    try {
      Graph g = WOTReader.read(unzipped);
      GraphWriter.write(g, filename);
      if (Config.getBoolean("WOT_READER_DELETE_WOT_FILE")) {
        String deleteFolder = Config.get("WOT_READER_DELETE_WOT_FOLDER");
        String deleteCmd = Config.get("WOT_READER_DELETE_WOT_CMD");
        deleteCmd = replace(deleteCmd, Y, M, D);
        execute(deleteCmd, deleteFolder);
      }
      if (Config.getBoolean("WOT_READER_DELETE_UNZIPPED_FILE")) {
        String deleteFolder = Config.get("WOT_READER_DELETE_UNZIPPED_FOLDER");
        String deleteCmd = Config.get("WOT_READER_DELETE_UNZIPPED_CMD");
        deleteCmd = replace(deleteCmd, Y, M, D);
        execute(deleteCmd, deleteFolder);
      }
      return true;
    } catch (Exception e) {
      e.printStackTrace();
    }

    return false;
  }

  private static String replace(String str, String Y, String M, String D) {
    return str.replace("%Y", Y).replace("%M", M).replace("%D", D);
  }

  private static boolean execute(String cmd, String dir) {
    if (PRINT_ERRORS) {
      System.out.println("EXECUTING " + cmd + " IN " + dir);
    }
    try {
      Process p = null;
      if (dir == null) {
        p = Runtime.getRuntime().exec(cmd);
      } else {
        p = Runtime.getRuntime().exec(cmd, null, new File(dir));
      }
      if (PRINT_ERRORS) {
        InputStream stderr = p.getErrorStream();
        InputStreamReader isr = new InputStreamReader(stderr);
        BufferedReader br = new BufferedReader(isr);
        String line = null;
        while ((line = br.readLine()) != null) {
          Output.writeln(line);
        }
      }
      p.waitFor();
      return true;
    } catch (IOException e) {
      e.printStackTrace();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    return false;
  }
}
コード例 #10
0
ファイル: WOTReader.java プロジェクト: GustavoNiewoehner/GTNA
  public static boolean getWOT(int y, int m, int d) {
    String Y = "" + y;
    String M = m < 10 ? "0" + m : "" + m;
    String D = d < 10 ? "0" + d : "" + d;

    String fetchOut = Config.get("WOT_READER_FETCH_OUT");
    String fetchFolder = Config.get("WOT_READER_FETCH_FOLDER");
    String fetchCmd = Config.get("WOT_READER_FETCH_CMD");
    fetchOut = replace(fetchOut, Y, M, D);
    fetchFolder = replace(fetchFolder, Y, M, D);
    fetchCmd = replace(fetchCmd, Y, M, D);
    fetchCmd = fetchCmd.replace("%OUT", fetchOut);

    String unzipOut = Config.get("WOT_READER_UNZIP_OUT");
    String unzipFolder = Config.get("WOT_READER_UNZIP_FOLDER");
    String unzipCmd = Config.get("WOT_READER_UNZIP_CMD");
    unzipOut = replace(unzipOut, Y, M, D);
    unzipFolder = replace(unzipFolder, Y, M, D);
    unzipCmd = replace(unzipCmd, Y, M, D);
    unzipCmd = unzipCmd.replace("%WOT", fetchOut);
    unzipCmd = unzipCmd.replace("%OUT", unzipOut);

    String wot = fetchFolder + fetchOut;
    String unzipped = unzipFolder + unzipOut;
    String filename = Config.get("WOT_READER_GRAPH_OUT");
    filename = replace(filename, Y, M, D);

    execute(fetchCmd, fetchFolder);
    if (!(new File(wot)).exists()) {
      return false;
    }

    execute(unzipCmd, unzipFolder);
    if (!(new File(unzipped)).exists()) {
      return false;
    }

    try {
      Graph g = WOTReader.read(unzipped);
      GraphWriter.write(g, filename);
      if (Config.getBoolean("WOT_READER_DELETE_WOT_FILE")) {
        String deleteFolder = Config.get("WOT_READER_DELETE_WOT_FOLDER");
        String deleteCmd = Config.get("WOT_READER_DELETE_WOT_CMD");
        deleteCmd = replace(deleteCmd, Y, M, D);
        execute(deleteCmd, deleteFolder);
      }
      if (Config.getBoolean("WOT_READER_DELETE_UNZIPPED_FILE")) {
        String deleteFolder = Config.get("WOT_READER_DELETE_UNZIPPED_FOLDER");
        String deleteCmd = Config.get("WOT_READER_DELETE_UNZIPPED_CMD");
        deleteCmd = replace(deleteCmd, Y, M, D);
        execute(deleteCmd, deleteFolder);
      }
      return true;
    } catch (Exception e) {
      e.printStackTrace();
    }

    return false;
  }