Skip to content

Instantly share code, notes, and snippets.

@jexp
Created July 31, 2014 14:33
Show Gist options
  • Save jexp/d8dd37e16e9c63b6229b to your computer and use it in GitHub Desktop.
Save jexp/d8dd37e16e9c63b6229b to your computer and use it in GitHub Desktop.
Neo4j Hipster4j Integration
package org.neo4j.contrib.hipster;
import es.usc.citius.hipster.util.graph.GraphEdge;
import es.usc.citius.hipster.util.graph.HipsterDirectedGraph;
import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
public class Neo4jHipsterDirectedGraphAdapter extends Neo4jHipsterGraphAdapter implements HipsterDirectedGraph<Node, Relationship> {
public Neo4jHipsterDirectedGraphAdapter(GraphDatabaseService graph) {
super(graph);
}
@Override
public Iterable<GraphEdge<Node, Relationship>> outgoingEdgesOf(Node node) {
return convertEdges(node.getRelationships(Direction.OUTGOING));
}
@Override
public Iterable<GraphEdge<Node, Relationship>> incomingEdgesOf(Node node) {
return convertEdges(node.getRelationships(Direction.INCOMING));
}
}
package org.neo4j.contrib.hipster;
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import es.usc.citius.hipster.util.graph.GraphEdge;
import es.usc.citius.hipster.util.graph.HipsterGraph;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.tooling.GlobalGraphOperations;
import javax.annotation.Nullable;
public class Neo4jHipsterGraphAdapter implements HipsterGraph<Node, Relationship> {
private GraphDatabaseService graph;
public Neo4jHipsterGraphAdapter(GraphDatabaseService graph) {
this.graph = graph;
}
@Override
public Iterable<GraphEdge<Node, Relationship>> edges() {
return convertEdges(GlobalGraphOperations.at(graph).getAllRelationships());
}
@Override
public Iterable<Node> vertices() {
return GlobalGraphOperations.at(graph).getAllNodes();
}
@Override
public Iterable<GraphEdge<Node, Relationship>> edgesOf(Node node) {
return convertEdges(node.getRelationships());
}
protected static Iterable<GraphEdge<Node,Relationship>> convertEdges(Iterable<Relationship> edges){
return Iterables.transform(edges, new Function<Relationship, GraphEdge<Node, Relationship>>() {
@Override
public GraphEdge<Node, Relationship> apply(Relationship edge) {
return new GraphEdge<Node, Relationship>(edge.getStartNode(), edge.getEndNode(), edge);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment