doc/graph_io.dox
author athos
Thu, 09 Jun 2005 09:49:48 +0000
changeset 1458 7a483c1d38b5
parent 1333 2640cf6547ff
child 1522 321661278137
permissions -rw-r--r--
Not ready, but I commit it for simplicity.
alpar@1118
     1
namespace lemon {
deba@1114
     2
/*!
deba@1114
     3
deba@1114
     4
deba@1114
     5
\page graph-io-page Graph Input-Output
deba@1114
     6
deba@1114
     7
The standard graph IO makes possible to store graphs and additional maps
alpar@1118
     8
in a flexible and efficient way. 
deba@1114
     9
deba@1114
    10
\section format The general file format
deba@1114
    11
deba@1114
    12
The graph file contains at most four section in the next order:
deba@1114
    13
deba@1114
    14
\li nodeset
deba@1114
    15
\li edgeset
deba@1114
    16
\li nodes
deba@1114
    17
\li edges
deba@1114
    18
deba@1114
    19
The nodeset section starts with the \c \@nodeset line.
deba@1114
    20
The next line contains the names of the maps separated by whitespaces.
deba@1114
    21
Each following line describes a node in the graph, it contains
deba@1333
    22
in the right order the values of the maps. The map named "id" should contain
deba@1333
    23
unique values because it regarded as ID-map. 
deba@1114
    24
deba@1114
    25
\code
deba@1114
    26
@nodeset
deba@1114
    27
id  x-coord  y-coord  color
deba@1114
    28
3   1.0      4.0      blue
deba@1114
    29
5   2.3      5.7      red
deba@1114
    30
12  7.8      2.3      green
deba@1114
    31
\endcode
deba@1114
    32
deba@1114
    33
The edgeset section is very similar to the nodeset section, it has
deba@1114
    34
same coloumn oriented structure. It starts with the line \c \@edgeset
deba@1114
    35
The next line contains the whitespace separated list of names of the map.
alpar@1118
    36
Each of the next lines describes one edge. The first two elements in the line
deba@1333
    37
are the ID of the source and target node as they occur in the ID node map. 
deba@1114
    38
deba@1114
    39
\code
deba@1114
    40
@edgeset
deba@1114
    41
             id    weight   label
deba@1114
    42
3   5        a     4.3      a-edge
deba@1114
    43
5   12       c     2.6      c-edge
deba@1114
    44
3   12       g     3.4      g-edge
deba@1114
    45
\endcode
deba@1114
    46
deba@1333
    47
The next section contains <em>labeled nodes</em> (i.e. nodes having a special
alpar@1118
    48
label on them). The section starts with
deba@1114
    49
\c \@nodes. Each of the next lines contains a label for a node in the graph 
deba@1333
    50
and then the ID described in the nodeset.
deba@1114
    51
deba@1114
    52
\code
deba@1114
    53
@nodes 
deba@1114
    54
source 3
deba@1114
    55
target 12
deba@1114
    56
\endcode
deba@1114
    57
deba@1333
    58
The last section describes the <em>labeled edges</em>
deba@1333
    59
(i.e. edges having a special label on them). It starts with \c \@edges
deba@1114
    60
and then each line contains the name of the edge and the ID.
deba@1114
    61
deba@1114
    62
\code
deba@1114
    63
@nodes 
deba@1114
    64
observed c
deba@1114
    65
\endcode
deba@1114
    66
deba@1114
    67
The file ends with the \c \@end line.
deba@1114
    68
deba@1114
    69
The file may contain empty lines and comment lines. The comment lines
deba@1114
    70
start with an \c # character.
deba@1114
    71
deba@1114
    72
\code
deba@1114
    73
@end
deba@1114
    74
\endcode
deba@1114
    75
deba@1114
    76
\section use Using graph input-output
deba@1114
    77
The graph input and output based on writing and reading commands. The user
alpar@1118
    78
adds writing and reading commands for the reader or writer class, then 
alpar@1118
    79
calls the \c run() method that executes all the given commands.
deba@1114
    80
deba@1114
    81
\subsection write Writing a graph
deba@1114
    82
deba@1114
    83
The \c GraphWriter class provides the graph output. To write a graph
deba@1114
    84
you should first give writing commands for the writer. You can declare
alpar@1118
    85
write command as \c NodeMap or \c EdgeMap writing and labeled Node and
deba@1114
    86
Edge writing.
deba@1114
    87
deba@1114
    88
\code
deba@1333
    89
GraphWriter<ListGraph> writer(std::cout, graph);
deba@1114
    90
\endcode
deba@1114
    91
deba@1394
    92
The \c writeNodeMap() function declares a \c NodeMap writing command in the
deba@1114
    93
\c GraphWriter. You should give as parameter the name of the map and the map
deba@1333
    94
object. The NodeMap writing command with name "id" should write a 
deba@1333
    95
unique map because it is regarded as ID map.
deba@1114
    96
deba@1114
    97
\see IdMap, DescriptorMap  
deba@1114
    98
deba@1114
    99
\code
deba@1114
   100
IdMap<ListGraph, Node> nodeIdMap;
deba@1394
   101
writer.writeNodeMap("id", nodeIdMap);
deba@1114
   102
deba@1394
   103
writer.writeNodeMap("x-coord", xCoordMap);
deba@1394
   104
writer.writeNodeMap("y-coord", yCoordMap);
deba@1394
   105
writer.writeNodeMap("color", colorMap);
deba@1114
   106
\endcode
deba@1114
   107
deba@1394
   108
With the \c writeEdgeMap() member function you can give an edge map
deba@1333
   109
writing command similar to the NodeMaps.
deba@1114
   110
deba@1114
   111
\see IdMap, DescriptorMap  
deba@1114
   112
\code
deba@1114
   113
DescriptorMap<ListGraph, Edge, ListGraph::EdgeMap<int> > edgeDescMap(graph);
deba@1394
   114
writer.writeEdgeMap("descriptor", edgeDescMap);
deba@1114
   115
deba@1394
   116
writer.writeEdgeMap("weight", weightMap);
deba@1394
   117
writer.writeEdgeMap("label", labelMap);
deba@1114
   118
\endcode
deba@1114
   119
deba@1394
   120
With \c writeNode() and \c writeEdge() functions you can point out Nodes and
deba@1114
   121
Edges in the graph. By example, you can write out the source and target
deba@1114
   122
of the graph.
deba@1114
   123
deba@1114
   124
\code
deba@1394
   125
writer.writeNode("source", sourceNode);
deba@1394
   126
writer.writeNode("target", targetNode);
deba@1114
   127
deba@1394
   128
writer.writeEdge("observed", edge);
deba@1114
   129
\endcode
deba@1114
   130
deba@1114
   131
After you give all write commands you must call the \c run() member
deba@1333
   132
function, which execute all the writer commands.
deba@1114
   133
deba@1114
   134
\code
deba@1114
   135
writer.run();
deba@1114
   136
\endcode
deba@1114
   137
deba@1114
   138
\subsection reading Reading a graph
deba@1114
   139
alpar@1118
   140
The given file format may contain several maps and labeled nodes or edges.
deba@1114
   141
If you read a graph you need not read all the maps and items just those
deba@1114
   142
that you need. The interface of the \c GraphReader is very similar to
deba@1114
   143
the GraphWriter but the reading method does not depend on the order the
deba@1114
   144
given commands.
deba@1114
   145
alpar@1118
   146
The reader object suppose that each not readed value does not contain 
alpar@1118
   147
whitespaces, therefore it has some extra possibilities to control how
alpar@1118
   148
it should skip the values when the string representation contains spaces.
deba@1114
   149
deba@1114
   150
\code
deba@1333
   151
GraphReader<ListGraph> reader(std::cin, graph);
deba@1114
   152
\endcode
deba@1114
   153
deba@1394
   154
The \c readNodeMap() function reads a map from the \c \@nodeset section.
alpar@1118
   155
If there is a map that you do not want to read from the file and there is
deba@1114
   156
whitespace in the string represenation of the values then you should
deba@1114
   157
call the \c skipNodeMap() template member function with proper parameters.
deba@1114
   158
deba@1114
   159
\see QuotedStringReader
deba@1114
   160
\code
deba@1394
   161
reader.readNodeMap("x-coord", xCoordMap);
deba@1394
   162
reader.readNodeMap("y-coord", yCoordMap);
deba@1114
   163
deba@1394
   164
reader.readNodeMap<QuotedStringReader>("label", labelMap);
deba@1114
   165
reader.skipNodeMap<QuotedStringReader>("description");
deba@1114
   166
deba@1394
   167
reader.readNodeMap("color", colorMap);
deba@1114
   168
\endcode
deba@1114
   169
deba@1394
   170
With the \c readEdgeMap() member function you can give an edge map
deba@1114
   171
reading command similar to the NodeMaps. 
deba@1114
   172
deba@1114
   173
\code
deba@1394
   174
reader.readEdgeMap("weight", weightMap);
deba@1394
   175
reader.readEdgeMap("label", labelMap);
deba@1114
   176
\endcode
deba@1114
   177
deba@1394
   178
With \c readNode() and \c readEdge() functions you can read labeled Nodes and
deba@1114
   179
Edges.
deba@1114
   180
deba@1114
   181
\code
deba@1394
   182
reader.readNode("source", sourceNode);
deba@1394
   183
reader.readNode("target", targetNode);
deba@1114
   184
deba@1394
   185
reader.readEdge("observed", edge);
deba@1114
   186
\endcode
deba@1114
   187
deba@1114
   188
After you give all read commands you must call the \c run() member
alpar@1118
   189
function, which execute all the commands.
deba@1114
   190
deba@1114
   191
\code
deba@1114
   192
reader.run();
deba@1114
   193
\endcode
deba@1114
   194
deba@1114
   195
\section types The background of the Reading and Writing
deba@1114
   196
The \c GraphReader should know how can read a Value from the given map.
deba@1114
   197
By the default implementation the input operator reads a value from
deba@1114
   198
the stream and the type of the readed value is the value type of the given map.
deba@1114
   199
When the reader should skip a value in the stream, because you do not
deba@1114
   200
want to store it in map, the reader skips a character sequence without 
deba@1114
   201
whitespace. 
deba@1114
   202
deba@1114
   203
If you want to change the functionality of the reader, you can use
deba@1114
   204
template parameters to specialize it. When you give a reading
deba@1114
   205
command for a map you can give a Reader type as template parameter.
deba@1333
   206
With this template parameter you can control how the Reader reads
deba@1114
   207
a value from the stream.
deba@1114
   208
deba@1114
   209
The reader has the next structure: 
deba@1114
   210
\code
deba@1114
   211
struct TypeReader {
deba@1114
   212
  typedef TypeName Value;
deba@1114
   213
deba@1114
   214
  void read(std::istream& is, Value& value);
deba@1114
   215
};
deba@1114
   216
\endcode
deba@1114
   217
deba@1114
   218
By example, the \c "strings" nodemap contains strings and you do not need
deba@1114
   219
the value of the string just the length. Then you can implement own Reader
deba@1114
   220
struct.
deba@1114
   221
deba@1114
   222
\code
deba@1114
   223
struct LengthReader {
deba@1114
   224
  typedef int Value;
deba@1114
   225
deba@1114
   226
  void read(std::istream& is, Value& value) {
deba@1114
   227
    std::string tmp;
deba@1114
   228
    is >> tmp;
deba@1114
   229
    value = tmp.length();
deba@1114
   230
  }
deba@1114
   231
};
deba@1114
   232
...
deba@1394
   233
reader.readNodeMap<LengthReader>("strings", lengthMap);
deba@1114
   234
\endcode  
deba@1114
   235
deba@1114
   236
The global functionality of the reader class can be changed by giving a
alpar@1118
   237
special template parameter for the GraphReader class. By default, the
alpar@1118
   238
template parameter is \c DefaultReaderTraits. A reader traits class 
deba@1114
   239
should provide an inner template class Reader for each type, and an 
deba@1114
   240
DefaultReader for skipping a value.
deba@1114
   241
deba@1114
   242
The specialization of the writing should be very similar to the reading.
deba@1114
   243
deba@1333
   244
\author Balazs Dezso
deba@1114
   245
*/
deba@1333
   246
}