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

LineHitsSource
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 {
360    public static final Object LABEL = "edu.ics.uci.jung.io.HypergraphReader.LABEL";
37     
380    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)
590    {
600        this.as_list = as_list;
610        this.edge_first = edge_first;
620        this.copy_action = copy_action;
630    }
64     
65     public HypergraphReader()
66     {
670        this(false, false, UserData.SHARED);
680    }
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     {
780        Hypergraph h = new SetHypergraph();
790        BufferedReader br = new BufferedReader(reader);
800        Map vertex_names = new HashMap();
810        Map edge_names = new HashMap();
82         
830        while (br.ready())
84         {
850            String curLine = br.readLine();
860            if (curLine == null || curLine.equals("end_of_file"))
870                break;
880            if (curLine.trim().length() == 0)
890                continue;
90             String[] parts;
91             
920            if (as_list)
930                parts = curLine.trim().split("\\s+");
94             else
950                parts = curLine.trim().split("\\s+", 2);
96  
970            if (edge_first)
98             {
990                Hyperedge e = (Hyperedge)edge_names.get(parts[0]);
1000                if (e == null)
101                 {
1020                    e = new SetHyperedge();
1030                    h.addEdge(e);
1040                    edge_names.put(parts[0], e);
1050                    e.addUserDatum(LABEL, parts[0], copy_action);
106                 }
1070                for (int i = 1; i < parts.length; i++)
108                 {
1090                    Hypervertex v = (Hypervertex)vertex_names.get(parts[i]);
1100                    if (v == null)
111                     {
1120                        v = new SetHypervertex();
1130                        h.addVertex(v);
1140                        vertex_names.put(parts[i], v);
1150                        v.addUserDatum(LABEL, parts[i], copy_action);
116                     }
1170                    if (!e.isIncident(v))
1180                        e.connectVertex(v);
119                     else
1200                        if (verbose)
1210                            System.out.println("duplicate line: " + curLine);
122                 }
123             }
124             else // vertex first, then edge(s)
125             {
1260                Hypervertex v = (Hypervertex)vertex_names.get(parts[0]);
1270                if (v == null)
128                 {
1290                    v = new SetHypervertex();
1300                    h.addVertex(v);
1310                    vertex_names.put(parts[0], v);
1320                    v.addUserDatum(LABEL, parts[0], copy_action);
133                 }
1340                for (int i = 1; i < parts.length; i++)
135                 {
1360                    Hyperedge e = (Hyperedge)edge_names.get(parts[i]);
1370                    if (e == null)
138                     {
1390                        e = new SetHyperedge();
1400                        h.addEdge(e);
1410                        edge_names.put(parts[i], e);
1420                        e.addUserDatum(LABEL, parts[i], copy_action);
143                     }
1440                    if (!e.isIncident(v))
1450                        e.connectVertex(v);
146                     else
1470                        if (verbose)
1480                            System.out.println("duplicate line: " + curLine);
149                 }
150             }
151  
152         }
153         
1540        return h;
155     }
156     
157     public void setVerboseMode(boolean b)
158     {
1590        verbose = b;
1600    }
161 }

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.