WebSource/Module.pm

filter: Statementstmt 8.0% 10 / 124 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 15.7% 3 / 19 Podpod 84.6% 11 / 13 totaltotal 12.5% 24 / 191 CC 22 SCAR 35.2
CC
22
Cov
6%
produce
90.0
_init_
56.0
push
20.0
CRAP 33.7
« WebSource/Query.pm ↑ Summary WebSource/Envelope.pm »
1 package WebSource::Module;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use WebSource::Envelope;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 WebSource::Module : WebSource module - class from which all modules inherit
10 Each module needs to define a C<handler> method.
11  
12 =head1 SYNOPSIS
13  
14 {
15 package MyModule;
16 our @ISA=('WebSource::Module');
17  
18 sub handler {
19 my ($self,$val) = @_;
20 # do something with $val and produce @res
21 return @res;
22 }
23  
24 }
25  
26 my $m = MyModule->new(
27 name => "GiveMeAName",
28 queue => $queue
29 );
30  
31 ... do some stuff ...
32  
33 $m->forwardTo($queue);
34  
35 ... do some stuff ...
36  
37 $m->run->join();
38  
39 =head1 METHODS
40  
41 =over 2
42  
43 =cut
44  
45 =item C<< $mod = WebSource::Module->new( name => $name ) >>
46  
47 Create a new instance of module;
48  
49 =cut
50  
51 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
52 0 my $class = shift;
53 0 my %params = @_;
54 0 my $self = bless \%params, $class;
55 0 $self->_init_;
56 0 return $self;
57 }
58  
59 =item C<< $mod->_init_ >>
60  
61 Does some initializations. Any operator $op inheriting from WebSource::Module
62 should call $op->SUPER::_init_ inside a redefined _init_ method.
63  
64 =cut
65  
66 0 ▶sub _init_ {
Subroutine _init_: not called CC=7 SCAR=40.3
67 0 my $self = shift;
68 0 ▶ if(!$self->{name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"name"}) 0 0 0
69 0 carp("No module name given. Using default noname");
70 0 $self->{name} = "noname";
71 }
72 0 $self->{producers} = [];
73 0 $self->{consumers} = [];
74 0 $self->{results} = [];
75 0 $self->log(1,"new module named ", $self->{name}, " created");
76 0 ▶ if(my $wsd = $self->{wsdnode}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $wsd = $self->{"wsdnode"}) 0 0 0
77 0 foreach my $dnode ($wsd->findnodes('data')) {
78 0 $self->push(WebSource::Envelope->new(
79 type => "text/string",
80 data => $dnode->textContent
81 ));
82 }
83 0 foreach my $dnode ($wsd->findnodes('xmldata')) {
84 0 ▶ if(my @enodes = $dnode->findnodes("*")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@enodes) = $dnode->findnodes('*')) { } 0 0 0
85 0 $self->push(WebSource::Envelope->new(
86 type => "object/dom-node",
87 data => $enodes[0]
88 ));
89 } else {
90 0 carp("Warning : xmldata without data detected\n");
91 }
92 }
93 0 foreach my $pnode ($wsd->findnodes('parameters/param')) {
94 0 my $key = $pnode->getAttribute("name");
95 0 my $val = $pnode->getAttribute("value");
96 0 $key =~ s/-/_/g;
97 0 $self->{$key} = $val;
98 }
99 }
100 }
101  
102 =item C<< $mod->log($level,$message) >>
103  
104 Use this modules logger to log a message $message with priority level
105 $level. This is actually used internally. Any inheriting module is encouraged
106 to use the logging facility.
107  
108 =cut
109  
110 0 ▶sub log {
Subroutine log: not called CC=2 SCAR=17.9
Pod documented
111 0 my $self = shift;
112 0 my $level = shift;
113 0 my $class = ref($self);
114 0 my $name = $self->{name};
115 $self->{logger} and
116 0 ▶ $self->{logger}->log($level, "[$class/$name] ", @_);
Branch: 1 branch
BranchTrueFalseTotal
$self->{"logger"} and $self->{"logger"}->log($level, "[$class/$name] ", @_) 0 0 0
117 }
118  
119 =item C<< $mod->will_log($level) >>
120  
121 Use this modules logger to check if a message at level
122 $level will be logged. This is actually used internally. Any inheriting module is encouraged
123 to use the logging facility.
124  
125 =cut
126  
127 0 ▶sub will_log {
Subroutine will_log: not called CC=2 SCAR=17.9
Pod documented
128 0 my $self = shift;
129 0 my $level = shift;
130 0 ▶ return ($self->{logger} && $self->{logger}->will_log($level));
Condition0%0/3
$self->{'logger'} && $self->{'logger'}->will_log($level)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{'logger'} && $self->{'logger'}->will_log($level)
$self->{'logger'} $self->{'logger'}->will_log($level)
Truth table0%0/3
A && B
A= $self->{'logger'}B= $self->{'logger'}->will_log($level)
ABresult
0X0
100
111
131 }
132  
133 =item C<< $mod->set_logger($log) >>
134  
135 Sets the logger associated to this module
136  
137 =cut
138  
139 0 ▶sub set_logger {
Subroutine set_logger: not called CC=1 SCAR=6.9
Pod documented
140 0 my $self = shift;
141 0 $self->{logger} = shift;
142 0 $self->log(1,"logger set");
143 }
144  
145 =item C<< $mod->push($val) >>
146  
147 Push $val into the module. This handles the given value and stores
148 it onto the stock.
149  
150 =cut
151  
152 0 ▶sub push {
Subroutine push: not called CC=4 SCAR=30.0
Pod documented
153 0 my $self = shift;
154 0 foreach my $env (@_) {
155 0 ▶ UNIVERSAL::isa($env,'WebSource::Envelope') or croak("Didn't push in an envelope");
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($env, "WebSource::Envelope") or croak("Didn't push in an envelope") 0 0 0
156 0 $self->log(3,"Handling $env : ", $env->as_string);
157 0 $self->log(8,"Content $env : ",$env->dataXML);
158 0 my @res = $self->handle($env);
159 0 ▶ map { defined($_) or croak("Undefined value generated") } @res;
Condition0%0/2
defined $_ || croak('Undefined value generated')
l!l
00
MC/DC0%0/1
defined $_ || croak('Undefined value generated')
defined $_
Truth table0%0/2
A
A= defined $_
Aresult
11
00
160 0 $self->log(3,"Obtained ",$#res+1," results");
161 0 push @{$self->{results}}, @res;
162 }
163 }
164  
165 =item C<< $mod->producers(@modules) >>
166  
167 Add a list of modules from where to ask for data (ie. set @modules
168 as producers of this module).
169 Also sets this module as a consumer of each of the modules in @modules.
170  
171 B<Note :> Only C<producers> or C<consumers> calls should be used
172  
173 =cut
174  
175 0 ▶sub producers {
Subroutine producers: not called CC=2 SCAR=17.9
Pod documented
176 0 my $self = shift;
177 0 foreach my $p (@_) {
178 0 $self->_producers($p);
179 0 $p->_consumers($self);
180 }
181 }
182  
183 =item C<< $mod->consumers(@modules) >>
184  
185 Add a list of modules where results should be sent to (ie. set @modules
186 as consumers of this modules production).
187 Also sets this module as a producer of each of the modules in @modules.
188  
189 B<Note :> Only C<producers> or C<consumers> calls should be used
190  
191 =cut
192  
193 0 ▶sub consumers {
Subroutine consumers: not called CC=2 SCAR=17.9
Pod documented
194 0 my $self = shift;
195 0 foreach my $c (@_) {
196 0 $self->_consumers($c);
197 0 $c->_producers($self);
198 }
199 }
200  
201 0 ▶sub _producers {
Subroutine _producers: not called CC=1 SCAR=6.9
202 0 my $self = shift;
203 0 CORE::push @{$self->{producers}}, @_;
204 }
205  
206 0 ▶sub _consumers {
Subroutine _consumers: not called CC=1 SCAR=6.9
207 0 my $self = shift;
208 0 CORE::push @{$self->{consumers}}, @_;
209 }
210  
211 =item C<< $self->produce >>
212  
213 Ask this module to produce a result. If some are available in the
214 stock, the first is sent to its consumers. If not, more results are produced
215 by asking the producers of this module to produce more.
216  
217 If a result is produced it is forwarded to the modules consumers and
218 returned. If no results can be produced undef is returned. (However if
219 in a consumer, with an originally empty stock, a simpler test on the
220 stock after having called produce on a producer will do).
221  
222 =cut
223  
224 0 ▶sub produce {
Subroutine produce: not called CC=9 SCAR=45.0
Pod documented
225 0 my $self = shift;
226 0 my %map = @_;
227  
228 0 ▶ if(!$self->{__started__}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"__started__"}) 0 0 0
229 0 $self->start();
230 0 $self->{__started__} = 1;
231 }
232  
233 0 ▶ defined($map{$self->{name}}) or $map{$self->{name}} = 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $map{$self->{"name"}} or $map{$self->{"name"}} = 0 0 0 0
234 0 ▶ $map{$self->{name}} >= 1 and return ();
Branch: 1 branch
BranchTrueFalseTotal
$map{$self->{"name"}} >= 1 and return () 0 0 0
235 0 $map{$self->{name}} += 1;
236  
237 0 my %save = %map;
238 0 $self->log(3,"Producing");
239 0 my @prods = @{$self->{producers}};
240 0 my $done = 0;
241 0 ▶ while(!@{$self->{results}} && !$done) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->{"results"};} or $done) 0 0 0
Condition0%0/3
@{$self->{"results"};} or $done
l!l&&r!l&&!r
000
MC/DC0%0/2
@{$self->{"results"};} or $done
@{$self->{"results"};} $done
Truth table0%0/3
A or B
A= @{$self->{"results"};}B= $done
ABresult
1X1
011
000
242 0 $self->log(1,"Asking our ",scalar(@prods)," producers to produce");
243 my @res = map {
244 0 $_->produce(%map)
245 } @prods;
246 0 $done = !@res;
247 0 $self->log(1,"Queue has ",scalar(@{$self->{results}})," pending items");
248 }
249 # while(!@{$self->{results}} && @prods) {
250 # my $prod = shift @prods;
251 # %map = %save;
252 # my @res = $prod->produce(%map);
253 # while(!@{$self->{results}} && @res && $res[0]) {
254 # %map = %save;
255 # @res = $prod->produce(%map);
256 # }
257 # }
258 0 ▶ if(@{$self->{results}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{'results'};}) { } 0 0 0
259 0 my $res = shift @{$self->{results}};
260 0 $self->log(3,"Produced ",$res->as_string," (forwarding)");
261 0 foreach my $c (@{$self->{consumers}}) {
262 0 $c->push($res);
263 }
264 0 $self->{abortIfEmpty} = 0;
265 0 return $res;
266 } else {
267 0 ▶ if(!$self->{abortIfEmpty}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'abortIfEmpty'}) { } 0 0 0
268 0 return ();
269 0 $self->{__started__} = 0;
270 0 $self->end();
271 } else {
272 0 die "No production for ".$self->{name}." with abort-if-empty marked yes\n";
273 }
274 }
275 }
276  
277  
278 =item C<< $mod->start() >>
279  
280 Called at before production has started
281  
282 =cut
283  
284 0 ▶sub start() {}
Subroutine start: not called CC=1 SCAR=0.0
Pod documented
285  
286 =item C<< $mod->end() >>
287  
288 Called when production has ended (no more producers have data)
289  
290 =cut
291  
292 0 ▶sub end() {}
Subroutine end: not called CC=1 SCAR=0.0
Pod documented
293  
294  
295 =item C<< $mod->handle($val) >>
296  
297 Called internally by push (ie. when data arrives).
298 When a value $val arrives, C<< $module->handle($val) >> is called
299 which produces C<@res>. The resulting values are stored onto the
300 stock;
301  
302 =cut
303  
304 0 ▶sub handle {
Subroutine handle: not called CC=1 SCAR=6.9
Pod documented
305 0 my $self = shift;
306 # my $class = ref($self);
307 # $self->log(1,"Warning : $class did not define a handler method");
308 0 return @_;
309 }
310  
311  
312 0 ▶sub print_state {
Subroutine print_state: not called CC=1 SCAR=6.9
Pod undocumented
313 0 my $self = shift;
314 0 print "--- State of module ",$self->{name}," ---\n";
315 0 print "In stock : ",join(",",@{$self->{results}}),"\n";
316 0 print "Producers : ", join(",",map { $_->{name} } @{$self->{producers}}),"\n";
317 0 print "Consumers : ", join(",",map { $_->{name} } @{$self->{consumers}}),"\n";
318 0 print "---\n";
319 }
320  
321 0 ▶sub as_string {
Subroutine as_string: not called CC=1 SCAR=6.9
Pod undocumented
322 0 my $self = shift;
323 0 my $name = $self->{name};
324 0 my $class = ref($self);
325 0 return "$class/$name";
326 }
327  
328 =head1 SEE ALSO
329  
330 WebSource::Fetch, WebSource::Extract, etc.
331  
332 =cut
333  
334 2 1;
« WebSource/Query.pm ↑ Summary WebSource/Envelope.pm »