blib/lib/Graph/XGMML.pm

filter: Statementstmt 97.8% 45 / 46 Branchbran 50.0% 4 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 100% 6 / 6 totaltotal 92.9% 66 / 71 CC 5 SCAR 5.2
CC
5
Cov
91%
new
3.1
DESTROY
2.1
start
2.0
CRAP 1.7
↑ Summary
1 package Graph::XGMML;
2  
3 =pod
4  
5 =head1 NAME
6  
7 Graph::XGMML - Simple Graph.pm-like interface for generating XGMML graphs
8  
9 =head1 SYNOPSIS
10  
11 use Graph::XGMML;
12
13 my $output = '';
14 my $graph = Graph::XGMML->new(
15 directed => 1,
16 OUTPUT => \$output,
17 );
18 $graph->add_node('foo');
19 $graph->add_node('bar');
20 $graph->add_edge('foo', 'bar');
21 $graph->end;
22  
23 =head1 DESCRIPTION
24  
25 To produce useful diagrams on extremely large graphs, sometimes it is necesary
26 to move beyond simple graphing tools to applications specifically designed for
27 rendering very large graphs, many of which were designed for biology
28 applications (such as my favourite Cytoscape).
29  
30 B<Graph::XGMML> is a module that can be used to generate export files for
31 arbitrarily large graphs in B<eXtensible Graph Modelling Markup Language>,
32 so that the graphs can be imported into these specialised tools.
33  
34 The API is intentionally designed to appear similar to more popular modules
35 such as L<Graph>, L<Graph::Easy> and L<GraphViz>.
36  
37 =head1 METHODS
38  
39 =cut
40  
41 2 ▶use 5.008005;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 2 ▶use XML::Writer ();
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 2 our $VERSION = '0.01';
47  
48 =pod
49  
50 =head2 new
51  
52 # Quick constructor to write to a file
53 $graph = Graph::XGMML->new(
54 directed => 1,
55 OUTPUT => IO::File->new('file', 'w'),
56 );
57
58 # Anonymous constructor
59 $graph = Graph::XGMML->new(
60 directed => 1,
61 );
62  
63 The C<new> constructor is used to create a graph writer.
64  
65 It takes a single optional boolean C<directed> parameter, which indicates if
66 the graphs you will be generating will be directed or undirected.
67  
68 If any additional parameters are passed to C<new>, the constructor will make an
69 additional call to the C<start> method to start writing the document header and
70 to open the root C<graph> node, passing it the extra parameters.
71  
72 Returns a new B<Graph::XGMML> object, or throws an exception (dies) on error.
73  
74 =cut
75  
76 1 ▶sub new {
Subroutine new: called CC=3 SCAR=11.2
Pod documented
77 1 my $class = shift;
78 1 my %param = @_;
79  
80 # Extract non-writer param
81 1 ▶ my $directed = delete($param{directed}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
delete $param{'directed'} ? : 1 0 1
82  
83 # Create the object
84 1 my $self = bless {
85 writer => undef,
86 started => 0,
87 directed => $directed,
88 }, $class;
89  
90 # If we have any params, autostart
91 1 ▶ if ( %param ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%param) 1 0 1
92 1 $self->start( %param );
93 }
94  
95 1 return $self;
96 }
97  
98 =pod
99  
100 =head2 start
101  
102 The C<start> method allows you to explicitly start the document writing, if the
103 original constructor was produced without additional parameters.
104  
105 Any parameters are passed directly to the underlying L<XML::Writer> constructor
106 which produces the object that will be used to generate the XML.
107  
108 Returns true if the document is started successfully, or throws an exception
109 (dies) on error.
110  
111 =cut
112  
113 1 ▶sub start {
Subroutine start: called CC=2 SCAR=7.1
Pod documented
114 1 my $self = shift;
115 1 $self->{writer} = XML::Writer->new( @_ );
116 1 ▶ unless ( $self->{writer} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"writer"}) 0 1 1
117 0 die("Failed to create XML::Writer object");
118 }
119 1 $self->{writer}->xmlDecl('UTF-8');
120 $self->{writer}->doctype(
121 1 'graph',
122 '-//John Punin//DTD graph description//EN',
123 'http://www.cs.rpi.edu/~puninj/XGMML/xgmml.dtd',
124 );
125 $self->{writer}->startTag('graph',
126 directed => $self->{directed},
127 1 );
128 1 $self->{started} = 1;
129 1 return 1;
130 }
131  
132 =pod
133  
134 =head2 add_node
135  
136 # Add a simple node
137 $graph->add_node( 'name' );
138
139 # Add optional tag attributes
140 $graph->add_node( 'name',
141 label => 'Tag Label',
142 weight => 100,
143 );
144  
145 The C<add_node> method is used to add a new node to the graph.
146  
147 Because the B<Graph::XGMML> object doesn't remember its state as it produces
148 the graph, you must specify all nodes in the graph explicitly.
149  
150 The first parameter is the identifier for the node. Any additional parameters
151 will be treated XGMML C<node> element tag pairs.
152  
153 Returns true or throws an exception (dies) on error.
154  
155 =cut
156  
157 2 ▶sub add_node {
Subroutine add_node: called CC=1 SCAR=0.0
Pod documented
158 2 my $self = shift;
159 2 my $name = shift;
160 2 $self->{writer}->startTag('node',
161 id => $name,
162 label => $name,
163 @_,
164 );
165 2 $self->{writer}->endTag('node');
166 2 return 1;
167 }
168  
169 =pod
170  
171 =head2 add_vertex
172  
173 The C<add_vertex> method is an alias to C<add_node>, provided for increased
174 compatibility with the L<Graph> API.
175  
176 It takes the same parameters as C<add_node>.
177  
178 =cut
179  
180 1 ▶sub add_vertex {
Subroutine add_vertex: called CC=1 SCAR=0.0
Pod documented
181 1 shift->add_node(@_);
182 }
183  
184 =pod
185  
186 =head2 add_edge
187  
188 # Add a simple edge
189 $graph->add_edge( 'foo' => 'bar' );
190
191 # Add with optional attributes
192 $graph->add_edge( 'foo' => 'bar',
193 weight => 1,
194 );
195  
196 The c<add_edge> method adds an edge to the graph.
197  
198 The first two parameters are the source and target of the edge. Any additional
199 parameters should be a set of key/value pairs of edge attributes.
200  
201 Returns true or throws an exception (dies) on error.
202  
203 =cut
204  
205 1 ▶sub add_edge {
Subroutine add_edge: called CC=1 SCAR=0.0
Pod documented
206 1 my $self = shift;
207 1 $self->{writer}->emptyTag('edge',
208 source => shift,
209 target => shift,
210 @_,
211 );
212 1 return 1;
213 }
214  
215 =pod
216  
217 =head2 end
218  
219 # Explicitly terminate the document
220 $graph->end;
221  
222 The C<end> method is used to indicate that the graph is completed that the XML
223 should be terminated.
224  
225 If you do not call it yourself, it will be called for you at C<DESTROY>-time.
226  
227 =cut
228  
229 1 ▶sub end {
Subroutine end: called CC=1 SCAR=0.0
Pod documented
230 1 my $self = shift;
231 1 $self->{writer}->endTag('graph');
232 1 $self->{writer}->end;
233 1 $self->{started} = 0;
234 1 delete $self->{writer};
235 1 return 1;
236 }
237  
238 1 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=7.2
239 1 ▶ if ( $_[0]->{started} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]{"started"}) 1 0 1
240 1 $_[0]->end;
241 }
242 }
243  
244 2 1;
245  
246 =pod
247  
248 =head1 SUPPORT
249  
250 Bugs should always be submitted via the CPAN bug tracker
251  
252 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Graph-XGMML>
253  
254 For other issues, contact the maintainer.
255  
256 =head1 AUTHOR
257  
258 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
259  
260 =head1 SEE ALSO
261  
262 L<Graph>, L<Graph::Easy>, L<GraphViz>
263  
264 =head1 COPYRIGHT
265  
266 Copyright 2009 Adam Kennedy.
267  
268 This program is free software; you can redistribute
269 it and/or modify it under the same terms as Perl itself.
270  
271 The full text of the license can be found in the
272 LICENSE file included with this module.
273  
274 =cut
↑ Summary