*
* @see GraphReaderWriter#read()
*/
public Graph read() throws Exception {
//read builder property
GraphBuilder builder = (GraphBuilder)getProperty(BUILDER);
//create file input stream
ObjectInputStream objin = new ObjectInputStream(
new BufferedInputStream(
new FileInputStream((String)getProperty(FILENAME))
)
);
//read header
int nnodes = objin.readInt();
int nedges = objin.readInt();
//rebuild edge collection, upon reading an edge, at the edge to the
// adjacency list of each of its nodes
int count = 0;
while(count++ < nedges) {
Edge e = (Edge)objin.readObject();
e.getNodeA().setVisited(false);
e.getNodeB().setVisited(false);
builder.addEdge(e);
}
//rebuild node collection
for (
Iterator itr = builder.getGraph().getEdges().iterator(); itr.hasNext();
) {
Edge e = (Edge)itr.next();
if (!e.getNodeA().isVisited()) {
e.getNodeA().setVisited(true);
builder.addNode(e.getNodeA());
}
if (!e.getNodeB().isVisited()) {
e.getNodeB().setVisited(true);
builder.addNode(e.getNodeB());
}
}
//check if object stream is empty, if not, there are nodes of degree 0
// in the graph
try {
Node n;
while((n = (Node)objin.readObject()) != null) {
builder.addNode(n);
}
}
catch(EOFException ignore) {}
return(builder.getGraph());
}