| Line | Hits | Source |
|---|---|---|
| 1 | /* | |
| 2 | * Created on Jul 8, 2005 | |
| 3 | * | |
| 4 | * Copyright (c) 2005, the JUNG Project and the Regents of the University | |
| 5 | * of California | |
| 6 | * All rights reserved. | |
| 7 | * | |
| 8 | * This software is open-source under the BSD license; see either | |
| 9 | * "license.txt" or | |
| 10 | * http://jung.sourceforge.net/license.txt for a description. | |
| 11 | */ | |
| 12 | package edu.uci.ics.jung.io; | |
| 13 | ||
| 14 | import java.io.BufferedReader; | |
| 15 | import java.io.IOException; | |
| 16 | import java.io.Reader; | |
| 17 | import java.util.HashMap; | |
| 18 | import java.util.Map; | |
| 19 | ||
| 20 | import edu.uci.ics.jung.graph.Hyperedge; | |
| 21 | import edu.uci.ics.jung.graph.Hypergraph; | |
| 22 | import edu.uci.ics.jung.graph.Hypervertex; | |
| 23 | import edu.uci.ics.jung.graph.impl.SetHyperedge; | |
| 24 | import edu.uci.ics.jung.graph.impl.SetHypergraph; | |
| 25 | import edu.uci.ics.jung.graph.impl.SetHypervertex; | |
| 26 | import edu.uci.ics.jung.utils.UserData; | |
| 27 | import edu.uci.ics.jung.utils.UserDataContainer.CopyAction; | |
| 28 | ||
| 29 | /** | |
| 30 | * | |
| 31 | * | |
| 32 | * @author Joshua O'Madadhain | |
| 33 | */ | |
| 34 | public class HypergraphReader | |
| 35 | { | |
| 36 | 0 | public static final Object LABEL = "edu.ics.uci.jung.io.HypergraphReader.LABEL"; |
| 37 | ||
| 38 | 0 | protected boolean verbose = false; |
| 39 | ||
| 40 | /** | |
| 41 | * <p>If <code>true</code>, specifies that each line of input implicitly | |
| 42 | * represents a list of edges, where the first token specifies one endpoint, | |
| 43 | * and the subsequent tokens specify a sequence of opposite endpoints. | |
| 44 | * Otherwise, each line of input represents a single edge.</p> | |
| 45 | */ | |
| 46 | protected boolean as_list; | |
| 47 | ||
| 48 | ||
| 49 | protected boolean edge_first; | |
| 50 | protected CopyAction copy_action; | |
| 51 | ||
| 52 | /** | |
| 53 | * | |
| 54 | * @param as_list | |
| 55 | * @param edge_first | |
| 56 | * @param copy_action | |
| 57 | */ | |
| 58 | public HypergraphReader(boolean as_list, boolean edge_first, CopyAction copy_action) | |
| 59 | 0 | { |
| 60 | 0 | this.as_list = as_list; |
| 61 | 0 | this.edge_first = edge_first; |
| 62 | 0 | this.copy_action = copy_action; |
| 63 | 0 | } |
| 64 | ||
| 65 | public HypergraphReader() | |
| 66 | { | |
| 67 | 0 | this(false, false, UserData.SHARED); |
| 68 | 0 | } |
| 69 | ||
| 70 | /** | |
| 71 | * | |
| 72 | * @param reader | |
| 73 | * @return the hypergraph represented by the reader | |
| 74 | * @throws IOException | |
| 75 | */ | |
| 76 | public Hypergraph load(Reader reader) throws IOException | |
| 77 | { | |
| 78 | 0 | Hypergraph h = new SetHypergraph(); |
| 79 | 0 | BufferedReader br = new BufferedReader(reader); |
| 80 | 0 | Map vertex_names = new HashMap(); |
| 81 | 0 | Map edge_names = new HashMap(); |
| 82 | ||
| 83 | 0 | while (br.ready()) |
| 84 | { | |
| 85 | 0 | String curLine = br.readLine(); |
| 86 | 0 | if (curLine == null || curLine.equals("end_of_file")) |
| 87 | 0 | break; |
| 88 | 0 | if (curLine.trim().length() == 0) |
| 89 | 0 | continue; |
| 90 | String[] parts; | |
| 91 | ||
| 92 | 0 | if (as_list) |
| 93 | 0 | parts = curLine.trim().split("\\s+"); |
| 94 | else | |
| 95 | 0 | parts = curLine.trim().split("\\s+", 2); |
| 96 | ||
| 97 | 0 | if (edge_first) |
| 98 | { | |
| 99 | 0 | Hyperedge e = (Hyperedge)edge_names.get(parts[0]); |
| 100 | 0 | if (e == null) |
| 101 | { | |
| 102 | 0 | e = new SetHyperedge(); |
| 103 | 0 | h.addEdge(e); |
| 104 | 0 | edge_names.put(parts[0], e); |
| 105 | 0 | e.addUserDatum(LABEL, parts[0], copy_action); |
| 106 | } | |
| 107 | 0 | for (int i = 1; i < parts.length; i++) |
| 108 | { | |
| 109 | 0 | Hypervertex v = (Hypervertex)vertex_names.get(parts[i]); |
| 110 | 0 | if (v == null) |
| 111 | { | |
| 112 | 0 | v = new SetHypervertex(); |
| 113 | 0 | h.addVertex(v); |
| 114 | 0 | vertex_names.put(parts[i], v); |
| 115 | 0 | v.addUserDatum(LABEL, parts[i], copy_action); |
| 116 | } | |
| 117 | 0 | if (!e.isIncident(v)) |
| 118 | 0 | e.connectVertex(v); |
| 119 | else | |
| 120 | 0 | if (verbose) |
| 121 | 0 | System.out.println("duplicate line: " + curLine); |
| 122 | } | |
| 123 | } | |
| 124 | else // vertex first, then edge(s) | |
| 125 | { | |
| 126 | 0 | Hypervertex v = (Hypervertex)vertex_names.get(parts[0]); |
| 127 | 0 | if (v == null) |
| 128 | { | |
| 129 | 0 | v = new SetHypervertex(); |
| 130 | 0 | h.addVertex(v); |
| 131 | 0 | vertex_names.put(parts[0], v); |
| 132 | 0 | v.addUserDatum(LABEL, parts[0], copy_action); |
| 133 | } | |
| 134 | 0 | for (int i = 1; i < parts.length; i++) |
| 135 | { | |
| 136 | 0 | Hyperedge e = (Hyperedge)edge_names.get(parts[i]); |
| 137 | 0 | if (e == null) |
| 138 | { | |
| 139 | 0 | e = new SetHyperedge(); |
| 140 | 0 | h.addEdge(e); |
| 141 | 0 | edge_names.put(parts[i], e); |
| 142 | 0 | e.addUserDatum(LABEL, parts[i], copy_action); |
| 143 | } | |
| 144 | 0 | if (!e.isIncident(v)) |
| 145 | 0 | e.connectVertex(v); |
| 146 | else | |
| 147 | 0 | if (verbose) |
| 148 | 0 | System.out.println("duplicate line: " + curLine); |
| 149 | } | |
| 150 | } | |
| 151 | ||
| 152 | } | |
| 153 | ||
| 154 | 0 | return h; |
| 155 | } | |
| 156 | ||
| 157 | public void setVerboseMode(boolean b) | |
| 158 | { | |
| 159 | 0 | verbose = b; |
| 160 | 0 | } |
| 161 | } |
|
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |