001package ca.bc.webarts.widgets.graph; 002 003import java.util.Iterator; 004import org.graphstream.graph.*; 005import org.graphstream.graph.implementations.*; 006 007import ca.bc.webarts.tools.SqlQuery; 008 009/** NetworkGraph is a class that creates a directed graph using the GraphStream library to 010 * represent a Power system distribution network. 011 **/ 012public class NetworkGraph 013{ 014 /** Main method for testing from the commandline. **/ 015 public static void main( String args[] ) 016 { 017 NetworkGraph instance = null; 018 if(args.length<1) 019 instance = new NetworkGraph(); 020 //lse 021 } 022 023 024 /** default constructor that initializes the cals with a simple sample network and displays it. **/ 025 public NetworkGraph() 026 { 027 Graph graph = new SingleGraph( "Sample Network" ); 028 029 graph.addAttribute( "ui.stylesheet", styleSheet ); 030 graph.setAutoCreate( true ); 031 graph.setStrict( false ); 032 033 // DISPLAY the graph 034 graph.display(); 035 036 addSampleEdges(graph); 037 038 for ( Node node : graph ) 039 { 040 node.addAttribute( "ui.label", node.getId() ); 041 } 042 043 explore( graph.getNode( "Root" ) ); 044 } 045 046 047 /** default constructor that initializes the cals with a simple sample network and displays it. **/ 048 public NetworkGraph(StringBuilder izSb) 049 { 050 051 Graph graph = new SingleGraph( "Network" ); 052 053 graph.addAttribute( "ui.stylesheet", styleSheet ); 054 graph.setAutoCreate( true ); 055 graph.setStrict( false ); 056 057 createSubstationCircuits(izSb); 058 059 // DISPLAY the graph 060 graph.display(); 061 062 //addEdges(graph, sb); 063 064 for ( Node node : graph ) 065 { 066 node.addAttribute( "ui.label", node.getId() ); 067 } 068 069 explore( graph.getNode( "Root" ) ); 070 } 071 072 073 public void explore( Node source ) 074 { 075 Iterator<? extends Node> k = source.getBreadthFirstIterator(); 076 077 while ( k.hasNext() ) 078 { 079 Node next = k.next(); 080 next.setAttribute( "ui.class", "marked" ); 081 sleep(); 082 } 083 } 084 085 086 /** simple method to sleep the current thread and hides and interupted exceptions. **/ 087 protected void sleep() 088 { 089 try 090 { Thread.sleep(1000 ); } 091 catch ( Exception e ) 092 { } 093 } 094 095 096 /** Adds a sample set of edges to the graph for demo purposes. **/ 097 protected void addSampleEdges(Graph graph) 098 { 099 graph.addEdge( "RootToB", "Root", "B" ); 100 graph.addEdge( "BC", "B", "C" ); 101 graph.addEdge( "BD", "B", "D" ); 102 graph.addEdge( "CE", "C", "E" ); 103 graph.addEdge( "EF", "E", "F" ); 104 graph.addEdge( "FG", "F", "G" ); 105 graph.addEdge( "FH", "F", "H" ); 106 graph.addEdge( "EI", "E", "I" ); 107 } 108 109 110 /** Addes edges to the graph based on the contents of the passed in StringBuiler that holds a delimeted . **/ 111 protected void addSampleEdges(Graph graph, StringBuilder sb) 112 { 113 graph.addEdge( "RootToB", "Root", "B" ); 114 graph.addEdge( "BC", "B", "C" ); 115 graph.addEdge( "BD", "B", "D" ); 116 graph.addEdge( "CE", "C", "E" ); 117 graph.addEdge( "EF", "E", "F" ); 118 graph.addEdge( "FG", "F", "G" ); 119 graph.addEdge( "FH", "F", "H" ); 120 graph.addEdge( "EI", "E", "I" ); 121 } 122 123 124 /** 125 * Takes a IZ StringBuffer and creATES THE GRAPH. 126 */ 127 private void createSubstationCircuits(StringBuffer sb) 128 { 129 boolean debugOut = false; 130 131 int numRows = sb.toString().split("\n").length-1; 132 String currFdr = ""; 133 String currIz = ""; 134 String deviceType = ""; 135 String deviceSysID = ""; 136 String deviceCapacity = ""; 137 String custCount = ""; 138 String childVertexName = ""; 139 String izParent = ""; 140 141 if (debugOut) System.out.println("numRows "+numRows); 142 if (debugOut) System.out.println(sb.toString()); 143 144 String [][] data = SqlQuery.sbToDataRowArray(sb); // [row][col] 145 146 for (int currRow=0; currRow<numRows; currRow++) 147 { 148 debugOut = false; 149 currFdr = data[currRow][1].trim(); 150 currIz = data[currRow][2].trim(); 151 deviceSysID = data[currRow][4].trim(); 152 deviceType = data[currRow][5].trim(); 153 if (data[currRow].length>6 && data[currRow][6].trim().length()>0) 154 deviceCapacity = data[currRow][6].trim(); 155 else 156 deviceCapacity = ""; 157 if (data[currRow].length>7 && data[currRow][7].trim().length()>0) 158 custCount = data[currRow][7].trim(); 159 else 160 custCount = "0"; 161 // ************ 162 childVertexName = createIzoneVertexName(currFdr,currIz,deviceType,deviceSysID,deviceCapacity,custCount); 163 // ************ 164 if (currRow==0) graph.addNode(currFdr.substring(5)); // create substation 165 166 /* 167 if (!vertexVector.contains(currFdr) ) 168 { 169 if (debugOut) System.out.println("ADDING: "+currFdr+" currRow="+currRow); 170 if (debugOut) System.out.println(" vertexVector: "+vertexVector); 171 vertexVector.addElement(currFdr); 172 graph.addVertex(currFdr); 173 } 174 */ 175 176 if (currFdr.equals(data[i][1].trim())) 177 { 178 if (("1.source".equals(currIz)||"01.source".equals(currIz))) 179 { 180 graph.addNode(currFdr+".source"); 181 } 182 else 183 { 184 if(currIz.lastIndexOf(".").lastIndexOf(".")>-1) 185 { 186 izParent = currIz.substring(0,currIz.lastIndexOf(".").lastIndexOf(".")); 187 graph.addEdge(childVertexName,izParent,currIz); 188 } 189 else 190 { 191 izParent = currFdr+".source"; 192 graph.addEdge(childVertexName,izParent,currIz); 193 } 194 } 195 196 } 197 } 198 } 199 200 201 /** 202 * 203 */ 204 private String createIzoneVertexName(String fdr, String iz, String deviceType, String deviceSysID) 205 { 206 return fdr+"}"+iz+"_"+deviceType+"-"+deviceSysID; 207 } 208 209 210 /** 211 * 212 */ 213 private String createIzoneVertexName(String fdr, String iz, String deviceType, String deviceSysID, String deviceCapacity) 214 { 215 return fdr+"}"+iz+"_"+deviceType+(deviceType.equalsIgnoreCase("cutout")?"("+deviceCapacity+")":"")+"-"+deviceSysID; 216 } 217 218 219 /** 220 * 221 */ 222 private String createIzoneVertexName(String fdr, String iz, String deviceType, String deviceSysID, String deviceCapacity, String custCount) 223 { 224 return fdr+"}"+iz+"_"+deviceType+(deviceType.equalsIgnoreCase("cutout")?"("+deviceCapacity+")":"")+"-"+deviceSysID+".Custs="+custCount; 225 } 226 227 228 protected String styleSheet = 229 "node {" + 230 " fill-color: black;" + 231 "}" + 232 "node.marked {" + 233 " fill-color: red;" + 234 "}"; 235}