Created
December 20, 2011 21:46
-
-
Save countvajhula/1503441 to your computer and use it in GitHub Desktop.
Test app for Blueprints Issue #197
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env groovy | |
import com.tinkerpop.blueprints.* | |
import com.tinkerpop.blueprints.pgm.* | |
import com.tinkerpop.blueprints.pgm.impls.neo4j.* | |
import com.tinkerpop.blueprints.pgm.impls.orientdb.* | |
import com.tinkerpop.blueprints.pgm.util.* | |
import com.tinkerpop.blueprints.pgm.util.graphml.GraphMLReader | |
import java.lang.Math | |
import java.util.Random | |
import com.tinkerpop.gremlin.* | |
public class GraphBugger { | |
static { | |
Gremlin.load() | |
} | |
// Random number generator | |
private Random randGen | |
public GraphBugger() { | |
randGen = new Random() //initialize random number generator | |
} | |
public void addNVertices(Graph graph, long n) { | |
println "Adding ${n} vertices..." | |
// add n vertices | |
for (int i=0; i<n; i++) { | |
Vertex vv = graph.addVertex(null) | |
// set some properties? | |
if (i % 1000 == 0) { | |
println "${i} vertices added..." | |
} | |
} | |
} | |
// adds random edges as per the Erdos-Renyi model (select 2 random vertices, add an edge between them) | |
public void addRandomEdgesER(Graph graph, long edgesPerVertex) { | |
println "Adding random edges to nodes..." | |
long numVertices = graph.V.count() | |
for (long i=0; i<edgesPerVertex*numVertices; i++) { | |
Vertex v1 = selectRandomVertex(graph, numVertices) | |
if (v1) { | |
Vertex v2 = selectRandomVertex(graph, numVertices) | |
if (v2) { | |
graph.addEdge(null, v1, v2, 'knows') | |
def v1count = v1.getProperty('count') | |
if (!v1count) { | |
v1count = 0 | |
v1.setProperty('count', 0) | |
} | |
def v2count = v2.getProperty('count') | |
if (!v2count) { | |
v2count = 0 | |
v2.setProperty('count', 0) | |
} | |
v1.setProperty('count', v1count+1) | |
v2.setProperty('count', v2count+1) | |
} | |
} | |
if (i%10000 == 0) { | |
println "${i} edges added..." | |
} | |
} | |
} | |
// random long function from http://stackoverflow.com/questions/2546078/java-random-long-number-in-0-x-n-range | |
private long nextLong(Random rng, long n) { | |
// error checking and 2^x checking removed for simplicity. | |
long bits, val; | |
bits = 0; val = n | |
while (bits-val+(n-1) < 0L) { | |
bits = (rng.nextLong() << 1) >>> 1; | |
val = bits % n; | |
} | |
return val; | |
} | |
private Vertex selectRandomVertex(Graph graph, long numVertices) { | |
long randNum = nextLong(randGen, numVertices) | |
Vertex v1 | |
int tries = 0 | |
while (!v1 && tries<1) { //might be good to set #tries to 1 | |
v1 = graph.getVertex(randNum) | |
tries++ | |
} | |
return v1 | |
} | |
// Gremlin traversals to return neighbors of a vertex (co-opted from the Pilot getNeighbors() function -- https://github.com/countvajhula/pilot/blob/master/src/main/groovy/com/pilot/GraphDbOperator.groovy ) | |
public List getVertexNeighbors(Vertex v1, String alongEdge) { | |
List neighbors = [] | |
List tempList = [] | |
v1.bothE(alongEdge).bothV.filter { node -> !node.id.equals(v1.id) }.aggregate(neighbors) >> -1 | |
//add current node back if there is a self-connecting edge | |
v1.outE(alongEdge).inV.filter { node -> node.id.equals(v1.id) }.aggregate(tempList) >> -1 | |
if (tempList.size() > 0) { | |
neighbors += tempList | |
} | |
return neighbors | |
} | |
public void createRandomGraph() { | |
String graphUrl = './dbs/myneodb' | |
long numVertices = 140 | |
long edgesPerVertex = 70 | |
Graph graph = new Neo4jGraph(graphUrl) | |
graph.clear() | |
// automatic transactions via transaction buffer | |
graph.setMaxBufferSize(1000) | |
// create a random graph with some number of vertices and randomly added edges | |
addNVertices(graph, numVertices) | |
addRandomEdgesER(graph, edgesPerVertex) | |
// flush buffer | |
graph.stopTransaction(TransactionalGraph.Conclusion.SUCCESS) | |
println "Selecting random vertices and adding edges to their immediate neighbors..." | |
//get neighbors and add edges | |
for (int j=0; j<100; j++) { | |
Vertex v1 = selectRandomVertex(graph, numVertices) | |
if (v1) { | |
def neighbors = getVertexNeighbors(v1, 'knows') | |
//graph.getNeighbors(v1, null, 'knows') | |
for (neighbor in neighbors) { | |
graph.addEdge(null, v1, neighbor, 'hasAThingFor') | |
} | |
} | |
} | |
graph.stopTransaction(TransactionalGraph.Conclusion.SUCCESS) | |
graph.shutdown() | |
} | |
public static void main(String[] args) { | |
GraphBugger gb = new GraphBugger() | |
gb.createRandomGraph() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment