File Coverage

blib/lib/SNA/Network/Node.pm
Criterion Covered Total %
statement 49 57 85.9
branch 1 6 16.6
condition n/a
subroutine 18 20 90.0
pod 14 14 100.0
total 82 97 84.5


line stmt bran cond sub pod time code
1             package SNA::Network::Node;
2              
3 14     14   77 use warnings;
  14         25  
  14         452  
4 14     14   70 use strict;
  14         24  
  14         477  
5              
6 14     14   64 use List::Util qw(sum first);
  14         26  
  14         949  
7 14     14   13664 use Object::Tiny::XS qw(index community);
  14         74205  
  14         96  
8              
9              
10 14     14   20220 use Module::List::Pluggable qw(import_modules);
  14         680648  
  14         10129  
11             import_modules('SNA::Network::Node::Plugin');
12              
13              
14             =head1 NAME
15              
16             SNA::Network::Node - Node class for SNA::Network
17              
18              
19             =head1 SYNOPSIS
20              
21             my $node = $net->node_at_index(0);
22             my @neighbours = $node->related_nodes;
23             ...
24             ...
25              
26              
27             =head1 METHODS
28              
29             =head2 new
30              
31             Creates a new node with the given named parameters.
32             Not intended for external use.
33              
34             =cut
35              
36             sub new {
37 660     660 1 1339 my ($package, %params) = @_;
38 660         6551 return bless { %params, outgoing_edges => [], incoming_edges => [] }, $package;
39             }
40              
41              
42             =head2 index
43              
44             Returns the index of the node
45              
46              
47             =head2 edges
48              
49             Returns the list of L objects associated with this node.
50              
51             =cut
52              
53             sub edges {
54 1491     1491 1 1847 my ($self) = @_;
55             #FIXME why does that not work???
56 1491         3380 return map { $_ } $self->outgoing_edges, $self->incoming_edges;
  23526         34456  
57             # return($self->outgoing_edges, $self->incoming_edges);
58             }
59              
60              
61             =head2 related_nodes
62              
63             Returns the list of L objects that are linked to this node in any direction via an edge.
64              
65             =cut
66              
67             sub related_nodes {
68 1103     1103 1 1394 my ($self) = @_;
69 1103         3236 return (map { $_->source } $self->incoming_edges), map { $_->target } $self->outgoing_edges;
  8771         17467  
  8771         16751  
70             }
71              
72              
73             =head2 incoming_edges
74              
75             Returns the list of L objects that point to this node.
76              
77             =cut
78              
79             sub incoming_edges {
80 3807     3807 1 6971 my ($self) = @_;
81 3807         4990 return @{ $self->{incoming_edges} };
  3807         15250  
82             }
83              
84              
85             =head2 incoming_nodes
86              
87             Returns the list of L objects that point to this node via an edge.
88              
89             =cut
90              
91             sub incoming_nodes {
92 90     90 1 105 my ($self) = @_;
93 90         152 return map { $_->source } $self->incoming_edges;
  143         327  
94              
95             }
96              
97              
98             =head2 outgoing_edges
99              
100             Returns the list of L objects pointing from this node to other nodes.
101              
102             =cut
103              
104             sub outgoing_edges {
105 22494     22494 1 22957 my ($self) = @_;
106 22494         22652 return @{ $self->{outgoing_edges} };
  22494         82531  
107             }
108              
109              
110             =head2 outgoing_nodes
111              
112             Returns the list of L objects that this node points to via an edge.
113              
114             =cut
115              
116             sub outgoing_nodes {
117 18655     18655 1 21091 my ($self) = @_;
118 18655         34332 return map { $_->target } $self->outgoing_edges;
  392989         556104  
119              
120             }
121              
122              
123             =head2 in_degree
124              
125             Returns the in-degree of this node, i.e. the number of incoming edges.
126              
127             =cut
128              
129             sub in_degree {
130 9     9 1 14 my ($self) = @_;
131 9         20 return int $self->incoming_edges;
132             }
133              
134              
135             =head2 out_degree
136              
137             Returns the out-degree of this node, i.e. the number of outgoing edges.
138              
139             =cut
140              
141             sub out_degree {
142 77     77 1 96 my ($self) = @_;
143 77         120 return int $self->outgoing_edges;
144             }
145              
146              
147             =head2 summed_degree
148              
149             Returns the summed degree of this node, i.e. the number of associated edges.
150              
151             =cut
152              
153             sub summed_degree {
154 2     2 1 4 my ($self) = @_;
155 2         6 return int $self->edges;
156             }
157              
158              
159             =head2 weighted_in_degree
160              
161             Returns the weighted in-degree of this node, i.e. the sum of all incoming edge weights.
162              
163             =cut
164              
165             sub weighted_in_degree {
166 0     0 1 0 my ($self) = @_;
167 0 0       0 return 0 unless $self->incoming_edges;
168 0         0 return sum map { $_->weight } $self->incoming_edges;
  0         0  
169             }
170              
171              
172             =head2 weighted_out_degree
173              
174             Returns the weighted out-degree of this node, i.e. the sum of all outgoing edge weights.
175              
176             =cut
177              
178             sub weighted_out_degree {
179 0     0 1 0 my ($self) = @_;
180 0 0       0 return 0 unless $self->outgoing_edges;
181 0         0 return sum map { $_->weight } $self->outgoing_edges;
  0         0  
182             }
183              
184              
185             =head2 weighted_summed_degree
186              
187             Returns the summed weighted degree of this node,
188             i.e. the sum of all incoming and all outgoing edgeweights.
189              
190             =cut
191              
192             sub weighted_summed_degree {
193 190     190 1 235 my ($self) = @_;
194 190 50       430 return 0 unless $self->edges;
195 190         526 return sum map { $_->weight() } $self->edges;
  2988         7780  
196             }
197              
198              
199             =head2 loop
200              
201             Returns the L object that connects this node with itself, a so-called loop, it such one exitst. Otherwise returns C.
202              
203             =cut
204              
205             sub loop {
206 54     54 1 80 my ($self) = @_;
207 54     54   217 return first { $_->target == $self } $self->outgoing_edges;
  54         273  
208             }
209              
210              
211             =head2 community
212              
213             Returns the index of the community the node belongs to after community identification by L
214              
215              
216              
217             =head1 AUTHOR
218              
219             Darko Obradovic, C<< >>
220              
221             =head1 BUGS
222              
223             Please report any bugs or feature requests to C, or through
224             the web interface at L. I will be notified, and then you'll
225             automatically be notified of progress on your bug as I make changes.
226              
227              
228              
229              
230             =head1 SUPPORT
231              
232             You can find documentation for this module with the perldoc command.
233              
234             perldoc SNA::Network
235              
236              
237             You can also look for information at:
238              
239             =over 4
240              
241             =item * RT: CPAN's request tracker
242              
243             L
244              
245             =item * AnnoCPAN: Annotated CPAN documentation
246              
247             L
248              
249             =item * CPAN Ratings
250              
251             L
252              
253             =item * Search CPAN
254              
255             L
256              
257             =back
258              
259              
260             =head1 ACKNOWLEDGEMENTS
261              
262              
263             =head1 COPYRIGHT & LICENSE
264              
265             Copyright 2009 Darko Obradovic, all rights reserved.
266              
267             This program is free software; you can redistribute it and/or modify it
268             under the same terms as Perl itself.
269              
270              
271             =cut
272              
273             1; # End of SNA::Network::Node