JunctionTreeBuilder { public JunctionTree build() { return build(true); } JunctionTreeBuilder(Graph<BayesVariable> graph); Graph<BayesVariable> getGraph(); JunctionTree build(); JunctionTree build(boolean init); JunctionTree build(Resource resource, String namespace, String name); JunctionTree build(Resource resource, String namespace, String name, boolean init); void moralize(); void moralize(GraphNode<BayesVariable> v, GraphNode v1); static void connect(boolean[][] adjMatrix, int v1, int v2); static void disconnect(boolean[][] adjMatrix, int v1, int v2); List<OpenBitSet> triangulate(); void eliminateVertex(PriorityQueue<EliminationCandidate> p, Map<Integer, EliminationCandidate> elmVertMap, boolean[][] clonedAdjMatrix, boolean[] adjList, Set<Integer> verticesToUpdate, EliminationCandidate v); void createClique(int v, boolean[][] clonedAdjMatrix, Set<Integer> verticesToUpdate, boolean[] adjList); static void updateCliques(List<OpenBitSet> cliques, OpenBitSet newClique); boolean[][] getAdjacencyMatrix(); static boolean[][] cloneAdjacencyMarix(boolean[][] src); JunctionTree junctionTree(List<OpenBitSet> cliques, boolean init); JunctionTree junctionTree(Resource resource, String namespace, String name, List<OpenBitSet> cliques, boolean init); void mergeGraphs(SeparatorSet[][][] graphs, SeparatorSet separatorSet); int createJunctionTreeGraph(SeparatorSet[][] sepGraph, JunctionTreeClique parent, JunctionTreeClique[] jtNodes, JunctionTreeSeparator[] jtSeps, int i); void mapNodeToCliqueFamily(OpenBitSet[] varNodeToCliques, JunctionTreeClique[] jtNodes); void mapVarNodeToCliques(OpenBitSet[] nodeToCliques, int id, OpenBitSet clique); static List<Integer> getAdjacentVertices(boolean[][] adjacencyMatrix, int i);  }
@Test public void testFullExample1() { Graph<BayesVariable> graph = new BayesNetwork(); GraphNode xa = addNode(graph); GraphNode xb = addNode(graph); GraphNode xc = addNode(graph); GraphNode xd = addNode(graph); GraphNode xe = addNode(graph); GraphNode xf = addNode(graph); GraphNode xg = addNode(graph); GraphNode xh = addNode(graph); connectParentToChildren(xa, xb, xc); connectParentToChildren(xb, xd); connectParentToChildren(xc, xe, xg); connectParentToChildren(xd, xf); connectParentToChildren(xe, xf, xh); connectParentToChildren(xg, xh); OpenBitSet clique1 = bitSet("00111000"); OpenBitSet clique2 = bitSet("00011100"); OpenBitSet clique3 = bitSet("01010100"); OpenBitSet clique4 = bitSet("11010000"); OpenBitSet clique5 = bitSet("00001110"); OpenBitSet clique6 = bitSet("00000111"); OpenBitSet clique1And2 = bitSet("00011000"); OpenBitSet clique2And3 = bitSet("00010100"); OpenBitSet clique2And5 = bitSet("00001100"); OpenBitSet clique3And4 = bitSet("01010000"); OpenBitSet clique5And6 = bitSet("00000110"); JunctionTreeBuilder jtBuilder = new JunctionTreeBuilder( graph ); JunctionTreeClique root = jtBuilder.build(false).getRoot(); assertEquals( clique1, root.getBitSet() ); assertEquals(1, root.getChildren().size()); JunctionTreeSeparator sep = root.getChildren().get(0); assertEquals( clique1And2, sep.getBitSet() ); JunctionTreeClique jtNode2 = sep.getChild(); assertEquals( clique1, sep.getParent().getBitSet() ); assertEquals( clique2, jtNode2.getBitSet() ); assertEquals(2, jtNode2.getChildren().size()); sep = jtNode2.getChildren().get(0); assertEquals( clique2And3, sep.getBitSet() ); JunctionTreeClique jtNode3 =sep.getChild(); assertEquals( clique2, sep.getParent().getBitSet() ); assertEquals( clique3, jtNode3.getBitSet() ); assertEquals( 1, jtNode3.getChildren().size()); sep = jtNode3.getChildren().get(0); assertEquals( clique3And4, sep.getBitSet() ); JunctionTreeClique jtNode4 = sep.getChild(); assertEquals( clique3, sep.getParent().getBitSet() ); assertEquals( clique4, jtNode4.getBitSet() ); assertEquals( 0, jtNode4.getChildren().size()); sep = jtNode2.getChildren().get(1); assertEquals( clique2And5, sep.getBitSet() ); JunctionTreeClique jtNode5 = sep.getChild(); assertEquals( clique2, sep.getParent().getBitSet() ); assertEquals( clique5, jtNode5.getBitSet() ); assertEquals( 1, jtNode5.getChildren().size()); sep = jtNode5.getChildren().get(0); assertEquals( clique5And6, sep.getBitSet() ); JunctionTreeClique jtNode6 = sep.getChild(); assertEquals( clique5, sep.getParent().getBitSet() ); assertEquals( clique6, jtNode6.getBitSet() ); assertEquals( 0, jtNode6.getChildren().size()); }
@Test public void testFullExample2() { Graph<BayesVariable> graph = new BayesNetwork(); GraphNode xElectricity = addNode(graph); GraphNode xTelecom = addNode(graph); GraphNode xRail = addNode(graph); GraphNode xAirTravel = addNode(graph); GraphNode xTransportation = addNode(graph); GraphNode xUtilities = addNode(graph); GraphNode xUSBanks = addNode(graph); GraphNode xUSStocks = addNode(graph); connectParentToChildren( xElectricity, xRail, xAirTravel, xUtilities, xTelecom ); connectParentToChildren( xTelecom, xUtilities, xUSBanks ); connectParentToChildren( xRail, xTransportation ); connectParentToChildren( xAirTravel, xTransportation ); connectParentToChildren( xUtilities, xUSStocks ); connectParentToChildren( xUSBanks, xUSStocks ); connectParentToChildren( xTransportation, xUSStocks ); OpenBitSet clique1 = bitSet("11110000"); OpenBitSet clique2 = bitSet("01110001"); OpenBitSet clique3 = bitSet("01100011"); OpenBitSet clique4 = bitSet("00011101"); OpenBitSet clique1And2 = bitSet("01110000"); OpenBitSet clique2And3 = bitSet("01100001"); OpenBitSet clique2And4 = bitSet("00010001"); xElectricity.setContent(new BayesVariable<String>("Electricity", xElectricity.getId(), new String[]{"Working", "Reduced", "NotWorking"}, new double[][]{{0.6, 0.3, 0.099}})); xTelecom.setContent(new BayesVariable<String>("Telecom", xTelecom.getId(), new String[]{"Working", "Reduced", "NotWorking"}, new double[][]{{0.544, 0.304, 0.151}})); xRail.setContent(new BayesVariable<String>("Rail", xRail.getId(), new String[]{"Working", "Reduced", "NotWorking"}, new double[][]{{0.579, 0.230, 0.190}})); xAirTravel.setContent(new BayesVariable<String>("AirTravel", xAirTravel.getId(), new String[]{"Working", "Reduced", "NotWorking"}, new double[][]{{0.449, 0.330, 0.219}})); xTransportation.setContent(new BayesVariable<String>("Transportation", xTransportation.getId(), new String[]{"Working", "Moderate", "Severe", "Failure"}, new double[][]{{0.658, 0.167, 0.097, 0.077}})); xUtilities.setContent(new BayesVariable<String>("Utilities", xUtilities.getId(), new String[]{"Working", "Moderate", "Severe", "Failure"}, new double[][]{{0.541, 0.272, 0.097, 0.088}})); xUSBanks.setContent(new BayesVariable<String>("USBanks", xUSBanks.getId(), new String[]{"Working", "Reduced", "NotWorking"}, new double[][]{{0.488, 0.370, 0.141}})); xUSStocks.setContent(new BayesVariable<String>("USStocks", xUSStocks.getId(), new String[]{"Up", "Down", "Crash"}, new double[][]{{0.433, 0.386, 0.179}})); JunctionTreeBuilder jtBuilder = new JunctionTreeBuilder( graph ); JunctionTreeClique root = jtBuilder.build(false).getRoot(); assertEquals( clique1, root.getBitSet() ); assertEquals( 1, root.getChildren().size() ); JunctionTreeSeparator sep = root.getChildren().get(0); assertEquals( clique1And2, sep.getBitSet() ); JunctionTreeClique jtNode2 = sep.getChild(); assertEquals( clique1, sep.getParent().getBitSet() ); assertEquals( clique2, jtNode2.getBitSet() ); assertEquals(2, jtNode2.getChildren().size()); assertSame( sep, jtNode2.getParentSeparator() ); sep = jtNode2.getChildren().get(0); assertEquals( clique2And3, sep.getBitSet() ); JunctionTreeClique jtNode3 = sep.getChild(); assertEquals( clique2, sep.getParent().getBitSet() ); assertEquals( clique3, jtNode3.getBitSet() ); assertEquals(0, jtNode3.getChildren().size()); sep = jtNode2.getChildren().get(1); assertEquals( clique2And4, sep.getBitSet() ); JunctionTreeClique jtNode4 = sep.getChild(); assertEquals( clique2, sep.getParent().getBitSet() ); assertEquals( clique4, jtNode4.getBitSet() ); assertEquals(0, jtNode4.getChildren().size()); }