### Question:
GraphPattern { public boolean isFullyConnected() { final Map<K, Set<TuplePattern<K, V>>> patternByVariable = new HashMap<>(); final Map<TuplePattern<K, V>, Set<K>> variableByPattern = new HashMap<>(); for (TuplePattern<K, V> p : patterns) { Set<K> vars = new HashSet<>(); variableByPattern.put(p, vars); for (VariableOrConstant<K, V> vc : p.getPattern()) { K var = vc.getVariable(); if (null != var) { vars.add(var); Set<TuplePattern<K, V>> set = patternByVariable.get(var); if (null == set) { set = new HashSet<>(); patternByVariable.put(var, set); } set.add(p); } } } SimpleDirectedGraph<TuplePattern<K, V>> graph = new SimpleDirectedGraph<>( new DirectedGraph<TuplePattern<K, V>>() { @Override public void forVertices(Consumer<TuplePattern<K, V>> visitor) { for (TuplePattern<K, V> vertex : patterns) { visitor.accept(vertex); } } @Override public void forOutEdges(TuplePattern<K, V> tail, Consumer<TuplePattern<K, V>> headVisitor) { for (K key : variableByPattern.get(tail)) { Set<TuplePattern<K, V>> heads = patternByVariable.get(key); if (null != heads) { heads.stream().filter(head -> head != tail).forEach(headVisitor::accept); } } } }); return graph.isFullyConnected(); } GraphPattern(TuplePattern<K, V>... patterns); TuplePattern<K, V>[] getPatterns(); boolean isFullyConnected();  }### Answer:
@Test public void testIsFullyConnected() { GraphPattern<String, String> pattern; pattern = graphPattern( tuplePattern("?x", "knows", "?y")); assertTrue(pattern.isFullyConnected()); pattern = graphPattern( tuplePattern("?x", "knows", "?y"), tuplePattern("?y", "knows", "Arthur")); assertTrue(pattern.isFullyConnected()); pattern = graphPattern( tuplePattern("Ford", "knows", "Arthur")); assertTrue(pattern.isFullyConnected()); pattern = graphPattern( tuplePattern("Ford", "knows", "Arthur"), tuplePattern("Arthur", "knows", "Trillian")); assertFalse(pattern.isFullyConnected()); pattern = graphPattern( tuplePattern("Ford", "knows", "Arthur"), tuplePattern("?x", "knows", "Arthur")); assertFalse(pattern.isFullyConnected()); pattern = graphPattern( tuplePattern("?x", "knows", "?y"), tuplePattern("?y", "knows", "Arthur"), tuplePattern("?z", "likes", "Beer")); assertFalse(pattern.isFullyConnected()); }