ProgressMonitor/AbstractStatefulMonitor.pm

filter: Statementstmt 89.9% 125 / 139 Branchbran 45.0% 18 / 40 Conditioncond 37.5% 3 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 28 / 28 Podpod 85.7% 6 / 7 totaltotal 79.3% 180 / 227 CC 25 SCAR 17.0
CC
25
Cov
78%
setMessage
19.0
tick
8.8
end
6.3
CRAP 5.5
« ProgressMonitor/Stringify/Fields/Counter.pm ↑ Summary ProgressMonitor/Stringify/ToCallback.pm »
1 package ProgressMonitor::AbstractStatefulMonitor;
2
3 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5
6 10 ▶use ProgressMonitor::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use ProgressMonitor::State;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use ProgressMonitor::SetMessageFlags;
Subroutine BEGIN: called CC=1 SCAR=0.0
9
10 10 require ProgressMonitor if 0;
11
12 use classes
13 10 ▶ extends => 'ProgressMonitor',
Subroutine BEGIN: called CC=1 SCAR=0.0
14 class_methods => ['_new'],
15 methods => {render => 'ABSTRACT',},
16 attrs_pr => [
17 'cfg', 'canceled', 'state', 'totalTicks', 'ticks', 'multiplier',
18 'message', 'prepareMessage', 'beginMessage', 'tickMessage', 'endMessage',
19 ],
20 throws => ['X::ProgressMonitor::InvalidState', 'X::ProgressMonitor::TooManyTicks', 'X::ProgressMonitor::UnknownSetMessageFlag',],
21 10 ;
22
23 sub begin
24 12 ▶{
Subroutine begin: called CC=3 SCAR=11.5
Pod documented
25 12 my $self = shift;
26 12 my $totalTicks = shift;
27
28 # it is allowed to call begin directly if there's no preparatory
29 # work to be done - so we call prepare if not already done
30 #
31 12 my $state = $self->_get_state;
32 12 ▶ $self->prepare if $state == STATE_NEW;
Branch: 1 branch
BranchTrueFalseTotal
if $state == 0 0 12 12
33
34 # enter the active state, signalling 'prepare complete'
35 #
36 12 $self->__shiftState(STATE_PREPARING, STATE_ACTIVE);
37
38 # record the total ticks that can be expected
39 # (may be undef, signalling 'unknown')
40 #
41 12 $self->{$ATTR_totalTicks} = $totalTicks;
42
43 # check for the presence of a 'begin' message...
44 #
45 #
46 12 ▶ if ($self->{$ATTR_beginMessage})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{$ProgressMonitor::AbstractStatefulMonitor::ATTR_beginMessage}) 0 12 12
47 {
48 0 $self->_set_message($self->{$ATTR_beginMessage});
49 0 $self->{$ATTR_beginMessage} = undef;
50 }
51
52 # conclude with a rendering of this state
53 #
54 12 $self->render;
55
56 12 return;
57 }
58
59 sub end
60 12 ▶{
Subroutine end: called CC=5 SCAR=18.3
Pod documented
61 12 my $self = shift;
62
63 # it is allowed to call end *directly* if there's no work
64 # to be done at all - so we call begin if not already done
65 #
66 12 my $state = $self->_get_state;
67 12 ▶ $self->begin if $state == STATE_NEW || $state == STATE_PREPARING;
Branch: 1 branch
BranchTrueFalseTotal
if $state == 0 or $state == 1 0 12 12
Condition33%1/3
$state == 0 or $state == 1
l!l&&r!l&&!r
0012
MC/DC0%0/2
$state == 0 or $state == 1
$state == 0 $state == 1
Truth table33%1/3
A or B
A= $state == 0B= $state == 1
ABresult
1X1
011
000
68
69 # going to the end state from the active state
70 #
71 12 $self->__shiftState(STATE_ACTIVE, STATE_DONE);
72
73 # automatically complete the ticks, if requested
74 #
75 12 ▶ $self->{$ATTR_ticks} = $self->{$ATTR_totalTicks} if ($self->{$ATTR_cfg}->get_completeAtEnd);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{$ProgressMonitor::AbstractStatefulMonitor::ATTR_cfg}->get_completeAtEnd 12 0 12
76
77 # check for the presence of an 'end' message...
78 #
79 #
80 12 ▶ if ($self->{$ATTR_endMessage})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{$ProgressMonitor::AbstractStatefulMonitor::ATTR_endMessage}) 0 12 12
81 {
82 0 $self->_set_message($self->{$ATTR_endMessage});
83 0 $self->{$ATTR_endMessage} = undef;
84 }
85
86 # ensure a final rendering is performed
87 #
88
89 12 $self->render;
90
91 12 return;
92 }
93
94 sub isCanceled
95 411 ▶{
Subroutine isCanceled: called CC=1 SCAR=0.0
Pod documented
96 411 my $self = shift;
97
98 # return the cancellation state
99 #
100 411 return $self->{$ATTR_canceled};
101 }
102
103 sub prepare
104 12 ▶{
Subroutine prepare: called CC=2 SCAR=7.6
Pod documented
105 12 my $self = shift;
106
107 # this is the first state transition after creation - signal prep stage
108 #
109 12 $self->__shiftState(STATE_NEW, STATE_PREPARING);
110
111 # check for the presence of a 'prepare' message...
112 #
113 #
114 12 ▶ if ($self->{$ATTR_prepareMessage})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{$ProgressMonitor::AbstractStatefulMonitor::ATTR_prepareMessage}) 0 12 12
115 {
116 0 $self->_set_message($self->{$ATTR_prepareMessage});
117 0 $self->{$ATTR_prepareMessage} = undef;
118 }
119
120 # render this state
121 #
122 12 $self->render;
123
124 12 return;
125 }
126
127 sub setCanceled
128 411 ▶{
Subroutine setCanceled: called CC=2 SCAR=7.1
Pod documented
129 411 my $self = shift;
130
131 # set the cancellation status
132 # only advisory - the client must call isCanceled actively
133 #
134 411 ▶ $self->{$ATTR_canceled} = shift() ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
shift() ? : 0 411 411
135
136 411 return;
137 }
138
139 sub setMessage
140 200 ▶{
Subroutine setMessage: called CC=7 SCAR=29.4
Pod undocumented
141 200 my $self = shift;
142 200 my $msg = shift;
143 200 ▶ my $when = shift || SM_NOW;
Condition50%1/2
shift() || 0
l!l
0200
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
144
145 200 ▶ if ($when == SM_NOW)
Branch: 5 branches
BranchTrueFalseTotal
if ($when == 0) { } 200 0 200
elsif ($when == 1) { } 0 0 0
elsif ($when == 2) { } 0 0 0
elsif ($when == 3) { } 0 0 0
elsif ($when == 4) { } 0 0 0
146 {
147 200 $self->_set_message($msg);
148 200 $self->render;
149 }
150 elsif ($when == SM_PREPARE)
151 {
152 0 $self->{$ATTR_prepareMessage} = $msg;
153 }
154 elsif ($when == SM_BEGIN)
155 {
156 0 $self->{$ATTR_beginMessage} = $msg;
157 }
158 elsif ($when == SM_TICK)
159 {
160 0 $self->{$ATTR_tickMessage} = $msg;
161 }
162 elsif ($when == SM_END)
163 {
164 0 $self->{$ATTR_endMessage} = $msg;
165 }
166 else
167 {
168 0 X::ProgressMonitor::UnknownSetMessageFlag->throw("Unknown value: $when");
169 }
170
171 200 return;
172 }
173
174 sub tick
175 188 ▶{
Subroutine tick: called CC=7 SCAR=21.8
Pod documented
176 188 my $self = shift;
177 188 my $ticks = shift;
178
179 # this method can be called during either prep or active states
180 #
181 188 $self->__assertAnyState([STATE_PREPARING, STATE_ACTIVE]);
182
183 # STATE_PREPARING is implicitly 'unknown', thus any supplied ticks are
184 # ignored unless we're in the active state
185 #
186 188 ▶ if ($self->{$ATTR_state} == STATE_ACTIVE)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{$ProgressMonitor::AbstractStatefulMonitor::ATTR_state} == 2) 108 80 188
187 {
188 # ...but even in active state, there may have been 'unknown' indicated
189 #
190 108 ▶ if (defined($self->{$ATTR_totalTicks}))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{$ProgressMonitor::AbstractStatefulMonitor::ATTR_totalTicks}) { } 108 0 108
191 {
192 # to avoid silly rounding errors at the end we round the tick number down by a small margin
193 #
194 108 my $m = $self->{$ATTR_multiplier};
195 108 ▶ $self->{$ATTR_ticks} += (int($ticks * $m + 1) / $m) if ($ticks && $ticks >= 0);
Branch: 1 branch
BranchTrueFalseTotal
if $ticks and $ticks >= 0 108 0 108
Condition33%1/3
$ticks and $ticks >= 0
!ll&&!rl&&r
00108
MC/DC0%0/2
$ticks and $ticks >= 0
$ticks $ticks >= 0
Truth table33%1/3
A and B
A= $ticksB= $ticks >= 0
ABresult
0X0
100
111
196
197 # complain if we get too many ticks
198 #
199 X::ProgressMonitor::TooManyTicks->throw("$self->{$ATTR_ticks} exceeds $self->{$ATTR_totalTicks}")
200 108 ▶ if int($self->{$ATTR_ticks}) > int($self->{$ATTR_totalTicks});
Branch: 1 branch
BranchTrueFalseTotal
if int $self->{$ProgressMonitor::AbstractStatefulMonitor::ATTR_ticks} > int $self->{$ProgressMonitor::AbstractStatefulMonitor::ATTR_totalTicks} 0 108 108
201 }
202 else
203 {
204 # for 'unknown', we inc the ticks by one, the renderer may be interested in displaying the number of calls for example
205 #
206 0 $self->{$ATTR_ticks}++;
207 }
208 }
209
210 # check for the presence of a 'tick' message...
211 #
212 #
213 188 ▶ if ($self->{$ATTR_tickMessage})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{$ProgressMonitor::AbstractStatefulMonitor::ATTR_tickMessage}) 0 188 188
214 {
215 0 $self->_set_message($self->{$ATTR_tickMessage});
216 0 $self->{$ATTR_tickMessage} = undef;
217 }
218
219 # render is always called!
220 #
221 188 $self->render;
222
223 188 return;
224
225 }
226
227 ### protected
228
229 sub _get_cfg
230 1445 ▶{
Subroutine _get_cfg: called CC=1 SCAR=0.0
231 1445 my $self = shift;
232
233 1445 return $self->{$ATTR_cfg};
234 }
235
236 sub _get_state
237 435 ▶{
Subroutine _get_state: called CC=1 SCAR=0.0
238 435 my $self = shift;
239
240 435 return $self->{$ATTR_state};
241 }
242
243 sub _get_ticks
244 411 ▶{
Subroutine _get_ticks: called CC=1 SCAR=0.0
245 411 my $self = shift;
246
247 411 return $self->{$ATTR_ticks};
248 }
249
250 sub _get_totalTicks
251 411 ▶{
Subroutine _get_totalTicks: called CC=1 SCAR=0.0
252 411 my $self = shift;
253
254 411 return $self->{$ATTR_totalTicks};
255 }
256
257 sub _get_message
258 600 ▶{
Subroutine _get_message: called CC=1 SCAR=0.0
259 600 my $self = shift;
260
261 600 return $self->{$ATTR_message};
262 }
263
264 sub _set_message
265 220 ▶{
Subroutine _set_message: called CC=1 SCAR=0.0
266 220 my $self = shift;
267 220 my $msg = shift;
268
269 220 $self->{$ATTR_message} = $msg;
270 }
271
272 # the protected ctor
273 #
274 sub _new
275 12 ▶{
Subroutine _new: called CC=1 SCAR=0.0
276 12 my $self = classes::new_only(shift);
277 12 my $cfg = shift;
278 12 my $cfgPkg = shift;
279
280 # make sure we have a (populated) cfg object
281 #
282 12 $cfg = $self->{$ATTR_cfg} = ProgressMonitor::AbstractConfiguration::ensureCfgObject($cfg, $cfgPkg);
283
284 # initialize the rest
285 #
286 12 $self->{$ATTR_state} = STATE_NEW;
287 12 $self->{$ATTR_canceled} = 0;
288 12 $self->{$ATTR_ticks} = 0;
289 12 $self->{$ATTR_totalTicks} = undef;
290 12 $self->{$ATTR_multiplier} = 0 + ("1" . "0" x $cfg->get_resolution);
291 12 $self->{$ATTR_message} = undef;
292 12 $self->{$ATTR_prepareMessage} = undef;
293 12 $self->{$ATTR_beginMessage} = undef;
294 12 $self->{$ATTR_tickMessage} = undef;
295 12 $self->{$ATTR_endMessage} = undef;
296
297 12 return $self;
298 }
299
300 ### private
301
302 # assert that our state is the one expected
303 #
304 sub __assertState
305 36 ▶{
Subroutine __assertState: called CC=1 SCAR=0.0
306 36 my $self = shift;
307 36 my $state = shift;
308
309 36 $self->__assertAnyState([$state]);
310
311 36 return;
312 }
313
314 # assert that our state is any one of the provided
315 #
316 sub __assertAnyState
317 224 ▶{
Subroutine __assertAnyState: called CC=4 SCAR=13.9
318 224 my $self = shift;
319 224 my $states = shift;
320
321 224 my $match = 0;
322 224 ▶ $match += ($self->{$ATTR_state} == $_) ? 1 : 0 for (@$states);
Branch: 1 branch
BranchTrueFalseTotal
$self->{$ProgressMonitor::AbstractStatefulMonitor::ATTR_state} == $_ ? : 224 188 412
323 224 ▶ X::ProgressMonitor::InvalidState->throw($self->{$ATTR_state}) unless $match;
Branch: 1 branch
BranchTrueFalseTotal
unless $match 0 224 224
324
325 224 return;
326 }
327
328 # move from one state to another
329 #
330 sub __shiftState
331 36 ▶{
Subroutine __shiftState: called CC=1 SCAR=0.0
332 36 my $self = shift;
333 36 my $state = shift;
334 36 my $newState = shift;
335
336 36 $self->__assertState($state);
337 36 $self->{$ATTR_state} = $newState;
338
339 36 return;
340 }
341
342 ###
343
344 package ProgressMonitor::AbstractStatefulMonitorConfiguration;
345
346 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
347 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
348
349 # The configuration class for the above class
350 #
351 # resolution
352 # Allow the user to set the number of decimals when rounding.
353 # Unlikely to ever need changing...
354 #
355 use classes
356 10 ▶ extends => 'ProgressMonitor::AbstractConfiguration',
Subroutine BEGIN: called CC=1 SCAR=0.0
357 attrs => ['resolution', 'completeAtEnd'],
358 10 ;
359
360 sub defaultAttributeValues
361 12 ▶{
Subroutine defaultAttributeValues: called CC=1 SCAR=0.0
362 12 my $self = shift;
363
364 12 return {%{$self->SUPER::defaultAttributeValues()}, resolution => 8, completeAtEnd => 1};
365 }
366
367 sub checkAttributeValues
368 12 ▶{
Subroutine checkAttributeValues: called CC=2 SCAR=7.0
369 12 my $self = shift;
370
371 12 $self->SUPER::checkAttributeValues;
372
373 12 ▶ X::Usage->throw("resolution must be positive") if $self->get_resolution < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_resolution < 0 0 12 12
374
375 12 return;
376 }
377
378 ############################
379
380 =head1 NAME
381
382 ProgressMonitor::AbstractStatefulMonitor - a reusable/abstract monitor implementation
383 keeping track of state
384
385 =head1 SYNOPSIS
386
387 ...
388 use classes
389 extends => 'ProgressMonitor::AbstractStatefulMonitor',
390 new => 'new',
391 ...
392 ;
393
394 sub new
395 {
396 my $class = shift;
397 my $cfg = shift;
398
399 my $self = $class->SUPER::_new($cfg, $CLASS);
400
401 ...
402 }
403
404 sub render
405 {
406 my $self = shift;
407
408 ...
409 }
410
411 =head1 DESCRIPTION
412
413 This class implements the fully abstract ProgressMonitor interface and is what
414 generally should be used as a base. It deals with tracking the state changes
415 and cancellation and calls the subclass through 'render' at appropriate
416 times. It is strict and throws exceptions if misused.
417
418 When extended it provides several accessors for 'protected' data, i.e. only for
419 the use of subclasses. These accessors are prefixed with '_'.
420
421 Subclassing this normally entails only defining the render method.
422
423 See L<ProgressMonitor> for the general description of a progress monitor behavior
424 with regard to state etc.
425
426 Inherits from ProgressMonitor.
427
428 =head1 METHODS
429
430 =over 2
431
432 =item begin( $totalTicks )
433
434 Enters the active state from the preparing state, setting the total ticks that
435 should be reached, or use undef to indicate that the number of ticks is unknown.
436
437 Throws X::InvalidState for an incorrect calling sequence.
438
439 =item end
440
441 Enters the done state from the active state, and the monitor can then not be used again.
442
443 Throws X::InvalidState for an incorrect calling sequence.
444
445 =item isCanceled
446
447 Returns the cancellation flag.
448
449 =item prepare
450
451 Enters the preparing state from the new state, and the monitor can now be used
452 while the code is figuring out how many ticks it will need for the active state.
453
454 Throws X::ProgressMonitor::InvalidState for an incorrect calling sequence.
455
456 =item setCanceled( $boolean )
457
458 Sets the cancellation flag.
459
460 =item tick( $ticks )
461
462 Advances the tick count towards the total tick count (depending on if its is in
463 the preparing state or if the total is unknown).
464
465 Throws X::ProgressMonitor::TooManyTicks if the tick count exceeds the total.
466
467 =back
468
469 =head1 PROTECTED METHODS
470
471 =over 2
472
473 =item _new( $hashRef, $package )
474
475 The constructor, needs to be called by subclasses.
476
477 Configuration data:
478 resolution (default => 8)
479 Should not needed to be used. Makes sure to round the results down to the given size
480 decimals so as to avoid wacky floating point rounding errors when using inexact
481 floating point values in calculations (this happens when using subtasks).
482
483 completeAtEnd (default => 1)
484 Will automatically add any ticks not performed when end is called.
485
486 =item _get_cfg
487
488 Returns the configuration object.
489
490 =item _get_state
491
492 Returns the current state value.
493
494 =item _get_ticks
495
496 Returns the current tick value.
497
498 =item _get_totalTicks
499
500 Returns the total tick value.
501
502 =back
503
504 =head1 AUTHOR
505
506 Kenneth Olwing, C<< <knth at cpan.org> >>
507
508 =head1 BUGS
509
510 I wouldn't be surprised! If you can come up with a minimal test that shows the
511 problem I might be able to take a look. Even better, send me a patch.
512
513 Please report any bugs or feature requests to
514 C<bug-progressmonitor at rt.cpan.org>, or through the web interface at
515 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=ProgressMonitor>.
516 I will be notified, and then you'll automatically be notified of progress on
517 your bug as I make changes.
518
519 =head1 SUPPORT
520
521 You can find general documentation for this module with the perldoc command:
522
523 perldoc ProgressMonitor
524
525 =head1 ACKNOWLEDGEMENTS
526
527 Thanks to my family. I'm deeply grateful for you!
528
529 =head1 COPYRIGHT & LICENSE
530
531 Copyright 2006,2007 Kenneth Olwing, all rights reserved.
532
533 This program is free software; you can redistribute it and/or modify it
534 under the same terms as Perl itself.
535
536 =cut
537
538 10 1; # End of ProgressMonitor::AbstractStatefulMonitor
« ProgressMonitor/Stringify/Fields/Counter.pm ↑ Summary ProgressMonitor/Stringify/ToCallback.pm »