📄 graph.java
字号:
edgegml != null; edgegml = gml.getNextGMLSubObject()) { Integer source, target; source = (Integer) edgegml.getValue("source", GMLobject.GMLinteger); target = (Integer) edgegml.getValue("target", GMLobject.GMLinteger); String properties; if ((properties = (String) edgegml.getValue("properties", GMLobject.GMLstring)) == null) properties = ""; Double bandwidth; if ((bandwidth = (Double) edgegml.getValue("bandwidth", GMLobject.GMLreal)) == null) bandwidth = new Double(0.0); Double costs; if ((costs = (Double) edgegml.getValue("costs", GMLobject.GMLreal)) == null) costs = new Double(0.0); Double delay; if ((delay = (Double) edgegml.getValue("delay", GMLobject.GMLreal)) == null) delay = new Double(0.0); Double distance; if ((distance = (Double) edgegml.getValue("distance", GMLobject.GMLreal)) == null) distance = new Double(0.0); Integer tmp; boolean directed = false; if ((tmp = (Integer) edgegml.getValue("simplex", GMLobject.GMLinteger)) != null) directed = (tmp.intValue() != 0); if (source != null && target != null) { from = keyList[source.intValue()]; to = keyList[target.intValue()]; if ((from < 0) || (to < 0)) throw new GraphException("illegal node id referenced in an edge"); if (directed) { graph.addLink(link = new Link(true, from, to)); } else { graph.addLink(link = new Link(false, from, to)); } link.getProperties().setLabel(properties); link.getProperties().setBandwidth(bandwidth.doubleValue()); link.getProperties().setDelay(delay.doubleValue()); link.getProperties().setDistance(distance.doubleValue()); link.getProperties().setCosts(costs.doubleValue()); link.getProperties().setDirection(directed); } } return graph; }} // GMLReader/** * Class to read BRITE files of the BRITE Generator */class BRITEReader { final static int MAXNODES = 100000; public static Graph readGraph(BufferedReader line) throws IOException, GraphException { Graph g = new Graph(); String aktLine; int[] keyList = new int[MAXNODES]; int nodeCount = 0; //number of nodes int edgeCount = 0; // number of edges int checkNodeCount = 0; int checkEdgeCount = 0; int expect = -1; int flag = 0; // for the detection of the next line int Topology = 1; int Nodes = 2; int Edges = 3; int i = 0; while ((aktLine = line.readLine()) != null) { //till there is no line left flag = 0; //Detection //empty line if (aktLine.length() == 0) { continue; } // then do nothing - next line //Comment line if (aktLine.charAt(0) == '#') { continue; } // do nothing - next line //keywords if (aktLine.startsWith("Topology:")) { //in brackets we find the number of nodes and edges //Topologie: StringTokenizer t = new StringTokenizer(aktLine); //cut String into token String trash = t.nextToken(); //first token "Topology:" trash = t.nextToken(); //second token "(" checkNodeCount = Integer.parseInt(t.nextToken()); //third token number of nodes trash = t.nextToken(); //fourth token "Nodes," checkEdgeCount = Integer.parseInt(t.nextToken()); //fifth token number of edges continue; } if (aktLine.startsWith("Model")) { // do nothing - next line continue; } if (aktLine.startsWith("Nodes:")) { //next lines contain the information of the vertices (nodes) expect = Nodes; flag = 1; continue; } if (aktLine.startsWith("Edges:")) { //next lines contain the information of the edges expect = Edges; if (i < checkNodeCount) { System.out.println("Less nodes than declared"); throw new GraphException("Wrong File Format Type BRITE: counted nodes " + i); } i = 0; flag = 1; continue; } //Nodes if (expect == Nodes && flag == 0) { i++; if (i > checkNodeCount) { System.out.println("More nodes than declared"); throw new GraphException("Wrong File Format Type BRITE at Line:" + (i + nodeCount + 3)); } StringTokenizer t = new StringTokenizer(aktLine); // cut String int nodeID = Integer.parseInt(t.nextToken()); //analyse first token double x = Integer.parseInt(t.nextToken()); //second token double y = Integer.parseInt(t.nextToken()); //third token int ASid = Integer.parseInt(t.nextToken()); ASid = Integer.parseInt(t.nextToken()); //String indegree = t.nextToken(); //fourth token indegree //String outdegree = t.nextToken(); //fifth token outdegree ASid = Integer.parseInt(t.nextToken()); //sixth token id of the AS //String type = t.nextToken(); //seventh token type AS or Router Node Node n = new Node(); int key = g.addNode(n); keyList[nodeCount++] = key; n.getProperties().setX(x); n.getProperties().setY(y); if (ASid < 0) { n.getProperties().setType(NodeProperties.CORE_NODE); } else { n.getProperties().setType(NodeProperties.EDGE_NODE); } } //end Nodes //Edges if (expect == Edges && flag == 0) { //this line contains edgeinformations i++; if (i > checkEdgeCount) { System.out.println("More edges than declared"); throw new GraphException("Wrong File Format Type BRITE at Line:" + (i + nodeCount + 6)); } StringTokenizer t = new StringTokenizer(aktLine); //cut String int edgeID = Integer.parseInt(t.nextToken()); //first token int sourceID = Integer.parseInt(t.nextToken()); //second token int destinationID = Integer.parseInt(t.nextToken()); //third token String trash = t.nextToken(); //fourth token (euklidian lenth) is not used double delay = Double.parseDouble(t.nextToken()); //fifth token double bandwidth = Double.parseDouble(t.nextToken()); //sixth token //int ASfrom = Integer.parseInt(t.nextToken()); //seventh token //int ASto = Integer.parseInt(t.nextToken()); //eighth token String type = t.nextToken(); type = t.nextToken(); type = t.nextToken(); //ninth token if (sourceID > nodeCount) { throw new GraphException("Invalid nodeId" + sourceID); } if (destinationID > nodeCount) { System.out.println("Warning: Invalid nodeId " + destinationID + "(in edges)"); //throw new GraphException("Invalid nodeId" + destinationID); } Link l = new Link(false, keyList[sourceID], keyList[destinationID]); double delay1 = 1000.0 * Math.pow(10, delay); l.getProperties().setDelay(delay1); l.getProperties().setBandwidth(bandwidth); edgeCount++; g.addLink(l); continue; } //end Edges } // end while //Check if checkEdgecount is equal to edgeCount if (checkEdgeCount != edgeCount) { System.out.println("Less edges than declared"); throw new GraphException("Wrong File Format Type BRITE: counted edges " + i); }; return g; } //end public static} //end BRITEReader/** * Class to read ALT files of the GT-ITM Generator */ class ALTReader { final static int MAXNODES = 100000; public static Graph readGraph(BufferedReader line) throws IOException, GraphException { Graph g = new Graph(); String aktLine; int[] keyList = new int[MAXNODES]; int nodeCount = 0; //number of nodes int edgeCount = 0; // number of edges int checkNodeCount = 0; int checkEdgeCount = 0; int expect = -1; int flag = 0; // for the detection of the next line int flag1 = 0; int GRAPH = 1; int VERTICES = 2; int EDGES = 3; int nodeID = -1; while ((aktLine = line.readLine()) != null) { //till there is no line left flag = 0; //empty line if (aktLine.length() == 0) { continue; } // then do nothing - next line //Comment line if ((aktLine.charAt(0)) == '#') { continue; } // do nothing - next line //detection if (aktLine.startsWith("GRAPH")) { //next line gives the number of nodes and edges expect = GRAPH; flag = 1; continue; } if (aktLine.startsWith("VERTICES")) { //next lines contain the information of the vertices (nodes) expect = VERTICES; flag = 1; continue; } if (aktLine.startsWith("EDGES")) { //next lines contain the information of the edges expect = EDGES; flag = 1; continue; } //GRAPH if (expect == GRAPH && flag == 0) { StringTokenizer t = new StringTokenizer(aktLine); //cut String into token checkNodeCount = Integer.parseInt(t.nextToken()); //first token checkEdgeCount = Integer.parseInt(t.nextToken()) / 2; //second token continue; } //GRAPH //VERTICES if (expect == VERTICES && flag == 0) { StringTokenizer t = new StringTokenizer(aktLine); // cut String nodeID = Integer.parseInt(t.nextToken()); //analyse first token String name = t.nextToken(); //second token double x = Double.parseDouble(t.nextToken()); //third token double y = Double.parseDouble(t.nextToken()); //fourth token Node n = new Node(); int key = g.addNode(n); if ((nodeCount != nodeID) && (flag1 == 0)) { System.out.println("Warning: unsteady node ID at line: " + nodeCount); flag1 = 1; throw new GraphException("unsteady node ID at line: " + nodeCount); } keyList[nodeCount++] = key; n.getProperties().setX(x); n.getProperties().setY(y); switch (name.charAt(0)) { case 'T' : n.getProperties().setType(NodeProperties.CORE_NODE); break; case 'S' : n.getProperties().setType(NodeProperties.EDGE_NODE); break; case '0' : case '1' : case '2' : case '3' : case '4' : case '5' : case '6' : case '7' : case '8' : case '9' : n.getProperties().setType(NodeProperties.CORE_NODE); break; default : throw new GraphException("Unexpected Node Type at " + (nodeCount - 1)); }; continue; } //end VERTICES //EDGES if (expect == EDGES && flag == 0) { //this line contains edgeinformations StringTokenizer t = new StringTokenizer(aktLine); //cut string int sourceID = Integer.parseInt(t.nextToken()); //first token int targetID = Integer.parseInt(t.nextToken()); //second token //int source2ID = Integer.parseInt(t.nextToken()); //third token //int target2ID = Integer.parseInt(t.nextToken()); //fourth token if (sourceID > nodeCount) { throw new GraphException("Invalid nodeId" + sourceID); } if (targetID > nodeID) { System.out.println("Data Error: Invalid nodeId " + targetID); System.out.println(" at line:" + (6 + nodeCount + edgeCount)); System.out.println(" Warning: not all edges may be shown"); throw new GraphException("Invalid nodeId " + targetID); } /* if (source2ID > nodeID) { System.out.println("Data Error: Invalid nodeId " + source2ID); System.out.println(" at line:" + (6 + nodeCount + edgeCount)); System.out.println(" Warning: not all edges may be shown"); throw new GraphException(" "); } if (target2ID > nodeID) { System.out.println("Data Error: Invalid nodeId " + source2ID); System.out.println(" at line:" + (6 + nodeCount + edgeCount)); System.out.println(" Warning: not all edges may be shown"); throw new GraphException("Invalid nodeId " + source2ID); } */ if (targetID > nodeCount) { System.out.println("Warning: unsteady nodeId " + targetID); throw new GraphException("unsteady nodeId " + targetID); } /* if (source2ID > nodeCount) { System.out.println("Warning: unsteady nodeId " + source2ID); throw new GraphException("unsteady nodeId " + source2ID); } if (target2ID > nodeCount) { System.out.println("Warning: unsteady nodeId " + target2ID); throw new GraphException("unsteady nodeId " + target2ID); } */ Link l = new Link(false, keyList[sourceID], keyList[targetID]); g.addLink(l); edgeCount++; //l = new Link(false, keyList[source2ID], keyList[target2ID]); //g.addLink(l); continue; } //end EDGES else { // Error message throw new GraphException("Wrong File Format Type ALT"); } } // end while //Check if checkNodeCount is equal to nodeCount if (checkNodeCount != nodeCount) { throw new GraphException("File Format Type: wrong number of vertices declared! "); } //Check if checkEdgecount is equal to edgeCount if (checkEdgeCount != edgeCount) { if (checkEdgeCount > edgeCount) { System.out.println("Warning: Found less edges than declared!"); } if (checkEdgeCount < edgeCount) { System.out.println("Warning: Found more edges than declared!"); } //throw new GraphException ("File Format Type: wrong number of edges declared!"); } return g; } //end public static} //end ALTReader/** * Class to read TIERS files of the TIERS Generator */class TIERSReader { final static int MAXNODES = 100000; public static Graph readGraph(BufferedReader line) throws IOException, GraphException { System.out.println("tiers reader"); Graph g = new Graph(); String aktLine; int[] keyList = new int[MAXNODES]; int nodeCount = 0; // number of nodes int edgeCount = 0; // number of edges while ((aktLine = line.readLine()) != null) { // do until no lines are left if (aktLine.length() != 0) { //comment line if (isCommentLine(aktLine)) { } // do nothing - goto next line //line with nodes else if (isVertexLine(aktLine)) { StringTokenizer t = new StringTokenizer(aktLine); //cut string int nodeID = Integer.parseInt(t.nextToken()); //analye first token double x = Double.parseDouble(t.nextToken()); //second token double y = Double.parseDouble(t.nextToken()); //third token int type = Integer.parseInt(t.nextToken()); //fourth token, 0=WAN, 1=MAN, 2=LAN Node n = new Node(); int key = g.addNode(n); keyList[nodeCount++] = key; n.getProperties().setX(x); n.getProperties().setY(y); switch (type) { case 0 : n.getProperties().setType(NodeProperties.CORE_NODE); //WAN_NODE break; case 1 : n.getProperties().setType(NodeProperties.CORE_NODE); //MAN_NODE break; case 2 : n.getProperties().setType(NodeProperties.EDGE_NODE); //LAN_NODE break; default : throw new GraphException("Unexpected Node Type at " + (nodeCount - 1)); }; } // line with edges else if (isEdgeLine(aktLine)) { StringTokenizer t = new StringTokenizer(aktLine); //cut string int sourceID = Integer.parseInt(t.nextToken()); //first token int targetID = Integer.parseInt(t.nextToken()); //second token double delayID = Double.parseDouble(t.nextToken()); //third token - no longer considered double bandwidth = Double.parseDouble(t.nextToken()); //fourth token int fromType = Integer.parseInt(t.nextToken()); //fifth token int toType = Integer.parseInt(t.nextToken()); //sixth token if (sourceID > nodeCount) { throw new GraphException("Invalid nodeId" + sourceID); } if (targetID > nodeCount) { throw new GraphException("Invalid nodeId" + targetID); } Link l = new Link(false, keyList[sourceID], keyList[targetID]); l.getProperties().setDelay(delayID); l.getProperties().setBandwidth(bandwidth); g.addLink(l); edgeCount++; } //end if edgeLine } //end if (aktLine.length()!=0) } // end while return g; } private static boolean isCommentLine(String a) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -