package Dijkstra;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import cgl.imr.base.SerializationException;
public class Graph {
private Hashtable<Integer, Node> nodes;
public Graph() {
this.nodes = new Hashtable<Integer, Node>();
}
public void setNodes(Hashtable<Integer, Node> n) {
this.nodes = n;
}
public Hashtable<Integer, Node> getNodes() {
return this.nodes;
}
public void addNode(int id, Node n) {
this.nodes.put(id, n);
}
public byte[] getBytes() throws SerializationException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
int count = this.nodes.size();
byte[] marshalledBytes = null;
byte[] data;
try {
dos.writeInt(count);
for (int k : this.nodes.keySet()) {
dos.writeInt(k);
data = this.nodes.get(k).getBytes();
dos.writeInt(data.length);
dos.write(data);
}
dos.flush();
marshalledBytes = bos.toByteArray();
bos = null;
dos = null;
} catch (IOException e) {
throw new SerializationException(e);
}
return marshalledBytes;
}
public void fromBytes(byte[] bytes) throws SerializationException {
ByteArrayInputStream byteArray = new ByteArrayInputStream(bytes);
DataInputStream dataStream = new DataInputStream(byteArray);
int length = 0;
byte[] data = null;
int key = 0;
try {
int count = dataStream.readInt();
for (int i = 0; i < count; i++) {
key = dataStream.readInt();
length = dataStream.readInt();
data = new byte[length];
dataStream.readFully(data);
this.nodes.put(key, new Node(data));
}
dataStream.close();
byteArray.close();
} catch (Exception ioe) {
throw new SerializationException(ioe);
}
}
public List<Graph> getSubGraphs(int num) {
ArrayList<Graph> subgraphs = new ArrayList<Graph>();
for (int i = 0; i < num; i++) {
Graph g = new Graph();
subgraphs.add(g);
}
for (int k : this.nodes.keySet()) {
Node n = this.nodes.get(k);
Graph g = subgraphs.get(k % num);
g.addNode(n.getID(), n);
}
return subgraphs;
}
public void loadFromFile(String filename) {
BufferedReader reader = null;
try {
System.out.println("Reading from file " + filename);
reader = new BufferedReader(new FileReader(filename.toString()));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
while (true) {
String values = reader.readLine();
System.out.println("values " + values);
if (values == null) {
break;
}
Node n = new Node(values);
this.addNode(n.getID(), n);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void printGraph() {
for (Node xnode : this.getNodes().values()) {
System.out.print(xnode.getID());
for (int x : xnode.getEdges().keySet())
System.out
.print(" " + x + "," + xnode.getEdgeWeight(x) + " |");
System.out.println();
}
}
public void storeToFile(String filename) throws IOException {
BufferedWriter writer = new BufferedWriter(new FileWriter(
filename.toString()));
for (int k : this.nodes.keySet()) {
Node vnode = this.nodes.get(k);
String value = "" + vnode.getID();
for (int v : vnode.getEdges().keySet()) {
value = value + " " + v;
value = value + "," + vnode.getEdgeWeight(v) + "| ";
}
value = value + "\n";
try {
writer.write(value);
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
writer.close();
}
}