假设我的图有以下路径
(3)<--[IS_LOCATED_AT,9]--(8)--[CARRIES,77]-->(53)<--[CARRIES,76]--(7)--[IS_LOCATED_AT,8]-->(2)
(3)<--[IS_LOCATED_AT,9]--(8)--[BELONGS_TO,7]-->(1)<--[BELONGS_TO,6]--(7)--[IS_LOCATED_AT,8]-->(2)
(3)<--[IS_LOCATED_AT,9]--(8)--[BELONGS_TO,7]-->(55)<--[BELONGS_TO,61]--(7)--[IS_LOCATED_AT,8]-->(2)
(3)<--[IS_LOCATED_AT,9]--(8)--[CARRIES,77]-->(57)<--[CARRIES,75]--(7)--[IS_LOCATED_AT,8]-->(2)
假设路径具有以下提到的属性
(53) node has the properties--{Name:A}
(1) node has properties--{Name:B}
(55) node has properties--{Name:C}
(57) node has properties-- {Name:D}
假设我调用节点(3)和(2)之间的最短路径,当调用下面提到的方法时,我将得到以下输出:
假设当前输出为:
(3)<--[IS_LOCATED_AT,9]--(8)--[BELONGS_TO,7]-->(1)<--[BELONGS_TO,6]--(7)--[IS_LOCATED_AT,8]-->(2)
(3)<--[IS_LOCATED_AT,9]--(8)--[BELONGS_TO,7]-->(55)<--[BELONGS_TO,61]--(7)--[IS_LOCATED_AT,8]-->(2)
但是我的要求是1)在调用shortestpath api之前,它应该根据节点属性过滤路径假设我的过滤器是{Name:D},这是上面提到的(57)节点的属性
那么当调用下面提到的短路径方法时,我的输出应该如下所示,因为下面的路径具有节点(57)具有属性{Name:D}。
带过滤器的期望输出
(3)<--[IS_LOCATED_AT,9]--(8)--[CARRIES,77]-->(57)<--[CARRIES,75]--(7)--[IS_LOCATED_AT,8]-->(2)
我不想使用cypher。有谁能帮帮我吗?
public static Iterable<Path> shortestPath(Node sourceNode, Node destNode)
{
Integer depth=100;
PathExpander<?> expander;
List<Path> paths = new ArrayList<>();
if ( orderedRelTypes == null )
{
expander = PathExpanders.allTypesAndDirections();
}
else
{
PathExpanderBuilder expanderBuilder = PathExpanderBuilder.empty();
expanderBuilder = expanderBuilder
.add(MyRelationshipTypes.BELONGS_TO, Direction.BOTH)
.add(MyRelationshipTypes.CARRIES, Direction.BOTH)
.add(MyRelationshipTypes.IS_LOCATED_AT, Direction.BOTH);
expander = expanderBuilder.build();
}
try (Transaction tx = graphDb.beginTx()) {
PathFinder<Path> shortestPath = GraphAlgoFactory.shortestPath(
expander, depth == null ? 100 : depth.intValue());
Iterable<Path> pathss = shortestPath.findAllPaths(sourceNode, destNode);
for(Path path1:pathss){
paths.add(path1);
System.out.println("result::::"+paths);
}
tx.success();
}
return paths;
}
}
如果该限制应用于路径上的至少一个节点(而不是每个节点),则上面的代码片段将不起作用。这里应用的策略如下:
- 使用
shortestPath.findAllPaths
而不过滤节点属性 - 过滤您在1)中找到的最短路径,如果其中任何一个包含具有给定属性的节点。如果是:done,否则:
- 使用
GraphAlgoFactory.pathsWithLength()
并请求长度为+1的路径(其中长度为从步骤1开始的最短路径的长度) - 检查长度为+1的路径是否满足条件(见2)。如果不满足,则返回
length=length+1
的3。
另一种方法是使用GraphDatabaseService.bidirectionalTraversalDescription()
并在冲突评估器中应用属性检查。但我认为这种方法比上面描述的策略要复杂得多。