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