public Graph(WeightedBVGraph graph, String[] names) { org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger("it.unimi.dsi.webgraph.ImmutableGraph"); logger.setLevel(org.apache.log4j.Level.FATAL); if (names.length != graph.numNodes()) throw new Error("Problem with the list of names for the nodes in the graph."); try { File auxFile = File.createTempFile("graph-maps-" + System.currentTimeMillis(), "aux"); auxFile.deleteOnExit(); RecordManager recMan = RecordManagerFactory.createRecordManager(auxFile.getAbsolutePath()); nodes = recMan.hashMap("nodes"); nodesReverse = recMan.hashMap("nodesReverse"); } catch (IOException ex) { throw new Error(ex); } nodes.clear(); nodesReverse.clear(); Constructor[] cons = WeightedArc.class.getDeclaredConstructors(); for (int i = 0; i < cons.length; i++) cons[i].setAccessible(true); this.graph = graph; WeightedArcSet list = new WeightedArcSet(); ArcLabelledNodeIterator it = graph.nodeIterator(); while (it.hasNext()) { if (commit++ % COMMIT_SIZE == 0) { commit(); list.commit(); } Integer aux1 = it.nextInt(); Integer aux2 = null; ArcLabelledNodeIterator.LabelledArcIterator suc = it.successors(); while ((aux2 = suc.nextInt()) != null && aux2 >= 0 && (aux2 < graph.numNodes())) try { WeightedArc arc = (WeightedArc) cons[0].newInstance(aux2, aux1, suc.label().getFloat()); list.add(arc); this.nodes.put(aux1, names[aux1]); this.nodes.put(aux2, names[aux2]); this.nodesReverse.put(names[aux1], aux1); this.nodesReverse.put(names[aux2], aux2); } catch (Exception ex) { throw new Error(ex); } } reverse = new WeightedBVGraph(list.toArray(new WeightedArc[0])); numArcs = list.size(); iterator = nodeIterator(); try { File auxFile = File.createTempFile("graph" + System.currentTimeMillis(), "aux"); auxFile.deleteOnExit(); String basename = auxFile.getAbsolutePath(); store(basename); } catch (IOException ex) { throw new Error(ex); } commit(); }
public Graph(String file) throws IOException { org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger("it.unimi.dsi.webgraph.ImmutableGraph"); logger.setLevel(org.apache.log4j.Level.FATAL); try { File auxFile = File.createTempFile("graph-maps-" + System.currentTimeMillis(), "aux"); auxFile.deleteOnExit(); RecordManager recMan = RecordManagerFactory.createRecordManager(auxFile.getAbsolutePath()); nodes = recMan.hashMap("nodes"); nodesReverse = recMan.hashMap("nodesReverse"); } catch (IOException ex) { throw new Error(ex); } nodes.clear(); nodesReverse.clear(); Constructor[] cons = WeightedArc.class.getDeclaredConstructors(); for (int i = 0; i < cons.length; i++) cons[i].setAccessible(true); String aux = null; Float weight = (float) 1.0; WeightedArcSet list = new WeightedArcSet(); BufferedReader br; try { br = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(file)))); } catch (Exception ex) { br = new BufferedReader(new FileReader(file)); } while ((aux = br.readLine()) != null) try { if (commit++ % COMMIT_SIZE == 0) { commit(); list.commit(); } String parts[] = aux.split("\t"); String l1 = new String(parts[0]); String l2 = new String(parts[1]); if (!nodesReverse.containsKey(l1)) { nodesReverse.put(l1, nodesReverse.size()); nodes.put(nodes.size(), l1); } if (!nodesReverse.containsKey(l2)) { nodesReverse.put(l2, nodesReverse.size()); nodes.put(nodes.size(), l2); } if (parts.length == 3) weight = new Float(parts[2]); list.add( (WeightedArc) cons[0].newInstance(nodesReverse.get(l1), nodesReverse.get(l2), weight)); } catch (Exception ex) { throw new Error(ex); } this.graph = new WeightedBVGraph(list.toArray(new WeightedArc[0])); br.close(); list = new WeightedArcSet(); br = new BufferedReader(new FileReader(file)); while ((aux = br.readLine()) != null) try { if (commit++ % COMMIT_SIZE == 0) { commit(); list.commit(); } String parts[] = aux.split("\t"); String l1 = new String(parts[0]); String l2 = new String(parts[1]); if (parts.length == 3) weight = new Float(parts[2]); list.add( (WeightedArc) cons[0].newInstance(nodesReverse.get(l2), nodesReverse.get(l1), weight)); } catch (Exception ex) { throw new Error(ex); } br.close(); this.reverse = new WeightedBVGraph(list.toArray(new WeightedArc[0])); numArcs = list.size(); iterator = nodeIterator(); try { File auxFile = File.createTempFile("graph" + System.currentTimeMillis(), "aux"); auxFile.deleteOnExit(); String basename = auxFile.getAbsolutePath(); store(basename); } catch (IOException ex) { throw new Error(ex); } commit(); }
public Graph(BVGraph graph) { org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger("it.unimi.dsi.webgraph.ImmutableGraph"); logger.setLevel(org.apache.log4j.Level.FATAL); try { File auxFile = File.createTempFile("graph-maps-" + System.currentTimeMillis(), "aux"); auxFile.deleteOnExit(); RecordManager recMan = RecordManagerFactory.createRecordManager(auxFile.getAbsolutePath()); nodes = recMan.hashMap("nodes"); nodesReverse = recMan.hashMap("nodesReverse"); } catch (IOException ex) { throw new Error(ex); } nodes.clear(); nodesReverse.clear(); Constructor[] cons = WeightedArc.class.getDeclaredConstructors(); for (int i = 0; i < cons.length; i++) cons[i].setAccessible(true); Integer aux1 = null; WeightedArcSet list = new WeightedArcSet(); it.unimi.dsi.webgraph.NodeIterator it = graph.nodeIterator(); while ((aux1 = it.nextInt()) != null) { LazyIntIterator suc = it.successors(); Integer aux2 = null; while ((aux2 = suc.nextInt()) != null && aux2 >= 0 && (aux2 < graph.numNodes())) try { if (commit++ % COMMIT_SIZE == 0) { list.commit(); } list.add((WeightedArc) cons[0].newInstance(aux1, aux2, (float) 1.0)); } catch (Exception ex) { throw new Error(ex); } } this.graph = new WeightedBVGraph(list.toArray(new WeightedArc[0])); list = new WeightedArcSet(); it = graph.nodeIterator(); while ((aux1 = it.nextInt()) != null) { LazyIntIterator suc = it.successors(); Integer aux2 = null; while ((aux2 = suc.nextInt()) != null && aux2 >= 0 && (aux2 < graph.numNodes())) try { if (commit++ % COMMIT_SIZE == 0) { commit(); list.commit(); } list.add((WeightedArc) cons[0].newInstance(aux2, aux1, (float) 1.0)); this.nodes.put(aux1, "" + aux1); this.nodes.put(aux2, "" + aux2); this.nodesReverse.put("" + aux1, aux1); this.nodesReverse.put("" + aux2, aux2); } catch (Exception ex) { throw new Error(ex); } } this.reverse = new WeightedBVGraph(list.toArray(new WeightedArc[0])); numArcs = list.size(); iterator = nodeIterator(); try { File auxFile = File.createTempFile("graph" + System.currentTimeMillis(), "aux"); auxFile.deleteOnExit(); String basename = auxFile.getAbsolutePath(); store(basename); } catch (IOException ex) { throw new Error(ex); } commit(); }