Coverage details for edu.uci.ics.jung.io.PartitionDecorationReader

LineHitsSource
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  */
390public 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     {
760        StringLabeller id_label = StringLabeller.getLabeller(bg, partition);
770        StringLabeller string_label =
78             StringLabeller.getLabeller(bg, string_key);
79         try
80         {
810            BufferedReader br = new BufferedReader(name_reader);
820            while (br.ready())
83             {
840                String curLine = br.readLine();
850                if (curLine == null || curLine.equals("end_of_file"))
860                    break;
870                if (curLine.trim().length() == 0)
880                    continue;
890                String[] parts = curLine.trim().split("\\s+", 2);
90     
910                Vertex v = id_label.getVertex(parts[0]);
920                if (v == null)
930                    throw new FatalException("Invalid vertex label");
94     
95                 // attach the string to this vertex
960                string_label.setLabel(v, parts[1]);
97             }
980            br.close();
990            name_reader.close();
100         }
1010        catch (IOException ioe)
102         {
1030            throw new FatalException(
104                 "Error loading names from reader " + name_reader,
105                 ioe);
106         }
1070        catch (UniqueLabelException ule)
108         {
1090            throw new FatalException(
110                 "Unexpected duplicate name in reader " + name_reader,
111                 ule);
1120        }
1130    }
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     {
1540        StringLabeller id_label = StringLabeller.getLabeller(bg, partition);
1550        Set types = new HashSet();
156         try
157         {
1580            BufferedReader br = new BufferedReader(count_reader);
159             
1600            while (br.ready())
161             {
1620                String curLine = br.readLine();
1630                if (curLine == null || curLine.equals("end_of_file"))
1640                    break;
1650                if (curLine.trim().length() == 0)
1660                    continue;
167                     
1680                StringTokenizer st = new StringTokenizer(curLine);
1690                String entity_id = st.nextToken();
1700                String type_id = st.nextToken();
1710                Integer count = new Integer(st.nextToken());
172     
1730                types.add(type_id);
174                 
1750                Vertex v = id_label.getVertex(entity_id);
176                 
1770                if (v == null)
1780                    throw new IllegalArgumentException("Unrecognized vertex " + entity_id);
179                 
1800                Map count_map = (Map)v.getUserDatum(count_key);
1810                if (count_map == null)
182                 {
1830                    count_map = new HashMap();
1840                    v.addUserDatum(count_key, count_map, copyact);
185                 }
1860                count_map.put(type_id, count);
187                 
188             }
1890            br.close();
1900            count_reader.close();
191         }
1920        catch (IOException ioe)
193         {
1940            throw new FatalException("Error in loading counts from " + count_reader);
1950        }
196         
1970        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     {
2040        StringLabeller id_label = StringLabeller.getLabeller(bg, partition);
205         try
206         {
2070            BufferedReader br = new BufferedReader(count_reader);
208     
2090            while (br.ready())
210             {
2110                String curLine = br.readLine();
2120                if (curLine == null || curLine.equals("end_of_file"))
2130                    break;
2140                if (curLine.trim().length() == 0)
2150                    continue;
216     
2170                StringTokenizer st = new StringTokenizer(curLine);
2180                String entity_id = st.nextToken();
2190                int type_id = new Integer(st.nextToken()).intValue() - 1;
2200                int count = new Integer(st.nextToken()).intValue();
221     
2220                Vertex v = id_label.getVertex(entity_id);
223     
2240                if (v == null)
2250                    throw new IllegalArgumentException("Unrecognized vertex "
226                             + entity_id);
227     
2280                double[] counts = (double[])v.getUserDatum(count_key);
2290                if (counts == null)
230                 {
2310                    counts = new double[num_types];
2320                    v.addUserDatum(count_key, counts, copyact);
233                 }
2340                counts[type_id] = count;
235             }
2360            br.close();
237         }
2380        catch (IOException ioe)
239         {
2400            throw new FatalException("Error in loading counts from "
241                     + count_reader);
2420        }
2430    }
244  
245 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.