package aolgraphexample;
import java.io.IOException;
import it.unimi.dsi.webgraph.ImmutableGraph;
import it.unimi.dsi.webgraph.NodeIterator;
import it.unipi.di.graph.GraphLabeler;
import it.unipi.di.util.Utils;
public class AOLGraphExample {
/**
* Invoke this class using two parameters: 1) the WebGraph basename (string
* "webgraph/wg-aol" for the AOL dataset) 2) the GraphLabeler configuration
* file (string "graphlabeler/graphlabeler.conf" for the AOL dataset)
*/
public static void main(String[] args) throws Exception {
// -----------------------------------------------------
// PART ONE
// WebGraph basename
// String graphFile = args[0];
String graphFile = "webgraph/wg-aol";
// Name of the GraphLabeler configuration file
String labelerFile = "graphlabeler/graphlabeler.conf";
System.out.print(" >> Opening databases... ");
// Take the starting time
long start = System.currentTimeMillis();
// load the graph structure in memory (i.e. WebGraph)
ImmutableGraph graph = ImmutableGraph.load(graphFile);
// load the data structure for the vertex/edge labels in memory (i.e.
// GraphLabeler)
GraphLabeler labeler = GraphLabeler.load(labelerFile);
// print the elapsed time for the first part
System.out.println("done in: " + Utils.elapsedTime(start, System.currentTimeMillis()));
// -----------------------------------------------------
System.out.println(" >> Running test N.1");
// [TEST 1]
//
// For each vertex, fetch the labels of its adjacent vertices in the
// graph.
// This method will fetch ~21.5 million of textual labels (edges are
// undirected).
neighborsLabeling(graph, labeler);
System.out.println(" >> Running test N.2");
// [TEST 2]
//
// For each vertex, fetch the value of the attribute "click" for each of
// its outgoing edges.
// This method will fetch ~21.5 million textual values (edges are
// undirected).
// Recall that the available attributes are: "click", "session", "time"
// and "rank"
fetchAttribute(graph, labeler, "click");
// close the labeler and release its resources
labeler.close();
}
private static void neighborsLabeling(ImmutableGraph graph, GraphLabeler labeler) throws IOException {
// count how many labels has been fetched so far
int counter = 0;
// the total number of nodes in the graph
int numNodes = graph.numNodes();
// take the starting time
long start = System.currentTimeMillis();
double perc = 0;
// iterate over the vertices of the graph
NodeIterator iter = graph.nodeIterator();
[COLOR="#FF0000"] while (iter.hasNext()) [/COLOR]{
// the next node
int node = iter.next();
// print the percentage of nodes examined so far
perc = printPerc(node, numNodes, perc);
// fetch the adjacency list of the current "node" by using WebGraph
// valid positions are those from 0 to outdegree(node)
int[] succ = iter.successorArray();
// fetch the labels in memory (then discard it)
String[] labels = labeler.getVertexLabels(succ, 0, iter.outdegree());
// count how many strings have been fetched so far
counter += labels.length;
}
// compute the overall elapsed time
long elapsed = System.currentTimeMillis() - start;
// print some statistics on stdout
printStats(counter, elapsed);
}
private static void fetchAttribute(ImmutableGraph graph, GraphLabeler labeler, String attribute) throws IOException {
// count how many labels have been fetched so far
int counter = 0;
// the total number of nodes in the graph
int numNodes = graph.numNodes();
// take the starting time
long start = System.currentTimeMillis();
double perc = 0;
for (int node = 0; node < numNodes; node++) {
// print the percentage of nodes examined so far
perc = printPerc(node, numNodes, perc);
// fetch the value of the requested "attribute" for each edge
// outgoing from "node"
String[] values = labeler.getOutgoingAttribute(node, attribute);
// count how many labels have been fetched so far
counter += values.length;
}
// compute the overall elapsed time
long elapsed = System.currentTimeMillis() - start;
// print some statistics on stdout
printStats(counter, elapsed);
}
private static double printPerc(int node, int numNodes, double percLimit) {
int perc = (int)(node / (double)numNodes * 100);
if (perc >= percLimit || node == 0) {
if (node == 0)
System.out.print(" >> Progress: ");
System.out.print(perc + "% ");
percLimit += 10;
}
return percLimit;
}
private static void printStats(int count, long elapsed) {
printPerc(1, 1, 0); // print 100%
int rate = (int)(count / (elapsed / 1000d)); // strings/sec
System.out.println();
System.out.println(" >> Rate: " + rate + " string/sec");
System.out.println(" >> Fetched strings: " + count);
System.out.println(" >> Overall time: " + Utils.elapsedTime(elapsed));
}
}