Line | Hits | Source |
---|---|---|
1 | /* | |
2 | * Created on May 2, 2004 | |
3 | * | |
4 | * Copyright (c) 2004, 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.HashSet; | |
19 | import java.util.Map; | |
20 | import java.util.Set; | |
21 | import java.util.StringTokenizer; | |
22 | ||
23 | import org.apache.commons.collections.Predicate; | |
24 | ||
25 | import edu.uci.ics.jung.exceptions.FatalException; | |
26 | import edu.uci.ics.jung.graph.Graph; | |
27 | import edu.uci.ics.jung.graph.Vertex; | |
28 | import edu.uci.ics.jung.graph.decorators.StringLabeller; | |
29 | import edu.uci.ics.jung.graph.decorators.StringLabeller.UniqueLabelException; | |
30 | import edu.uci.ics.jung.utils.UserData; | |
31 | ||
32 | ||
33 | /** | |
34 | * Reads decorations for vertices in a specified partition from a | |
35 | * <code>Reader</code>. | |
36 | * | |
37 | * @author Joshua O'Madadhain | |
38 | */ | |
39 | 0 | public class PartitionDecorationReader |
40 | { | |
41 | ||
42 | /** | |
43 | * Decorates vertices in the specified partition with strings. The | |
44 | * decorations are specified by a text file with the following format: | |
45 | * | |
46 | * <pre> | |
47 | * vid_1 label_1 | |
48 | * vid_2 label_2 ... | |
49 | * </pre> | |
50 | * | |
51 | * <p> | |
52 | * The strings must be unique within this partition; duplicate strings will | |
53 | * cause a <code>UniqueLabelException</code> to be thrown. | |
54 | * | |
55 | * <p> | |
56 | * The end of the file may be artificially set by putting the string <code>end_of_file</code> | |
57 | * on a line by itself. | |
58 | * </p> | |
59 | * | |
60 | * @param bg | |
61 | * the bipartite graph whose vertices are to be decorated | |
62 | * @param name_reader | |
63 | * the reader containing the decoration information | |
64 | * @param partition | |
65 | * the vertex partition whose decorations are specified by this | |
66 | * file | |
67 | * @param string_key | |
68 | * the user data key for the decorations created | |
69 | */ | |
70 | public static void loadStrings( | |
71 | Graph bg, | |
72 | Reader name_reader, | |
73 | Predicate partition, | |
74 | Object string_key) | |
75 | { | |
76 | 0 | StringLabeller id_label = StringLabeller.getLabeller(bg, partition); |
77 | 0 | StringLabeller string_label = |
78 | StringLabeller.getLabeller(bg, string_key); | |
79 | try | |
80 | { | |
81 | 0 | BufferedReader br = new BufferedReader(name_reader); |
82 | 0 | while (br.ready()) |
83 | { | |
84 | 0 | String curLine = br.readLine(); |
85 | 0 | if (curLine == null || curLine.equals("end_of_file")) |
86 | 0 | break; |
87 | 0 | if (curLine.trim().length() == 0) |
88 | 0 | continue; |
89 | 0 | String[] parts = curLine.trim().split("\\s+", 2); |
90 | ||
91 | 0 | Vertex v = id_label.getVertex(parts[0]); |
92 | 0 | if (v == null) |
93 | 0 | throw new FatalException("Invalid vertex label"); |
94 | ||
95 | // attach the string to this vertex | |
96 | 0 | string_label.setLabel(v, parts[1]); |
97 | } | |
98 | 0 | br.close(); |
99 | 0 | name_reader.close(); |
100 | } | |
101 | 0 | catch (IOException ioe) |
102 | { | |
103 | 0 | throw new FatalException( |
104 | "Error loading names from reader " + name_reader, | |
105 | ioe); | |
106 | } | |
107 | 0 | catch (UniqueLabelException ule) |
108 | { | |
109 | 0 | throw new FatalException( |
110 | "Unexpected duplicate name in reader " + name_reader, | |
111 | ule); | |
112 | 0 | } |
113 | 0 | } |
114 | ||
115 | /** | |
116 | * Decorates vertices in the specified partition with typed count data. | |
117 | * The data must be contained in a text file in the following format: | |
118 | * | |
119 | * <pre> | |
120 | * vid_1 type_1 count_1 | |
121 | * vid_2 type_2 count_2 | |
122 | * ... | |
123 | * </pre> | |
124 | * | |
125 | * <p>where <code>count_i</code> (an integer value) represents | |
126 | * the number of elements of | |
127 | * type <code>type_i</code> possessed by the vertex with ID | |
128 | * <code>vid_i</code> (as defined by <code>BipartiteGraphReader.load()</code>) | |
129 | * for the <code>i</code>th line in the file.</p> | |
130 | * | |
131 | * <p>For example, the vertices might represent authors, the type might | |
132 | * represent a topic, and the count might represent the number of | |
133 | * papers that the specified author had written on that topic.<p> | |
134 | * | |
135 | * <p>If <code>normalize</code> is <code>true</code>, then the | |
136 | * count data will be scaled so that the counts for | |
137 | * each vertex will sum to 1. (In this case, each vertex must have | |
138 | * a positive total count value.) | |
139 | * | |
140 | * <p>The end of the file may be artificially set by putting the string | |
141 | * <code>end_of_file</code> on a line by itself.</p> | |
142 | * | |
143 | * @return the total number of types observed | |
144 | * @param bg the bipartite graph whose vertices are to be decorated | |
145 | * @param count_reader the reader containing the decoration data | |
146 | * @param partition the partition whose decorations are specified by this file | |
147 | * @param count_key the user key for the decorations | |
148 | * @param copyact the copy action for the decorations | |
149 | */ | |
150 | public static int loadCounts(Graph bg, Reader count_reader, | |
151 | Predicate partition, | |
152 | Object count_key, UserData.CopyAction copyact) | |
153 | { | |
154 | 0 | StringLabeller id_label = StringLabeller.getLabeller(bg, partition); |
155 | 0 | Set types = new HashSet(); |
156 | try | |
157 | { | |
158 | 0 | BufferedReader br = new BufferedReader(count_reader); |
159 | ||
160 | 0 | while (br.ready()) |
161 | { | |
162 | 0 | String curLine = br.readLine(); |
163 | 0 | if (curLine == null || curLine.equals("end_of_file")) |
164 | 0 | break; |
165 | 0 | if (curLine.trim().length() == 0) |
166 | 0 | continue; |
167 | ||
168 | 0 | StringTokenizer st = new StringTokenizer(curLine); |
169 | 0 | String entity_id = st.nextToken(); |
170 | 0 | String type_id = st.nextToken(); |
171 | 0 | Integer count = new Integer(st.nextToken()); |
172 | ||
173 | 0 | types.add(type_id); |
174 | ||
175 | 0 | Vertex v = id_label.getVertex(entity_id); |
176 | ||
177 | 0 | if (v == null) |
178 | 0 | throw new IllegalArgumentException("Unrecognized vertex " + entity_id); |
179 | ||
180 | 0 | Map count_map = (Map)v.getUserDatum(count_key); |
181 | 0 | if (count_map == null) |
182 | { | |
183 | 0 | count_map = new HashMap(); |
184 | 0 | v.addUserDatum(count_key, count_map, copyact); |
185 | } | |
186 | 0 | count_map.put(type_id, count); |
187 | ||
188 | } | |
189 | 0 | br.close(); |
190 | 0 | count_reader.close(); |
191 | } | |
192 | 0 | catch (IOException ioe) |
193 | { | |
194 | 0 | throw new FatalException("Error in loading counts from " + count_reader); |
195 | 0 | } |
196 | ||
197 | 0 | return types.size(); |
198 | } | |
199 | ||
200 | public static void loadCounts(Graph bg, Reader count_reader, | |
201 | Predicate partition, Object count_key, UserData.CopyAction copyact, | |
202 | int num_types) | |
203 | { | |
204 | 0 | StringLabeller id_label = StringLabeller.getLabeller(bg, partition); |
205 | try | |
206 | { | |
207 | 0 | BufferedReader br = new BufferedReader(count_reader); |
208 | ||
209 | 0 | while (br.ready()) |
210 | { | |
211 | 0 | String curLine = br.readLine(); |
212 | 0 | if (curLine == null || curLine.equals("end_of_file")) |
213 | 0 | break; |
214 | 0 | if (curLine.trim().length() == 0) |
215 | 0 | continue; |
216 | ||
217 | 0 | StringTokenizer st = new StringTokenizer(curLine); |
218 | 0 | String entity_id = st.nextToken(); |
219 | 0 | int type_id = new Integer(st.nextToken()).intValue() - 1; |
220 | 0 | int count = new Integer(st.nextToken()).intValue(); |
221 | ||
222 | 0 | Vertex v = id_label.getVertex(entity_id); |
223 | ||
224 | 0 | if (v == null) |
225 | 0 | throw new IllegalArgumentException("Unrecognized vertex " |
226 | + entity_id); | |
227 | ||
228 | 0 | double[] counts = (double[])v.getUserDatum(count_key); |
229 | 0 | if (counts == null) |
230 | { | |
231 | 0 | counts = new double[num_types]; |
232 | 0 | v.addUserDatum(count_key, counts, copyact); |
233 | } | |
234 | 0 | counts[type_id] = count; |
235 | } | |
236 | 0 | br.close(); |
237 | } | |
238 | 0 | catch (IOException ioe) |
239 | { | |
240 | 0 | throw new FatalException("Error in loading counts from " |
241 | + count_reader); | |
242 | 0 | } |
243 | 0 | } |
244 | ||
245 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |