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

📄 convert.java

📁 发布/订阅系统路由重配算法,可应用于ad hoc环境
💻 JAVA
字号:
package util;import util.*;import graph.*;import java.io.*;import java.util.*;public class Convert {	public static final int DELAY = 1;	public static final int BANDWITH = 2;	public static final int LENGTH = 3;	public static String inFile;	public static String outFile = "out";	public static int n = 100;	public static int type = LENGTH;	/**	 * @param args	 */	public static void main(String[] args) {		// Parse the arguments		if(args.length<1){			System.err.println("Convert: missing input file.");			System.exit(-1);		}		if(args.length>=2){			outFile=args[1];		}		if(args.length>=3){			n = Integer.parseInt(args[2]);		}		if(args.length>=4){			if(args[3].equalsIgnoreCase("DELAY")){				type = DELAY;			}			if(args[3].equalsIgnoreCase("BANDWITH")){				type = BANDWITH;			}			if(args[3].equalsIgnoreCase("LENGTH")){				type = LENGTH;			}		}				try{						// Parse the BRITE file			LineNumberReader in;			String line;			String[] tokens;					in = new LineNumberReader(new FileReader(args[0]));			// Forward to Nodes			while((line = in.readLine())!= null){				//System.out.println(line);				line = line.trim();				if(line.startsWith("Nodes:")){					break;				}			}						// Parse the nodes			Graph net = new Graph();			SimpleHashtable nodes = new SimpleHashtable();			int id1;			int id2;			double c;			Vertex v, w;			Edge e;			Property costs = new Property();			while((line = in.readLine())!= null){				line = line.trim();				if(line.equals("")){					continue;				}				if(line.startsWith("Edges:")){					System.out.println(line);					break;				}				//System.out.println(line);				tokens = line.split("\t");				id1 = Integer.parseInt(tokens[0]);				v = new Vertex();				nodes.put(new Integer(id1),v);				net.addVertex(v);			}						// Parse the edges			while((line = in.readLine())!= null){				line = line.trim();				if(line.equals("")){					continue;				}				tokens = line.split("\t");				//System.out.println(tokens[0]);				id1 = Integer.parseInt(tokens[1]);				id2 = Integer.parseInt(tokens[2]);				if(type==BANDWITH){					c = Double.parseDouble(tokens[5]);				}else if(type==LENGTH){					c = Double.parseDouble(tokens[3]);				}else{					c = Double.parseDouble(tokens[4]); 				}								v = (Vertex)nodes.get(new Integer(id1));				w = (Vertex)nodes.get(new Integer(id2));				e = new Edge(v,w);				e.setProperty(costs,new Double(c));				net.addEdge(e);			}						// Chose the Brokers			Random rand = new Random();			int r;			Property broker = new Property();			for(int i=0; i<n; i++){				boolean selected = false;				while(!selected){					r = rand.nextInt(net.getNumberOfVertices());					Iterator it = net.vertexIterator();					for(int j=0;j<net.getNumberOfVertices();j++){						v = (Vertex)it.next();						if(j==r){							if(v.getProperty(broker)==null){								v.setProperty(broker,"YES");								selected = true;								break;							}else{								break;							}						}					}				}			}						// Write out			PrintWriter out = new PrintWriter(new FileWriter(outFile),true);			out.println("# Test");			out.println("Nodes: ");			Property id = new Property();			id1 = 1;			id2 = 1;			for(Iterator it = net.vertexIterator(); it.hasNext(); id1++){				v = (Vertex)it.next();				v.setProperty(id,new Integer(id1));				if(v.getProperty(broker)!=null){					out.println(id1+"\t"+"BROKER"+"\t"+id2);					id2++;				}else{					out.println(id1+"\t"+"ROUTER");				}			}						out.println();			out.println("Edges:");			for(Iterator it = net.edgeIterator(); it.hasNext(); ){				e = (Edge)it.next();				id1 = ((Integer)e.s.getProperty(id)).intValue();				id2 = ((Integer)e.t.getProperty(id)).intValue();				c = ((Double)e.getProperty(costs)).doubleValue();				out.println(id1+"\t"+id2+"\t"+c);			}						out.flush();			out.close();					}catch(Exception e){			System.err.println(e);			e.printStackTrace();			System.exit(-1);		}		}}

⌨️ 快捷键说明

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