Circle/Rule/Store.pm

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