blib/lib/Circle/Rule/Store.pm

filter: Statementstmt 30.7% 64 / 208 Branchbran 7.4% 4 / 54 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 40.4% 17 / 42 Podpod 0.0% 0 / 30 totaltotal 24.8% 85 / 342 SCAR 63.5
CC
36
Cov
25%
parse_cond
42.0
parse_action
42.0
get_cond
12.0
CRAP 572.6
« blib/lib/Circle/Loggable.pm ↑ Summary blib/lib/Circle/WindowItem.pm »
1 # You may distribute under the terms of the GNU General Public License
2 #
3 # (C) Paul Evans, 2008-2010 -- leonerd@leonerd.org.uk
4  
5 package Circle::Rule::Store;
6  
7 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 ▶use Circle::Rule::Chain;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Circle::Rule::Resultset;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 4 ▶use Text::Balanced qw( extract_bracketed );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 4 ▶use Attribute::Storage qw( get_subattrs );
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 #############################################
20 ### Attribute handlers for command_* subs ###
21 #############################################
22  
23 sub Rule_description :ATTR(CODE)
24 {
25 64 ▶ my $class = shift;
Subroutine Rule_description: called CC=1 SCAR=0.0
Pod undocumented
26 64 my ( $text ) = @_;
27  
28 64 return $text;
29 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 sub Rule_format :ATTR(CODE)
32 {
33 64 ▶ my $class = shift;
Subroutine Rule_format: called CC=1 SCAR=0.0
Pod undocumented
34 64 my ( $format ) = @_;
35  
36 64 return $format;
37 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 sub new
40 {
41 3 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
42 3 my %args = @_;
43  
44 my $self = bless {
45 cond => {},
46 action => {},
47  
48 parent => $args{parent},
49  
50 3 chains => {},
51 }, $class;
52  
53 3 $self->register_cond( not => $self );
54 3 $self->register_cond( any => $self );
55 3 $self->register_cond( all => $self );
56  
57 3 return $self;
58 }
59  
60 sub register_cond
61 {
62 12 ▶ my $self = shift;
Subroutine register_cond: called CC=4 SCAR=14.1
Pod undocumented
63 12 my ( $name, $obj ) = @_;
64  
65 12 ▶ croak "Already have a condition function called $name" if exists $self->{cond}->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"cond"}{$name} 0 12 12
66  
67 12 foreach my $method ( "parse_cond_$name", "deparse_cond_$name", "eval_cond_$name" ) {
68 36 ▶ eval { $obj->can( $method ) } or
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $obj->can($method) } } or croak("Expected that $obj can $method") 0 36 36
69 croak "Expected that $obj can $method";
70 }
71  
72 12 $self->{cond}->{$name} = { obj => $obj };
73 }
74  
75 sub list_conds
76 {
77 0 ▶ my $self = shift;
Subroutine list_conds: not called CC=2 SCAR=17.9
Pod undocumented
78 0 ▶ return ( keys %{ $self->{cond} } ),
Branch: 1 branch
BranchTrueFalseTotal
$self->{'parent'} ? : 0 0 0
79 0 ( $self->{parent} ? $self->{parent}->list_conds : () );
80 }
81  
82 sub get_cond
83 {
84 0 ▶ my $self = shift;
Subroutine get_cond: not called CC=3 SCAR=24.8
Pod undocumented
85 0 my ( $name ) = @_;
86  
87 0 ▶ return $self->{cond}->{$name} if $self->{cond}->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cond"}{$name} 0 0 0
88 0 ▶ return $self->{parent}->get_cond( $name ) if $self->{parent};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"parent"} 0 0 0
89  
90 0 die "No such condition '$name'\n";
91 }
92  
93 sub parse_cond
94 {
95 0 ▶ my $self = shift;
Subroutine parse_cond: not called CC=6 SCAR=37.4
Pod undocumented
96 # my ( $spec ) = @_ but we'll use $_[0] for alias
97  
98 0 ▶ $_[0] =~ s/^(\w+)\s*// or die "Expected a condition name\n";
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ s/^(\w+)\s*// or die "Expected a condition name\n" 0 0 0
99 0 my $condname = $1;
100  
101 0 my $cond = $self->get_cond( $condname );
102  
103 0 my $condspec;
104 0 ▶ if( $_[0] =~ m/^\(/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\(/) 0 0 0
105 0 $condspec = extract_bracketed( $_[0], q{("')} );
106 0 ▶ defined $condspec or die "Bad argument spec '$condspec' for condition $condname\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $condspec or die "Bad argument spec '${condspec}' for condition $condname\n" 0 0 0
107 0 s/^\(\s*//, s/\s*\)$// for $condspec;
108 }
109  
110 0 my $method = "parse_cond_$condname";
111  
112 0 my @condargs = eval { $cond->{obj}->$method( $condspec ) };
113 0 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
114 0 my $err = $@; chomp $err;
115 0 die "$err while parsing condition spec '$condspec' for $condname\n";
116 }
117  
118 0 return [ $condname, @condargs ];
119 }
120  
121 sub deparse_cond
122 {
123 0 ▶ my $self = shift;
Subroutine deparse_cond: not called CC=2 SCAR=17.9
Pod undocumented
124 0 my ( $condref ) = @_;
125  
126 0 my ( $name, @args ) = @$condref;
127  
128 0 my $cond = $self->get_cond( $name );
129  
130 0 my $method = "deparse_cond_$name";
131 0 my $argspec = $cond->{obj}->$method( @args );
132  
133 0 ▶ return defined $argspec ? "$name($argspec)" : $name;
Branch: 1 branch
BranchTrueFalseTotal
defined $argspec ? : 0 0 0
134 }
135  
136 sub eval_cond
137 {
138 0 ▶ my $self = shift;
Subroutine eval_cond: not called CC=1 SCAR=6.9
Pod undocumented
139 0 my ( $condref, $event, $results ) = @_;
140  
141 0 my ( $name, @args ) = @$condref;
142  
143 0 my $cond = $self->get_cond( $name );
144  
145 0 my $method = "eval_cond_$name";
146 0 return $cond->{obj}->$method( $event, $results, @args );
147 }
148  
149 sub describe_cond
150 {
151 0 ▶ my $self = shift;
Subroutine describe_cond: not called CC=1 SCAR=6.9
Pod undocumented
152 0 my ( $name ) = @_;
153  
154 0 my $cond = $self->get_cond( $name );
155  
156 0 my $attrs = get_subattrs( $cond->{obj}->can( "parse_cond_$name" ) );
157
158 return {
159 desc => $attrs->{Rule_description},
160 format => $attrs->{Rule_format},
161 0 };
162 }
163  
164 sub register_action
165 {
166 12 ▶ my $self = shift;
Subroutine register_action: called CC=4 SCAR=14.1
Pod undocumented
167 12 my ( $name, $obj ) = @_;
168  
169 12 ▶ croak "Already have a action function called $name" if exists $self->{action}->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"action"}{$name} 0 12 12
170  
171 12 foreach my $method ( "parse_action_$name", "deparse_action_$name", "eval_action_$name" ) {
172 36 ▶ eval { $obj->can( $method ) } or
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $obj->can($method) } } or croak("Expected that $obj can $method") 0 36 36
173 croak "Expected that $obj can $method";
174 }
175  
176 12 $self->{action}->{$name} = { obj => $obj };
177 }
178  
179 sub list_actions
180 {
181 0 ▶ my $self = shift;
Subroutine list_actions: not called CC=2 SCAR=17.9
Pod undocumented
182 0 ▶ return ( keys %{ $self->{action} } ),
Branch: 1 branch
BranchTrueFalseTotal
$self->{'parent'} ? : 0 0 0
183 0 ( $self->{parent} ? $self->{parent}->list_actions : () );
184 }
185  
186 sub get_action
187 {
188 0 ▶ my $self = shift;
Subroutine get_action: not called CC=3 SCAR=24.8
Pod undocumented
189 0 my ( $name ) = @_;
190  
191 0 ▶ return $self->{action}->{$name} if $self->{action}->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"action"}{$name} 0 0 0
192 0 ▶ return $self->{parent}->get_action( $name ) if $self->{parent};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"parent"} 0 0 0
193  
194 0 die "No such action '$name'\n";
195 }
196  
197 sub parse_action
198 {
199 0 ▶ my $self = shift;
Subroutine parse_action: not called CC=6 SCAR=37.4
Pod undocumented
200 # my ( $spec ) = @_ but we'll use $_[0] for alias
201  
202 0 ▶ $_[0] =~ s/^(\w+)\s*// or die "Expected an action name, found '$_[0]'\n";
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ s/^(\w+)\s*// or die "Expected an action name, found '$_[0]'\n" 0 0 0
203 0 my $actionname = $1;
204  
205 0 my $action = $self->get_action( $actionname );
206  
207 0 my $actionspec;
208 0 ▶ if( $_[0] =~ m/^\(/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\(/) 0 0 0
209 0 $actionspec = extract_bracketed( $_[0], q{("')} );
210 0 ▶ defined $actionspec or die "Bad argument spec '$actionspec' for action $actionname\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $actionspec or die "Bad argument spec '${actionspec}' for action $actionname\n" 0 0 0
211 0 s/^\(\s*//, s/\s*\)$// for $actionspec;
212 }
213  
214 0 my $method = "parse_action_$actionname";
215  
216 0 my @actionargs = eval { $action->{obj}->$method( $actionspec ) };
217 0 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
218 0 my $err = $@; chomp $err;
219 0 die "$err while parsing condition spec '$actionspec' for $actionname\n";
220 }
221  
222 0 return [ $actionname, @actionargs ];
223 }
224  
225 sub deparse_action
226 {
227 0 ▶ my $self = shift;
Subroutine deparse_action: not called CC=2 SCAR=17.9
Pod undocumented
228 0 my ( $actionref ) = @_;
229  
230 0 my ( $name, @args ) = @$actionref;
231  
232 0 my $action = $self->get_action( $name );
233  
234 0 my $method = "deparse_action_$name";
235 0 my $argspec = $action->{obj}->$method( @args );
236  
237 0 ▶ return defined $argspec ? "$name($argspec)" : $name;
Branch: 1 branch
BranchTrueFalseTotal
defined $argspec ? : 0 0 0
238 }
239  
240 sub eval_action
241 {
242 0 ▶ my $self = shift;
Subroutine eval_action: not called CC=1 SCAR=6.9
Pod undocumented
243 0 my ( $actionref, $event, $results ) = @_;
244  
245 0 my ( $name, @args ) = @$actionref;
246  
247 0 my $action = $self->get_action( $name );
248  
249 0 my $method = "eval_action_$name";
250 0 return $action->{obj}->$method( $event, $results, @args );
251 }
252  
253 sub describe_action
254 {
255 0 ▶ my $self = shift;
Subroutine describe_action: not called CC=1 SCAR=6.9
Pod undocumented
256 0 my ( $name ) = @_;
257  
258 0 my $action = $self->get_action( $name );
259  
260 0 my $attrs = get_subattrs( $action->{obj}->can( "parse_action_$name" ) );
261
262 return {
263 desc => $attrs->{Rule_description},
264 format => $attrs->{Rule_format},
265 0 };
266 }
267  
268 sub new_chain
269 {
270 0 ▶ my $self = shift;
Subroutine new_chain: not called CC=2 SCAR=17.9
Pod undocumented
271 0 my ( $name ) = @_;
272  
273 0 ▶ $self->{chains}->{$name} ||= Circle::Rule::Chain->new( $self );
Condition0%0/3
$self->{"chains"}{$name} ||= "Circle::Rule::Chain"->new($self)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"chains"}{$name} ||= "Circle::Rule::Chain"->new($self)
$self->{"chains"}{$name} "Circle::Rule::Chain"->new($self)
Truth table0%0/3
A ||= B
A= $self->{"chains"}{$name}B= "Circle::Rule::Chain"->new($self)
ABresult
1X1
011
000
274 }
275  
276 sub chains
277 {
278 0 ▶ my $self = shift;
Subroutine chains: not called CC=1 SCAR=6.9
Pod undocumented
279 0 return keys %{ $self->{chains} };
280 }
281  
282 sub get_chain
283 {
284 0 ▶ my $self = shift;
Subroutine get_chain: not called CC=2 SCAR=17.9
Pod undocumented
285 0 my ( $chainname ) = @_;
286  
287 0 ▶ return $self->{chains}->{$chainname} || die "No such rulechain called $chainname\n";
Condition0%0/2
$self->{'chains'}{$chainname} || die("No such rulechain called $chainname\n")
l!l
00
MC/DC0%0/1
$self->{'chains'}{$chainname} || die("No such rulechain called $chainname\n")
$self->{'chains'}{$chainname}
Truth table0%0/2
A
A= $self->{'chains'}{$chainname}
Aresult
11
00
288 }
289  
290 sub run
291 {
292 0 ▶ my $self = shift;
Subroutine run: not called CC=2 SCAR=17.9
Pod undocumented
293 0 my ( $chainname, $event ) = @_;
294  
295 0 ▶ my $chain = $self->{chains}->{$chainname} or die "No such rulechain called $chainname\n";
Branch: 1 branch
BranchTrueFalseTotal
my $chain = $self->{"chains"}{$chainname} or die "No such rulechain called $chainname\n" 0 0 0
296  
297 0 $chain->run( $event );
298 }
299  
300 # Internal rules for boolean logic
301  
302 sub parse_cond_not
303 : Rule_description("Invert the sense of a sub-condition")
304 : Rule_format('condition')
305 {
306 0 ▶ my $self = shift;
Subroutine parse_cond_not: not called CC=1 SCAR=6.9
Pod undocumented
307 0 my ( $spec ) = @_;
308  
309 0 return $self->parse_cond( $spec );
310 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
311  
312 sub deparse_cond_not
313 {
314 0 ▶ my $self = shift;
Subroutine deparse_cond_not: not called CC=1 SCAR=6.9
Pod undocumented
315 0 my ( $cond ) = @_;
316  
317 0 return $self->deparse_cond( $cond );
318 }
319  
320 sub eval_cond_not
321 {
322 0 ▶ my $self = shift;
Subroutine eval_cond_not: not called CC=1 SCAR=6.9
Pod undocumented
323 0 my ( $event, $results, $cond ) = @_;
324  
325 # Construct a new result set which we throw away
326 0 return not $self->eval_cond( $cond, $event, Circle::Rule::Resultset->new() );
327 }
328  
329 sub parse_cond_any
330 : Rule_description("Check if any sub-condition is true")
331 : Rule_format('condition ...')
332 {
333 0 ▶ my $self = shift;
Subroutine parse_cond_any: not called CC=3 SCAR=24.8
Pod undocumented
334 0 my ( $spec ) = @_;
335  
336 0 my @conds;
337 0 ▶ while( length $spec ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $spec) 0 0 0
338 0 push @conds, $self->parse_cond( $spec );
339  
340 0 $spec =~ s/\s+//; # trim ws
341 }
342  
343 0 ▶ @conds or die "Expected at least one condition\n";
Branch: 1 branch
BranchTrueFalseTotal
@conds or die "Expected at least one condition\n" 0 0 0
344  
345 0 return @conds;
346 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
347  
348 sub deparse_cond_any
349 {
350 0 ▶ my $self = shift;
Subroutine deparse_cond_any: not called CC=1 SCAR=6.9
Pod undocumented
351 0 my ( @conds ) = @_;
352  
353 0 return join( " ", map { $self->deparse_cond( $_ ) } @conds );
354 }
355  
356 sub eval_cond_any
357 {
358 0 ▶ my $self = shift;
Subroutine eval_cond_any: not called CC=3 SCAR=24.8
Pod undocumented
359 0 my ( $event, $results, @conds ) = @_;
360  
361 0 foreach my $cond ( @conds ) {
362 0 ▶ return 1 if $self->eval_cond( $cond, $event, $results );
Branch: 1 branch
BranchTrueFalseTotal
if $self->eval_cond($cond, $event, $results) 0 0 0
363 }
364  
365 0 return 0;
366 }
367  
368 sub parse_cond_all
369 : Rule_description("Check if all sub-conditions are true")
370 : Rule_format('condition ...')
371 {
372 0 ▶ my $self = shift;
Subroutine parse_cond_all: not called CC=3 SCAR=24.8
Pod undocumented
373 0 my ( $spec ) = @_;
374  
375 0 my @conds;
376 0 ▶ while( length $spec ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $spec) 0 0 0
377 0 push @conds, $self->parse_cond( $spec );
378  
379 0 $spec =~ s/\s+//; # trim ws
380 }
381  
382 0 ▶ @conds or die "Expected at least one condition\n";
Branch: 1 branch
BranchTrueFalseTotal
@conds or die "Expected at least one condition\n" 0 0 0
383  
384 0 return @conds;
385 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
386  
387 sub deparse_cond_all
388 {
389 0 ▶ my $self = shift;
Subroutine deparse_cond_all: not called CC=1 SCAR=6.9
Pod undocumented
390 0 my ( @conds ) = @_;
391  
392 0 return join( " ", map { $self->deparse_cond( $_ ) } @conds );
393 }
394  
395 sub eval_cond_all
396 {
397 0 ▶ my $self = shift;
Subroutine eval_cond_all: not called CC=3 SCAR=24.8
Pod undocumented
398 0 my ( $event, $results, @conds ) = @_;
399  
400 # Construct sub-results because we don't want any results to apply if a
401 # later failure causes us to fail after an earlier cond was successful and
402 # stored results
403 0 my $subresults = Circle::Rule::Resultset->new();
404  
405 0 foreach my $cond ( @conds ) {
406 0 ▶ return 0 unless $self->eval_cond( $cond, $event, $subresults );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->eval_cond($cond, $event, $subresults) 0 0 0
407 }
408  
409 0 $results->merge_from( $subresults );
410 0 return 1;
411 }
412  
413 4 0x55AA;
« blib/lib/Circle/Loggable.pm ↑ Summary blib/lib/Circle/WindowItem.pm »