| 1 | package Lab::Moose::Sweep::Continuous::Time; | ||||||||||||||
| 2 | 2 | $Lab::Moose::Sweep::Continuous::Time::VERSION = '3.931'; | |||||||||||||
| 3 | #ABSTRACT: Time sweep | ||||||||||||||
| 4 | |||||||||||||||
| 5 | 2 | ▶ | use v5.20; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | |||||||||||||||
| 8 | 2 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 2 | ▶ | use Time::HiRes qw/time sleep/; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 2 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | |||||||||||||||
| 12 | 2 | extends 'Lab::Moose::Sweep::Continuous'; | |||||||||||||
| 13 | |||||||||||||||
| 14 | # | ||||||||||||||
| 15 | # Public attributes | ||||||||||||||
| 16 | # | ||||||||||||||
| 17 | |||||||||||||||
| 18 | 2 | has [qw/ +instrument +points +rates/] => ( required => 0 ); | |||||||||||||
| 19 | |||||||||||||||
| 20 | 2 | has duration => ( is => 'ro', isa => 'Lab::Moose::PosNum' ); | |||||||||||||
| 21 | |||||||||||||||
| 22 | 2 | has durations => ( | |||||||||||||
| 23 | is => 'ro', | ||||||||||||||
| 24 | isa => 'ArrayRef[Lab::Moose::PosNum]', | ||||||||||||||
| 25 | traits => ['Array'], | ||||||||||||||
| 26 | handles => { | ||||||||||||||
| 27 | get_duration => 'get', shift_durations => 'shift', | ||||||||||||||
| 28 | num_durations => 'count', durations_array => 'elements', | ||||||||||||||
| 29 | }, | ||||||||||||||
| 30 | writer => '_durations' | ||||||||||||||
| 31 | ); | ||||||||||||||
| 32 | |||||||||||||||
| 33 | # TODO: make duration optional => infinite | ||||||||||||||
| 34 | |||||||||||||||
| 35 | 1 | ▶ | sub BUILD { | ||||||||||||
|
Subroutine
BUILD: called CC=9 SCAR=29.5
Pod
undocumented
| |||||||||||||||
| 36 | 1 | my $self = shift; | |||||||||||||
| 37 | |||||||||||||||
| 38 | # Do not mess with intervals/durations attribute arrays. Make copies of them. | ||||||||||||||
| 39 | 1 | my @intervals; | |||||||||||||
| 40 | my @durations; | ||||||||||||||
| 41 | |||||||||||||||
| 42 | 1 | ▶ | if ( defined $self->interval ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 43 | 1 | ▶ | if ( defined $self->intervals ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 44 | 0 | croak "Use either interval or intervals attribute"; | |||||||||||||
| 45 | } | ||||||||||||||
| 46 | 1 | @intervals = ( $self->interval ); | |||||||||||||
| 47 | } | ||||||||||||||
| 48 | elsif ( defined $self->intervals ) { | ||||||||||||||
| 49 | 0 | @intervals = $self->intervals_array; | |||||||||||||
| 50 | } | ||||||||||||||
| 51 | else { | ||||||||||||||
| 52 | # default interval | ||||||||||||||
| 53 | 0 | @intervals = (0); | |||||||||||||
| 54 | } | ||||||||||||||
| 55 | |||||||||||||||
| 56 | 1 | ▶ | if ( defined $self->duration ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 57 | 1 | ▶ | if ( defined $self->durations ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 58 | 0 | croak "Use either duration or durations attribute"; | |||||||||||||
| 59 | } | ||||||||||||||
| 60 | 1 | @durations = ( $self->duration ); | |||||||||||||
| 61 | } | ||||||||||||||
| 62 | elsif ( defined $self->durations ) { | ||||||||||||||
| 63 | 0 | @durations = $self->durations_array; | |||||||||||||
| 64 | } | ||||||||||||||
| 65 | else { | ||||||||||||||
| 66 | 0 | croak "Missing mandatory duration or durations argument"; | |||||||||||||
| 67 | } | ||||||||||||||
| 68 | |||||||||||||||
| 69 | 1 | $self->_intervals( \@intervals ); | |||||||||||||
| 70 | 1 | $self->_durations( \@durations ); | |||||||||||||
| 71 | |||||||||||||||
| 72 | 1 | ▶ | if ( $self->num_intervals < 1 ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 73 | 0 | croak "need at least one interval"; | |||||||||||||
| 74 | } | ||||||||||||||
| 75 | |||||||||||||||
| 76 | 1 | ▶ | if ( $self->num_intervals != $self->num_durations ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 77 | 0 | croak "need same number of intervals and durations"; | |||||||||||||
| 78 | } | ||||||||||||||
| 79 | } | ||||||||||||||
| 80 | |||||||||||||||
| 81 | 1 | ▶ | sub go_to_sweep_start { | ||||||||||||
|
Subroutine
go_to_sweep_start: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 82 | 1 | my $self = shift; | |||||||||||||
| 83 | 1 | $self->reset_index(); | |||||||||||||
| 84 | 1 | $self->reset_points_index(); | |||||||||||||
| 85 | 1 | $self->inc_points_index(); | |||||||||||||
| 86 | } | ||||||||||||||
| 87 | |||||||||||||||
| 88 | 1 | ▶ | sub start_sweep { | ||||||||||||
|
Subroutine
start_sweep: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 89 | 1 | my $self = shift; | |||||||||||||
| 90 | 1 | $self->_start_time( time() ); | |||||||||||||
| 91 | } | ||||||||||||||
| 92 | |||||||||||||||
| 93 | 7 | ▶ | sub sweep_finished { | ||||||||||||
|
Subroutine
sweep_finished: called CC=3 SCAR=11.7
Pod
undocumented
| |||||||||||||||
| 94 | 7 | my $self = shift; | |||||||||||||
| 95 | |||||||||||||||
| 96 | 7 | my $duration = $self->get_duration( $self->points_index - 1 ); | |||||||||||||
| 97 | |||||||||||||||
| 98 | 7 | ▶ | if ( time() - $self->start_time < $duration ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 99 | |||||||||||||||
| 100 | # still in duration | ||||||||||||||
| 101 | 6 | return 0; | |||||||||||||
| 102 | } | ||||||||||||||
| 103 | |||||||||||||||
| 104 | # duration is finished. | ||||||||||||||
| 105 | 1 | $self->inc_points_index(); | |||||||||||||
| 106 | |||||||||||||||
| 107 | # Are there more durations? | ||||||||||||||
| 108 | 1 | ▶ | if ( $self->points_index < $self->num_durations() ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 109 | 0 | $self->reset_index(); | |||||||||||||
| 110 | 0 | $self->start_sweep(); | |||||||||||||
| 111 | 0 | return 0; | |||||||||||||
| 112 | } | ||||||||||||||
| 113 | else { | ||||||||||||||
| 114 | 1 | return 1; | |||||||||||||
| 115 | } | ||||||||||||||
| 116 | } | ||||||||||||||
| 117 | |||||||||||||||
| 118 | 0 | ▶ | sub get_value { | ||||||||||||
|
Subroutine
get_value: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 119 | 0 | my $self = shift; | |||||||||||||
| 120 | 0 | return time(); | |||||||||||||
| 121 | } | ||||||||||||||
| 122 | |||||||||||||||
| 123 | 2 | __PACKAGE__->meta->make_immutable(); | |||||||||||||
| 124 | 2 | 1; | |||||||||||||
| 125 | |||||||||||||||
| 126 | __END__ | ||||||||||||||