Add printvertex test case
@@ -4,10 +4,14 @@ import com.google.common.collect.ImmutableSet;
import io.improbable.keanu.algorithms.mcmc.MetropolisHastings;
import io.improbable.keanu.network.BayesianNetwork;
import io.improbable.keanu.tensor.dbl.DoubleTensor;
+import io.improbable.keanu.tensor.intgr.IntegerTensor;
+import io.improbable.keanu.vertices.NonSaveableVertex;
import io.improbable.keanu.vertices.Vertex;
import io.improbable.keanu.vertices.dbl.DoubleVertex;
import io.improbable.keanu.vertices.dbl.nonprobabilistic.ConstantDoubleVertex;
import io.improbable.keanu.vertices.dbl.probabilistic.UniformVertex;
+import io.improbable.keanu.vertices.generic.nonprobabilistic.operators.unary.UnaryOpVertex;
+import io.improbable.keanu.vertices.intgr.nonprobabilistic.ConstantIntegerVertex;
import java.io.PrintStream;
import org.junit.Before;
import org.junit.Test;
@@ -71,6 +75,20 @@ public class PrintVertexTest {
verify(printStream).print(expected);
}
+ @Test
+ public void whenPrintVertexHasAChildThenChildCanGetGrandParentsValue() {
+ // this use case is quite unlikely, the Print vertex is meant to hang off its parent but not have any children
+ final Vertex<IntegerTensor> parent = new ConstantIntegerVertex(30);
+
+ final PrintVertex<IntegerTensor> sut = new PrintVertex<>(parent);
+
+
+ final UnaryOpVertex<IntegerTensor, IntegerTensor> child = new PlusOneOp(new long[]{1, 1}, sut);
+
+ assertThat(sut.getValue().scalar(), equalTo(30));
+ assertThat(child.getValue().scalar(), equalTo(31));
+ }
+
@Test
public void whenPrefixIsSuppliedThenItIsUsedInPresentation() {
final Vertex<DoubleTensor> parent = new ConstantDoubleVertex(30);
@@ -112,4 +130,15 @@ public class PrintVertexTest {
verify(printStream, atLeast(nSamples)).print(any(String.class));
}
+
+ private static class PlusOneOp extends UnaryOpVertex<IntegerTensor, IntegerTensor> implements NonSaveableVertex {
+ public PlusOneOp(final long[] shape, final Vertex<IntegerTensor> inputVertex) {
+ super(shape, inputVertex);
+ }
+
+ @Override
+ protected IntegerTensor op(final IntegerTensor a) {
+ return a.plus(1);
+ }
+ }
}
