Cascade.pm

filter: Statementstmt 80.9% 183 / 226 Branchbran 59.6% 74 / 124 Conditioncond 43.4% 40 / 92 MC/DCmcdc 18.3% 9 / 49 Subroutinesub 83.8% 26 / 31 Podpod 31.5% 6 / 19 totaltotal 62.4% 338 / 541 SCAR 60.6
CC
97
Cov
67%
value_ref_if_recomputed
59.5
__ANON__
20.0
target_actual_stamp
20.0
CRAP 429.2
↑ Summary Cascade/Rule.pm »
1 package CHI::Cascade;
2  
3 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 15 our $VERSION = 0.300_002;
7  
8 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 15 ▶use CHI::Cascade::Value ':state';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use CHI::Cascade::Rule;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 ▶use CHI::Cascade::Target;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 15 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 15 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 0 ▶sub min ($$) { $_[0] < $_[1] ? $_[0] : $_[1] }
Subroutine min: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] < $_[1] ? : 0 0 0
17  
18 sub new {
19 1 ▶ my ($class, %opts) = @_;
Subroutine new: called CC=3 SCAR=12.7
Pod documented
20  
21 1 ▶ my $self = bless {
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
22 %opts,
23 plain_targets => {},
24 qr_targets => [],
25 stats => { recompute => 0, run => 0, dependencies_lookup => 0 }
26  
27 }, ref($class) || $class;
28  
29 1 ▶ $self->{target_chi} ||= $self->{chi};
Condition33%1/3
$self->{"target_chi"} ||= $self->{"chi"}
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{"target_chi"} ||= $self->{"chi"}
$self->{"target_chi"} $self->{"chi"}
Truth table33%1/3
A ||= B
A= $self->{"target_chi"}B= $self->{"chi"}
ABresult
1X1
011
000
30  
31 1 $self;
32 }
33  
34 sub rule {
35 3 ▶ my ($self, %opts) = @_;
Subroutine rule: called CC=3 SCAR=11.2
Pod documented
36  
37 3 my $rule = CHI::Cascade::Rule->new( cascade => $self, %opts );
38  
39 3 ▶ if (ref($rule->{target}) eq 'Regexp') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $rule->{'target'} eq 'Regexp') { } 1 2 3
elsif (not ref $rule->{'target'}) { } 2 0 2
40 1 push @{ $self->{qr_targets} }, $rule;
41 }
42 elsif (! ref($rule->{target})) {
43 2 $self->{plain_targets}{$rule->{target}} = $rule;
44 }
45 else {
46 0 croak qq{The rule's target "$rule->{target}" is unknown type};
47 }
48 }
49  
50 sub target_computing {
51 29 ▶ my $trg_obj;
Subroutine target_computing: called CC=3 SCAR=11.1
Pod undocumented
52  
53 ( $trg_obj = $_[0]->{target_chi}->get("t:$_[1]") )
54 29 ▶ ? ( ( ${ $_[2] } = $trg_obj->ttl ), $trg_obj->locked ? 1 : 0 )
Branch: 2 branches
BranchTrueFalseTotal
($trg_obj = $_[0]{'target_chi'}->get("t:$_[1]")) ? : 25 4 29
$trg_obj->locked ? : 0 25 25
55 : 0;
56 }
57  
58 sub target_is_actual {
59 6 ▶ my ( $self, $target, $actual_term ) = @_;
Subroutine target_is_actual: called CC=2 SCAR=6.9
Pod undocumented
60  
61 6 my $trg_obj;
62  
63 6 ▶ ( $trg_obj = $self->{target_chi}->get("t:$target") )
Branch: 1 branch
BranchTrueFalseTotal
($trg_obj = $self->{'target_chi'}->get("t:$target")) ? : 5 1 6
64 ? $trg_obj->is_actual( $actual_term )
65 : 0;
66 }
67  
68 sub target_time {
69 40 ▶ my ($self, $target) = @_;
Subroutine target_time: called CC=2 SCAR=6.9
Pod undocumented
70  
71 40 my $trg_obj;
72  
73 40 ▶ return ( ( $trg_obj = $self->{target_chi}->get("t:$target") )
Branch: 1 branch
BranchTrueFalseTotal
($trg_obj = $self->{'target_chi'}->get("t:$target")) ? : 36 4 40
74 ? $trg_obj->time
75 : 0
76 );
77 }
78  
79 sub get_value {
80 11 ▶ my ($self, $target) = @_;
Subroutine get_value: called CC=2 SCAR=7.6
Pod undocumented
81  
82 11 my $value = $self->{chi}->get("v:$target");
83  
84 11 ▶ return $value->state( CASCADE_FROM_CACHE )
Branch: 1 branch
BranchTrueFalseTotal
if $value 11 0 11
85 if ($value);
86  
87 0 CHI::Cascade::Value->new( state => CASCADE_NO_CACHE );
88 }
89  
90 sub target_lock {
91 11 ▶ my ( $self, $rule ) = @_;
Subroutine target_lock: called CC=3 SCAR=11.0
Pod undocumented
92  
93 11 my $target = $rule->target;
94  
95 # If target is already locked - a return
96 return
97 11 ▶ if ( $self->target_locked( $rule ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->target_locked($rule) 3 8 11
98  
99 8 my $trg_obj;
100 $trg_obj = CHI::Cascade::Target->new
101 8 ▶ unless ( ( $trg_obj = $self->{target_chi}->get("t:$target") ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $trg_obj = $self->{"target_chi"}->get("t:$target") 4 4 8
102  
103 8 $trg_obj->lock;
104 8 $self->{target_chi}->set( "t:$target", $trg_obj, $rule->target_expires( $trg_obj ) );
105  
106 8 $rule->{run_instance}{target_locks}{$target} = 1;
107 }
108  
109 sub target_unlock {
110 8 ▶ my ( $self, $rule, $value ) = @_;
Subroutine target_unlock: called CC=6 SCAR=18.7
Pod undocumented
111  
112 8 my $target = $rule->target;
113  
114 8 ▶ if ( my $trg_obj = $self->{target_chi}->get( "t:$target" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $trg_obj = $self->{"target_chi"}->get("t:$target")) 8 0 8
115 8 $trg_obj->unlock;
116  
117 8 ▶ if ( $value && $value->state & CASCADE_RECOMPUTED ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value and $value->state & 32) 8 0 8
Condition33%1/3
$value and $value->state & 32
!ll&&!rl&&r
008
MC/DC0%0/2
$value and $value->state & 32
$value $value->state & 32
Truth table33%1/3
A and B
A= $valueB= $value->state & 32
ABresult
0X0
100
111
118 8 $trg_obj->touch;
119 $trg_obj->actual_stamp
120 8 ▶ if $rule->{run_instance}{run_opts}{actual_term} && $rule->{run_instance}{orig_target} eq $target;
Branch: 1 branch
BranchTrueFalseTotal
if $rule->{"run_instance"}{"run_opts"}{"actual_term"} and $rule->{"run_instance"}{"orig_target"} eq $target 3 5 8
Condition66%2/3
$rule->{"run_instance"}{"run_opts"}{"actual_term"} and $rule->{"run_instance"}{"orig_target"} eq $target
!ll&&!rl&&r
503
MC/DC50%1/2
$rule->{"run_instance"}{"run_opts"}{"actual_term"} and $rule->{"run_instance"}{"orig_target"} eq $target
$rule->{"run_instance"}{"run_opts"}{"actual_term"} $rule->{"run_instance"}{"orig_target"} eq $target
Truth table66%2/3
A and B
A= $rule->{"run_instance"}{"run_opts"}{"actual_term"}B= $rule->{"run_instance"}{"orig_target"} eq $target
ABresult
0X0
100
111
121 }
122  
123 8 $self->{target_chi}->set( "t:$target", $trg_obj, $rule->target_expires( $trg_obj ) );
124 }
125  
126 8 delete $rule->{run_instance}{target_locks}{$target};
127 }
128  
129 sub target_actual_stamp {
130 0 ▶ my ( $self, $rule, $value ) = @_;
Subroutine target_actual_stamp: not called CC=4 SCAR=30.0
Pod undocumented
131  
132 0 my $target = $rule->target;
133  
134 0 ▶ if ( $value && $value->state & CASCADE_ACTUAL_VALUE && ( my $trg_obj = $self->{target_chi}->get( "t:$target" ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value and $value->state & 16 and my $trg_obj = $self->{"target_chi"}->get("t:$target")) 0 0 0
Condition0%0/6
$value and $value->state & 16 and my $trg_obj = $self->{"target_chi"}->get("t:$target")
!ll&&!rl&&r
000
$value and $value->state & 16
!ll&&!rl&&r
000
MC/DC0%0/3
$value and $value->state & 16 and my $trg_obj = $self->{"target_chi"}->get("t:$target")
$value $value->state & 16 my $trg_obj = $self->{"target_chi"}->get("t:$target")
Truth table0%0/4
A and B and C
A= $valueB= $value->state & 16C= my $trg_obj = $self->{"target_chi"}->get("t:$target")
ABCresult
0XX0
10X0
1100
1111
135 0 $trg_obj->actual_stamp;
136 0 $self->{target_chi}->set( "t:$target", $trg_obj, $rule->target_expires( $trg_obj ) );
137 }
138 }
139  
140 sub target_start_ttl {
141 0 ▶ my ( $self, $rule, $start_time ) = @_;
Subroutine target_start_ttl: not called CC=2 SCAR=17.9
Pod undocumented
142  
143 0 my $target = $rule->target;
144  
145 0 ▶ if ( my $trg_obj = $self->{target_chi}->get( "t:$target" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $trg_obj = $self->{"target_chi"}->get("t:$target")) 0 0 0
146 0 $trg_obj->ttl( $rule->ttl, $start_time );
147 0 $self->{target_chi}->set( "t:$target", $trg_obj, $rule->target_expires( $trg_obj ) );
148 }
149 }
150  
151 sub target_remove {
152 0 ▶ my ($self, $target) = @_;
Subroutine target_remove: not called CC=1 SCAR=6.9
Pod documented
153  
154 0 $self->{target_chi}->remove("t:$target");
155 }
156  
157 sub touch {
158 2 ▶ my ( $self, $target ) = @_;
Subroutine touch: called CC=2 SCAR=7.0
Pod documented
159  
160 2 ▶ if ( my $trg_obj = $self->{target_chi}->get("t:$target") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $trg_obj = $self->{"target_chi"}->get("t:$target")) 2 0 2
161 2 $trg_obj->touch;
162 2 $self->{target_chi}->set( "t:$target", $trg_obj, $self->find( $target )->target_expires( $trg_obj ) );
163 }
164 }
165  
166 sub target_locked {
167 91 ▶ my ( $self, $rule ) = @_;
Subroutine target_locked: called CC=1 SCAR=0.0
Pod undocumented
168  
169 91 exists $rule->{run_instance}{target_locks}{ $rule->target };
170 }
171  
172 sub recompute {
173 8 ▶ my ( $self, $rule, $target, $dep_values) = @_;
Subroutine recompute: called CC=5 SCAR=17.6
Pod undocumented
174  
175 die CHI::Cascade::Value->new( state => CASCADE_DEFERRED )
176 8 ▶ if $rule->{run_instance}{run_opts}{defer};
Branch: 1 branch
BranchTrueFalseTotal
if $rule->{"run_instance"}{"run_opts"}{"defer"} 0 8 8
177  
178 8 my $ret = eval { $rule->{code}->( $rule, $target, $rule->{dep_values} = $dep_values ) };
179  
180 8 $self->{stats}{recompute}++;
181  
182 8 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 8 8
183 0 my $error = $@;
184 0 ▶ die( ( eval { $error->isa('CHI::Cascade::Value') } ) ? $error->thrown_from_code(1) : "CHI::Cascade: the target $target - error in the code: $error" );
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $error->isa('CHI::Cascade::Value') } } ? : 0 0 0
185 }
186  
187 8 my $value;
188  
189 # For performance a value should not expire in anyway (only target marker if need)
190 8 $self->{chi}->set( "v:$target", $value = CHI::Cascade::Value->new->value($ret), 'never' );
191  
192 8 $value->state( CASCADE_ACTUAL_VALUE | CASCADE_RECOMPUTED );
193  
194 $rule->{recomputed}->( $rule, $target, $value )
195 8 ▶ if ( ref $rule->{recomputed} eq 'CODE' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $rule->{"recomputed"} eq "CODE" 6 2 8
196  
197 8 return $value;
198 }
199  
200 sub value_ref_if_recomputed {
201 29 ▶ my ( $self, $rule, $target, $only_from_cache ) = @_;
Subroutine value_ref_if_recomputed: called CC=26 SCAR=40.9
Pod undocumented
202  
203 return undef
204 29 ▶ unless defined $rule;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rule 0 29 29
205  
206 29 my $run_instance = $rule->{run_instance};
207  
208 29 my @qr_params = $rule->qr_params;
209  
210 29 my ( $ret_state, $ttl, $should_be_recomputed ) = ( CASCADE_ACTUAL_VALUE );
211  
212 29 ▶ if ( $self->target_computing( $target, \$ttl ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->target_computing($target, \$ttl)) 0 29 29
213 # If we have any target as a being computed (dependencie/original)
214 # there is no need to compute anything - trying to return original target value
215 0 die CHI::Cascade::Value->new->state( CASCADE_COMPUTING );
216 }
217  
218 29 my ( %dep_values, $dep_name );
219  
220 29 ▶ if ( $only_from_cache ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($only_from_cache) 9 20 29
221  
222 # Trying to get value from cache
223 9 my $value = $self->get_value($target);
224  
225 9 ▶ return $value
Branch: 1 branch
BranchTrueFalseTotal
if $value->is_value 9 0 9
226 if $value->is_value;
227  
228 # If no in cache - we should recompute it again
229 0 $self->target_lock($rule);
230 }
231  
232 20 push @{ $run_instance->{target_stack} }, $target;
233  
234 20 my $ret = eval {
235 20 my $dep_target;
236  
237 my $catcher = sub {
238 17 ▶ my $sub = shift;
Subroutine __ANON__: called CC=6 SCAR=29.9
239  
240 17 my $ret = eval { $sub->() };
241  
242 17 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 17 17
243 0 my $exception = $@;
244  
245 $rule->{depends_catch}->( $rule, $exception, $dep_values{$dep_target}->[0], $dep_target )
246 if ( exists $rule->{depends_catch}
247 && ref $rule->{depends_catch} eq 'CODE'
248 0 ▶ && eval { $exception->isa('CHI::Cascade::Value') }
Branch: 1 branch
BranchTrueFalseTotal
if exists $rule->{"depends_catch"} and ref $rule->{"depends_catch"} eq "CODE" and eval { do { $exception->isa("CHI::Cascade::Value") } } and $exception->thrown_from_code 0 0 0
Condition0%0/9
exists $rule->{"depends_catch"} and ref $rule->{"depends_catch"} eq "CODE" and eval { do { $exception->isa("CHI::Cascade::Value") } } and $exception->thrown_from_code
!ll&&!rl&&r
000
exists $rule->{"depends_catch"} and ref $rule->{"depends_catch"} eq "CODE" and eval { do { $exception->isa("CHI::Cascade::Value") } }
!ll&&!rl&&r
000
exists $rule->{"depends_catch"} and ref $rule->{"depends_catch"} eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/4
exists $rule->{"depends_catch"} and ref $rule->{"depends_catch"} eq "CODE" and eval { do { $exception->isa("CHI::Cascade::Value") } } and $exception->thrown_from_code
exists $rule->{"depends_catch"} ref $rule->{"depends_catch"} eq "CODE" eval { do { $exception->isa("CHI::Cascade::Value") } } $exception->thrown_from_code
Truth table0%0/5
A and B and C and D
A= exists $rule->{"depends_catch"}B= ref $rule->{"depends_catch"} eq "CODE"C= eval { do { $exception->isa("CHI::Cascade::Value") } }D= $exception->thrown_from_code
ABCDresult
0XXX0
10XX0
110X0
11100
11111
249 && $exception->thrown_from_code );
250  
251 0 die $exception;
252 }
253  
254 17 return $ret;
255 20 };
256  
257 20 ▶ $self->target_lock($rule)
Branch: 1 branch
BranchTrueFalseTotal
unless $self->target_time($target) 4 16 20
258 if ! $self->target_time($target);
259  
260 20 $should_be_recomputed = $self->target_locked($rule);
261  
262 20 ▶ if ( defined $ttl && $ttl > 0 && ! $should_be_recomputed ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ttl and $ttl > 0 and not $should_be_recomputed) { } 0 20 20
Condition33%2/6
defined $ttl and $ttl > 0 and not $should_be_recomputed
!ll&&!rl&&r
2000
defined $ttl and $ttl > 0
!ll&&!rl&&r
2000
MC/DC0%0/3
defined $ttl and $ttl > 0 and not $should_be_recomputed
defined $ttl $ttl > 0 not $should_be_recomputed
Truth table25%1/4
A and B and not(C)
A= defined $ttlB= $ttl > 0C= not $should_be_recomputed
ABCresult
0XX0
10X0
1100
1111
263 0 $ret_state = CASCADE_TTL_INVOLVED;
264 0 $run_instance->{ttl} = $ttl;
265 }
266 else {
267 my (
268 20 ▶ $rule_ttl,
Branch: 1 branch
BranchTrueFalseTotal
$only_from_cache ? : 0 20 20
269 $circle_hash,
270 $start_time,
271 $min_start_time
272 ) = (
273 $rule->ttl,
274 $only_from_cache ? 'only_cache_chain' : 'chain'
275 );
276  
277 20 foreach my $depend (@{ $rule->depends }) {
278 11 ▶ $dep_target = ref($depend) eq 'CODE' ? $depend->( $rule, @qr_params ) : $depend;
Branch: 1 branch
BranchTrueFalseTotal
ref $depend eq 'CODE' ? : 0 11 11
279  
280 11 $dep_values{$dep_target}->[0] = $self->find( $dep_target, $rule->{run_instance} );
281  
282 0 die "Found circle dependencies (trace: " . join( '->', @{ $run_instance->{target_stack} }, $dep_target ) . ") - aborted!"
283 11 ▶ if ( exists $run_instance->{ $circle_hash }{$target}{$dep_target} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $run_instance->{$circle_hash}{$target}{$dep_target} 0 11 11
284  
285 11 $run_instance->{ $circle_hash }{$target}{$dep_target} = 1;
286  
287 $catcher->( sub {
288 11 ▶ if ( ! $only_from_cache
Subroutine __ANON__: called CC=12 SCAR=30.0
Branch: 1 branch
BranchTrueFalseTotal
if (not $only_from_cache and $start_time = (++$self->{"stats"}{"dependencies_lookup"}, ($dep_values{$dep_target}[1] = $self->value_ref_if_recomputed($dep_values{$dep_target}[0], $dep_target))->state & 32 && &Time::HiRes::time() || ($start_time = $self->target_time($dep_target)) > $self->target_time($target) && $start_time)) 7 4 11
Condition75%9/12
not $only_from_cache and $start_time = (++$self->{"stats"}{"dependencies_lookup"}, ($dep_values{$dep_target}[1] = $self->value_ref_if_recomputed($dep_values{$dep_target}[0], $dep_target))->state & 32 && &Time::HiRes::time() || ($start_time = $self->target_time($dep_target)) > $self->target_time($target) && $start_time)
!ll&&!rl&&r
065
($dep_values{$dep_target}[1] = $self->value_ref_if_recomputed($dep_values{$dep_target}[0], $dep_target))->state & 32 && &Time::HiRes::time() || ($start_time = $self->target_time($dep_target)) > $self->target_time($target) && $start_time
l!l&&r!l&&!r
164
($dep_values{$dep_target}[1] = $self->value_ref_if_recomputed($dep_values{$dep_target}[0], $dep_target))->state & 32 and &Time::HiRes::time()
!ll&&!rl&&r
1001
($start_time = $self->target_time($dep_target)) > $self->target_time($target) and $start_time
!ll&&!rl&&r
406
MC/DC50%1/2
not $only_from_cache and $start_time = (++$self->{"stats"}{"dependencies_lookup"}, ($dep_values{$dep_target}[1] = $self->value_ref_if_recomputed($dep_values{$dep_target}[0], $dep_target))->state & 32 && &Time::HiRes::time() || ($start_time = $self->target_time($dep_target)) > $self->target_time($target) && $start_time)
not $only_from_cache $start_time = (++$self->{"stats"}{"dependencies_lookup"}, ($dep_values{$dep_target}[1] = $self->value_ref_if_recomputed($dep_values{$dep_target}[0], $dep_target))->state & 32 && &Time::HiRes::time() || ($start_time = $self->target_time($dep_target)) > $self->target_time($target) && $start_time)
MC/DC50%2/4
($dep_values{$dep_target}[1] = $self->value_ref_if_recomputed($dep_values{$dep_target}[0], $dep_target))->state & 32 && &Time::HiRes::time() || ($start_time = $self->target_time($dep_target)) > $self->target_time($target) && $start_time
($dep_values{$dep_target}[1] = $self->value_ref_if_recomputed($dep_values{$dep_target}[0], $dep_target))->state & 32 &Time::HiRes::time() ($start_time = $self->target_time($dep_target)) > $self->target_time($target) $start_time
Truth table66%2/3
not(A) and B
A= not $only_from_cacheB= $start_time = (++$self->{"stats"}{"dependencies_lookup"}, ($dep_values{$dep_target}[1] = $self->value_ref_if_recomputed($dep_values{$dep_target}[0], $dep_target))->state & 32 && &Time::HiRes::time() || ($start_time = $self->target_time($dep_target)) > $self->target_time($target) && $start_time)
ABresult
0X0
100
111
Truth table42%3/7
A and B || C and D
A= ($dep_values{$dep_target}[1] = $self->value_ref_if_recomputed($dep_values{$dep_target}[0], $dep_target))->state & 32B= &Time::HiRes::time()C= ($start_time = $self->target_time($dep_target)) > $self->target_time($target)D= $start_time
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
289 && ( $start_time = ( $self->{stats}{dependencies_lookup}++,
290 ( $dep_values{$dep_target}->[1] = $self->value_ref_if_recomputed( $dep_values{$dep_target}->[0], $dep_target ) )->state & CASCADE_RECOMPUTED && Time::HiRes::time
291 || ( $start_time = $self->target_time($dep_target) ) > $self->target_time($target) && $start_time ) ) )
292 {
293 7 ▶ if ( ! $should_be_recomputed
Branch: 1 branch
BranchTrueFalseTotal
if (not $should_be_recomputed || defined $ttl and defined $rule_ttl and $rule_ttl > 0 and $start_time + $rule_ttl > &Time::HiRes::time()) { } 0 7 7
Condition50%6/12
not $should_be_recomputed || defined $ttl and defined $rule_ttl and $rule_ttl > 0 and $start_time + $rule_ttl > &Time::HiRes::time()
!ll&&!rl&&r
700
not $should_be_recomputed || defined $ttl and defined $rule_ttl and $rule_ttl > 0
!ll&&!rl&&r
700
not $should_be_recomputed || defined $ttl and defined $rule_ttl
!ll&&!rl&&r
340
$should_be_recomputed || defined $ttl
l!l&&r!l&&!r
304
MC/DC0%0/5
not $should_be_recomputed || defined $ttl and defined $rule_ttl and $rule_ttl > 0 and $start_time + $rule_ttl > &Time::HiRes::time()
$should_be_recomputed defined $ttl defined $rule_ttl $rule_ttl > 0 $start_time + $rule_ttl > &Time::HiRes::time()
Truth table33%2/6
not(A || B) and C and D and E
A= $should_be_recomputedB= defined $ttlC= defined $rule_ttlD= $rule_ttl > 0E= $start_time + $rule_ttl > &Time::HiRes::time()
ABCDEresult
1XXXX0
01XXX0
000XX0
0010X0
001100
001111
294 && ! defined $ttl
295 && defined $rule_ttl
296 && $rule_ttl > 0
297 && ( $start_time + $rule_ttl ) > Time::HiRes::time )
298 {
299 0 ▶ $min_start_time = defined $min_start_time ? min( $start_time, $min_start_time ) : $start_time;
Branch: 1 branch
BranchTrueFalseTotal
defined $min_start_time ? : 0 0 0
300 }
301 else {
302 7 $self->target_lock($rule);
303 }
304 }
305 11 } );
306  
307 11 delete $run_instance->{ $circle_hash }{$target}{$dep_target};
308 }
309  
310 20 ▶ if ( defined $min_start_time ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $min_start_time) 0 20 20
311 0 $ret_state = CASCADE_TTL_INVOLVED;
312 0 $self->target_start_ttl( $rule, $min_start_time );
313 0 $run_instance->{ttl} = $min_start_time + $rule_ttl - Time::HiRes::time;
314 }
315 }
316  
317 20 ▶ if ( $self->target_locked($rule) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->target_locked($rule)) 8 12 20
318 # We should recompute this target
319 # So we should recompute values for other dependencies
320 8 foreach $dep_target (keys %dep_values) {
321 7 ▶ if ( ! defined $dep_values{$dep_target}->[1]
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dep_values{$dep_target}[1] and $dep_values{$dep_target}[1]->is_value) 6 1 7
Condition66%2/3
defined $dep_values{$dep_target}[1] and $dep_values{$dep_target}[1]->is_value
!ll&&!rl&&r
061
MC/DC50%1/2
defined $dep_values{$dep_target}[1] and $dep_values{$dep_target}[1]->is_value
defined $dep_values{$dep_target}[1] $dep_values{$dep_target}[1]->is_value
Truth table66%2/3
A and B
A= defined $dep_values{$dep_target}[1]B= $dep_values{$dep_target}[1]->is_value
ABresult
0X0
100
111
322 || ! $dep_values{$dep_target}->[1]->is_value )
323 {
324 6 $self->{stats}{dependencies_lookup}++;
325 $catcher->( sub {
326 6 ▶ if ( ! ( $dep_values{$dep_target}->[1] = $self->value_ref_if_recomputed( $dep_values{$dep_target}->[0], $dep_target, 1 ) )->is_value ) {
Subroutine __ANON__: called CC=2 SCAR=9.2
Branch: 1 branch
BranchTrueFalseTotal
unless (($dep_values{$dep_target}[1] = $self->value_ref_if_recomputed($dep_values{$dep_target}[0], $dep_target, 1))->is_value) 0 6 6
327 0 $self->target_remove($dep_target);
328 0 return 1;
329 }
330 6 return 0;
331 6 ▶ } ) == 1 && return undef;
Branch: 1 branch
BranchTrueFalseTotal
&$catcher(sub { unless (($dep_values{$dep_target}[1] = $self->value_ref_if_recomputed($dep_values{$dep_target}[0], $dep_target, 1))->is_value) { $self->target_remove($dep_target); return 1; } ; return 0; } ) == 1 and return undef 0 6 6
332 }
333 }
334 }
335  
336 20 ▶ return $self->recompute( $rule, $target, { map { $_ => $dep_values{$_}->[1]->value } keys %dep_values } )
Branch: 1 branch
BranchTrueFalseTotal
if $self->target_locked($rule) 8 12 20
337 if $self->target_locked($rule);
338  
339 12 return CHI::Cascade::Value->new( state => $ret_state );
340 };
341  
342 20 pop @{ $run_instance->{target_stack} };
343  
344 20 my $e = $@;
345  
346 20 ▶ if ( $self->target_locked($rule) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->target_locked($rule)) { } 8 12 20
elsif ($run_instance->{'run_opts'}{'actual_term'} and not $only_from_cache and $run_instance->{'orig_target'} eq $target) { } 0 19 19
347 8 $self->target_unlock( $rule, $ret );
348 }
349 ▶ elsif ( $run_instance->{run_opts}{actual_term} && ! $only_from_cache && $run_instance->{orig_target} eq $target ) {
Condition66%4/6
$run_instance->{"run_opts"}{"actual_term"} and not $only_from_cache and $run_instance->{"orig_target"} eq $target
!ll&&!rl&&r
750
$run_instance->{"run_opts"}{"actual_term"} and not $only_from_cache
!ll&&!rl&&r
705
MC/DC0%0/3
$run_instance->{"run_opts"}{"actual_term"} and not $only_from_cache and $run_instance->{"orig_target"} eq $target
$run_instance->{"run_opts"}{"actual_term"} not $only_from_cache $run_instance->{"orig_target"} eq $target
Truth table50%2/4
A and not(B) and C
A= $run_instance->{"run_opts"}{"actual_term"}B= not $only_from_cacheC= $run_instance->{"orig_target"} eq $target
ABCresult
0XX0
10X0
1100
1111
350 0 $self->target_actual_stamp( $rule, $ret );
351 }
352  
353 20 ▶ die $e if $e;
Branch: 1 branch
BranchTrueFalseTotal
if $e 0 20 20
354  
355 20 ▶ return $ret || CHI::Cascade::Value->new;
Condition33%1/3
$ret || 'CHI::Cascade::Value'->new
l!l&&r!l&&!r
2000
MC/DC0%0/2
$ret || 'CHI::Cascade::Value'->new
$ret 'CHI::Cascade::Value'->new
Truth table33%1/3
A || B
A= $retB= 'CHI::Cascade::Value'->new
ABresult
1X1
011
000
356 }
357  
358 0 ▶sub stash { exists $_[0]->{stash} && $_[0]->{stash} || die "The stash method from outside run method!" }
Subroutine stash: not called CC=3 SCAR=24.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
exists $_[0]{"stash"} and $_[0]{"stash"} or die "The stash method from outside run method!" 0 0 0
Condition0%0/3
exists $_[0]{"stash"} and $_[0]{"stash"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $_[0]{"stash"} and $_[0]{"stash"}
exists $_[0]{"stash"} $_[0]{"stash"}
Truth table0%0/3
A and B
A= exists $_[0]{"stash"}B= $_[0]{"stash"}
ABresult
0X0
100
111
359  
360 sub run {
361 12 ▶ my ( $self, $target, %opts ) = @_;
Subroutine run: called CC=12 SCAR=26.3
Pod documented
362  
363 12 my $view_dependencies = 1;
364  
365 # The run's instance parameters. This instance to be needed to give ability to run the run method from deep inside run calls.
366 # It's for internal using.
367 12 my $run_instance = {};
368  
369 12 $run_instance->{run_opts} = \%opts;
370 12 $run_instance->{ttl} = undef;
371 12 $run_instance->{target_locks} = {};
372  
373 12 ▶ $opts{actual_term} ||= $self->find($target)->{actual_term};
Condition100%3/3
$opts{"actual_term"} ||= $self->find($target)->{"actual_term"}
l!l&&r!l&&!r
336
MC/DC100%2/2
$opts{"actual_term"} ||= $self->find($target)->{"actual_term"}
$opts{"actual_term"} $self->find($target)->{"actual_term"}
Truth table100%3/3
A ||= B
A= $opts{"actual_term"}B= $self->find($target)->{"actual_term"}
ABresult
1X1
011
000
374 12 $self->{stats}{run}++;
375  
376 12 ▶ $run_instance->{stash} = $opts{stash} && ref $opts{stash} eq 'HASH' && $opts{stash} || {};
Condition37%3/8
$opts{'stash'} && ref $opts{'stash'} eq 'HASH' && $opts{'stash'} || {}
l!l
012
$opts{"stash"} and ref $opts{"stash"} eq "HASH" and $opts{"stash"}
!ll&&!rl&&r
1200
$opts{"stash"} and ref $opts{"stash"} eq "HASH"
!ll&&!rl&&r
1200
MC/DC0%0/3
$opts{'stash'} && ref $opts{'stash'} eq 'HASH' && $opts{'stash'} || {}
$opts{"stash"} ref $opts{"stash"} eq "HASH" $opts{"stash"}
Truth table25%1/4
A and B and C
A= $opts{"stash"}B= ref $opts{"stash"} eq "HASH"C= $opts{"stash"}
ABCresult
1111
0XX0
10X0
1100
377  
378 # FIXME to delete in future. Now it's depricated
379 12 $self->{stash} = $run_instance->{stash};
380  
381 $view_dependencies = ! $self->target_is_actual( $target, $opts{actual_term} )
382 12 ▶ if ( $opts{actual_term} );
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"actual_term"} 6 6 12
383  
384 12 my $res = $self->_run( ! $view_dependencies, $target, $run_instance );
385  
386 $res->state( CASCADE_ACTUAL_TERM )
387 12 ▶ if ( $opts{actual_term} && ! $view_dependencies );
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"actual_term"} and not $view_dependencies 3 9 12
Condition100%3/3
$opts{"actual_term"} and not $view_dependencies
!ll&&!rl&&r
633
MC/DC100%2/2
$opts{"actual_term"} and not $view_dependencies
$opts{"actual_term"} not $view_dependencies
Truth table100%3/3
A and not(B)
A= $opts{"actual_term"}B= not $view_dependencies
ABresult
0X0
100
111
388  
389 12 ▶ if ( defined $run_instance->{ttl} && $run_instance->{ttl} > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $run_instance->{"ttl"} and $run_instance->{"ttl"} > 0) 0 12 12
Condition33%1/3
defined $run_instance->{"ttl"} and $run_instance->{"ttl"} > 0
!ll&&!rl&&r
1200
MC/DC0%0/2
defined $run_instance->{"ttl"} and $run_instance->{"ttl"} > 0
defined $run_instance->{"ttl"} $run_instance->{"ttl"} > 0
Truth table33%1/3
A and B
A= defined $run_instance->{"ttl"}B= $run_instance->{"ttl"} > 0
ABresult
0X0
100
111
390 0 $res->state( CASCADE_TTL_INVOLVED );
391 }
392  
393 0 ${ $opts{ttl} } = $run_instance->{ttl}
394 12 ▶ if ( $opts{ttl} );
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"ttl"} 0 12 12
395  
396 6 ${ $opts{state} } = $res->state
397 12 ▶ if ( $opts{state} );
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"state"} 6 6 12
398  
399 12 $res->value;
400 }
401  
402 sub _run {
403 12 ▶ my ( $self, $only_from_cache, $target, $run_instance ) = @_;
Subroutine _run: called CC=10 SCAR=29.1
404  
405 12 ▶ croak qq{The target ($target) for run should be string} if ref($target);
Branch: 1 branch
BranchTrueFalseTotal
if ref $target 0 12 12
406 12 ▶ croak qq{The target for run is empty} if $target eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $target eq "" 0 12 12
407  
408 12 $run_instance->{chain} = {};
409 12 $run_instance->{only_cache_chain} = {};
410 12 $run_instance->{target_stack} = [];
411  
412 12 my $ret = eval {
413 12 $run_instance->{orig_target} = $target;
414  
415 12 return $self->value_ref_if_recomputed( $run_instance->{orig_rule} = $self->find( $target, $run_instance ), $target, $only_from_cache );
416 };
417  
418 12 my $terminated;
419  
420 12 ▶ if ( $terminated = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($terminated = $@) 0 12 12
421 0 $ret = $@;
422  
423 die $ret
424 0 ▶ unless eval { $ret->isa('CHI::Cascade::Value') };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $ret->isa("CHI::Cascade::Value") } } 0 0 0
425  
426 0 ▶ $ret->state( CASCADE_CODE_EXCEPTION )
Branch: 1 branch
BranchTrueFalseTotal
unless $ret->state 0 0 0
427 unless $ret->state;
428 }
429  
430 12 ▶ if ( ! $ret->is_value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret->is_value) 2 10 12
431 2 my $from_cache = $self->get_value( $target );
432  
433 2 ▶ return $from_cache->state( $ret->state )
Branch: 1 branch
BranchTrueFalseTotal
if $terminated or $from_cache->is_value 2 0 2
Condition33%1/3
$terminated or $from_cache->is_value
l!l&&r!l&&!r
020
MC/DC0%0/2
$terminated or $from_cache->is_value
$terminated $from_cache->is_value
Truth table33%1/3
A or B
A= $terminatedB= $from_cache->is_value
ABresult
1X1
011
000
434 if ( $terminated || $from_cache->is_value );
435  
436 0 ▶ return $self->_run( 1, $target )
Branch: 1 branch
BranchTrueFalseTotal
unless $only_from_cache 0 0 0
437 if ! $only_from_cache;
438 }
439  
440 10 return $ret;
441 }
442  
443 sub find {
444 34 ▶ my ($self, $plain_target, $run_instance) = @_;
Subroutine find: called CC=7 SCAR=19.6
Pod undocumented
445  
446 34 ▶ die "CHI::Cascade::find : got empty target\n" if $plain_target eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $plain_target eq "" 0 34 34
447  
448 34 my $new_rule;
449  
450 # If target is flat text
451 34 ▶ if (exists $self->{plain_targets}{$plain_target}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"plain_targets"}{$plain_target}) 17 17 34
452 17 ▶ return $self->{plain_targets}{$plain_target}
Branch: 1 branch
BranchTrueFalseTotal
unless $run_instance 5 12 17
453 unless $run_instance;
454 12 ( $new_rule = $self->{plain_targets}{$plain_target}->new( run_instance => $run_instance ) )->{matched_target} = $plain_target;
455 12 return $new_rule;
456 }
457  
458 # If rule's target is Regexp type
459 17 foreach my $rule (@{$self->{qr_targets}}) {
460 17 my @qr_params;
461  
462 17 ▶ if (@qr_params = $plain_target =~ $rule->{target}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@qr_params = $plain_target =~ /$rule->{"target"}/) 17 0 17
463 17 ▶ return $rule
Branch: 1 branch
BranchTrueFalseTotal
unless $run_instance 6 11 17
464 unless $run_instance;
465 11 ( $new_rule = $rule->new( run_instance => $run_instance ) )->qr_params(@qr_params);
466 11 $new_rule->{matched_target} = $plain_target;
467 11 return $new_rule;
468 }
469 }
470  
471 0 die "CHI::Cascade::find : cannot find the target $plain_target\n";
472 }
473  
474  
475 15 1;
476 __END__
↑ Summary Cascade/Rule.pm »