Skip to content

Commit

Permalink
Prevented path expander termination filter from filtering below minLevel
Browse files Browse the repository at this point in the history
  • Loading branch information
InverseFalcon committed Apr 8, 2017
1 parent 2697eb3 commit 5cf89d3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/main/java/apoc/path/PathExplorer.java
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,14 @@ public LabelEvaluator(String labelFilter, boolean filterStartNode, long limit, i
@Override
public Evaluation evaluate(Path path) {
int depth = path.length();
// if start node shouldn't be filtered
Node check = path.endNode();

// below minLevel always exclude; continue if blacklist and whitelist allow it
if (depth < minLevel) {
return labelExists(check, blacklistLabels) || !whitelistAllowed(check) ? EXCLUDE_AND_PRUNE : EXCLUDE_AND_CONTINUE;
}

// if start node shouldn't be filtered, exclude/include based on if using termination/endnode filter or not
if (depth == 0 && !filterStartNode) {
return whitelistAllowedEvaluation;
}
Expand All @@ -244,10 +251,9 @@ public Evaluation evaluate(Path path) {
return EXCLUDE_AND_PRUNE;
}

Node check = path.endNode();
Evaluation result = labelExists(check, blacklistLabels) ? EXCLUDE_AND_PRUNE :
labelExists(check, terminationLabels) ? filterEndNode(check, depth, true) :
labelExists(check, endNodeLabels) ? filterEndNode(check, depth, false) :
labelExists(check, terminationLabels) ? filterEndNode(check, true) :
labelExists(check, endNodeLabels) ? filterEndNode(check, false) :
whitelistAllowed(check) ? whitelistAllowedEvaluation : EXCLUDE_AND_PRUNE;

return result;
Expand All @@ -270,10 +276,8 @@ private boolean whitelistAllowed(Node node) {
return whitelistLabels.isEmpty() || labelExists(node, whitelistLabels);
}

private Evaluation filterEndNode(Node node, int depth, boolean isTerminationFilter) {
if (depth >= minLevel) {
resultCount++;
}
private Evaluation filterEndNode(Node node, boolean isTerminationFilter) {
resultCount++;
return isTerminationFilter || !whitelistAllowed(node) ? INCLUDE_AND_PRUNE : INCLUDE_AND_CONTINUE;
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/apoc/path/ExpandPathTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,20 @@ public void testLimitPlaysNiceWithMinLevel() {
assertEquals("Clint Eastwood", path.endNode().getProperty("name"));
});
}

@Test
public void testTerminationFilterDoesNotPruneBelowMinLevel() {
db.execute("MATCH (c:Person) WHERE c.name in ['Clint Eastwood', 'Gene Hackman'] SET c:Western");

TestUtil.testResult(db,
"MATCH (k:Person {name:'Keanu Reeves'}) " +
"CALL apoc.path.expandConfig(k, {relationshipFilter:'ACTED_IN|PRODUCED|DIRECTED', labelFilter:'/Western', uniqueness: 'NODE_GLOBAL', minLevel:3}) yield path " +
"return path",
result -> {
List<Map<String, Object>> maps = Iterators.asList(result);
assertEquals(1, maps.size());
Path path = (Path) maps.get(0).get("path");
assertEquals("Clint Eastwood", path.endNode().getProperty("name"));
});
}
}

0 comments on commit 5cf89d3

Please sign in to comment.