View Javadoc

1   package joeq.Util.SyntheticGraphs;
2   
3   import java.io.PrintStream;
4   import java.util.HashMap;
5   import java.util.Iterator;
6   import java.util.LinkedList;
7   import java.util.List;
8   import java.util.Map;
9   
10  public class Graph {
11      public static class Direction {
12          String _dir;
13          public Direction(String dir){
14              _dir = dir;
15          }
16          public String toString(){
17              return _dir;
18          }
19          public static final String LR = "LR";
20          public static final String TB = "TB"; 
21      }
22      protected String      _name;
23      protected HashMap     _nodeMap;
24      protected List        _edgeList;
25      protected Direction _dir;
26      
27      public class Edge{
28          protected String n1, n2;
29          public Edge(String n1, String n2){
30              this.n1 = n1; this.n2 = n2;
31          }
32          public String toString(){
33              return n1 + " -> " + n2;
34          }
35      }
36      
37      public Graph(String name, Direction dir){
38          _name    = name;
39          _nodeMap = new HashMap();
40          _edgeList = new LinkedList();
41          _dir      = dir; 
42      }
43      
44      public Graph(){
45          _name     = "none";
46          _dir      = new Direction(Direction.LR);
47          _nodeMap  = new HashMap();
48          _edgeList = new LinkedList(); 
49      }
50      
51      public void addNode(String num, String name){
52          _nodeMap.put(num, name);
53      }
54      
55      public void addNode(long num, String name){
56          String num_str = new Long(num).toString();
57          _nodeMap.put("n" + num_str, name);
58      }
59      
60      public void addEdge(String num1, String num2){
61          Edge e = new Edge(num1, num2);
62          _edgeList.add(e);            
63      }
64      
65      public void addEdge(long num1, long num2){
66          String num1_str = "n" + new Long(num1).toString();
67          String num2_str = "n" + new Long(num2).toString();
68          
69          Edge e = new Edge(num1_str, num2_str);
70          _edgeList.add(e);
71      }
72      
73      public void printDot(PrintStream out){
74          out.println("digraph \"" + _name + "\" { \n" + "\trankdir=\"" + _dir.toString() + "\";");
75          Iterator iter = _nodeMap.entrySet().iterator();
76          while(iter.hasNext()){
77              Map.Entry e = (Map.Entry)iter.next();
78  
79              String num  = (String)e.getKey();
80              String name = (String)e.getValue();
81              
82              out.println("\t" + num + " [label=\"" + name + "\"];");
83          }
84          out.println("");
85  
86          
87          Iterator iter2 = _edgeList.iterator();
88          while(iter2.hasNext()){
89              Graph.Edge e = (Graph.Edge)iter2.next();
90              out.println("\t" + e.toString() + " [label = \"\"];");
91          }
92          
93          out.println("}; ");
94      } 
95  }
96  
97  class TestGraph {
98      public static void main(String argv[]){
99          //simpleTest();
100         //makeTest1();
101         makeTest2();
102     }
103     
104     static void simpleTest(){
105         Graph g = new Graph("small test", new Graph.Direction(Graph.Direction.LR));
106         g.addNode("n0", "name0");
107         g.addNode("n1", "name1");
108         g.addEdge("n0", "n1");
109 
110         g.printDot(System.out);
111     }
112     
113     //    a bunch of single nodes connected to one node in the clique each.
114     static void makeTest1(){
115         Graph g = new Graph();
116         for(int i = 0; i < 20; i++){
117             g.addNode(i, "node"+new Integer(i).toString());
118         }
119         
120         for(int i = 0; i < 10; i++){
121             for(int j = 0; j < 10; j++){
122                 g.addEdge(i, j);
123             }
124         }
125         
126         java.util.Random r = new java.util.Random();
127         for(int i = 11; i < 20; i++){
128             int link_to = r.nextInt(10)/* random 0..9 */;
129             g.addEdge(i, link_to);
130         }
131         g.printDot(System.out);
132     }
133     
134     // a bunch of single nodes connected to all nodes in the clique
135     static void makeTest2(){
136         Graph g = new Graph();
137         for(int i = 0; i < 20; i++){
138             g.addNode(i, "node"+new Integer(i).toString());
139         }
140     
141         for(int i = 0; i < 10; i++){
142             for(int j = 0; j < 10; j++){
143                 //if(Random())
144                 g.addEdge(i, j);
145             }
146         }
147     
148         for(int i = 11; i < 20; i++){
149             for(int j = 0; j < 10; j++){
150                 g.addEdge(i, j);
151             }
152         }
153         g.printDot(System.out);
154     }
155 }
156