TheJit.groovy | |
---|---|
package org.cdlib.snac.kibbles; | |
The Jit in this case = javascript infovis toolkit; not just in time compiler The JSON this produces is used by this HTML/js
| import com.tinkerpop.blueprints.pgm.Edge;
import com.tinkerpop.blueprints.pgm.Graph;
import com.tinkerpop.blueprints.pgm.Vertex;
import com.tinkerpop.rexster.extension.*;
import com.tinkerpop.rexster.RexsterResourceContext;
import org.codehaus.jettison.json.JSONObject;
import org.codehaus.jettison.json.JSONArray;
import com.tinkerpop.gremlin.groovy.Gremlin;
import javax.ws.rs.core.Response; |
example URL
| @ExtensionNaming(namespace = "snac", name = "theJit")
public class TheJit extends AbstractRexsterExtension {
static {
Gremlin.load();
}
@ExtensionDefinition(extensionPoint = ExtensionPoint.VERTEX)
@ExtensionDescriptor(description = "JSON needed for graphs in the JavaScript InfoVis Toolkit")
public ExtensionResponse evaluate(@RexsterContext RexsterResourceContext context,
@RexsterContext Vertex v) { |
JSONArray output = new JSONArray(); | |
popular graph neighbors get aggregated into this array | def neighbors = []; |
collect neighbors; sorted by "popularity"
.score was precomputed with: | v.out.dedup.sort{-it.score}._()[0..49].aggregate(neighbors).iterate(); |
add JSON for the "center" node to the output array first | output.put(buildNode(v, neighbors)); |
for each popular graph neighbor; build neighbors JSON and add to output array | neighbors.each{ output.put(buildNode(it, neighbors)); } |
return the results | return new ExtensionResponse(Response.ok(output).build());
} |
build the JSON for the node | private buildNode(vertex, neighbors) {
def node = new JSONObject();
node.put("id", vertex.id as String);
node.put("name", vertex.identity);
node.put("adjacencies", buildAdjacencies(vertex, neighbors));
return node;
} |
build array of adjacent nodes | private buildAdjacencies(vertex, neighbors) {
JSONArray collect = new JSONArray();
def self = [];
vertex.aggregate(self).iterate(); |
the graph visualization would explode if we loaded all the neighbors of all the neighbors, so do this to seach for relationships this neighbor has with the other popular neighbors of the subject we are centered on | vertex.both.dedup().retain(neighbors).except(self).each {
collect.put(it.id as String);
};
return collect;
}
}
|