import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;
/**
* 类GraphFromFile用于从文件读入图的信息。
* 包括:图的结点个数及其邻接关系、图中边的权值、图中结点显示的位置坐标
* 以及各结点对应的名称。
* <p>
* <b>其中有关图的邻接表文件的格式为:</b>
* </p>
* <pre>
* [Graph]
* NodeNum = 25
*
* [Nodes]
* Node0 = (1,242)
* Node1 = (0,242)(2,305)
* Node2 = (1,305)(3,397)(4,704)
* Node3 = (2,397)
* </pre>
* <p>
* <b>图中结点显示位置坐标的文件格式为:</b>
* <pre>
* 0(37,5)
* 1(34,8)
* 2(31,11)
* 3(30,14)
* 4(26,13)
* 5(26,17)
* 6(31,20)
* </pre>
* <p>
* <b>图中各结点对应名称的文件格式为:</b>
* </p>
* <pre>
* 0 (哈尔滨)
* 1 (长春)
* 2 (沈阳)
* 3 (大连)
* 4 (天津)
* 5 (徐州)
* 6 (上海)
* </pre>
* <p>
* <b>注意各文件格式必须与上述例子相同!但可以忽略空格的影响。</b>
* </p>
* @author Fe
*/
public class GraphFromFile{
/**
* 邻接表的引用,通过graphAdjList可访问整张图的所有信息。若图不存在,则graphAdjList为null。
*/
GraphAdjList graphAdjList[] = null;
/**
* 图中结点的个数。
*/
int nodeNum = 0;
/**
* 图中各结点对应的显示坐标数组。
*/
int nodePosition[][] = null;
/**
* 图中各结点对应的名称。
*/
String nodeID[] = null;
/**
* 从文件读入一张图,以及各结点显示坐标。不包括结点名称。
* @param graphAdjListFile
* 邻接链表文件名
* @param graphPositionFile
* 结点坐标文件名
*/
public GraphFromFile(String graphAdjListFile, String graphPositionFile) {
try {
getGraphAdjList(graphAdjListFile);
getGraphPosition(graphPositionFile);
} catch (IOException e) {
}
}
/**
* 从文件读入一张图,包括结点显示坐标以及每一个结点的名称。
* @param graphAdjListFile
* 邻接链表文件名
* @param graphPositionFile
* 结点坐标文件名
* @param nodeIDFile
* 结点名称文件名
*/
public GraphFromFile(String graphAdjListFile, String graphPositionFile, String nodeIDFile) {
try {
getGraphAdjList(graphAdjListFile);
getGraphPosition(graphPositionFile);
getGraphID(nodeIDFile);
} catch (IOException e) {
}
}
/**
* 返回邻接链表的引用。
* @return
* 邻接链表的引用
*/
public GraphAdjList[] getList() {
return graphAdjList;
}
/**
* 返回图中的结点数。
* @return
* 图中的结点数
*/
public int getNodeNum() {
return nodeNum;
}
/**
* 返回图中结点坐标。
* @return
* 图中结点坐标
*/
public int[][] getNodePosition() {
return nodePosition;
}
/**
* 返回图中结点名称。
* @return
* 图中结点名称
*/
public String[] getNodeID() {
return nodeID;
}
/**
* 过滤器,用于将读入文件中的空格过滤掉。
* @param tp
* 从文件读入的一行字符串。
* @return
* 返回过滤后的字符串。
*/
private static String filter(String tp) {
StringTokenizer t = new StringTokenizer(tp, " ");
String temp = "";
try {
while(true) {
temp += t.nextToken();
}
} catch (NoSuchElementException e) {
}
return temp;
}
/**
* 从文件中读入一张图的邻接链表。
* @param graphAdjListFile
* 邻接链表文件名
* @throws IOException
* 若文件读入错误,则抛出IOException。
*/
private void getGraphAdjList(String graphAdjListFile) throws IOException {
InputStream in = new FileInputStream(graphAdjListFile);
BufferedReader inReader = new BufferedReader(new InputStreamReader(in));
String temp = null;
while ((temp = inReader.readLine()) != null) {
temp = filter(temp);
if (temp.startsWith("NodeNum")) {
nodeNum = Integer.parseInt(temp.substring(temp.indexOf('=') + 1));
graphAdjList = new GraphAdjList[nodeNum];
} else if (temp.startsWith("Node")) {
int currentNode = Integer.parseInt(temp.substring(temp.indexOf('e')+1, temp.indexOf('=')));
String tp = temp.substring(temp.indexOf('(')+1, temp.lastIndexOf(')'));
StringTokenizer t = new StringTokenizer(tp, ")(");
try {
String tmp = null;
int adjNodeNum = -1;
int weight = -1;
NextAdjNode nextTemp = null;
NextAdjNode lastAdjNode = null;
boolean isFistTime = true;
while (true) {
tmp = t.nextToken();
adjNodeNum = Integer.parseInt(tmp.substring(0, tmp.indexOf(',')));
weight = Integer.parseInt(tmp.substring(tmp.indexOf(',')+1));
nextTemp = new NextAdjNode(adjNodeNum, weight);
if (isFistTime) {
graphAdjList[currentNode] = new GraphAdjList(nextTemp);
isFistTime = false;
} else {
lastAdjNode.nextNode = nextTemp;
}
lastAdjNode = nextTemp;
}
} catch (NoSuchElementException e) {
}
}
}
in.close();
}
/**
* 从文件读入图中结点显示坐标。
* @param grapPositionFile
* 结点坐标文件名
* @throws IOException
* 若文件读入错误,则抛出IOException。
*/
private void getGraphPosition(String grapPositionFile) throws IOException {
InputStream in = new FileInputStream(grapPositionFile);
BufferedReader inReader = new BufferedReader(new InputStreamReader(in));
nodePosition = new int[nodeNum][2];
String temp = null;
while((temp = inReader.readLine()) != null) {
temp = filter(temp);
int node = Integer.parseInt(temp.substring(0, temp.indexOf('(')));
nodePosition[node][0] = Integer.parseInt(temp.substring(temp.indexOf('(')+1, temp.indexOf(',')));
nodePosition[node][1] = Integer.parseInt(temp.substring(temp.indexOf(',')+1, temp.indexOf(')')));
}
in.close();
}
/**
* 从文件中读入结点对应名称。
* @param nodeIDFile
* 结点名称文件名
* @throws IOException
* 若文件读入错误,则抛出IOException。
*/
private void getGraphID(String nodeIDFile) throws IOException {
BufferedReader inReader = new BufferedReader(new FileReader(nodeIDFile));
nodeID = new String[nodeNum];
String temp = null;
while((temp = inReader.readLine()) != null) {
temp = filter(temp);
int node = Integer.parseInt(temp.substring(0, temp.indexOf('(')));
nodeID[node] = temp.substring(temp.indexOf('(')+1, temp.indexOf(')'));
}
}
}