blib/lib/Pipeline.pm

filter: Statementstmt 96.0% 96 / 100 Branchbran 92.3% 24 / 26 Conditioncond 45.4% 5 / 11 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 95.6% 22 / 23 Podpod 100% 16 / 16 totaltotal 90.1% 164 / 182 CC 21 SCAR 10.5
CC
21
Cov
91%
dispatch
5.5
process_indv_result
5.0
process_results
4.0
CRAP 2.9
« blib/lib/Pipeline/Dumper.pm ↑ Summary blib/lib/Pipeline/Store/ISA.pm »
1 package Pipeline;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 11 ▶use Pipeline::Segment;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Pipeline::Dispatch;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Pipeline::Store::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use Scalar::Util qw( blessed weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use base qw( Pipeline::Segment );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 11 our $VERSION = "3.12";
13  
14 25 ▶sub init {
Subroutine init: called CC=2 SCAR=7.1
Pod documented
15 25 my $self = shift;
16 25 ▶ if ($self->SUPER::init( @_ )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->SUPER::init(@_)) { } 25 0 25
17 25 $self->debug( 0 );
18 25 $self->store( Pipeline::Store::Simple->new() );
19 25 $self->dispatcher( Pipeline::Dispatch->new() );
20 25 $self->segments( [] );
21 25 return $self;
22 } else {
23 0 return undef;
24 }
25 }
26  
27 24 ▶sub add_segment {
Subroutine add_segment: called CC=1 SCAR=0.0
Pod documented
28 24 my $self = shift;
29 24 $self->dispatcher->add( @_ );
30 24 $self;
31 }
32  
33 0 ▶sub get_segment {
Subroutine get_segment: not called CC=1 SCAR=6.9
Pod documented
34 0 my $self = shift;
35 0 my $idx = shift;
36 0 return $self->dispatcher()->get( $idx );
37 }
38  
39 1 ▶sub del_segment {
Subroutine del_segment: called CC=1 SCAR=0.0
Pod documented
40 1 my $self = shift;
41 1 my $idx = shift;
42 1 my $seg = $self->segments()->[ $idx ];
43 1 $self->dispatcher()->delete( $idx );
44 1 $seg;
45 }
46  
47 31 ▶sub segments {
Subroutine segments: called CC=1 SCAR=0.0
Pod documented
48 31 my $self = shift;
49 31 return $self->dispatcher()->segments( @_ );
50 }
51  
52 17 ▶sub dispatch {
Subroutine dispatch: called CC=5 SCAR=17.0
Pod documented
53 17 my $self = shift;
54  
55 17 my $result = $self->dispatch_loop();
56 17 my $cleanup_result = $self->cleanup;
57  
58 17 $self->dispatcher()->reset();
59  
60 17 ▶ if (blessed( $result )) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($result)) { } 4 13 17
61 4 ▶ return $result->isa('Pipeline::Production') ?
Branch: 1 branch
BranchTrueFalseTotal
$result->isa('Pipeline::Production') ? : 4 0 4
Condition0%0/2
$result or 1
l!l
00
MC/DC0%0/1
$result or 1
$result
Truth table0%0/2
A
A= $result
Aresult
11
00
62 $result->contents :
63 $result || 1;
64 } else {
65 13 ▶ return $result || 1;
Condition50%1/2
$result || 1
l!l
130
MC/DC0%0/1
$result || 1
$result
Truth table50%1/2
A
A= $result
Aresult
11
00
66 }
67 }
68  
69 23 ▶sub start_dispatch {
Subroutine start_dispatch: called CC=1 SCAR=0.0
Pod documented
70 23 my $self = shift;
71 23 $self->store->start_transaction;
72 }
73  
74 23 ▶sub end_dispatch {
Subroutine end_dispatch: called CC=1 SCAR=0.0
Pod documented
75 23 my $self = shift;
76 23 $self->store->end_transaction;
77 }
78  
79 20 ▶sub process_indv_result {
Subroutine process_indv_result: called CC=5 SCAR=16.1
Pod documented
80 20 my $self = shift;
81 20 my $thing = shift;
82 20 my $production = undef;
83 20 ▶ return $production unless blessed( $thing );
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($thing) 13 7 20
84 7 ▶ if ($thing->isa( 'Pipeline::Segment' )) {
Branch: 2 branches
BranchTrueFalseTotal
if ($thing->isa('Pipeline::Segment')) { } 1 6 7
elsif ($thing->isa('Pipeline::Production')) { } 4 2 6
85 1 $self->cleanups->add_segment( $thing );
86 } elsif ($thing->isa('Pipeline::Production')) {
87 4 $production = $thing;
88 4 $self->store->set( $thing->contents );
89 } else {
90 2 $self->store->set( $thing );
91 }
92 7 ▶ return $production || undef;
Condition100%2/2
$production || undef
l!l
43
MC/DC100%1/1
$production || undef
$production
Truth table100%2/2
A
A= $production
Aresult
11
00
93 }
94  
95 20 ▶sub process_results {
Subroutine process_results: called CC=4 SCAR=13.9
Pod documented
96 20 my $self = shift;
97 20 my $args = shift;
98 20 my $final;
99 20 foreach my $result ( @$args ) {
100 20 my $product = $self->process_indv_result( $result );
101 20 ▶ $final = $product if $product;
Branch: 1 branch
BranchTrueFalseTotal
if $product 4 16 20
102 }
103 20 ▶ return $final if $final;
Branch: 1 branch
BranchTrueFalseTotal
if $final 4 16 20
104 16 return undef;
105 }
106  
107 17 ▶sub dispatch_loop {
Subroutine dispatch_loop: called CC=3 SCAR=11.0
Pod documented
108 17 my $self = shift;
109  
110 ## turn on debugging for the dispatcher if we need to
111 17 $self->dispatcher->debug( $self->debug );
112  
113 17 ▶ while($self->dispatcher->segment_available) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dispatcher->segment_available) 20 13 33
114 20 my $unrefined = [ $self->dispatcher->next( $self ) ];
115 20 my $refined = $self->process_results( $unrefined );
116 20 ▶ if (defined( $refined )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $refined) 4 16 20
117 4 return $refined
118 }
119 }
120 13 return 1;
121 }
122  
123 ## be careful here
124 17 ▶sub cleanup {
Subroutine cleanup: called CC=3 SCAR=11.1
Pod documented
125 17 my $self = shift;
126 17 ▶ if ($self->{ cleanup_pipeline }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cleanup_pipeline"}) 2 15 17
127 return (
128 2 ▶ $self->{ cleanup_pipeline }->debug( $self->debug || 0 )
Condition50%1/2
$self->debug || 0
l!l
02
MC/DC0%0/1
$self->debug || 0
$self->debug
Truth table50%1/2
A
A= $self->debug
Aresult
11
00
129 ->parent( $self )
130 ->store( $self->store() )
131 ->dispatch()
132 );
133 }
134 # $self->end_dispatch();
135 }
136  
137 169 ▶sub dispatcher {
Subroutine dispatcher: called CC=2 SCAR=6.9
Pod documented
138 169 my $self = shift;
139 169 my $obj = shift;
140 169 ▶ if (defined( $obj )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $obj) { } 25 144 169
141 25 $self->{ dispatcher } = $obj;
142 25 return $self;
143 } else {
144 144 return $self->{ dispatcher };
145 }
146 }
147  
148 2 ▶sub cleanups {
Subroutine cleanups: called CC=2 SCAR=8.1
Pod documented
149 2 my $self = shift;
150 2 ▶ $self->{ cleanup_pipeline } ||= ref($self)->new();
Condition33%1/3
$self->{"cleanup_pipeline"} ||= ref($self)->new
l!l&&r!l&&!r
020
MC/DC0%0/2
$self->{"cleanup_pipeline"} ||= ref($self)->new
$self->{"cleanup_pipeline"} ref($self)->new
Truth table33%1/3
A ||= B
A= $self->{"cleanup_pipeline"}B= ref($self)->new
ABresult
1X1
011
000
151 }
152  
153 48 ▶sub debug {
Subroutine debug: called CC=1 SCAR=0.0
Pod documented
154 48 my $self = shift;
155 48 $self->SUPER::debug( @_ );
156 }
157  
158 2 ▶sub debug_all {
Subroutine debug_all: called CC=3 SCAR=11.0
Pod documented
159 2 my $self = shift;
160 2 my $debug = shift;
161 2 foreach my $segment (@{ $self->segments }) {
162 2 ▶ $segment->isa( 'Pipeline' )
Branch: 1 branch
BranchTrueFalseTotal
$segment->isa('Pipeline') ? : 1 1 2
163 ? $segment->debug_all( $debug )
164 : $segment->debug( $debug );
165 }
166  
167 2 $self->debug( $debug );
168 }
169  
170 11 1;
171  
172 =head1 NAME
173  
174 Pipeline - Generic pipeline interface
175  
176 =head1 SYNOPSIS
177  
178 use Pipeline;
179 my $pipeline = Pipeline->new();
180 $pipeline->add_segment( @segments );
181 $pipeline->dispatch();
182  
183 =head1 DESCRIPTION
184  
185 C<Pipelines> are a mechanism to process data. They are designed to
186 be plugged together to make fairly complex operations act in a
187 fairly straightforward manner, cleanly, and simply.
188  
189 =head1 USING THE PIPELINE MODULE
190  
191 The usage of the generic pipeline module is fairly simple. You
192 instantiate a Pipeline object by using the I<new()> constructor.
193  
194 Segments can be added to the pipeline with the add_segment method.
195  
196 The store that the Pipeline will use can be set by calling the
197 I<store()> method later on. If a store is not set by the time
198 a pipeline is executing then it will use a store of the type
199 C<Pipeline::Store::Simple>.
200  
201 To start the pipeline running call the I<dispatch()> method on your
202 Pipeline object.
203  
204 If a segment returns a Pipeline::Production object then the pipeline
205 will be terminated early and the production will be returned to the
206 user. Regardless of when the pipeline is terminated the pipeline's
207 cleanup pipeline is executed. Segments can be added to the cleanup
208 pipeline either explicitly by calling the cleanups method to get the
209 cleanup pipeline and then adding the segment, or implicitly by
210 returning a segment object from a segment.
211  
212 To see what is being dispatched within a pipeline dispatch set the
213 pipeline's debug_all value to true.
214  
215 =head2 INHERITANCE
216  
217 Pipelines are designed to be inherited from. The inheritance tree is
218 somewhat warped and should look a little like this:
219  
220 MySegment --> Pipeline::Segment <--- Pipeline
221  
222 In other words, everything is a pipeline segment.
223  
224 =head1 METHODS
225  
226 The Pipeline class inherits from the C<Pipeline::Segment> class and
227 therefore also has any additional methods that its superclass may have.
228  
229 =over 4
230  
231 =item init( @_ )
232  
233 Things to do at construction time. If you do override this, it will
234 often be fairly important that you call and check the value of
235 $self->SUPER::init(@_) to make sure that the setup is done correctly.
236 Returns itself on success, undef on failure. The constructor will
237 fail if you return a false value.
238  
239 =item add_segment( LIST )
240  
241 Adds a segment or segments to the pipeline. Returns itself.
242  
243 =item get_segment( INTEGER )
244  
245 Returns the segment located at the index specified by INTEGER
246  
247 =item del_segment( INTEGER )
248  
249 Deletes and returns the segment located at the index specified
250 by INTEGER
251  
252 =item process_results( ARRAYREF )
253  
254 Examines each result of a segment and calls process_indv_result with
255 each element of ARRAYREF. In the case that process_indv_result returns
256 a production then it is returned to the caller.
257  
258 =item process_indv_result( SCALAR )
259  
260 Examines a single result and does the appripriate thing with it (ie, if it
261 is an object it puts it into the store, if it is a production it returns
262 it to the caller, and if it is a simple value it gets thrown away. In
263 the case that a value is returned from process_indv_result the pipeline
264 should terminate.
265  
266 =item dispatch()
267  
268 Starts the pipeline execution. It calls process_results on anything
269 that a segment returns. The pipeline always returns the production
270 or true.
271  
272 =item dispatch_loop( Pipeline, [ ARRAYREF ] )
273  
274 The C<dispatch_loop> method performs the processing for the pipeline
275  
276 =item start_dispatch
277  
278 Prepares all elements of the pipeline to begin processing a segment.
279  
280 =item end_dispatch
281  
282 Cleans up all elements of the pipeline after processing a segment.
283  
284 =item dispatch_segment( Pipeline::Segment )
285  
286 The C<dispatch_segment> method handles the execution of an individual
287 segment object.
288  
289 =item dispatcher( [Pipeline::Dispatch] )
290  
291 The C<dispatcher> method gets and sets the pipeline dispatcher object
292 that will be used to traverse the pipeline.
293  
294 =item cleanups()
295  
296 Returns the cleanup pipeline. This is a pipeline in and of itself,
297 and all the methods you can call on a pipeline can also be called on
298 this.
299  
300 =item cleanup()
301  
302 Calls the dispatch method on the cleanup pipeline.
303  
304 =item segments( [ value ] )
305  
306 C<segments> gets and sets the value of the pipeline list. At
307 initialization this is set to an array reference.
308  
309 =item debug_all( value )
310  
311 Sets debug( value ) recursively for each segment in this pipeline.
312  
313 =back
314  
315 =head1 SEE ALSO
316  
317 C<Pipeline::Segment>, C<Pipeline::Store>, C<Pipeline::Store::Simple>
318 C<Pipeline::Production>, C<Pipeline::Dispatch>
319  
320 =head1 AUTHORS
321  
322 James A. Duncan <jduncan@fotango.com>
323 Leon Brocard <acme@astray.com>
324  
325 =head1 COPYRIGHT
326  
327 Copyright 2003 Fotango Ltd.
328 Licensed under the same terms as Perl itself.
329  
330 =cut
331  
« blib/lib/Pipeline/Dumper.pm ↑ Summary blib/lib/Pipeline/Store/ISA.pm »