⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 graph.java

📁 p2p仿真
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
			bwriter.write("\t]");			bwriter.newLine();		}	}	private String ns2Header =		"\n"			+ "# Generated automatically with KOM Topology Converter\n"			+ "# see http://www.kom.e-technik.tu-darmstadt.de/~heckmann/topologies/\n"			+ "# function to create a node\n"			+ "proc create-node {} {\n"			+ "   set ns [Simulator instance]\n"			+ "   return [$ns node]\n"			+ "}\n"			+ "\n"			+ "# function to create a link\n"			+ "proc create-link {from_ to_ duplex_ bw_ delay_ drop_} {\n"			+ "   set ns [Simulator instance]\n"			+ "   switch $duplex_ {\n"			+ "     0 { return [$ns simplex-link $from_ $to_ $bw_ $delay_ $drop_]}\n"			+ "     1 { return [$ns duplex-link $from_ $to_ $bw_ $delay_ $drop_]}\n"			+ "   }\n"			+ "} \n";	/**	 * write to a file in the NS2 script	 * @param filename file name	 */	public void writeToNS2(String filename) throws IOException, GraphException {		writeToNS2(new File(filename));	}	/**	 * write to a file in the NS2 script	 * @param f File object	 */		public void writeToNS2(File f) throws IOException, GraphException {		BufferedWriter bwriter = new BufferedWriter(new FileWriter(f));		if ((comment != null) && (!comment.equals(""))) {			bwriter.write("# Comment: \"" + comment + "\"");			bwriter.newLine();		}		if ((creator != null) && (!creator.equals(""))) {			bwriter.write("# Creator: \"" + creator + "\"");			bwriter.newLine();		}		bwriter.write(ns2Header);		bwriter.newLine();		Node[] nodes = this.getAllNodes();		for (int i = 0; i < nodes.length; i++) {			if (nodes[i] != null) {				bwriter.write("set n(" + i + ") [create-node]");				bwriter.newLine();			}		}		Link[] links = this.getAllLinks();		for (int i = 0; i < links.length; i++) {			Link l = links[i];			String properties = (l.getProperties().getBandwidth()/1024) + "Kb " + (l.getProperties().getDelay()*1000)+"ms DropTail";			bwriter.write(				"set l("					+ i					+ ") [create-link $n("					+ l.fromNode()					+ ") $n("					+ l.toNode()					+ ") "					+ (l.isDirected() ? "0" : "1")					+ " "					+ properties +"]");			bwriter.newLine();		}		bwriter.close();	}	/**	 * Read from a file in the BRITE topology file format	 * @param filename file name	 * @return graph object	 */	public static Graph fromBRITEFile(String filename) throws IOException, GraphException {		return fromBRITEFile(new BufferedReader(new FileReader(filename)));	}		/**	 * Read from a file in the BRITE topology file format	 * @param file File object	 * @return graph object	 */	public static Graph fromBRITEFile(File file) throws IOException, GraphException {		return fromBRITEFile(new BufferedReader(new FileReader(file)));	}	/**	 * Read from a file in the BRITE topology file format	 * @param r buffered reader	 * @return graph object	 * @throws IOException	 * @throws GraphException	 */	public static Graph fromBRITEFile(BufferedReader r) throws IOException, GraphException {		return BRITEReader.readGraph(r);	}	/**	 * Read from a file in the ALT topology file format	 * @param filename file name	 * @return graph object	 */	public static Graph fromALTFile(String filename) throws IOException, GraphException {		return fromALTFile(new BufferedReader(new FileReader(filename)));	}	/**	 * Read from a file in the ALT topology file format	 * @param file File object	 * @return graph object	 */		public static Graph fromALTFile(File file) throws IOException, GraphException {		return fromALTFile(new BufferedReader(new FileReader(file)));	}	/**	 * Read from a file in the ALT topology file format	 * @param r buffered reader	 * @return graph object	 * @throws IOException	 * @throws GraphException	 */	public static Graph fromALTFile(BufferedReader r) throws IOException, GraphException {		return ALTReader.readGraph(r);	}	/**	 * Read from a file in the TIERS topology file format	 * @param filename file name	 * @return graph object	 */	public static Graph fromTIERSFile(String filename) throws IOException, GraphException {		return fromTIERSFile(new BufferedReader(new FileReader(filename)));	}	/**	 * Read from a file in the TIERS topology file format	 * @param file File object	 * @return graph object	 */	public static Graph fromTIERSFile(File file) throws IOException, GraphException {		return fromTIERSFile(new BufferedReader(new FileReader(file)));	}	/**	 * Read from a file in the TIERS topology file format	 * @param r buffered reader	 * @return graph object	 * @throws IOException	 * @throws GraphException	 */	public static Graph fromTIERSFile(BufferedReader r) throws IOException, GraphException {		return TIERSReader.readGraph(r);	}	/**	 * Read from a file in the GTTS topology file format	 * @param filename file name	 * @return graph object	 */	public static Graph fromGTTSFile(String filename) throws IOException, GraphException {		return fromGTTSFile(new BufferedReader(new FileReader(filename)));	}	/**	 * Read from a file in the GTTS topology file format	 * @param file File object	 * @return graph object	 */	public static Graph fromGTTSFile(File file) throws IOException, GraphException {		return fromGTTSFile(new BufferedReader(new FileReader(file)));	}	/**	 * Read from a file in the GTTS topology file format	 * @param r buffered reader	 * @return graph object	 * @throws IOException	 * @throws GraphException	 */	public static Graph fromGTTSFile(BufferedReader r) throws IOException, GraphException {		return ALTReader.readGraph(r);	}	/**	 * Read from a file in the INET topology file format	 * @param filename file name	 * @return graph object	 */	public static Graph fromINETFile(String filename) throws IOException, GraphException {		return fromINETFile(new BufferedReader(new FileReader(filename)));	}	/**	 * Read from a file in the INET topology file format	 * @param file File object	 * @return graph object	 */	public static Graph fromINETFile(File file) throws IOException, GraphException {		return fromINETFile(new BufferedReader(new FileReader(file)));	}	/**	 * Read from a file in the INET topology file format	 * @param r buffered reader	 * @return graph object	 * @throws IOException	 * @throws GraphException	 */	public static Graph fromINETFile(BufferedReader r) throws IOException, GraphException {		return INETReader.readGraph(r);	}	/**	 * Read from a file in the NLANR topology file format	 * @param filename file name	 * @return graph object	 */	public static Graph fromNLANRFile(String filename) throws IOException, GraphException {		return fromNLANRFile(new BufferedReader(new FileReader(filename)));	}	/**	 * Read from a file in the NLANR topology file format	 * @param file File object	 * @return graph object	 */	public static Graph fromNLANRFile(File file) throws IOException, GraphException {		return fromNLANRFile(new BufferedReader(new FileReader(file)));	}	/**	 * Read from a file in the NLANR topology file format	 * @param r buffered reader	 * @return graph object	 * @throws IOException	 * @throws GraphException	 */	public static Graph fromNLANRFile(BufferedReader r) throws IOException, GraphException {		return NLANRReader.readGraph(r);	}	/**	 * Read from a file in the CVS topology file format	 * @param filename file name	 * @return graph object	 */	public static Graph fromCSVFile(String filename) throws IOException, GraphException {		return fromCSVFile(new BufferedReader(new FileReader(filename)));	}	/**	 * Read from a file in the CVS topology file format	 * @param file File object	 * @return graph object	 */	public static Graph fromCSVFile(File file) throws IOException, GraphException {		return fromCSVFile(new BufferedReader(new FileReader(file)));	}	/**	 * Read from a file in the CVS topology file format	 * @param r buffered reader	 * @return graph object	 * @throws IOException	 * @throws GraphException	 */	public static Graph fromCSVFile(BufferedReader r) throws IOException, GraphException {		return CSVReader.readGraph(r);	}	/**	 * command line converter	 * @param args arguments	 */	public static void main(String[] args){				String USAGE =		"Usage: java gps.network.Graph InFile OutFile\n"			+ "   the following input file types are recognized:\n"			+ "      .gml (Letsqos Graph Modeling Language)\n"			+ "      .csv (Comma Separated Value)\n"			+ "      .alt\n"			+ "      .brite\n"			+ "      .tiers\n"			+ "      .gtts\n"			+ "      .inet\n"			+ "      .nlanr\n"			+ "   the following output file types are recognized:\n"			+ "      .gml (Letsqos Graph Modeling Language)\n"			+ "      .ns2 (NS2 OTcl script)";		try {			if (args.length != 2) {				System.out.println(USAGE);				System.exit(0);			}			Graph g = null;			System.out.println("processing " + args[0]);						if (args[0].endsWith(".gml") || args[0].endsWith(".GML")) {				System.out.println("reading gml file");				g = Graph.fromGMLFile(args[0]);			}			if (args[0].endsWith(".alt") || args[0].endsWith(".ALT")) {				System.out.println("reading alt file");				g = Graph.fromALTFile(args[0]);			}			if (args[0].endsWith(".brite") || args[0].endsWith(".BRITE")) {				System.out.println("reading brite file");				g = Graph.fromBRITEFile(args[0]);			}			if (args[0].endsWith(".tiers") || args[0].endsWith(".TIERS")) {				System.out.println("reading tiers file");				g = Graph.fromTIERSFile(args[0]);			}			if (args[0].endsWith(".gtts") || args[0].endsWith(".GTTS")) {				System.out.println("reading gtts file");				g = Graph.fromGTTSFile(args[0]);			}			if (args[0].endsWith(".inet") || args[0].endsWith(".INET")) {				System.out.println("reading inet file");				g = Graph.fromINETFile(args[0]);			}			if (args[0].endsWith(".nlanr") || args[0].endsWith(".NLANR")) {				System.out.println("reading nlanr file");				g = Graph.fromNLANRFile(args[0]);			}			if (args[0].endsWith(".csv") || args[0].endsWith(".CSV")) {				System.out.println("reading csv (comma separated value) file");				g = Graph.fromCSVFile(args[0]);			}			if (g != null) {				System.out.println("topology has " + g.getNumberOfNodes() + " nodes, " + g.getNumberOfLinks() + " links");							if (args[1].endsWith(".tcl") || args[1].endsWith(".TCL")) {					System.out.println("saving as NS2 otcl script");					g.writeToNS2(args[1]);				} else if (args[1].endsWith(".ns2") || args[1].endsWith(".NS2")) {					System.out.println("saving as NS2 otcl script");					g.writeToNS2(args[1]);				} else if (args[1].endsWith(".otcl") || args[1].endsWith(".OTCL")) {					System.out.println("saving as NS2 otcl script");					g.writeToNS2(args[1]);				} else {					System.out.println("saving in GML format");					g.writeToGML(args[1]);				}			}		} catch (Exception ex) {			ex.printStackTrace();		}		}}/** *  Class to read GML files */class GMLReader {	final static int MAXNODES = 100000;	public static Graph readGraph(InputStream input) throws IOException, GraphException {		Graph graph = new Graph();		int[] keyList = new int[MAXNODES];		for (int i = 0; i < keyList.length; i++)			keyList[i] = -1;		int nodecount = 0;		GMLlexer lexer = new GMLlexer(input);		GMLobject gml = new GMLobject(lexer, null);		gml = gml.getGMLSubObject("graph", GMLobject.GMLlist, false);		String comment;		String creator;		if ((comment = (String) gml.getValue("comment", GMLobject.GMLstring)) == null)			comment = "";		if ((creator = (String) gml.getValue("creator", GMLobject.GMLstring)) == null)			creator = "";		graph.setCreator(creator);		graph.setComment(comment);		Node node;		GMLobject nodegml;		for (nodegml = gml.getGMLSubObject("node", GMLobject.GMLlist, false);			nodegml != null;			nodegml = gml.getNextGMLSubObject()) {			double x = 0.0;			double y = 0.0;			int node_id = -1;			GMLobject graphics;			if ((graphics = nodegml.getGMLSubObject("graphics", GMLobject.GMLlist, false)) != null) {				Double tmp1;				GMLobject center;				if ((center = graphics.getGMLSubObject("center", GMLobject.GMLlist, false)) != null) {					if ((tmp1 = (Double) center.getValue("x", GMLobject.GMLreal)) != null)						x = tmp1.doubleValue();					if ((tmp1 = (Double) center.getValue("y", GMLobject.GMLreal)) != null)						y = tmp1.doubleValue();				}			}			String properties;			if ((properties = (String) nodegml.getValue("properties", GMLobject.GMLstring)) == null)				properties = "";			int type;			if ((type = ((Integer) nodegml.getValue("type", GMLobject.GMLinteger)).intValue()) < 0)				type = 0;			Integer id;			if ((id = (Integer) nodegml.getValue("id", GMLobject.GMLinteger)) != null) {				node_id = id.intValue();			}			String traffic;			if ((traffic = (String) nodegml.getValue("traffic", GMLobject.GMLstring)) == null)				traffic = "";			node = new Node();			int key = graph.addNode(node);			keyList[node_id] = key;			if (node_id < 0)				throw new GraphException("node id < 0 or no node_id found!");			// ID is ignored in the moment			node.getProperties().setX(x);			node.getProperties().setY(y);			node.getProperties().setLabel(properties);			node.getProperties().setType(type);			node.getProperties().setTraffic(traffic);		}		GMLobject edgegml;		Link link;		int from, to, index;		index = 0;		for (edgegml = gml.getGMLSubObject("edge", GMLobject.GMLlist, false);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -