VarDependencies { public List<Var> getUsedAndIndirectUsedVars(ClauseId clauseId) { Set<Var> result = new HashSet<>(this.getUsedVars(clauseId)); Stack<Var> stack = new Stack<>(); List<Relationship> relsToInspect = relationships.entrySet().stream() .filter(a -> a.getKey().compareTo(clauseId) < 0) .flatMap(x -> x.getValue().stream()) .collect(Collectors.toList()); stack.addAll(result); while (!stack.isEmpty()) { Var var = stack.pop(); if (var instanceof NodeVar) { relsToInspect.stream().filter(r -> r.leftNode == var).forEach(rel -> { if (!result.contains(rel.rightNode)) { stack.add(rel.rightNode); result.add(rel.rightNode); } }); relsToInspect.stream().filter(r -> r.rightNode == var).forEach(rel -> { if (!result.contains(rel.leftNode)) { stack.add(rel.leftNode); result.add(rel.leftNode); } }); } } return Collections.unmodifiableList(result.stream().collect(Collectors.toList())); } VarDependencies(final Statement statement); List<Var> getUsedVars(ClauseId clauseId); Set<Var> getAllVariables(); List<Var> getUsedAndIndirectUsedVars(ClauseId clauseId); AvailableVariables getReachableVars(ClauseId clauseId); List<Relationship> getRelationships(ClauseId clauseId); List<Expr> getReturnExprs();  }
@Test public void testGetUsedAndIndirectUsedVars1() { String cypher = "" + "MATCH (a)--(b)\n" + "MATCH (b)--(c)\n" + "MATCH (a)--(d)\n" + "RETURN 42"; Statement st = PassAvailables.parseCypher(cypher); SingleQuery sq = (SingleQuery) st.query.part; VarDependencies dependencies = new VarDependencies(st); Iterator<Clause> clauses = sq.clauses.iterator(); List<Var> match0 = dependencies.getUsedAndIndirectUsedVars( this.genClauseForASTNode(this.getPatternPart(clauses.next()).next())); List<Var> match1 = dependencies.getUsedAndIndirectUsedVars( this.genClauseForASTNode(this.getPatternPart(clauses.next()).next())); List<Var> match2 = dependencies.getUsedAndIndirectUsedVars( this.genClauseForASTNode(this.getPatternPart(clauses.next()).next())); Assert.assertEquals(match0.size(), 2); Assert.assertEquals(match1.size(), 3); Assert.assertEquals(match2.size(), 4); }
@Test public void testGetUsedAndIndirectUsedVars2() { String cypher = "" + "MATCH (a)--(e)\n" + "MATCH (b)--(c)\n" + "MATCH (a)--(d)\n" + "RETURN 42"; Statement st = PassAvailables.parseCypher(cypher); SingleQuery sq = (SingleQuery) st.query.part; VarDependencies dependencies = new VarDependencies(st); Iterator<Clause> clauses = sq.clauses.iterator(); List<Var> match0 = dependencies.getUsedAndIndirectUsedVars( this.genClauseForASTNode(this.getPatternPart(clauses.next()).next())); List<Var> match1 = dependencies.getUsedAndIndirectUsedVars( this.genClauseForASTNode(this.getPatternPart(clauses.next()).next())); List<Var> match2 = dependencies.getUsedAndIndirectUsedVars( this.genClauseForASTNode(this.getPatternPart(clauses.next()).next())); Assert.assertEquals(match0.size(), 2); Assert.assertEquals(match1.size(), 2); Assert.assertEquals(match2.size(), 3); }