Class PajekNetReader<G extends Graph<V,E>,V,E>
Graph
from a Pajek NET formatted source.
If the edge constraints specify that the graph is strictly undirected,
and an "*Arcs" section is encountered, or if the edge constraints specify that the
graph is strictly directed, and an "*Edges" section is encountered,
an IllegalArgumentException
is thrown.
If the edge constraints do not permit parallel edges, only the first encountered of a set of parallel edges will be read; subsequent edges in that set will be ignored.
More restrictive edge constraints will cause vertices to be generated that are more time- and space-efficient. At the moment, only supports the part of the specification that defines:
- vertex ids (each must have a value from 1 to n, where n is the number of vertices)
- vertex labels (must be in quotes if interrupted by whitespace)
- directed edge connections (single or list)
- undirected edge connections (single or list)
- edge weights (not compatible with edges specified in list form)
note: this version of PajekNetReader does not support multiple edge weights, as PajekNetFile does; this behavior is consistent with the NET format. - vertex locations (x and y; z coordinate is ignored)
Here is an example format for a directed graph without edge weights
and edges specified in list form:
*vertices [# of vertices] 1 "a" 2 "b" 3 "c" *arcslist 1 2 3 2 3Here is an example format for an undirected graph with edge weights and edges specified in non-list form:
*vertices [# of vertices] 1 "a" 2 "b" 3 "c" *edges 1 2 0.1 1 3 0.9 2 3 1.0
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static class
A Predicate which evaluates totrue
if the argument ends with the string "list".protected static class
A Predicate which evaluates totrue
if the argument starts with the constructor-specified String. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final com.google.common.base.Predicate
<String> private static final com.google.common.base.Predicate
<String> private static final com.google.common.base.Predicate
<String> protected com.google.common.base.Supplier
<E> protected SettableTransformer
<E, Number> protected static final com.google.common.base.Predicate
<String> private static final com.google.common.base.Predicate
<String> private static final com.google.common.base.Predicate
<String> Used to specify whether the most recently read line is a Pajek-specific tag.protected com.google.common.base.Supplier
<V> protected SettableTransformer
<V, String> The map for vertex labels (if any) created by this class.protected SettableTransformer
<V, Point2D> The map for vertex locations (if any) defined by this class. -
Constructor Summary
ConstructorsConstructorDescriptionPajekNetReader
(com.google.common.base.Supplier<E> edge_factory) Creates a PajekNetReader instance with the specified edge Supplier, and whose vertex objects correspond to the integer IDs assigned in the file.PajekNetReader
(com.google.common.base.Supplier<V> vertex_factory, com.google.common.base.Supplier<E> edge_factory) Creates a PajekNetReader instance with the specified vertex and edge factories. -
Method Summary
Modifier and TypeMethodDescriptionprotected E
createAddEdge
(StringTokenizer st, V v1, EdgeType directed, Graph<V, E> g, List<V> id, com.google.common.base.Supplier<E> edge_factory) Returns the graph created by parsing the specified reader, as created by the specified Supplier.Populates the graphg
with the graph represented by the Pajek-format data supplied byreader
.Returns the graph created by parsing the specified file, as created by the specified Supplier.Returns the graph created by parsing the specified file, by populating the specified graph.private String
readArcsOrEdges
(String curLine, BufferedReader br, Graph<V, E> g, List<V> id, com.google.common.base.Supplier<E> edge_factory) private void
readVertex
(String curLine, List<V> id, int num_vertices) ParsescurLine
as a reference to a vertex, and optionally assigns label and location information.void
setEdgeWeightTransformer
(SettableTransformer<E, Number> edge_weights) Provides a Function which will be used to write out edge weights.void
setVertexLabeller
(SettableTransformer<V, String> vertex_labels) Provides a Function which will be used to write out the vertex labels.void
setVertexLocationTransformer
(SettableTransformer<V, Point2D> vertex_locations) Provides a Function which will be used to write out the vertex locations.protected String
skip
(BufferedReader br, com.google.common.base.Predicate<String> p) Returns the first line read frombr
for whichp
returnstrue
, ornull
if there is no such line.
-
Field Details
-
vertex_factory
-
edge_factory
-
vertex_labels
The map for vertex labels (if any) created by this class. -
vertex_locations
The map for vertex locations (if any) defined by this class. -
edge_weights
-
v_pred
Used to specify whether the most recently read line is a Pajek-specific tag. -
a_pred
-
e_pred
-
t_pred
-
c_pred
-
l_pred
-
-
Constructor Details
-
PajekNetReader
public PajekNetReader(com.google.common.base.Supplier<V> vertex_factory, com.google.common.base.Supplier<E> edge_factory) Creates a PajekNetReader instance with the specified vertex and edge factories.- Parameters:
vertex_factory
- the Supplier to use to create vertex objectsedge_factory
- the Supplier to use to create edge objects
-
PajekNetReader
Creates a PajekNetReader instance with the specified edge Supplier, and whose vertex objects correspond to the integer IDs assigned in the file. Note that this requiresV
to be assignment-compatible with anInteger
value.- Parameters:
edge_factory
- the Supplier to use to create edge objects
-
-
Method Details
-
load
public G load(String filename, com.google.common.base.Supplier<? extends G> graph_factory) throws IOException Returns the graph created by parsing the specified file, as created by the specified Supplier.- Parameters:
filename
- the file from which the graph is to be readgraph_factory
- used to provide a graph instance- Returns:
- a graph parsed from the specified file
- Throws:
IOException
- if the graph cannot be loaded
-
load
public G load(Reader reader, com.google.common.base.Supplier<? extends G> graph_factory) throws IOException Returns the graph created by parsing the specified reader, as created by the specified Supplier.- Parameters:
reader
- the reader instance from which the graph is to be readgraph_factory
- used to provide a graph instance- Returns:
- a graph parsed from the specified reader
- Throws:
IOException
- if the graph cannot be loaded
-
load
Returns the graph created by parsing the specified file, by populating the specified graph.- Parameters:
filename
- the file from which the graph is to be readg
- the graph instance to populate- Returns:
- a graph parsed from the specified file
- Throws:
IOException
- if the graph cannot be loaded
-
load
Populates the graphg
with the graph represented by the Pajek-format data supplied byreader
. Stores edge weights, if any, according tonev
(if non-null).Any existing vertices/edges of
g
, if any, are unaffected.The edge data are filtered according to
g
's constraints, if any; thus, ifg
only accepts directed edges, any undirected edges in the input are ignored.- Parameters:
reader
- the reader from which the graph is to be readg
- the graph instance to populate- Returns:
- a graph parsed from the specified reader
- Throws:
IOException
- if the graph cannot be loaded
-
readVertex
ParsescurLine
as a reference to a vertex, and optionally assigns label and location information. -
readArcsOrEdges
private String readArcsOrEdges(String curLine, BufferedReader br, Graph<V, E> g, List<V> id, com.google.common.base.Supplier<E> edge_factory) throws IOException- Throws:
IOException
-
createAddEdge
-
skip
protected String skip(BufferedReader br, com.google.common.base.Predicate<String> p) throws IOException Returns the first line read frombr
for whichp
returnstrue
, ornull
if there is no such line.- Parameters:
br
- the reader from which the graph is being readp
- predicate specifying what line to accept- Returns:
- the first line from
br
that matchesp
, or null - Throws:
IOException
- if an error is encountered while reading frombr
-
getVertexLocationTransformer
- Returns:
- the vertexLocationTransformer
-
setVertexLocationTransformer
Provides a Function which will be used to write out the vertex locations.- Parameters:
vertex_locations
- a container for the vertex locations
-
getVertexLabeller
- Returns:
- a mapping from vertices to their labels
-
setVertexLabeller
Provides a Function which will be used to write out the vertex labels.- Parameters:
vertex_labels
- a container for the vertex labels
-
getEdgeWeightTransformer
- Returns:
- a mapping from edges to their weights
-
setEdgeWeightTransformer
Provides a Function which will be used to write out edge weights.- Parameters:
edge_weights
- a container for the edge weights
-