Hi all,
I'm working on a procedure that should calculate a lowest-weight path
between two nodes using Dijkstra's algorithm. The procedure should only
returns paths whose all nodes match specific property values. if a path
contains at least one node which isn't valid according this logic, then the
entire path becomes invalid and the algorithm should look for the next
lowest-weight path.
In order to achieve this, I'm using a *PathExpanderBuilder *with node
filters, but they don't seem to filter anything.
This is my code:
public class Demo {
@Procedure
@Description("apoc.algo.dijkstraWithFilters(startNode, endNode, " +
"'distance', 10, 'prop1', 2, 'prop2', [100, 200], 'prop3') " +
" YIELD path, weight - run dijkstra with relationship property name
as cost function" +
" and a default weight if the property does not exist")
public Stream<WeightedPathResult> dijkstraWithFilters(
@Name("startNode") Node startNode,
@Name("endNode") Node endNode,
@Name("weightPropertyName") String weightPropertyName,
@Name("defaultWeight") double defaultWeight,
@Name("longPropName") String longPropName,
@Name("longPropValue") long longPropValue,
@Name("listPropName") String listPropName,
@Name("listPropValues") List<Long> listPropValues,
@Name("boolPropName") String boolPropName) {
PathFinder<WeightedPath> algo = GraphAlgoFactory.dijkstra(
buildPathExpanderByPermissions(longPropName, longPropValue,
listPropName, listPropValues, boolPropName),
(relationship, direction) ->
convertToDouble(relationship.getProperty(weightPropertyName, defaultWeight))
);
return WeightedPathResult.streamWeightedPathResult(startNode, endNode,
algo);
}
private double convertToDouble(Object property) {
if (property instanceof Double)
return (double) property;
else if (property instanceof Long)
return ((Long) property).doubleValue();
else if (property instanceof Integer)
return ((Integer) property).doubleValue();
return 1;
}
private PathExpander<Object> buildPathExpanderByPermissions(
String longPropName,
long longPropValue,
String listPropName,
List<Long> listPropValue,
String boolPropName
) {
PathExpanderBuilder builder =
PathExpanderBuilder.allTypesAndDirections();
builder.addNodeFilter(
node -> !node.hasProperty(longPropName) ||
node.getProperty(longPropName) instanceof Long &&
(long) node.getProperty(longPropName) <
longPropValue
);
builder.addNodeFilter(
node -> {
try {
return !node.hasProperty(listPropName) ||
(boolean) node.getProperty(boolPropName, false)
||
!Collections.disjoint((List<Long>)
node.getProperty(listPropName), listPropValue);
}
catch (Exception e){
return false;
}
}
);
return builder.build();
}
}
What am I missing here? Am I making a wrong use of *PathExpanderBuilder*?
--
You received this message because you are subscribed to the Google Groups
"Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.