Skip to content

Commit

Permalink
Merge pull request #78 from rgdoliveira/sync_main
Browse files Browse the repository at this point in the history
Sync main branch with Apache main branch
  • Loading branch information
rgdoliveira authored Sep 9, 2024
2 parents 5bb3d4f + 6a994de commit 80ceac5
Show file tree
Hide file tree
Showing 33 changed files with 284 additions and 742 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private Map<String, Declaration> getAllExtendedDeclaration(RuleImpl rule, Map<St

public Declaration getDeclaration(String identifier) {
// it may be a local bound variable
for (final Iterator<RuleConditionElement> iterator = buildList.descendingIterator(); iterator.hasNext();) {
for (final Iterator<RuleConditionElement> iterator = buildList.iterator(); iterator.hasNext();) {
final Declaration declaration = iterator.next().resolveDeclaration( identifier );
if ( declaration != null ) {
return declaration;
Expand Down
6 changes: 3 additions & 3 deletions drools-beliefs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -63,7 +62,7 @@ public class GlobalUpdateTest {
final List<String> messageResults = new ArrayList<String>();
final List<String> globalUpdateResults = new ArrayList<String>();

@Before
@BeforeEach
public void startUp() {
int i = 0;
List<JunctionTreeSeparator> list = new ArrayList<JunctionTreeSeparator>();
Expand Down Expand Up @@ -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
Expand All @@ -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<BayesVariable> graph, JunctionTreeClique parent, List list, JunctionTreeClique... children) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -56,11 +54,11 @@ public static boolean assertLinkedVertex(boolean[][] adjMatrix, int... ints) {
int id = ints[0];

Collection<Integer> 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;
Expand All @@ -75,14 +73,6 @@ public static GraphNode<BayesVariable> addNode(Graph<BayesVariable> 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) });
Expand Down
Loading

0 comments on commit 80ceac5

Please sign in to comment.