-
Notifications
You must be signed in to change notification settings - Fork 493
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
126 additions
and
54 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package apoc.algo; | ||
|
||
import apoc.Extended; | ||
import apoc.result.WeightedPathResult; | ||
import org.neo4j.graphalgo.BasicEvaluationContext; | ||
import org.neo4j.graphalgo.CommonEvaluators; | ||
import org.neo4j.graphalgo.GraphAlgoFactory; | ||
import org.neo4j.graphalgo.PathFinder; | ||
import org.neo4j.graphalgo.WeightedPath; | ||
import org.neo4j.graphdb.GraphDatabaseService; | ||
import org.neo4j.graphdb.Node; | ||
import org.neo4j.graphdb.Transaction; | ||
import org.neo4j.procedure.Context; | ||
import org.neo4j.procedure.Description; | ||
import org.neo4j.procedure.Name; | ||
import org.neo4j.procedure.Procedure; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import static apoc.algo.PathFinding.buildPathExpander; | ||
|
||
@Extended | ||
public class PathFindingFull { | ||
|
||
@Context | ||
public GraphDatabaseService db; | ||
|
||
@Context | ||
public Transaction tx; | ||
|
||
@Procedure | ||
@Description("apoc.algo.aStarWithPoint(startNode, endNode, 'relTypesAndDirs', 'distance','pointProp') - " + | ||
"equivalent to apoc.algo.aStar but accept a Point type as a pointProperty instead of Number types as latitude and longitude properties") | ||
public Stream<WeightedPathResult> aStarWithPoint( | ||
@Name("startNode") Node startNode, | ||
@Name("endNode") Node endNode, | ||
@Name("relationshipTypesAndDirections") String relTypesAndDirs, | ||
@Name("weightPropertyName") String weightPropertyName, | ||
@Name("pointPropertyName") String pointPropertyName) { | ||
|
||
PathFinder<WeightedPath> algo = GraphAlgoFactory.aStar( | ||
new BasicEvaluationContext(tx, db), | ||
buildPathExpander(relTypesAndDirs), | ||
CommonEvaluators.doubleCostEvaluator(weightPropertyName), | ||
new PathFinding.GeoEstimateEvaluatorPointCustom(pointPropertyName)); | ||
return WeightedPathResult.streamWeightedPathResult(startNode, endNode, algo); | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
apoc.algo.aStarWithPoint | ||
apoc.bolt.execute | ||
apoc.bolt.load | ||
apoc.bolt.load.fromLocal | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package apoc.algo; | ||
|
||
import apoc.util.TestUtil; | ||
import org.junit.BeforeClass; | ||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
import org.neo4j.test.rule.DbmsRule; | ||
import org.neo4j.test.rule.ImpermanentDbmsRule; | ||
|
||
import static apoc.algo.AlgoUtil.SETUP_GEO; | ||
import static apoc.util.TestUtil.testResult; | ||
|
||
public class PathFindingFullTest { | ||
|
||
@ClassRule | ||
public static DbmsRule db = new ImpermanentDbmsRule(); | ||
|
||
|
||
@BeforeClass | ||
public static void setUp() throws Exception { | ||
TestUtil.registerProcedure(db, PathFindingFull.class); | ||
} | ||
|
||
@Test | ||
public void testAStarWithPoint() { | ||
db.executeTransactionally(SETUP_GEO); | ||
testResult(db, | ||
"MATCH (from:City {name:'München'}), (to:City {name:'Hamburg'}) " + | ||
"CALL apoc.algo.aStarWithPoint(from, to, 'DIRECT', 'dist', 'coords') yield path, weight " + | ||
"RETURN path, weight" , | ||
AlgoUtil::assertAStarResult | ||
); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package apoc.algo; | ||
|
||
import org.neo4j.graphdb.Node; | ||
import org.neo4j.graphdb.Path; | ||
import org.neo4j.graphdb.Result; | ||
import org.neo4j.internal.helpers.collection.Iterables; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class AlgoUtil { | ||
public static final String SETUP_GEO = "CREATE (b:City {name:'Berlin', coords: point({latitude:52.52464,longitude:13.40514}), lat:52.52464,lon:13.40514})\n" + | ||
"CREATE (m:City {name:'München', coords: point({latitude:48.1374,longitude:11.5755, height: 1}), lat:48.1374,lon:11.5755})\n" + | ||
"CREATE (f:City {name:'Frankfurt',coords: point({latitude:50.1167,longitude:8.68333, height: 1}), lat:50.1167,lon:8.68333})\n" + | ||
"CREATE (h:City {name:'Hamburg', coords: point({latitude:53.554423,longitude:9.994583, height: 1}), lat:53.554423,lon:9.994583})\n" + | ||
"CREATE (b)-[:DIRECT {dist:255.64*1000}]->(h)\n" + | ||
"CREATE (b)-[:DIRECT {dist:504.47*1000}]->(m)\n" + | ||
"CREATE (b)-[:DIRECT {dist:424.12*1000}]->(f)\n" + | ||
"CREATE (f)-[:DIRECT {dist:304.28*1000}]->(m)\n" + | ||
"CREATE (f)-[:DIRECT {dist:393.15*1000}]->(h)"; | ||
|
||
|
||
public static void assertAStarResult(Result r) { | ||
assertEquals(true, r.hasNext()); | ||
Map<String, Object> row = r.next(); | ||
assertEquals(697, ((Number)row.get("weight")).intValue()/1000) ; | ||
Path path = (Path) row.get("path"); | ||
assertEquals(2, path.length()) ; // 3nodes, 2 rels | ||
List<Node> nodes = Iterables.asList(path.nodes()); | ||
assertEquals("München", nodes.get(0).getProperty("name")) ; | ||
assertEquals("Frankfurt", nodes.get(1).getProperty("name")) ; | ||
assertEquals("Hamburg", nodes.get(2).getProperty("name")) ; | ||
assertEquals(false,r.hasNext()); | ||
} | ||
} |