class VarDependencies {

    public List<Var> getUsedAndIndirectUsedVars(ClauseId clauseId) {
        // Start with the used variable.
        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());

        // Start with the used variables:
        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()));
    }

    public  VarDependencies(final Statement statement);

}