-
Notifications
You must be signed in to change notification settings - Fork 489
/
PathFinding.java
145 lines (128 loc) · 6.81 KB
/
PathFinding.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package apoc.algo;
import org.neo4j.procedure.Description;
import apoc.path.RelationshipTypeAndDirections;
import apoc.result.PathResult;
import apoc.result.WeightedPathResult;
import apoc.util.Util;
import org.neo4j.graphalgo.CommonEvaluators;
import org.neo4j.graphalgo.GraphAlgoFactory;
import org.neo4j.graphalgo.PathFinder;
import org.neo4j.graphalgo.WeightedPath;
import org.neo4j.graphdb.*;
import org.neo4j.helpers.collection.Pair;
import org.neo4j.procedure.Context;
import org.neo4j.procedure.Name;
import org.neo4j.procedure.Procedure;
import java.util.Collections;
import java.util.Map;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
public class PathFinding {
@Context
public GraphDatabaseService db;
@Procedure
@Description("apoc.algo.aStar(startNode, endNode, 'KNOWS|<WORKS_WITH|IS_MANAGER_OF>', 'distance','lat','lon') " +
"YIELD path, weight - run A* with relationship property name as cost function")
public Stream<WeightedPathResult> aStar(
@Name("startNode") Node startNode,
@Name("endNode") Node endNode,
@Name("relationshipTypesAndDirections") String relTypesAndDirs,
@Name("weightPropertyName") String weightPropertyName,
@Name("latPropertyName") String latPropertyName,
@Name("lonPropertyName") String lonPropertyName) {
PathFinder<WeightedPath> algo = GraphAlgoFactory.aStar(
buildPathExpander(relTypesAndDirs),
CommonEvaluators.doubleCostEvaluator(weightPropertyName),
CommonEvaluators.geoEstimateEvaluator(latPropertyName, lonPropertyName));
return WeightedPathResult.streamWeightedPathResult(startNode, endNode, algo);
}
@Procedure
@Description("apoc.algo.aStar(startNode, endNode, 'KNOWS|<WORKS_WITH|IS_MANAGER_OF>', {weight:'dist',default:10," +
"x:'lon',y:'lat'}) YIELD path, weight - run A* with relationship property name as cost function")
public Stream<WeightedPathResult> aStarConfig(
@Name("startNode") Node startNode,
@Name("endNode") Node endNode,
@Name("relationshipTypesAndDirections") String relTypesAndDirs,
@Name("config") Map<String, Object> config) {
config = config == null ? Collections.emptyMap() : config;
String relationshipCostPropertyKey = config.getOrDefault("weight", "distance").toString();
double defaultCost = ((Number) config.getOrDefault("default", Double.MAX_VALUE)).doubleValue();
String latPropertyName = config.getOrDefault("y", "latitude").toString();
String lonPropertyName = config.getOrDefault("x", "longitude").toString();
PathFinder<WeightedPath> algo = GraphAlgoFactory.aStar(
buildPathExpander(relTypesAndDirs),
CommonEvaluators.doubleCostEvaluator(relationshipCostPropertyKey, defaultCost),
CommonEvaluators.geoEstimateEvaluator(latPropertyName, lonPropertyName));
return WeightedPathResult.streamWeightedPathResult(startNode, endNode, algo);
}
@Procedure
@Description("apoc.algo.dijkstra(startNode, endNode, 'KNOWS|<WORKS_WITH|IS_MANAGER_OF>', 'distance', defaultValue, numberOfWantedResults) YIELD path," +
" weight - run dijkstra with relationship property name as cost function")
public Stream<WeightedPathResult> dijkstra(
@Name("startNode") Node startNode,
@Name("endNode") Node endNode,
@Name("relationshipTypesAndDirections") String relTypesAndDirs,
@Name("weightPropertyName") String weightPropertyName,
@Name(value = "defaultWeight", defaultValue = "NaN") double defaultWeight,
@Name(value = "numberOfWantedPaths", defaultValue = "1") long numberOfWantedPaths) {
PathFinder<WeightedPath> algo = GraphAlgoFactory.dijkstra(
buildPathExpander(relTypesAndDirs),
(relationship, direction) -> Util.toDouble(relationship.getProperty(weightPropertyName, defaultWeight)),
(int)numberOfWantedPaths
);
return WeightedPathResult.streamWeightedPathResult(startNode, endNode, algo);
}
@Procedure
@Description("apoc.algo.allSimplePaths(startNode, endNode, 'KNOWS|<WORKS_WITH|IS_MANAGER_OF>', 5) YIELD path, " +
"weight - run allSimplePaths with relationships given and maxNodes")
public Stream<PathResult> allSimplePaths(
@Name("startNode") Node startNode,
@Name("endNode") Node endNode,
@Name("relationshipTypesAndDirections") String relTypesAndDirs,
@Name("maxNodes") long maxNodes) {
PathFinder<Path> algo = GraphAlgoFactory.allSimplePaths(
buildPathExpander(relTypesAndDirs),
(int) maxNodes
);
Iterable<Path> allPaths = algo.findAllPaths(startNode, endNode);
return StreamSupport.stream(allPaths.spliterator(), false)
.map(PathResult::new);
}
@Procedure(deprecatedBy = "apoc.algo.dijkstra")
@Description("apoc.algo.dijkstraWithDefaultWeight(startNode, endNode, 'KNOWS|<WORKS_WITH|IS_MANAGER_OF>', " +
"'distance', 10) YIELD path, weight - run dijkstra with relationship property name as cost function" +
" and a default weight if the property does not exist")
@Deprecated
public Stream<WeightedPathResult> dijkstraWithDefaultWeight(
@Name("startNode") Node startNode,
@Name("endNode") Node endNode,
@Name("relationshipTypesAndDirections") String relTypesAndDirs,
@Name("weightPropertyName") String weightPropertyName,
@Name("defaultWeight") double defaultWeight) {
PathFinder<WeightedPath> algo = GraphAlgoFactory.dijkstra(
buildPathExpander(relTypesAndDirs),
(relationship, direction) -> Util.toDouble(relationship.getProperty(weightPropertyName, defaultWeight))
);
return WeightedPathResult.streamWeightedPathResult(startNode, endNode, algo);
}
private PathExpander<Object> buildPathExpander(String relationshipsAndDirections) {
PathExpanderBuilder builder = PathExpanderBuilder.empty();
for (Pair<RelationshipType, Direction> pair : RelationshipTypeAndDirections
.parse(relationshipsAndDirections)) {
if (pair.first() == null) {
if (pair.other() == null) {
builder = PathExpanderBuilder.allTypesAndDirections();
} else {
builder = PathExpanderBuilder.allTypes(pair.other());
}
} else {
if (pair.other() == null) {
builder = builder.add(pair.first());
} else {
builder = builder.add(pair.first(), pair.other());
}
}
}
return builder.build();
}
}