package Sequenic.Graph; import java.util.*; import org.junit.* ; @SuppressWarnings("unchecked") public class GraphTest1 { @Test public void test1() { System.out.println("\n*** No loop, one node has 2 parents.") ; Vertex root = new Vertex() ; Vertex v1 = new Vertex() ; Vertex v2 = new Vertex() ; root.setId(0); v1.setId(1); v2.setId(2); root.addChild(v1,Vertex.LABEL_TRUE) ; root.addChild(v2,Vertex.LABEL_FALSE); v1.addChild(v2, Vertex.LABEL_DEFAULT) ; Graph g1 = new Graph() ; g1.nodes.add(root) ; g1.nodes.add(v1) ; g1.nodes.add(v2) ; System.out.println("Graph:" + g1) ; LinkedList cycles = g1.getCycles() ; // Cycles: System.out.println("Cycles:") ; for (LinkedList C : cycles) System.out.println(" " + PathUtil.pathSimpleShow(C)) ; assert cycles.size() == 0 ; // Maximal paths: LinkedList paths = g1.getMaxPaths(root) ; System.out.println("Max. paths from " + root.getId() + ":") ; for (LinkedList P : paths) System.out.println(" " + PathUtil.pathSimpleShow(P)) ; assert paths.size() == 2 ; LinkedList P1 = paths.get(0) ; assert PathUtil.pathSimpleShow(P1).equals("[ 0, 1, 2 ]") ; LinkedList P2 = paths.get(1) ; assert PathUtil.pathSimpleShow(P2).equals("[ 0, 2 ]") ; // Prime paths: System.out.println("Prime paths:") ; paths = PrimePathUtil.getPrimePaths(g1,root) ; for (LinkedList P : paths) System.out.println(" " + PathUtil.pathSimpleShow(P)) ; assert paths.size() == 2 ; } @Test public void test2() { System.out.println("\n** Two roots, one (cross-) cycle.") ; Vertex v1 = new Vertex() ; Vertex v2 = new Vertex() ; Vertex v3 = new Vertex() ; Vertex v4 = new Vertex() ; Vertex v5 = new Vertex() ; v1.setId(1); v2.setId(2); v3.setId(3); v4.setId(4); v5.setId(5); v1.addChild(v3,Vertex.LABEL_DEFAULT) ; v2.addChild(v4,Vertex.LABEL_DEFAULT) ; v3.addChild(v4,Vertex.LABEL_TRUE) ; v3.addChild(v5,Vertex.LABEL_FALSE) ; v4.addChild(v3,Vertex.LABEL_DEFAULT) ; Graph g1 = new Graph() ; g1.nodes.add(v1) ; g1.nodes.add(v2) ; g1.nodes.add(v3) ; g1.nodes.add(v4) ; g1.nodes.add(v5) ; System.out.println("Graph:" + g1) ; LinkedList cycles = g1.getCycles() ; // Cycles: System.out.println("Cycles:") ; for (LinkedList C : cycles) System.out.println(" " + PathUtil.pathSimpleShow(C)) ; assert cycles.size() == 1 ; assert cycles.get(0).size() == 3 ; assert v3 == cycles.get(0).get(0) ; assert v4 == cycles.get(0).get(1) ; assert v3 == cycles.get(0).get(2) ; // Max paths: LinkedList paths = g1.getMaxPaths(v1) ; System.out.println("Max. paths from " + v1.getId() + ":") ; for (LinkedList P : paths) System.out.println(" " + PathUtil.pathSimpleShow(P)) ; assert paths.size() == 1 ; assert PathUtil.pathSimpleShow(paths.get(0)).equals("[ 1, 3, 5 ]") ; paths = g1.getMaxPaths(v2) ; System.out.println("Max. paths from " + v2.getId() + ":") ; for (LinkedList P : paths) System.out.println(" " + PathUtil.pathSimpleShow(P)) ; assert paths.size() == 1 ; assert PathUtil.pathSimpleShow(paths.get(0)).equals("[ 2, 4, 3, 5 ]") ; // Prime paths: System.out.println("Prime paths:") ; paths = PrimePathUtil.getPrimePaths(g1) ; for (LinkedList P : paths) System.out.println(" " + PathUtil.pathSimpleShow(P)) ; assert paths.size() == 3 ; } @Test public void test3() { System.out.println("\n*** One root, 2 cycles, nested.") ; Vertex v1 = new Vertex() ; Vertex v2 = new Vertex() ; Vertex v3 = new Vertex() ; Vertex v4 = new Vertex() ; v1.setId(1); v2.setId(2); v3.setId(3); v4.setId(4); v1.addChild(v2,Vertex.LABEL_DEFAULT) ; v2.addChild(v3,Vertex.LABEL_DEFAULT) ; v3.addChild(v2,Vertex.LABEL_DEFAULT) ; v3.addChild(v4,Vertex.LABEL_TRUE) ; v3.addChild(v1,Vertex.LABEL_FALSE) ; Graph g1 = new Graph() ; g1.nodes.add(v1) ; g1.nodes.add(v2) ; g1.nodes.add(v3) ; g1.nodes.add(v4) ; System.out.println("Graph:" + g1) ; LinkedList cycles = g1.getCycles() ; System.out.println("Cycles: " + cycles.size()) ; for (LinkedList C : cycles) System.out.println(" " + PathUtil.pathSimpleShow(C)) ; assert cycles.size() == 2 ; assert cycles.get(0).size() == 3 ; assert v2 == cycles.get(0).get(0) ; assert v3 == cycles.get(0).get(1) ; assert v2 == cycles.get(0).get(2) ; assert cycles.get(1).size() == 4 ; assert v1 == cycles.get(1).get(0) ; assert v2 == cycles.get(1).get(1) ; assert v3 == cycles.get(1).get(2) ; assert v1 == cycles.get(1).get(3) ; LinkedList paths = g1.getMaxPaths(v1) ; System.out.println("Max. paths from " + v1.getId() + ":") ; for (LinkedList P : paths) System.out.println(" " + PathUtil.pathSimpleShow(P)) ; assert paths.size() == 1 ; assert PathUtil.pathSimpleShow(paths.get(0)).equals("[ 1, 2, 3, 4 ]") ; // Prime paths: System.out.println("Prime paths:") ; paths = PrimePathUtil.getPrimePaths(g1,v1) ; for (LinkedList P : paths) System.out.println(" " + PathUtil.pathSimpleShow(P)) ; assert paths.size() == 3 ; } @Test public void test4() { System.out.println("\n*** One root, cycle with 2x branches within, --> 2 cycles.") ; Vertex v1 = new Vertex() ; Vertex v2 = new Vertex() ; Vertex v3 = new Vertex() ; Vertex v4 = new Vertex() ; v1.setId(1); v2.setId(2); v3.setId(3); v4.setId(4); v1.addChild(v2,Vertex.LABEL_DEFAULT) ; v2.addChild(v3,Vertex.LABEL_FALSE) ; v2.addChild(v4,Vertex.LABEL_TRUE) ; v3.addChild(v4,Vertex.LABEL_DEFAULT) ; v4.addChild(v1,Vertex.LABEL_DEFAULT) ; Graph g1 = new Graph() ; g1.nodes.add(v1) ; g1.nodes.add(v2) ; g1.nodes.add(v3) ; g1.nodes.add(v4) ; System.out.println("Graph:" + g1) ; LinkedList cycles = g1.getCycles() ; System.out.println("Cycles: " + cycles.size()) ; for (LinkedList C : cycles) System.out.println(" " + PathUtil.pathSimpleShow(C)) ; assert cycles.size() == 2 ; LinkedList C1 = cycles.get(0) ; assert C1.size() == 5 ; assert v1 == C1.get(0) ; assert v2 == C1.get(1) ; assert v3 == C1.get(2) ; assert v4 == C1.get(3) ; assert v1 == C1.get(4) ; LinkedList C2 = cycles.get(1) ; assert C2.size() == 4 ; assert v1 == C2.get(0) ; assert v2 == C2.get(1) ; assert v4 == C2.get(2) ; assert v1 == C2.get(3) ; LinkedList paths = g1.getMaxPaths(v1) ; System.out.println("Max. paths from " + v1.getId() + ":") ; for (LinkedList P : paths) System.out.println(" " + PathUtil.pathSimpleShow(P)) ; assert paths.size() == 0 ; // Prime paths: System.out.println("Prime paths:") ; paths = PrimePathUtil.getPrimePaths(g1,v1) ; for (LinkedList P : paths) System.out.println(" " + PathUtil.pathSimpleShow(P)) ; assert paths.size() == 2 ; } @Test public void test5() { System.out.println("\n*** 2 roots, 2 terminals, X-form.") ; Vertex root1 = new Vertex() ; Vertex root2 = new Vertex() ; Vertex middle = new Vertex() ; Vertex end1 = new Vertex() ; Vertex end2 = new Vertex() ; root1.setId(1); root2.setId(2); middle.setId(3); end1.setId(4); end2.setId(5); root1.addChild(middle,Vertex.LABEL_DEFAULT) ; root2.addChild(middle,Vertex.LABEL_DEFAULT) ; middle.addChild(end1,Vertex.LABEL_FALSE) ; middle.addChild(end2,Vertex.LABEL_TRUE) ; Graph g1 = new Graph() ; g1.nodes.add(root1) ; g1.nodes.add(root2) ; g1.nodes.add(end1) ; g1.nodes.add(end2) ; g1.nodes.add(middle) ; System.out.println("Graph:" + g1) ; // Cycles: LinkedList cycles = g1.getCycles() ; System.out.println("Cycles: " + cycles.size()) ; for (LinkedList C : cycles) System.out.println(" " + PathUtil.pathSimpleShow(C)) ; assert cycles.size() == 0 ; // Paths: LinkedList paths = g1.getMaxPaths(root1) ; System.out.println("Max. paths from " + root1.getId() + ":") ; for (LinkedList P : paths) System.out.println(" " + PathUtil.pathSimpleShow(P)) ; assert paths.size() == 2 ; assert PathUtil.pathSimpleShow(paths.get(0)).equals("[ 1, 3, 4 ]") ; assert PathUtil.pathSimpleShow(paths.get(1)).equals("[ 1, 3, 5 ]") ; paths = g1.getMaxPaths(root2) ; System.out.println("Max. paths from " + root2.getId() + ":") ; for (LinkedList P : paths) System.out.println(" " + PathUtil.pathSimpleShow(P)) ; assert paths.size() == 2 ; assert PathUtil.pathSimpleShow(paths.get(0)).equals("[ 2, 3, 4 ]") ; assert PathUtil.pathSimpleShow(paths.get(1)).equals("[ 2, 3, 5 ]") ; // Prime paths: System.out.println("Prime paths:") ; paths = PrimePathUtil.getPrimePaths(g1) ; for (LinkedList P : paths) System.out.println(" " + PathUtil.pathSimpleShow(P)) ; assert paths.size() == 4 ; } public class VORT extends Vertex { } @Test public void test6() { LinkedList V = new LinkedList() ; V.add(new VORT()) ; LinkedList W = (LinkedList) ((LinkedList) V) ; VORT a = W.getFirst() ; } }