/* * (non-Javadoc) * * @see gtna.io.graphReader.GraphReader#read(java.lang.String) */ @Override public Graph read(String filename) { String sep1 = Config.get("SNAP_SEPARATOR_1"); String ffd = Config.get("FILESYSTEM_FOLDER_DELIMITER"); SNAPFileReader fr = new SNAPFileReader(filename); try { String line = null; String name = filename.substring(filename.lastIndexOf(ffd) + 1); Graph graph = new Graph(name); Map<Integer, Integer> idMapping = new HashMap<Integer, Integer>(); int nodecounter = 0; LinkedList<String> edges = new LinkedList<String>(); graphtype type = null; line = fr.readLine(); type = (line.contains("Directed graph")) ? graphtype.DIRECTED : graphtype.UNDIRECTED; while (!line.contains("FromNodeId") && !line.contains("ToNodeId")) { line = fr.readLine(); } nodecounter = parseEdges(sep1, fr, idMapping, nodecounter, edges, type); Node[] nodes = Node.init(idMapping.size(), graph); Edges e = new Edges(nodes, edges.size()); ListIterator<String> edgeIterator = edges.listIterator(); while (edgeIterator.hasNext()) { String[] pair = edgeIterator.next().split("-"); e.add(Integer.parseInt(pair[0]), Integer.parseInt(pair[1])); } e.fill(); graph.setNodes(nodes); return graph; } catch (Exception e) { return null; } finally { fr.close(); } }
@Override public Graph read(String filename) { String delimiter = Config.get("GRAPH_WRITER_DELIMITER"); Filereader fr = new Filereader(filename); String name = fr.readLine(); int N = Integer.parseInt(fr.readLine()); int E = Integer.parseInt(fr.readLine()); Graph graph = new Graph(name); Node[] nodes = Node.init(N, graph); Edges edges = new Edges(nodes, E); String line = null; while ((line = fr.readLine()) != null) { String[] temp = line.split(delimiter); edges.add(Integer.parseInt(temp[0]), Integer.parseInt(temp[1])); } edges.fill(); graph.setNodes(nodes); fr.close(); return graph; }
public static Graph read(String filename) throws Exception { Timer timer = new Timer(); RandomAccessFile file = new RandomAccessFile(new File(filename), "r"); // seek to names while (!file.readLine().startsWith("names/")) {} // read names Vector<WOTNode> wotNodes = new Vector<WOTNode>(); int index = 0; String line; while (!(line = file.readLine()).startsWith("keys/")) { if (!line.equals("")) { WOTNode node = new WOTNode(index, line); wotNodes.add(node); index += 1; } } // read keys for (index = 0; index < wotNodes.size(); index++) { byte[] key = new byte[4]; file.read(key); WOTNode node = wotNodes.get(index); node.key = Util.toInt(key); } // skip the "signatures/" line String signaturesLine = file.readLine(); if (!signaturesLine.startsWith("signatures/")) { // FIXME an IllegalArgumentException isn't really semantically // correct throw new IllegalArgumentException("A line starting with \"signatures/\" was expected."); } // read signatures for (index = 0; index < wotNodes.size(); index++) { WOTNode node = wotNodes.get(index); // read how many keys are signed by this key byte[] buffer = new byte[4]; file.read(buffer); int signatureCount = Util.toInt(buffer); // inspect all signatures for (int signatureIndex = 0; signatureIndex < signatureCount; signatureIndex++) { byte[] signatureBuffer = new byte[4]; file.read(signatureBuffer); int signature = Util.toInt(signatureBuffer); // get the signature type // TODO do something with the signature type // int signatureType = signature & SIGNATURE_TYPE_MASK; // get the target key int targetIndex = signature & SIGNATURE_INDEX_MASK; if (targetIndex >= wotNodes.size()) { // FIXME an IllegalArgumentException isn't really // semantically correct throw new IllegalArgumentException("A target index that matches a node was expected."); } // retrieve the target node WOTNode target = wotNodes.get(targetIndex); node.signatures.add(target); } } // skip the "debug/" line String debugLine = file.readLine(); if (!debugLine.startsWith("debug/")) { // FIXME an IllegalArgumentException isn't really semantically // correct throw new IllegalArgumentException("A line starting with \"debug/was expected."); } file.close(); timer.end(); // prepare return graph Graph graph = new Graph("WOT - read from " + filename); Node[] nodes = Node.init(wotNodes.size(), graph); Edges edges = new Edges(nodes, nodes.length); for (WOTNode source : wotNodes) { for (WOTNode target : source.signatures) { edges.add(source.index, target.index); } } edges.fill(); graph.setNodes(nodes); return graph; }