From 36b9fdfaa2f9591c8f4c4d494b566830a50275b1 Mon Sep 17 00:00:00 2001 From: Paolo Bizzarri Date: Fri, 6 Sep 2024 11:04:04 +0200 Subject: [PATCH] Assertj cleanup. Junit5 migration --- drools-beliefs/pom.xml | 6 +- .../beliefs/bayes/BayesAbsorbtionTest.java | 15 ++-- .../beliefs/bayes/BayesProjectionTest.java | 12 +-- .../beliefs/bayes/GlobalUpdateTest.java | 62 ++++++------- .../org/drools/beliefs/bayes/GraphTest.java | 14 +-- .../bayes/JunctionTreeBuilderTest.java | 87 +++++++++---------- .../beliefs/bayes/JunctionTreeTest.java | 38 +++----- .../drools/beliefs/bayes/LikelyhoodTest.java | 27 ------ .../beliefs/bayes/MarginalizerTest.java | 2 +- .../drools/beliefs/bayes/PassMessageTest.java | 10 +-- .../beliefs/bayes/example/EarthQuakeTest.java | 69 +++++++-------- .../beliefs/bayes/example/SprinkerTest.java | 76 ++++++++-------- .../bayes/integration/AssemblerTest.java | 2 +- .../integration/BayesBeliefSystemTest.java | 2 +- .../bayes/integration/BayesRuntimeTest.java | 2 +- .../beliefs/bayes/integration/ParserTest.java | 50 +++++------ .../beliefs/bayes/integration/WeaverTest.java | 2 +- 17 files changed, 202 insertions(+), 274 deletions(-) delete mode 100644 drools-beliefs/src/test/java/org/drools/beliefs/bayes/LikelyhoodTest.java diff --git a/drools-beliefs/pom.xml b/drools-beliefs/pom.xml index d3e31fc78f1..cfd1cbaefb8 100644 --- a/drools-beliefs/pom.xml +++ b/drools-beliefs/pom.xml @@ -95,10 +95,10 @@ test - junit - junit + org.junit.jupiter + junit-jupiter test - + org.assertj assertj-core diff --git a/drools-beliefs/src/test/java/org/drools/beliefs/bayes/BayesAbsorbtionTest.java b/drools-beliefs/src/test/java/org/drools/beliefs/bayes/BayesAbsorbtionTest.java index 07c5a481ddf..e2a8cd601f5 100644 --- a/drools-beliefs/src/test/java/org/drools/beliefs/bayes/BayesAbsorbtionTest.java +++ b/drools-beliefs/src/test/java/org/drools/beliefs/bayes/BayesAbsorbtionTest.java @@ -20,15 +20,16 @@ import org.drools.beliefs.graph.Graph; import org.drools.beliefs.graph.GraphNode; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.Arrays; +import static org.assertj.core.api.Assertions.assertThat; import static org.drools.beliefs.bayes.GraphTest.addNode; import static org.drools.beliefs.bayes.GraphTest.bitSet; -import static org.drools.beliefs.bayes.JunctionTreeTest.assertArray; import static org.drools.beliefs.bayes.JunctionTreeTest.scaleDouble; + public class BayesAbsorbtionTest { @Test @@ -37,7 +38,7 @@ public void testDivide1() { double[] oldD = new double[] { 2, 4, 1 }; double[] r = BayesAbsorption.dividePotentials(newD, oldD); - assertArray(new double[]{5, 2, 4}, scaleDouble(3, r)); + assertThat(scaleDouble(3, r)).containsExactly(5, 2, 4); } @Test @@ -46,7 +47,7 @@ public void testDivide2() { double[] oldD = new double[] { 0.1, 0.2, 0.3, 0.4 }; double[] r = BayesAbsorption.dividePotentials(newD, oldD); - assertArray(new double[]{5.0, 5.0, 5.0, 5.0}, scaleDouble(3, r)); + assertThat(scaleDouble(3, r)).containsExactly(5.0, 5.0, 5.0, 5.0); } @Test @@ -94,7 +95,7 @@ public void testAbsorption1() { BayesAbsorption p = new BayesAbsorption(sepVarPos, oldSepPotentials, sep.getPotentials(), sepVarMultipliers, vars, node1.getPotentials()); p.absorb(); - assertArray(new double[]{0.035, 0.135, 0.3, 0.529}, scaleDouble(3, node1.getPotentials())); + assertThat(scaleDouble(3, node1.getPotentials())).containsExactly(0.035, 0.135, 0.3, 0.529); } @Test @@ -145,7 +146,7 @@ public void testAbsorption2() { BayesAbsorption p = new BayesAbsorption(sepVarPos, oldSepPotentials, sep.getPotentials(), sepVarMultipliers, vars, node1.getPotentials()); p.absorb(); - assertArray(new double[]{ 0.01, 0.019, 0.055, 0.073, 0.137, 0.163, 0.254, 0.289 }, scaleDouble(3, node1.getPotentials())); + assertThat(scaleDouble(3, node1.getPotentials())).containsExactly(0.01, 0.019, 0.055, 0.073, 0.137, 0.163, 0.254, 0.289); } @Test @@ -197,6 +198,6 @@ public void testAbsorption3() { BayesAbsorption p = new BayesAbsorption(sepVarPos, oldSepPotentials, sep.getPotentials(), sepVarMultipliers, vars, node1.getPotentials()); p.absorb(); - assertArray(new double[]{0.01, 0.038, 0.028, 0.075, 0.139, 0.222, 0.194, 0.295}, scaleDouble(3, node1.getPotentials())); + assertThat(scaleDouble(3, node1.getPotentials())).containsExactly(0.01, 0.038, 0.028, 0.075, 0.139, 0.222, 0.194, 0.295); } } \ No newline at end of file diff --git a/drools-beliefs/src/test/java/org/drools/beliefs/bayes/BayesProjectionTest.java b/drools-beliefs/src/test/java/org/drools/beliefs/bayes/BayesProjectionTest.java index bb6aabcacea..2d72957bb91 100644 --- a/drools-beliefs/src/test/java/org/drools/beliefs/bayes/BayesProjectionTest.java +++ b/drools-beliefs/src/test/java/org/drools/beliefs/bayes/BayesProjectionTest.java @@ -20,11 +20,11 @@ import org.drools.beliefs.graph.Graph; import org.drools.beliefs.graph.GraphNode; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; import static org.drools.beliefs.bayes.GraphTest.addNode; import static org.drools.beliefs.bayes.GraphTest.bitSet; -import static org.drools.beliefs.bayes.JunctionTreeTest.assertArray; import static org.drools.beliefs.bayes.JunctionTreeTest.scaleDouble; public class BayesProjectionTest { @@ -66,7 +66,7 @@ public void testProjection1() { BayesProjection p = new BayesProjection(vars, node1.getPotentials(), sepVarPos, sepVarMultipliers, projectedSepPotentials); p.project(); - assertArray(new double[]{0.1, 0.2, 0.3, 0.4}, scaleDouble(3, projectedSepPotentials)); + assertThat(scaleDouble(3, projectedSepPotentials)).containsExactly(0.1, 0.2, 0.3, 0.4); } @Test @@ -110,7 +110,7 @@ public void testProjection2() { p.project(); // remember it's been normalized, from 0.3, 0.7, 1.1, 1.5 - assertArray(new double[]{0.083, 0.194, 0.306, 0.417}, scaleDouble(3, projectedSepPotentials)); + assertThat(scaleDouble(3, projectedSepPotentials)).containsExactly(0.083, 0.194, 0.306, 0.417); } @Test @@ -156,7 +156,7 @@ public void testProjection3() { p.project(); // remember it's been normalized, from 0.4, 0.6, 1.2, 1.4 - assertArray(new double[]{0.111, 0.167, 0.333, 0.389}, scaleDouble(3, projectedSepPotentials)); + assertThat(scaleDouble(3, projectedSepPotentials)).containsExactly(0.111, 0.167, 0.333, 0.389); } @Test @@ -202,7 +202,7 @@ public void testProjection4() { p.project(); // remember it's been normalized, from 0.6 0.8 1.0 1.2 - assertArray(new double[]{0.167, 0.222, 0.278, 0.333}, scaleDouble(3, projectedSepPotentials)); + assertThat(scaleDouble(3, projectedSepPotentials)).containsExactly(0.167, 0.222, 0.278, 0.333); } } \ No newline at end of file diff --git a/drools-beliefs/src/test/java/org/drools/beliefs/bayes/GlobalUpdateTest.java b/drools-beliefs/src/test/java/org/drools/beliefs/bayes/GlobalUpdateTest.java index 3955b007eca..a60859989e8 100644 --- a/drools-beliefs/src/test/java/org/drools/beliefs/bayes/GlobalUpdateTest.java +++ b/drools-beliefs/src/test/java/org/drools/beliefs/bayes/GlobalUpdateTest.java @@ -20,11 +20,10 @@ import org.drools.beliefs.graph.Graph; import org.drools.beliefs.graph.GraphNode; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; @@ -63,7 +62,7 @@ public class GlobalUpdateTest { final List messageResults = new ArrayList(); final List globalUpdateResults = new ArrayList(); - @Before + @BeforeEach public void startUp() { int i = 0; List list = new ArrayList(); @@ -111,92 +110,92 @@ public void afterGlobalUpdate(CliqueState clique) { @Test public void testCollectFromRootClique() { bayesInstance.collectEvidence(n0); - assertThat(messageResults).isEqualTo(asList("3:2", "4:2", "2:1", "7:6", "8:6", "6:5", "5:1", "1:0")); + assertThat(messageResults).containsExactly("3:2", "4:2", "2:1", "7:6", "8:6", "6:5", "5:1", "1:0"); } @Test public void testCollectFromMidTipClique() { bayesInstance.collectEvidence(n4); - assertThat(messageResults).isEqualTo(asList("0:1", "7:6", "8:6", "6:5", "5:1", "1:2", "3:2", "2:4")); + assertThat(messageResults).containsExactly("0:1", "7:6", "8:6", "6:5", "5:1", "1:2", "3:2", "2:4"); } @Test public void testCollectFromEndTipClique() { bayesInstance.collectEvidence(n7); - assertThat(messageResults).isEqualTo(asList("0:1", "3:2", "4:2", "2:1", "1:5", "5:6", "8:6", "6:7")); + assertThat(messageResults).containsExactly("0:1", "3:2", "4:2", "2:1", "1:5", "5:6", "8:6", "6:7"); } @Test public void testCollectFromMidClique() { bayesInstance.collectEvidence(n5); - assertThat(messageResults).isEqualTo(asList("0:1", "3:2", "4:2", "2:1", "1:5", "7:6", "8:6", "6:5")); + assertThat(messageResults).containsExactly("0:1", "3:2", "4:2", "2:1", "1:5", "7:6", "8:6", "6:5"); } @Test public void testDistributeFromRootClique() { bayesInstance.distributeEvidence(n0); - assertThat(messageResults).isEqualTo(asList("0:1", "1:2", "2:3", "2:4", "1:5", "5:6", "6:7", "6:8")); + assertThat(messageResults).containsExactly("0:1", "1:2", "2:3", "2:4", "1:5", "5:6", "6:7", "6:8"); } @Test public void testDistributeFromMidTipClique() { bayesInstance.distributeEvidence(n4); - assertThat(messageResults).isEqualTo(asList("4:2", "2:1", "1:0", "1:5", "5:6", "6:7", "6:8", "2:3")); + assertThat(messageResults).containsExactly("4:2", "2:1", "1:0", "1:5", "5:6", "6:7", "6:8", "2:3"); } @Test public void testDistributeFromEndTipClique() { bayesInstance.distributeEvidence(n7); - assertThat(messageResults).isEqualTo(asList("7:6", "6:5", "5:1", "1:0", "1:2", "2:3", "2:4", "6:8")); + assertThat(messageResults).containsExactly("7:6", "6:5", "5:1", "1:0", "1:2", "2:3", "2:4", "6:8"); } @Test public void testDistributeFromMidClique() { bayesInstance.distributeEvidence(n5); - assertThat(messageResults).isEqualTo(asList("5:1", "1:0", "1:2", "2:3", "2:4", "5:6", "6:7", "6:8")); + assertThat(messageResults).containsExactly("5:1", "1:0", "1:2", "2:3", "2:4", "5:6", "6:7", "6:8"); } @Test public void testGlobalUpdateFromRootClique() { bayesInstance.globalUpdate(n0); - assertThat(messageResults).isEqualTo(asList("3:2", "4:2", "2:1", "7:6", "8:6", "6:5", "5:1", "1:0", //n0 + assertThat(messageResults).containsExactly("3:2", "4:2", "2:1", "7:6", "8:6", "6:5", "5:1", "1:0", //n0 "0:1", "1:2", "2:3", "2:4", "1:5", "5:6", "6:7", "6:8" //n0 - )); - assertThat(globalUpdateResults).isEqualTo(asList("0")); + ); + assertThat(globalUpdateResults).containsExactly("0"); } @Test public void testGlobalUpdateFromMidTipClique() { bayesInstance.globalUpdate(n4); - assertThat(messageResults).isEqualTo(asList("0:1", "7:6", "8:6", "6:5", "5:1", "1:2", "3:2", "2:4", //n4 + assertThat(messageResults).containsExactly("0:1", "7:6", "8:6", "6:5", "5:1", "1:2", "3:2", "2:4", //n4 "4:2", "2:1", "1:0", "1:5", "5:6", "6:7", "6:8", "2:3" //n4 - )); - assertThat(globalUpdateResults).isEqualTo(asList("4")); + ); + assertThat(globalUpdateResults).containsExactly("4"); } @Test public void testGlobalUpdateFromEndTipClique() { bayesInstance.globalUpdate(n7); - assertThat(messageResults).isEqualTo(asList("0:1", "3:2", "4:2", "2:1", "1:5", "5:6", "8:6", "6:7", //n7 + assertThat(messageResults).containsExactly("0:1", "3:2", "4:2", "2:1", "1:5", "5:6", "8:6", "6:7", //n7 "7:6", "6:5", "5:1", "1:0", "1:2", "2:3", "2:4", "6:8" //n7 - )); - assertThat(globalUpdateResults).isEqualTo(asList("7")); + ); + assertThat(globalUpdateResults).containsExactly("7"); } @Test public void testGlobalUpdateFromMidClique() { bayesInstance.globalUpdate(n5); - assertThat(messageResults).isEqualTo(asList("0:1", "3:2", "4:2", "2:1", "1:5", "7:6", "8:6", "6:5", //n5 + assertThat(messageResults).containsExactly("0:1", "3:2", "4:2", "2:1", "1:5", "7:6", "8:6", "6:5", //n5 "5:1", "1:0", "1:2", "2:3", "2:4", "5:6", "6:7", "6:8" //n5 - )); - assertThat(globalUpdateResults).isEqualTo(asList("5")); + ); + assertThat(globalUpdateResults).containsExactly("5"); } @Test public void testDistributeFromGlobalUpdate() { bayesInstance.globalUpdate(); - assertThat(messageResults).isEqualTo(asList("3:2", "4:2", "2:1", "7:6", "8:6", "6:5", "5:1", "1:0", //n0 + assertThat(messageResults).containsExactly("3:2", "4:2", "2:1", "7:6", "8:6", "6:5", "5:1", "1:0", //n0 "0:1", "1:2", "2:3", "2:4", "1:5", "5:6", "6:7", "6:8" //n0 // "0:1", "3:2", "4:2", "2:1", "7:6", "8:6", "6:5", "5:1", //n1 // "1:0", "1:2", "2:3", "2:4", "1:5", "5:6", "6:7", "6:8", //n1 @@ -214,18 +213,9 @@ public void testDistributeFromGlobalUpdate() { // "7:6", "6:5", "5:1", "1:0", "1:2", "2:3", "2:4", "6:8", //n7 // "0:1", "3:2", "4:2", "2:1", "1:5", "5:6", "7:6", "6:8", //n8 // "8:6", "6:5", "5:1", "1:0", "1:2", "2:3", "2:4", "6:7" //n8 - )); + ); -// assertEquals( asList( "0", "1", "2", "3", "4", "5", "6", "7", "8"), globalUpdateResults); - assertThat(globalUpdateResults).isEqualTo(asList("0")); - } - - public void testGlobalUpdate() { - bayesInstance.globalUpdate(); - } - - public List asList(String... items) { - return Arrays.asList(items); + assertThat(globalUpdateResults).containsExactly("0"); } public void connectChildren(Graph graph, JunctionTreeClique parent, List list, JunctionTreeClique... children) { diff --git a/drools-beliefs/src/test/java/org/drools/beliefs/bayes/GraphTest.java b/drools-beliefs/src/test/java/org/drools/beliefs/bayes/GraphTest.java index 640b78a32a7..f844aec9655 100644 --- a/drools-beliefs/src/test/java/org/drools/beliefs/bayes/GraphTest.java +++ b/drools-beliefs/src/test/java/org/drools/beliefs/bayes/GraphTest.java @@ -23,9 +23,7 @@ import org.drools.beliefs.graph.impl.EdgeImpl; import org.drools.util.bitmask.OpenBitSet; -import java.util.ArrayList; import java.util.Collection; -import java.util.List; import static org.assertj.core.api.Assertions.assertThat; @@ -56,11 +54,11 @@ public static boolean assertLinkedVertex(boolean[][] adjMatrix, int... ints) { int id = ints[0]; Collection adjVert = JunctionTreeBuilder.getAdjacentVertices(adjMatrix, id); - assertThat(adjVert.size()).isEqualTo(ints.length - 1); + assertThat(adjVert).hasSize(ints.length - 1); for ( int i = 1; i < ints.length; i++ ) { assertThat(adjMatrix[id][ints[i]]).as("link was not true " + id + ", " + i).isTrue(); assertThat(adjMatrix[ints[i]][id]).as("link was not true " + i + ", " + id).isTrue(); - assertThat(adjVert.contains(ints[i])).as("does not contain " + ints[i]).isTrue(); + assertThat(adjVert).as("does not contain " + ints[i]).contains(ints[i]); } return false; @@ -75,14 +73,6 @@ public static GraphNode addNode(Graph graph) { - public static List asList(int[] array) { - List list = new ArrayList(array.length); - for (int i = 0; i < array.length; i++) { - list.add(array[i]); - } - return list; - } - public static OpenBitSet bitSet(String s) { OpenBitSet bitSet = new OpenBitSet( ); bitSet.setBits(new long[] { Long.valueOf(s, 2) }); diff --git a/drools-beliefs/src/test/java/org/drools/beliefs/bayes/JunctionTreeBuilderTest.java b/drools-beliefs/src/test/java/org/drools/beliefs/bayes/JunctionTreeBuilderTest.java index 56fcb2675ac..a284033836a 100644 --- a/drools-beliefs/src/test/java/org/drools/beliefs/bayes/JunctionTreeBuilderTest.java +++ b/drools-beliefs/src/test/java/org/drools/beliefs/bayes/JunctionTreeBuilderTest.java @@ -21,10 +21,9 @@ import org.drools.beliefs.graph.Graph; import org.drools.beliefs.graph.GraphNode; import org.drools.util.bitmask.OpenBitSet; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -187,8 +186,8 @@ public void testCreateClique() { boolean[][] clonedAdjMatrix = JunctionTreeBuilder.cloneAdjacencyMarix(jtBuilder.getAdjacencyMatrix()); jtBuilder.createClique(dX1.getId(), clonedAdjMatrix, vertices, adjList ); - assertThat(vertices.size()).isEqualTo(3); - assertThat(vertices.containsAll(Arrays.asList(2, 3, 4))).isTrue(); + assertThat(vertices).hasSize(3); + assertThat(vertices).containsExactly(2, 3, 4); assertLinkedNode(jtBuilder, 1, 2, 3, 4); assertLinkedNode(jtBuilder, 2, 1, 3, 4); @@ -204,18 +203,18 @@ public void testCliqueSuperSet() { List cliques = new ArrayList(); OpenBitSet OpenBitSet1 = bitSet("00011110"); jtBuilder.updateCliques(cliques, OpenBitSet1); - assertThat(cliques.size()).isEqualTo(1); + assertThat(cliques).hasSize(1); // ignore subset OpenBitSet OpenBitSet2 = bitSet("00000110"); jtBuilder.updateCliques(cliques, OpenBitSet2); - assertThat(cliques.size()).isEqualTo(1); + assertThat(cliques).hasSize(1); assertThat(cliques.get(0)).isEqualTo(OpenBitSet1); // add overlapping, as not a pure subset OpenBitSet OpenBitSet3 = bitSet("01000110"); jtBuilder.updateCliques(cliques, OpenBitSet3); - assertThat(cliques.size()).isEqualTo(2); + assertThat(cliques).hasSize(2); assertThat(cliques.get(0)).isEqualTo(OpenBitSet1); assertThat(cliques.get(1)).isEqualTo(OpenBitSet3); } @@ -288,7 +287,7 @@ public void testPriorityQueueWithMinimalNewEdges() { assertThat(id).isEqualTo(8); assertThat(v.getNewEdgesRequired()).isEqualTo(6); - assertThat(p.size()).isEqualTo(0); + assertThat(p).isEmpty(); } @Test @@ -363,7 +362,7 @@ public void testPriorityQueueWithMaximalCliqueWeight() { assertThat(id).isEqualTo(5); assertThat(v.getWeightRequired()).isEqualTo(81); - assertThat(p.size()).isEqualTo(0); + assertThat(p).isEmpty(); } @Test @@ -443,8 +442,8 @@ public void testIterativeEliminationUsingEdgeAndWeight() { Set verticesToUpdate = new HashSet(); boolean[] adjList = clonedAdjMatrix[ id ]; jtBuilder.createClique(5, clonedAdjMatrix, verticesToUpdate, adjList); - assertThat(verticesToUpdate.size()).isEqualTo(4); - assertThat(verticesToUpdate.containsAll(Arrays.asList(1, 3, 6))).isTrue(); + assertThat(verticesToUpdate).hasSize(4); + assertThat(verticesToUpdate).contains(1, 3, 6); jtBuilder.eliminateVertex(p, elmVertMap, clonedAdjMatrix, adjList, verticesToUpdate, v ); // assert all new edges @@ -477,8 +476,8 @@ public void testIterativeEliminationUsingEdgeAndWeight() { verticesToUpdate = new HashSet(); adjList = clonedAdjMatrix[ id ]; jtBuilder.createClique(4, clonedAdjMatrix, verticesToUpdate, adjList); - assertThat(verticesToUpdate.size()).isEqualTo(3); - assertThat(verticesToUpdate.containsAll(Arrays.asList(1, 2, 6))).isTrue(); // don't forget 3 and 5 were already eliminated + assertThat(verticesToUpdate).hasSize(3); + assertThat(verticesToUpdate).containsExactly(1, 2, 6); // don't forget 3 and 5 were already eliminated jtBuilder.eliminateVertex(p, elmVertMap, clonedAdjMatrix, adjList, verticesToUpdate, v ); // assert all new edges @@ -518,7 +517,7 @@ public void testIterativeEliminationUsingEdgeAndWeight() { jtBuilder.createClique(6, clonedAdjMatrix, verticesToUpdate, adjList); jtBuilder.eliminateVertex(p, elmVertMap, clonedAdjMatrix, adjList, verticesToUpdate, v ); - assertThat(p.size()).isEqualTo(0); + assertThat(p).isEmpty(); } @Test @@ -611,11 +610,11 @@ public void testTriangulate2() { assertLinkedVertex(jtBuilder.getAdjacencyMatrix(), 5, 2, 3, 6); assertLinkedVertex(jtBuilder.getAdjacencyMatrix(), 6, 2, 5); - assertThat(cliques.size()).isEqualTo(5); // 5th is 0, which is just a dummy V to get numbers aligned - assertThat(cliques.contains(bitSet("1110"))).isTrue(); // x1, x2, x3 //a, b, c - assertThat(cliques.contains(bitSet("10100"))).isTrue(); // x2, x4 - assertThat(cliques.contains(bitSet("1100100"))).isTrue(); // x2, x5, x6 - assertThat(cliques.contains(bitSet("101100"))).isTrue(); // x2, x3, x5 + assertThat(cliques).hasSize(5); // 5th is 0, which is just a dummy V to get numbers aligned + assertThat(cliques).contains(bitSet("1110")); // x1, x2, x3 //a, b, c + assertThat(cliques).contains(bitSet("10100")); // x2, x4 + assertThat(cliques).contains(bitSet("1100100")); // x2, x5, x6 + assertThat(cliques).contains(bitSet("101100")); // x2, x3, x5 } @Test @@ -805,7 +804,7 @@ public void testJunctionTreeNoPruning() { assertThat(jtNode.getBitSet()).isEqualTo(OpenBitSet1); - assertThat(jtNode.getChildren().size()).isEqualTo(1); + assertThat(jtNode.getChildren()).hasSize(1); JunctionTreeSeparator sep = jtNode.getChildren().get(0); assertThat(sep.getParent().getBitSet()).isEqualTo(OpenBitSet1); assertThat(sep.getChild().getBitSet()).isEqualTo(OpenBitSet2); @@ -813,7 +812,7 @@ public void testJunctionTreeNoPruning() { jtNode = sep.getChild(); assertThat(jtNode.getBitSet()).isEqualTo(OpenBitSet2); - assertThat(jtNode.getChildren().size()).isEqualTo(1); + assertThat(jtNode.getChildren()).hasSize(1); sep = jtNode.getChildren().get(0); assertThat(sep.getParent().getBitSet()).isEqualTo(OpenBitSet2); assertThat(sep.getChild().getBitSet()).isEqualTo(OpenBitSet3); @@ -853,16 +852,16 @@ public void testJunctionWithPruning1() { assertThat(jtNode.getBitSet()).isEqualTo(OpenBitSet1); - assertThat(jtNode.getChildren().size()).isEqualTo(2); + assertThat(jtNode.getChildren()).hasSize(2); JunctionTreeSeparator sep = jtNode.getChildren().get(0); assertThat(sep.getParent().getBitSet()).isEqualTo(OpenBitSet1); assertThat(sep.getChild().getBitSet()).isEqualTo(OpenBitSet2); - assertThat(sep.getChild().getChildren().size()).isEqualTo(0); + assertThat(sep.getChild().getChildren()).isEmpty(); sep = jtNode.getChildren().get(1); assertThat(sep.getParent().getBitSet()).isEqualTo(OpenBitSet1); assertThat(sep.getChild().getBitSet()).isEqualTo(OpenBitSet3); - assertThat(sep.getChild().getChildren().size()).isEqualTo(0); + assertThat(sep.getChild().getChildren()).isEmpty(); } @@ -904,26 +903,26 @@ public void testJunctionWithPruning2() { assertThat(root.getBitSet()).isEqualTo(OpenBitSet1); - assertThat(root.getChildren().size()).isEqualTo(2); + assertThat(root.getChildren()).hasSize(2); JunctionTreeSeparator sep = root.getChildren().get(0); assertThat(sep.getParent().getBitSet()).isEqualTo(OpenBitSet1); assertThat(sep.getChild().getBitSet()).isEqualTo(OpenBitSet2); - assertThat(sep.getChild().getChildren().size()).isEqualTo(1); + assertThat(sep.getChild().getChildren()).hasSize(1); jtNode = sep.getChild(); assertThat(jtNode.getBitSet()).isEqualTo(OpenBitSet2); - assertThat(jtNode.getChildren().size()).isEqualTo(1); + assertThat(jtNode.getChildren()).hasSize(1); sep = jtNode.getChildren().get(0); assertThat(sep.getParent().getBitSet()).isEqualTo(OpenBitSet2); assertThat(sep.getChild().getBitSet()).isEqualTo(OpenBitSet3); assertThat(sep.getBitSet()).isEqualTo(intersect2And3); - assertThat(sep.getChild().getChildren().size()).isEqualTo(0); + assertThat(sep.getChild().getChildren()).isEmpty(); sep = root.getChildren().get(1); assertThat(sep.getParent().getBitSet()).isEqualTo(OpenBitSet1); assertThat(sep.getChild().getBitSet()).isEqualTo(OpenBitSet4); assertThat(sep.getBitSet()).isEqualTo(intersect1And4); - assertThat(sep.getChild().getChildren().size()).isEqualTo(0); + assertThat(sep.getChild().getChildren()).isEmpty(); } @Test @@ -966,26 +965,26 @@ public void testJunctionWithPruning3() { JunctionTreeClique root = jtNode; assertThat(root.getBitSet()).isEqualTo(OpenBitSet1); - assertThat(root.getChildren().size()).isEqualTo(2); + assertThat(root.getChildren()).hasSize(2); JunctionTreeSeparator sep = root.getChildren().get(0); assertThat(sep.getParent().getBitSet()).isEqualTo(OpenBitSet1); assertThat(sep.getChild().getBitSet()).isEqualTo(OpenBitSet2); - assertThat(sep.getChild().getChildren().size()).isEqualTo(0); + assertThat(sep.getChild().getChildren()).isEmpty(); sep = root.getChildren().get(1); assertThat(sep.getParent().getBitSet()).isEqualTo(OpenBitSet1); assertThat(sep.getChild().getBitSet()).isEqualTo(OpenBitSet4); assertThat(sep.getBitSet()).isEqualTo(intersect1And4); - assertThat(sep.getChild().getChildren().size()).isEqualTo(1); + assertThat(sep.getChild().getChildren()).hasSize(1); jtNode = sep.getChild(); assertThat(jtNode.getBitSet()).isEqualTo(OpenBitSet4); - assertThat(jtNode.getChildren().size()).isEqualTo(1); + assertThat(jtNode.getChildren()).hasSize(1); sep = jtNode.getChildren().get(0); assertThat(sep.getParent().getBitSet()).isEqualTo(OpenBitSet4); assertThat(sep.getChild().getBitSet()).isEqualTo(OpenBitSet3); assertThat(sep.getBitSet()).isEqualTo(intersect3And4); - assertThat(sep.getChild().getChildren().size()).isEqualTo(0); + assertThat(sep.getChild().getChildren()).isEmpty(); } @@ -1109,7 +1108,7 @@ public void testFullExample1() { JunctionTreeBuilder jtBuilder = new JunctionTreeBuilder( graph ); JunctionTreeClique root = jtBuilder.build(false).getRoot(); assertThat(root.getBitSet()).isEqualTo(clique1); - assertThat(root.getChildren().size()).isEqualTo(1); + assertThat(root.getChildren()).hasSize(1); // clique2 JunctionTreeSeparator sep = root.getChildren().get(0); @@ -1117,7 +1116,7 @@ public void testFullExample1() { JunctionTreeClique jtNode2 = sep.getChild(); assertThat(sep.getParent().getBitSet()).isEqualTo(clique1); assertThat(jtNode2.getBitSet()).isEqualTo(clique2); - assertThat(jtNode2.getChildren().size()).isEqualTo(2); + assertThat(jtNode2.getChildren()).hasSize(2); // clique3 sep = jtNode2.getChildren().get(0); @@ -1125,7 +1124,7 @@ public void testFullExample1() { JunctionTreeClique jtNode3 =sep.getChild(); assertThat(sep.getParent().getBitSet()).isEqualTo(clique2); assertThat(jtNode3.getBitSet()).isEqualTo(clique3); - assertThat(jtNode3.getChildren().size()).isEqualTo(1); + assertThat(jtNode3.getChildren()).hasSize(1); // clique4 sep = jtNode3.getChildren().get(0); @@ -1133,7 +1132,7 @@ public void testFullExample1() { JunctionTreeClique jtNode4 = sep.getChild(); assertThat(sep.getParent().getBitSet()).isEqualTo(clique3); assertThat(jtNode4.getBitSet()).isEqualTo(clique4); - assertThat(jtNode4.getChildren().size()).isEqualTo(0); + assertThat(jtNode4.getChildren()).isEmpty(); // clique5 sep = jtNode2.getChildren().get(1); @@ -1141,7 +1140,7 @@ public void testFullExample1() { JunctionTreeClique jtNode5 = sep.getChild(); assertThat(sep.getParent().getBitSet()).isEqualTo(clique2); assertThat(jtNode5.getBitSet()).isEqualTo(clique5); - assertThat(jtNode5.getChildren().size()).isEqualTo(1); + assertThat(jtNode5.getChildren()).hasSize(1); //clique 6 sep = jtNode5.getChildren().get(0); @@ -1149,7 +1148,7 @@ public void testFullExample1() { JunctionTreeClique jtNode6 = sep.getChild(); assertThat(sep.getParent().getBitSet()).isEqualTo(clique5); assertThat(jtNode6.getBitSet()).isEqualTo(clique6); - assertThat(jtNode6.getChildren().size()).isEqualTo(0); + assertThat(jtNode6.getChildren()).isEmpty(); } @Test @@ -1214,7 +1213,7 @@ public void testFullExample2() { // clique1 assertThat(root.getBitSet()).isEqualTo(clique1); - assertThat(root.getChildren().size()).isEqualTo(1); + assertThat(root.getChildren()).hasSize(1); // clique2 JunctionTreeSeparator sep = root.getChildren().get(0); @@ -1222,7 +1221,7 @@ public void testFullExample2() { JunctionTreeClique jtNode2 = sep.getChild(); assertThat(sep.getParent().getBitSet()).isEqualTo(clique1); assertThat(jtNode2.getBitSet()).isEqualTo(clique2); - assertThat(jtNode2.getChildren().size()).isEqualTo(2); + assertThat(jtNode2.getChildren()).hasSize(2); // clique3 assertThat(jtNode2.getParentSeparator()).isSameAs(sep); @@ -1231,7 +1230,7 @@ public void testFullExample2() { JunctionTreeClique jtNode3 = sep.getChild(); assertThat(sep.getParent().getBitSet()).isEqualTo(clique2); assertThat(jtNode3.getBitSet()).isEqualTo(clique3); - assertThat(jtNode3.getChildren().size()).isEqualTo(0); + assertThat(jtNode3.getChildren()).isEmpty(); // clique4 sep = jtNode2.getChildren().get(1); @@ -1239,7 +1238,7 @@ public void testFullExample2() { JunctionTreeClique jtNode4 = sep.getChild(); assertThat(sep.getParent().getBitSet()).isEqualTo(clique2); assertThat(jtNode4.getBitSet()).isEqualTo(clique4); - assertThat(jtNode4.getChildren().size()).isEqualTo(0); + assertThat(jtNode4.getChildren()).isEmpty(); } } diff --git a/drools-beliefs/src/test/java/org/drools/beliefs/bayes/JunctionTreeTest.java b/drools-beliefs/src/test/java/org/drools/beliefs/bayes/JunctionTreeTest.java index 9787252a3e1..cb8d228fc6a 100644 --- a/drools-beliefs/src/test/java/org/drools/beliefs/bayes/JunctionTreeTest.java +++ b/drools-beliefs/src/test/java/org/drools/beliefs/bayes/JunctionTreeTest.java @@ -18,12 +18,10 @@ */ package org.drools.beliefs.bayes; -import junit.framework.AssertionFailedError; - import org.drools.beliefs.graph.Graph; import org.drools.beliefs.graph.GraphNode; import org.drools.util.bitmask.OpenBitSet; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.math.BigDecimal; import java.util.Arrays; @@ -109,11 +107,11 @@ public void testPotentialMultiplication1() { PotentialMultiplier m = new PotentialMultiplier(b.getProbabilityTable(), 1, parentVarPos, parentIndexMultipliers, vars, multipliers, potentials); m.multiple(); - assertArray(new double[]{0.1, 0.2, 0.3, 0.4}, potentials); + assertThat(potentials).containsExactly(0.1, 0.2, 0.3, 0.4); // test that it's applying variable multiplications correctly ontop of each other. This simulates the application of project variabe multiplications m.multiple(); - assertArray(new double[]{0.01, 0.04, 0.09, 0.16}, scaleDouble( 3, potentials )); + assertThat(scaleDouble(3, potentials)).containsExactly(0.01, 0.04, 0.09, 0.16); } @Test @@ -145,11 +143,11 @@ public void testPotentialMultiplication2() { PotentialMultiplier m = new PotentialMultiplier(c.getProbabilityTable(), 2, parentVarPos, parentIndexMultipliers, vars, multipliers, potentials); m.multiple(); - assertArray(new double[]{0.1, 0.1, 0.2, 0.2, 0.3, 0.3, 0.4, 0.4, 0.5, 0.5, 0.6, 0.6, 0.7, 0.7, 0.8, 0.8}, scaleDouble( 3, potentials )); + assertThat(scaleDouble( 3, potentials )).containsExactly(0.1, 0.1, 0.2, 0.2, 0.3, 0.3, 0.4, 0.4, 0.5, 0.5, 0.6, 0.6, 0.7, 0.7, 0.8, 0.8); // test that it's applying variable multiplications correctly ontop of each other. This simulates the application of project variabe multiplications m.multiple(); - assertArray(new double[]{0.01, 0.01, 0.04, 0.04, 0.09, 0.09, 0.16, 0.16, 0.25, 0.25, 0.36, 0.36, 0.49, 0.49, 0.64, 0.64}, scaleDouble( 3, potentials ) ); + assertThat(scaleDouble( 3, potentials )).containsExactly(0.01, 0.01, 0.04, 0.04, 0.09, 0.09, 0.16, 0.16, 0.25, 0.25, 0.36, 0.36, 0.49, 0.49, 0.64, 0.64); } @Test @@ -181,11 +179,11 @@ public void testPotentialMultiplication3() { PotentialMultiplier m = new PotentialMultiplier(c.getProbabilityTable(), 2, parentVarPos, parentIndexMultipliers, vars, multipliers, potentials); m.multiple(); - assertArray(new double[]{0.1, 0.3, 0.2, 0.4, 0.5, 0.7, 0.6, 0.8, 0.1, 0.3, 0.2, 0.4, 0.5, 0.7, 0.6, 0.8}, potentials); + assertThat(potentials).containsExactly(0.1, 0.3, 0.2, 0.4, 0.5, 0.7, 0.6, 0.8, 0.1, 0.3, 0.2, 0.4, 0.5, 0.7, 0.6, 0.8); // test that it's applying variable multiplications correctly ontop of each other. This simulates the application of project variabe multiplications m.multiple(); - assertArray(new double[]{0.01, 0.09, 0.04, 0.16, 0.25, 0.49, 0.36, 0.64, 0.01, 0.09, 0.04, 0.16, 0.25, 0.49, 0.36, 0.64}, scaleDouble( 3, potentials ) ); + assertThat(scaleDouble( 3, potentials )).containsExactly(0.01, 0.09, 0.04, 0.16, 0.25, 0.49, 0.36, 0.64, 0.01, 0.09, 0.04, 0.16, 0.25, 0.49, 0.36, 0.64); } @Test @@ -228,27 +226,11 @@ public void testJunctionTreeInitialisation() { JunctionTree jtree = new JunctionTree(graph, node1, new JunctionTreeClique[] { node1, node2 }, null ); - assertArray(new double[]{0.1, 0.2, 0.1, 0.2}, scaleDouble( 3, node1.getPotentials() )); - assertArray(new double[]{0.01, 0.02, 0.06, 0.08}, scaleDouble( 3, node2.getPotentials() )); + assertThat(scaleDouble(3, node1.getPotentials())).containsExactly(0.1, 0.2, 0.1, 0.2); + + assertThat(scaleDouble(3, node2.getPotentials())).containsExactly(0.01, 0.02, 0.06, 0.08); } - public static void assertArray(double[] expected, double[] actual) { - if ( !Arrays.equals(expected, actual) ) { - System.err.print( "expected " ); - for ( int i = 0; i ("Cloudy", cloudyNode.getId(), new String[]{"true", "false"}, new double[][]{{0.5, 0.5}}); BayesVariable sprinkler = new BayesVariable("Sprinkler", sprinklerNode.getId(), new String[]{"true", "false"}, new double[][]{{0.5, 0.5}, {0.9, 0.1}}); - BayesVariable rain = new BayesVariable( "Rain", rainNode.getId(), new String[] { "true", "false" }, new double[][] { { 0.8, 0.2 }, { 0.2, 0.8 } } ); - BayesVariable wetGrass = new BayesVariable( "WetGrass", wetGrassNode.getId(), new String[] { "true", "false" }, new double[][] { { 1.0, 0.0 }, { 0.1, 0.9 }, { 0.1, 0.9 }, { 0.01, 0.99 } } ); + BayesVariable rain = new BayesVariable("Rain", rainNode.getId(), new String[] { "true", "false" }, new double[][] { { 0.8, 0.2 }, { 0.2, 0.8 } }); + BayesVariable wetGrass = new BayesVariable("WetGrass", wetGrassNode.getId(), new String[] { "true", "false" }, new double[][] { { 1.0, 0.0 }, { 0.1, 0.9 }, { 0.1, 0.9 }, { 0.01, 0.99 } }); JunctionTree jTree; - @Before + @BeforeEach public void setUp() { - connectParentToChildren( cloudyNode, sprinklerNode, rainNode); - connectParentToChildren( sprinklerNode, wetGrassNode); - connectParentToChildren( rainNode, wetGrassNode); + connectParentToChildren(cloudyNode, sprinklerNode, rainNode); + connectParentToChildren(sprinklerNode, wetGrassNode); + connectParentToChildren(rainNode, wetGrassNode); cloudyNode.setContent(cloudy); - sprinklerNode.setContent( sprinkler); - rainNode.setContent( rain ); - wetGrassNode.setContent( wetGrass ); + sprinklerNode.setContent(sprinkler); + rainNode.setContent(rain); + wetGrassNode.setContent(wetGrass); - JunctionTreeBuilder jtBuilder = new JunctionTreeBuilder( graph ); + JunctionTreeBuilder jtBuilder = new JunctionTreeBuilder(graph); jTree = jtBuilder.build(); } @@ -71,79 +71,79 @@ public void testInitialize() { JunctionTreeClique jtNode = jTree.getRoot(); // cloud, rain sprinkler - assertArray(new double[]{0.2, 0.05, 0.2, 0.05, 0.09, 0.36, 0.01, 0.04 }, scaleDouble( 3, jtNode.getPotentials() )); + assertThat(scaleDouble(3, jtNode.getPotentials())).containsExactly(0.2, 0.05, 0.2, 0.05, 0.09, 0.36, 0.01, 0.04); // wetGrass jtNode = jTree.getRoot().getChildren().get(0).getChild(); - assertArray(new double[]{1.0, 0.0, 0.1, 0.9, 0.1, 0.9, 0.01, 0.99 }, scaleDouble( 3, jtNode.getPotentials() )); + assertThat(scaleDouble(3, jtNode.getPotentials())).containsExactly(1.0, 0.0, 0.1, 0.9, 0.1, 0.9, 0.01, 0.99); } @Test public void testNoEvidence() { - JunctionTreeBuilder jtBuilder = new JunctionTreeBuilder( graph ); + JunctionTreeBuilder jtBuilder = new JunctionTreeBuilder(graph); JunctionTree jTree = jtBuilder.build(); JunctionTreeClique jtNode = jTree.getRoot(); BayesInstance bayesInstance = new BayesInstance(jTree); bayesInstance.globalUpdate(); - assertArray(new double[]{0.5, 0.5}, scaleDouble(3, bayesInstance.marginalize("Cloudy").getDistribution())); + assertThat(scaleDouble(3, bayesInstance.marginalize("Cloudy").getDistribution())).containsExactly(0.5, 0.5); - assertArray( new double[]{0.5, 0.5}, scaleDouble( 3, bayesInstance.marginalize("Rain").getDistribution() ) ); + assertThat(scaleDouble(3, bayesInstance.marginalize("Rain").getDistribution())).containsExactly(0.5, 0.5); - assertArray( new double[]{0.7, 0.3}, scaleDouble(3, bayesInstance.marginalize("Sprinkler").getDistribution()) ); + assertThat(scaleDouble(3, bayesInstance.marginalize("Sprinkler").getDistribution())).containsExactly(0.7, 0.3); - assertArray( new double[]{0.353, 0.647}, scaleDouble( 3, bayesInstance.marginalize("WetGrass").getDistribution() ) ); + assertThat(scaleDouble(3, bayesInstance.marginalize("WetGrass").getDistribution())).containsExactly(0.353, 0.647); } @Test public void testGrassWetEvidence() { - JunctionTreeBuilder jtBuilder = new JunctionTreeBuilder( graph ); + JunctionTreeBuilder jtBuilder = new JunctionTreeBuilder(graph); JunctionTree jTree = jtBuilder.build(); JunctionTreeClique jtNode = jTree.getRoot(); BayesInstance bayesInstance = new BayesInstance(jTree); - bayesInstance.setLikelyhood( "WetGrass", new double[]{1.0, 0.0} ); + bayesInstance.setLikelyhood("WetGrass", new double[]{1.0, 0.0}); bayesInstance.globalUpdate(); - assertArray(new double[]{0.639, 0.361}, scaleDouble(3, bayesInstance.marginalize("Cloudy").getDistribution())); + assertThat(scaleDouble(3, bayesInstance.marginalize("Cloudy").getDistribution())).containsExactly(0.639, 0.361); - assertArray( new double[]{0.881, 0.119}, scaleDouble( 3, bayesInstance.marginalize("Rain").getDistribution() ) ); + assertThat(scaleDouble(3, bayesInstance.marginalize("Rain").getDistribution())).containsExactly(0.881, 0.119); - assertArray( new double[]{0.938, 0.062}, scaleDouble(3, bayesInstance.marginalize("Sprinkler").getDistribution()) ); + assertThat(scaleDouble(3, bayesInstance.marginalize("Sprinkler").getDistribution())).containsExactly(0.938, 0.062); - assertArray( new double[]{1.0, 0.0}, scaleDouble( 3, bayesInstance.marginalize("WetGrass").getDistribution() ) ); + assertThat(scaleDouble(3, bayesInstance.marginalize("WetGrass").getDistribution())).containsExactly(1.0, 0.0); } @Test public void testSprinklerEvidence() { - JunctionTreeBuilder jtBuilder = new JunctionTreeBuilder( graph ); + JunctionTreeBuilder jtBuilder = new JunctionTreeBuilder(graph); JunctionTree jTree = jtBuilder.build(); JunctionTreeClique jtNode = jTree.getRoot(); BayesInstance bayesInstance = new BayesInstance(jTree); - bayesInstance.setLikelyhood( "Sprinkler", new double[]{1.0, 0.0} ); - bayesInstance.setLikelyhood( "Cloudy", new double[]{1.0, 0.0} ); + bayesInstance.setLikelyhood("Sprinkler", new double[]{1.0, 0.0}); + bayesInstance.setLikelyhood("Cloudy", new double[]{1.0, 0.0}); bayesInstance.globalUpdate(); - assertArray(new double[]{1.0, 0.0}, scaleDouble(3, bayesInstance.marginalize("Cloudy").getDistribution())); + assertThat(scaleDouble(3, bayesInstance.marginalize("Cloudy").getDistribution())).containsExactly(1.0, 0.0); - assertArray( new double[]{0.8, 0.2}, scaleDouble( 3, bayesInstance.marginalize("Rain").getDistribution() ) ); + assertThat(scaleDouble(3, bayesInstance.marginalize("Rain").getDistribution())).containsExactly(0.8, 0.2); - assertArray( new double[]{1.0, 0.0}, scaleDouble(3, bayesInstance.marginalize("Sprinkler").getDistribution()) ); + assertThat(scaleDouble(3, bayesInstance.marginalize("Sprinkler").getDistribution())).containsExactly(1.0, 0.0); - assertArray( new double[]{0.82, 0.18}, scaleDouble( 3, bayesInstance.marginalize("WetGrass").getDistribution() ) ); + assertThat(scaleDouble(3, bayesInstance.marginalize("WetGrass").getDistribution())).containsExactly(0.82, 0.18); } public static void marginalize(BayesVariableState varState, CliqueState cliqueState) { JunctionTreeClique jtNode = cliqueState.getJunctionTreeClique(); - new Marginalizer(jtNode.getValues().toArray( new BayesVariable[jtNode.getValues().size()]), cliqueState.getPotentials(), varState.getVariable(), varState.getDistribution() ); - System.out.print( varState.getVariable().getName() + " " ); - for ( double d : varState.getDistribution() ) { + new Marginalizer(jtNode.getValues().toArray(new BayesVariable[jtNode.getValues().size()]), cliqueState.getPotentials(), varState.getVariable(), varState.getDistribution()); + System.out.print(varState.getVariable().getName() + " "); + for (double d : varState.getDistribution()) { System.out.print(d); System.out.print(" "); } @@ -152,12 +152,12 @@ public static void marginalize(BayesVariableState varState, CliqueState cliqueS public static GraphNode addNode(Graph graph) { GraphNode x = graph.addNode(); - x.setContent( new BayesVariable( "x" + x.getId(), x.getId(), new String[] { "a", "b" }, new double[][] { { 0.1, 0.1 } } ) ); + x.setContent(new BayesVariable("x" + x.getId(), x.getId(), new String[] { "a", "b" }, new double[][] { { 0.1, 0.1 } })); return x; } public static void connectParentToChildren(GraphNode parent, GraphNode... children) { - for ( GraphNode child : children ) { + for (GraphNode child : children) { EdgeImpl e = new EdgeImpl(); e.setOutGraphNode(parent); e.setInGraphNode(child); diff --git a/drools-beliefs/src/test/java/org/drools/beliefs/bayes/integration/AssemblerTest.java b/drools-beliefs/src/test/java/org/drools/beliefs/bayes/integration/AssemblerTest.java index 639433d3e8c..bee6c7eb0ce 100644 --- a/drools-beliefs/src/test/java/org/drools/beliefs/bayes/integration/AssemblerTest.java +++ b/drools-beliefs/src/test/java/org/drools/beliefs/bayes/integration/AssemblerTest.java @@ -20,8 +20,8 @@ import org.drools.beliefs.bayes.assembler.BayesPackage; import org.drools.compiler.builder.impl.KnowledgeBuilderImpl; +import org.junit.jupiter.api.Test; import org.drools.base.definitions.InternalKnowledgePackage; -import org.junit.Test; import org.kie.api.io.ResourceType; import org.kie.internal.io.ResourceFactory; diff --git a/drools-beliefs/src/test/java/org/drools/beliefs/bayes/integration/BayesBeliefSystemTest.java b/drools-beliefs/src/test/java/org/drools/beliefs/bayes/integration/BayesBeliefSystemTest.java index 1736cef5411..2789052745c 100644 --- a/drools-beliefs/src/test/java/org/drools/beliefs/bayes/integration/BayesBeliefSystemTest.java +++ b/drools-beliefs/src/test/java/org/drools/beliefs/bayes/integration/BayesBeliefSystemTest.java @@ -33,7 +33,7 @@ import org.drools.kiesession.rulebase.InternalKnowledgeBase; import org.drools.kiesession.rulebase.KnowledgeBaseFactory; import org.drools.kiesession.session.StatefulKnowledgeSessionImpl; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.kie.api.io.ResourceType; import org.kie.api.runtime.KieSession; import org.kie.api.runtime.KieSessionConfiguration; diff --git a/drools-beliefs/src/test/java/org/drools/beliefs/bayes/integration/BayesRuntimeTest.java b/drools-beliefs/src/test/java/org/drools/beliefs/bayes/integration/BayesRuntimeTest.java index 18ade7b8550..72fdc12ecaa 100644 --- a/drools-beliefs/src/test/java/org/drools/beliefs/bayes/integration/BayesRuntimeTest.java +++ b/drools-beliefs/src/test/java/org/drools/beliefs/bayes/integration/BayesRuntimeTest.java @@ -24,7 +24,7 @@ import org.drools.kiesession.rulebase.InternalKnowledgeBase; import org.drools.kiesession.rulebase.SessionsAwareKnowledgeBase; import org.drools.kiesession.session.StatefulKnowledgeSessionImpl; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.kie.api.io.ResourceType; import org.kie.internal.io.ResourceFactory; diff --git a/drools-beliefs/src/test/java/org/drools/beliefs/bayes/integration/ParserTest.java b/drools-beliefs/src/test/java/org/drools/beliefs/bayes/integration/ParserTest.java index e8207f4dd7f..03084d43a23 100644 --- a/drools-beliefs/src/test/java/org/drools/beliefs/bayes/integration/ParserTest.java +++ b/drools-beliefs/src/test/java/org/drools/beliefs/bayes/integration/ParserTest.java @@ -26,9 +26,8 @@ import org.drools.beliefs.bayes.model.Variable; import org.drools.beliefs.bayes.model.XmlBifParser; import org.drools.beliefs.graph.GraphNode; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -46,39 +45,34 @@ public void testSprinklerLoadBif() { assertThat(network.getProperties().get(0)).isEqualTo("package = org.drools.beliefs.bayes.integration"); Map varMap = varToMap( network.getVariables() ); - assertThat(varMap.size()).isEqualTo(4); + assertThat(varMap).hasSize(4); Variable var = varMap.get("WetGrass"); assertThat(var.getName()).isEqualTo("WetGrass"); - assertThat(var.getOutComes().size()).isEqualTo(2); - assertThat(Arrays.asList("false", "true")).isEqualTo(var.getOutComes()); + assertThat(var.getOutComes()).hasSize(2).containsExactly("false", "true"); assertThat(var.getProperties().get(0)).isEqualTo("position = (0,10)"); var = varMap.get("Cloudy"); assertThat(var.getName()).isEqualTo("Cloudy"); - assertThat(var.getOutComes().size()).isEqualTo(2); - assertThat(Arrays.asList("false", "true")).isEqualTo(var.getOutComes()); + assertThat(var.getOutComes()).hasSize(2).containsExactly("false", "true"); assertThat(var.getProperties().get(0)).isEqualTo("position = (0,-10)"); var = varMap.get("Sprinkler"); assertThat(var.getName()).isEqualTo("Sprinkler"); - assertThat(var.getOutComes().size()).isEqualTo(2); - assertThat(Arrays.asList("false", "true")).isEqualTo(var.getOutComes()); + assertThat(var.getOutComes()).hasSize(2).containsExactly("false", "true"); assertThat(var.getProperties().get(0)).isEqualTo("position = (13,0)"); var = varMap.get("Rain"); assertThat(var.getName()).isEqualTo("Rain"); - assertThat(var.getOutComes().size()).isEqualTo(2); - assertThat(Arrays.asList("false", "true")).isEqualTo(var.getOutComes()); + assertThat(var.getOutComes()).hasSize(2).containsExactly("false", "true"); assertThat(var.getProperties().get(0)).isEqualTo("position = (-12,0)"); Map defMap = defToMap( network.getDefinitions() ); - assertThat(defMap.size()).isEqualTo(4); + assertThat(defMap).hasSize(4); Definition def = defMap.get( "WetGrass"); assertThat(def.getName()).isEqualTo("WetGrass"); - assertThat(def.getGiven().size()).isEqualTo(2); - assertThat(Arrays.asList("Sprinkler", "Rain")).isEqualTo(def.getGiven()); + assertThat(def.getGiven()).hasSize(2).containsExactly("Sprinkler", "Rain"); assertThat(def.getProbabilities()).isEqualTo("1.0 0.0 0.1 0.9 0.1 0.9 0.01 0.99"); def = defMap.get( "Cloudy"); @@ -88,8 +82,7 @@ public void testSprinklerLoadBif() { def = defMap.get( "Sprinkler"); assertThat(def.getName()).isEqualTo("Sprinkler"); - assertThat(def.getGiven().size()).isEqualTo(1); - assertThat(def.getGiven().get(0)).isEqualTo("Cloudy"); + assertThat(def.getGiven()).hasSize(1).containsExactly("Cloudy"); assertThat(def.getProbabilities().trim()).isEqualTo("0.5 0.5 0.9 0.1"); def = defMap.get( "Rain"); @@ -107,29 +100,28 @@ public void testSprinklerBuildBayesNework() { GraphNode node = map.get( "WetGrass" ); BayesVariable wetGrass = node.getContent(); - assertThat(Arrays.asList(wetGrass.getOutcomes())).isEqualTo(Arrays.asList("false", "true")); - assertThat(wetGrass.getGiven().length).isEqualTo(2); - assertThat(Arrays.asList("Sprinkler", "Rain")).isEqualTo(Arrays.asList(wetGrass.getGiven())); - assertThat(Arrays.deepEquals(new double[][]{{1.0, 0.0}, {0.1, 0.9}, {0.1, 0.9}, {0.01, 0.99}}, wetGrass.getProbabilityTable())).isTrue(); + assertThat(wetGrass.getOutcomes()).containsExactly("false", "true"); + assertThat(wetGrass.getGiven()).hasSize(2).containsExactly("Sprinkler", "Rain"); + assertThat(wetGrass.getProbabilityTable()).isDeepEqualTo(new double[][]{{1.0, 0.0}, {0.1, 0.9}, {0.1, 0.9}, {0.01, 0.99}}); node = map.get( "Sprinkler" ); BayesVariable sprinkler = node.getContent(); - assertThat(Arrays.asList(sprinkler.getOutcomes())).isEqualTo(Arrays.asList("false", "true")); - assertThat(sprinkler.getGiven().length).isEqualTo(1); + assertThat(wetGrass.getOutcomes()).containsExactly("false", "true"); + assertThat(sprinkler.getGiven()).hasSize(1); assertThat(sprinkler.getGiven()[0]).isEqualTo("Cloudy"); - assertThat(Arrays.deepEquals(new double[][]{{0.5, 0.5}, {0.9, 0.1}}, sprinkler.getProbabilityTable())).isTrue(); + assertThat(sprinkler.getProbabilityTable()).isDeepEqualTo(new double[][]{{0.5, 0.5}, {0.9, 0.1}}); node = map.get( "Cloudy" ); BayesVariable cloudy = node.getContent(); - assertThat(Arrays.asList(cloudy.getOutcomes())).isEqualTo(Arrays.asList("false", "true")); - assertThat(cloudy.getGiven().length).isEqualTo(0); - assertThat(Arrays.deepEquals(new double[][]{{0.5, 0.5}}, cloudy.getProbabilityTable())).isTrue(); + assertThat(wetGrass.getOutcomes()).containsExactly("false", "true"); + assertThat(cloudy.getGiven()).isEmpty(); + assertThat(cloudy.getProbabilityTable()).isDeepEqualTo(new double[][]{{0.5, 0.5}}); node = map.get( "Rain" ); BayesVariable rain = node.getContent(); - assertThat(Arrays.asList(rain.getOutcomes())).isEqualTo(Arrays.asList("false", "true")); - assertThat(rain.getGiven().length).isEqualTo(0); - assertThat(Arrays.deepEquals(new double[][]{{0.5, 0.5}}, rain.getProbabilityTable())).isTrue(); + assertThat(rain.getOutcomes()).containsExactly("false", "true"); + assertThat(rain.getGiven()).isEmpty(); + assertThat(rain.getProbabilityTable()).isDeepEqualTo(new double[][]{{0.5, 0.5}}); } Map> nodeToMap(BayesNetwork network) { diff --git a/drools-beliefs/src/test/java/org/drools/beliefs/bayes/integration/WeaverTest.java b/drools-beliefs/src/test/java/org/drools/beliefs/bayes/integration/WeaverTest.java index 60daf82220b..a7951f34bc7 100644 --- a/drools-beliefs/src/test/java/org/drools/beliefs/bayes/integration/WeaverTest.java +++ b/drools-beliefs/src/test/java/org/drools/beliefs/bayes/integration/WeaverTest.java @@ -25,7 +25,7 @@ import org.drools.base.definitions.ResourceTypePackageRegistry; import org.drools.kiesession.rulebase.InternalKnowledgeBase; import org.drools.kiesession.rulebase.KnowledgeBaseFactory; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.kie.api.io.ResourceType; import org.kie.internal.io.ResourceFactory;