| 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::Chain; | ||||||||||||||||||||||||||||
| 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 Circle::Rule::Resultset; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | sub new | ||||||||||||||||||||||||||||
| 13 | { | ||||||||||||||||||||||||||||
| 14 | 0 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 15 | 0 | my ( $store ) = @_; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 0 | my $self = bless { | |||||||||||||||||||||||||||
| 18 | store => $store, | ||||||||||||||||||||||||||||
| 19 | rules => [], | ||||||||||||||||||||||||||||
| 20 | }, $class; | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 0 | return $self; | |||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | sub parse_rule | ||||||||||||||||||||||||||||
| 26 | { | ||||||||||||||||||||||||||||
| 27 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
parse_rule: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 28 | 0 | my ( $spec ) = @_; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 0 | my $store = $self->{store}; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 0 | my @conds; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 0 | ▶ | while( length $spec and $spec !~ m/^:/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
length $spec and not $spec =~ /^:/
MC/DC0%0/2
length $spec and not $spec =~ /^:/
length $spec
not $spec =~ /^:/
Truth table0%0/3
A and not(B)
A= length $specB= not $spec =~ /^:/
| |||||||||||||||||||||||||||||
| 35 | 0 | push @conds, $store->parse_cond( $spec ); | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 0 | $spec =~ s/^\s+//; # trim ws | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | $spec =~ s/^:\s*// or die "Expected ':' to separate condition and action\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 0 | my @actions; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | ▶ | while( length $spec ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 0 | push @actions, $store->parse_action( $spec ); | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | $spec =~ s/^\s+//; # trim ws | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | @actions or die "Expected at least one action\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | return [ \@conds, \@actions ]; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | sub append_rule | ||||||||||||||||||||||||||||
| 56 | { | ||||||||||||||||||||||||||||
| 57 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
append_rule: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 58 | 0 | my ( $spec ) = @_; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | push @{ $self->{rules} }, $self->parse_rule( $spec ); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | sub insert_rule | ||||||||||||||||||||||||||||
| 64 | { | ||||||||||||||||||||||||||||
| 65 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
insert_rule: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 66 | 0 | my ( $index, $spec ) = @_; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # TODO: Consider what happens if index is OOB | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | splice @{ $self->{rules} }, $index, 0, $self->parse_rule( $spec ); | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | sub delete_rule | ||||||||||||||||||||||||||||
| 74 | { | ||||||||||||||||||||||||||||
| 75 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
delete_rule: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 76 | 0 | my ( $index ) = @_; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | $index < @{ $self->{rules} } or die "No rule at index $index\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | splice @{ $self->{rules} }, $index, 1, (); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | sub clear | ||||||||||||||||||||||||||||
| 84 | { | ||||||||||||||||||||||||||||
| 85 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
clear: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | @{ $self->{rules} } = (); | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | sub deparse_rules | ||||||||||||||||||||||||||||
| 91 | { | ||||||||||||||||||||||||||||
| 92 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
deparse_rules: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | my $store = $self->{store}; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | my @ret; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | foreach my $rule ( @{ $self->{rules} } ) { | |||||||||||||||||||||||||||
| 99 | 0 | my ( $conds, $actions ) = @$rule; | |||||||||||||||||||||||||||
| 100 | 0 | push @ret, join( " ", map { $store->deparse_cond( $_ ) } @$conds ) . | |||||||||||||||||||||||||||
| 101 | ": " . | ||||||||||||||||||||||||||||
| 102 | 0 | join( " ", map { $store->deparse_action( $_ ) } @$actions ); | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | return @ret; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | sub run | ||||||||||||||||||||||||||||
| 109 | { | ||||||||||||||||||||||||||||
| 110 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
run: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 111 | 0 | my ( $event ) = @_; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | my $store = $self->{store}; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | RULE: foreach my $rule ( @{ $self->{rules} } ) { | |||||||||||||||||||||||||||
| 116 | 0 | my ( $conds, $actions ) = @$rule; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | my $results = Circle::Rule::Resultset->new(); | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | foreach my $cond ( @$conds ) { | |||||||||||||||||||||||||||
| 121 | 0 | ▶ | $store->eval_cond( $cond, $event, $results ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | or next RULE; | ||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | # We've got this far - run the actions | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | foreach my $action ( @$actions ) { | |||||||||||||||||||||||||||
| 128 | # TODO: Consider eval{} wrapping | ||||||||||||||||||||||||||||
| 129 | 0 | $store->eval_action( $action, $event, $results ); | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # All rules are independent - for now at least | ||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 4 | 0x55AA; | |||||||||||||||||||||||||||