Skip to content

Instantly share code, notes, and snippets.

@ALRubinger
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ALRubinger/1bb712de02a7ede347f1 to your computer and use it in GitHub Desktop.
Save ALRubinger/1bb712de02a7ede347f1 to your computer and use it in GitHub Desktop.
Using TinkerPop Frames to map a Java object model to a Graph, then TinkerPop Blueprints to persist it to underlying Graph storage (impl in OrientDB)
public interface Organization {
String PROP_NAME = "name";
String REL_PROJECTS = "sponsors";
/**
* Gets the name of this {@link org.jboss.metrics.playground.model.Organization}
* @return
*/
@Property(PROP_NAME)
String getName();
/**
* Sets the name of this {@link org.jboss.metrics.playground.model.Organization}
* @param name
*/
@Property(PROP_NAME)
void setName(String name);
@Adjacency(label = REL_PROJECTS)
Iterable<Project> getProjects();
@Adjacency(label = REL_PROJECTS)
void addProject(Project project);
}
public interface Project {
String PROP_NAME = "name";
String REL_ORGANIZATION = "sponsoredBy";
/**
* Obtains the name of this {@link org.jboss.metrics.playground.model.Project}
* @return
*/
@Property(PROP_NAME)
String getName();
/**
* Sets the name of this {@link org.jboss.metrics.playground.model.Project}
* @param name
*/
@Property(PROP_NAME)
void setName(String name);
/**
* Returns the sponsoring organization
* @return
*/
@Adjacency(label = REL_ORGANIZATION)
Organization getOrganization();
/**
* Sets the sponsoring organization
* @param organization
*/
@Adjacency(label = REL_ORGANIZATION)
void setOrganization(Organization organization);
}
@Test
public void testOrientDbFrameGraphApi() {
final FramedGraphFactory framedGraphFactory = new FramedGraphFactory();
final FramedGraph framedGraph = framedGraphFactory.create(graphDb);
final Vertex jbossVertex = framedGraph.addVertex(null);
System.out.println("Just added Vertex ID for JBoss org: " + jbossVertex.getId());
final Vertex shrinkwrapVertex = framedGraph.addVertex(null);
System.out.println("Just added Vertex ID for ShrinkWrap: " + shrinkwrapVertex.getId());
//TODO Why do I need this cast? By API I shouldn't.
final Project shrinkwrap = (Project)framedGraph.frame(shrinkwrapVertex, Project.class);
final Organization jboss = (Organization)framedGraph.frame(jbossVertex, Organization.class);
jboss.setName("JBoss");
shrinkwrap.setName("ShrinkWrap");
jboss.addProject(shrinkwrap);
shrinkwrap.setOrganization(jboss);
final Iterable<Vertex> vertices = graphDb.getVertices();
for(final Vertex vertexRoundtrip : vertices){
System.out.println("Found Vertex: " + vertexRoundtrip.getId());
final Set<String> propertyKeys = vertexRoundtrip.getPropertyKeys();
for(final String propertyKey : propertyKeys){
System.out.println("\tFound property... " + propertyKey + ":" + vertexRoundtrip.getProperty(propertyKey));
}
final Iterable<Vertex> adjacentVertexes = vertexRoundtrip.getVertices(Direction.OUT,Project.REL_ORGANIZATION, Organization.REL_PROJECTS);
for(final Vertex adjacentVertex : adjacentVertexes){
System.out.println("\tFound adjacent vertex: " + adjacentVertex.getId());
}
}
}
Just added Vertex ID for JBoss org: #9:0
Just added Vertex ID for ShrinkWrap: #9:1
Found Vertex: #9:0
Found property... name:JBoss
Found adjacent vertex: #9:1
Found Vertex: #9:1
Found property... name:ShrinkWrap
Found adjacent vertex: #9:0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment