0
votes

I am using Neo4J as my underlying GraphDB, with Tinkerpop 3.3.1 on top in my Java application. I create the Graph in Java with Neo4JGraph.open ("/tmp/filename"), and then from there I only use the Tinkerpop Java API to create nodes, edges and run traversals.

I want to perform a Dijkstra shortest path on my Tinkerpop graph. Neo4J, which is my underlying GraphDB, has this algorithm built in, so I would like to use it. I use the make this call to get the Neo4J graph, and get a few Neo4JNode types from it.

Neo4jGraphAPI neo4J = this.graphPV.getBaseGraph();
Neo4jNode start = neo4J.getNodeById(1);
Neo4jNode end = neo4J.getNodeById(2);

This works, but now I'm not sure what to do next. I figured I would pass these nodes to the Neo4J pathfinder, but the findSinglePath method takes a Node type, not a Tinkerpop based Neo4JNode. I tried casting, that threw an exception. Can I convert the Tinkerpop Neo4JNode into a Neo4J Node type that the underlying Neo4J API will accept ?

dijkstraPathFinder = GraphAlgoFactory.dijkstra(expander, costEvaluator );
WeightedPath path = dijkstraPathFinder.findSinglePath((Node)start, (Node)end );

Results in:

WARNING: service exception java.lang.ClassCastException: org.neo4j.tinkerpop.api.impl.Neo4jNodeImpl cannot be cast to org.neo4j.graphdb.Node

1

1 Answers

1
votes

Neo4jNodeImpl and Neo4jRelationshipImpl both extend Neo4jEntityImpl, which has a public getEntity() method that returns the native neo4j Node/Relationship.

Unfortunately, getEntity() is not in the Neo4jEntity interface (and is therefore not in the Neo4jNode and Neo4jRelationship interfaces). So, to use getEntity(), your project will have to add a dependency on the impl module and use a class cast. For example:

Neo4jGraphAPI neo4J = this.graphPV.getBaseGraph();
Neo4jNode start = neo4J.getNodeById(1);
Node nativeStart = ((Neo4jNodeImpl)start).getEntity();

Alternatively, if you do not want to depend on the impl module, there is a less performant option. You can use the Neo4jEntity.getId() method to get the native ID for the underlying neo4j Node/Relationship, and then perform a neo4j query to get that Node/Relationship.