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