### Question:
KeanuComputableGraph implements ComputableGraph { @Override public Map<VariableReference, ?> compute(Map<VariableReference, ?> inputs) { for (Map.Entry<VariableReference, ?> input : inputs.entrySet()) { vertexLookup.get(input.getKey()).setValue(input.getValue()); } for (int i = 0; i < topoSortedGraph.size(); i++) { final Vertex vertex = topoSortedGraph.get(i); if (!vertex.isProbabilistic() && !vertex.isObserved()) { vertex.setValue(((NonProbabilistic) vertex).calculate()); } } return outputs.stream() .collect(toMap(Vertex::getReference, v -> (Object) v.getValue())); } KeanuComputableGraph(List<Vertex> topoSortedGraph, Set<Vertex> outputs); @Override Map<VariableReference, ?> compute(Map<VariableReference, ?> inputs); @Override T getInput(VariableReference input);  }### Answer:
@Test public void canConvertSimpleGraphToComputationalGraph() { String cLabel = "C"; DoubleVertex C = A.plus(B); C.getValue(); C.setLabel(cLabel); List<Vertex> toposortedGraph = C.getConnectedGraph().stream() .sorted(Comparator.comparing(Vertex::getId, Comparator.naturalOrder())) .collect(Collectors.toList()); KeanuComputableGraph computableGraph = new KeanuComputableGraph(toposortedGraph, ImmutableSet.of(C)); computableGraph.compute(ImmutableMap.of( A.getReference(), DoubleTensor.scalar(1), B.getReference(), DoubleTensor.scalar(1) )); DoubleTensor evaluatedC = (DoubleTensor) computableGraph.compute(ImmutableMap.of( A.getReference(), DoubleTensor.scalar(2), B.getReference(), DoubleTensor.scalar(3) )).get(C.getReference()); assertEquals(DoubleTensor.scalar(5), evaluatedC); }
@Test public void canConvertGraphToComputationalGraph() { DoubleVertex C = A.plus(B); DoubleVertex D = C.plus(A); DoubleVertex E = C.plus(B); DoubleVertex F = D.plus(E); F.getValue(); List<Vertex> toposortedGraph = F.getConnectedGraph().stream() .sorted(Comparator.comparing(Vertex::getId, Comparator.naturalOrder())) .collect(Collectors.toList()); KeanuComputableGraph computableGraph = new KeanuComputableGraph(toposortedGraph, ImmutableSet.of(F)); computableGraph.compute(ImmutableMap.of( A.getReference(), DoubleTensor.scalar(1), B.getReference(), DoubleTensor.scalar(1) )); DoubleTensor evaluatedC = (DoubleTensor) computableGraph.compute(ImmutableMap.of( A.getReference(), DoubleTensor.scalar(2), B.getReference(), DoubleTensor.scalar(3) )).get(F.getReference()); assertEquals(DoubleTensor.scalar(15), evaluatedC); }