File: Store.pm

package info (click to toggle)
libcircle-be-perl 0.173320-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 388 kB
  • sloc: perl: 6,042; makefile: 2; sh: 1
file content (415 lines) | stat: -rw-r--r-- 8,603 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#  You may distribute under the terms of the GNU General Public License
#
#  (C) Paul Evans, 2008-2010 -- leonerd@leonerd.org.uk

package Circle::Rule::Store;

use strict;
use warnings;

our $VERSION = '0.173320';

use Carp;

use Circle::Rule::Chain;
use Circle::Rule::Resultset;

use Text::Balanced qw( extract_bracketed );

use Attribute::Storage qw( get_subattrs );

#############################################
### Attribute handlers for command_* subs ###
#############################################

sub Rule_description :ATTR(CODE)
{
   my $class = shift;
   my ( $text ) = @_;

   return $text;
}

sub Rule_format :ATTR(CODE)
{
   my $class = shift;
   my ( $format ) = @_;

   return $format;
}

sub new
{
   my $class = shift;
   my %args = @_;

   my $self = bless {
      cond   => {},
      action => {},

      parent => $args{parent},

      chains => {},
   }, $class;

   $self->register_cond( not => $self );
   $self->register_cond( any => $self );
   $self->register_cond( all => $self );

   return $self;
}

sub register_cond
{
   my $self = shift;
   my ( $name, $obj ) = @_;

   croak "Already have a condition function called $name" if exists $self->{cond}->{$name};

   foreach my $method ( "parse_cond_$name", "deparse_cond_$name", "eval_cond_$name" ) {
      eval { $obj->can( $method ) } or 
         croak "Expected that $obj can $method";
   }

   $self->{cond}->{$name} = { obj => $obj };
}

sub list_conds
{
   my $self = shift;
   return ( keys %{ $self->{cond} } ),
          ( $self->{parent} ? $self->{parent}->list_conds : () );
}

sub get_cond
{
   my $self = shift;
   my ( $name ) = @_;

   return $self->{cond}->{$name} if $self->{cond}->{$name};
   return $self->{parent}->get_cond( $name ) if $self->{parent};

   die "No such condition '$name'\n";
}

sub parse_cond
{
   my $self = shift;
   # my ( $spec ) = @_ but we'll use $_[0] for alias

   $_[0] =~ s/^(\w+)\s*// or die "Expected a condition name\n";
   my $condname = $1;

   my $cond = $self->get_cond( $condname );

   my $condspec;
   if( $_[0] =~ m/^\(/ ) {
      $condspec = extract_bracketed( $_[0], q{("')} );
      defined $condspec or die "Bad argument spec '$condspec' for condition $condname\n";
      s/^\(\s*//, s/\s*\)$// for $condspec;
   }

   my $method = "parse_cond_$condname";

   my @condargs = eval { $cond->{obj}->$method( $condspec ) };
   if( $@ ) {
      my $err = $@; chomp $err;
      die "$err while parsing condition spec '$condspec' for $condname\n";
   }

   return [ $condname, @condargs ];
}

sub deparse_cond
{
   my $self = shift;
   my ( $condref ) = @_;

   my ( $name, @args ) = @$condref;

   my $cond = $self->get_cond( $name );

   my $method = "deparse_cond_$name";
   my $argspec = $cond->{obj}->$method( @args );

   return defined $argspec ? "$name($argspec)" : $name;
}

sub eval_cond
{
   my $self = shift;
   my ( $condref, $event, $results ) = @_;

   my ( $name, @args ) = @$condref;

   my $cond = $self->get_cond( $name );

   my $method = "eval_cond_$name";
   return $cond->{obj}->$method( $event, $results, @args );
}

sub describe_cond
{
   my $self = shift;
   my ( $name ) = @_;

   my $cond = $self->get_cond( $name );

   my $attrs = get_subattrs( $cond->{obj}->can( "parse_cond_$name" ) );
   
   return { 
      desc   => $attrs->{Rule_description},
      format => $attrs->{Rule_format},
   };
}

sub register_action
{
   my $self = shift;
   my ( $name, $obj ) = @_;

   croak "Already have a action function called $name" if exists $self->{action}->{$name};

   foreach my $method ( "parse_action_$name", "deparse_action_$name", "eval_action_$name" ) {
      eval { $obj->can( $method ) } or 
         croak "Expected that $obj can $method";
   }

   $self->{action}->{$name} = { obj => $obj };
}

sub list_actions
{
   my $self = shift;
   return ( keys %{ $self->{action} } ),
          ( $self->{parent} ? $self->{parent}->list_actions : () );
}

sub get_action
{
   my $self = shift;
   my ( $name ) = @_;

   return $self->{action}->{$name} if $self->{action}->{$name};
   return $self->{parent}->get_action( $name ) if $self->{parent};

   die "No such action '$name'\n";
}

sub parse_action
{
   my $self = shift;
   # my ( $spec ) = @_ but we'll use $_[0] for alias

   $_[0] =~ s/^(\w+)\s*// or die "Expected an action name, found '$_[0]'\n";
   my $actionname = $1;

   my $action = $self->get_action( $actionname );

   my $actionspec;
   if( $_[0] =~ m/^\(/ ) {
      $actionspec = extract_bracketed( $_[0], q{("')} );
      defined $actionspec or die "Bad argument spec '$actionspec' for action $actionname\n";
      s/^\(\s*//, s/\s*\)$// for $actionspec;
   }

   my $method = "parse_action_$actionname";

   my @actionargs = eval { $action->{obj}->$method( $actionspec ) };
   if( $@ ) {
      my $err = $@; chomp $err;
      die "$err while parsing condition spec '$actionspec' for $actionname\n";
   }

   return [ $actionname, @actionargs ];
}

sub deparse_action
{
   my $self = shift;
   my ( $actionref ) = @_;

   my ( $name, @args ) = @$actionref;

   my $action = $self->get_action( $name );

   my $method = "deparse_action_$name";
   my $argspec = $action->{obj}->$method( @args );

   return defined $argspec ? "$name($argspec)" : $name;
}

sub eval_action
{
   my $self = shift;
   my ( $actionref, $event, $results ) = @_;

   my ( $name, @args ) = @$actionref;

   my $action = $self->get_action( $name );

   my $method = "eval_action_$name";
   return $action->{obj}->$method( $event, $results, @args );
}

sub describe_action
{
   my $self = shift;
   my ( $name ) = @_;

   my $action = $self->get_action( $name );

   my $attrs = get_subattrs( $action->{obj}->can( "parse_action_$name" ) );
   
   return { 
      desc   => $attrs->{Rule_description},
      format => $attrs->{Rule_format},
   };
}

sub new_chain
{
   my $self = shift;
   my ( $name ) = @_;

   $self->{chains}->{$name} ||= Circle::Rule::Chain->new( $self );
}

sub chains
{
   my $self = shift;
   return keys %{ $self->{chains} };
}

sub get_chain
{
   my $self = shift;
   my ( $chainname ) = @_;

   return $self->{chains}->{$chainname} || die "No such rulechain called $chainname\n";
}

sub run
{
   my $self = shift;
   my ( $chainname, $event ) = @_;

   my $chain = $self->{chains}->{$chainname} or die "No such rulechain called $chainname\n";

   $chain->run( $event );
}

# Internal rules for boolean logic

sub parse_cond_not
   : Rule_description("Invert the sense of a sub-condition")
   : Rule_format('condition')
{
   my $self = shift;
   my ( $spec ) = @_;

   return $self->parse_cond( $spec );
}

sub deparse_cond_not
{
   my $self = shift;
   my ( $cond ) = @_;

   return $self->deparse_cond( $cond );
}

sub eval_cond_not
{
   my $self = shift;
   my ( $event, $results, $cond ) = @_;

   # Construct a new result set which we throw away
   return not $self->eval_cond( $cond, $event, Circle::Rule::Resultset->new() );
}

sub parse_cond_any
   : Rule_description("Check if any sub-condition is true")
   : Rule_format('condition ...')
{
   my $self = shift;
   my ( $spec ) = @_;

   my @conds;
   while( length $spec ) {
      push @conds, $self->parse_cond( $spec );

      $spec =~ s/\s+//; # trim ws
   }

   @conds or die "Expected at least one condition\n";

   return @conds;
}

sub deparse_cond_any
{
   my $self = shift;
   my ( @conds ) = @_;

   return join( " ", map { $self->deparse_cond( $_ ) } @conds );
}

sub eval_cond_any
{
   my $self = shift;
   my ( $event, $results, @conds ) = @_;

   foreach my $cond ( @conds ) {
      return 1 if $self->eval_cond( $cond, $event, $results );
   }

   return 0;
}

sub parse_cond_all
   : Rule_description("Check if all sub-conditions are true")
   : Rule_format('condition ...')
{
   my $self = shift;
   my ( $spec ) = @_;

   my @conds;
   while( length $spec ) {
      push @conds, $self->parse_cond( $spec );

      $spec =~ s/\s+//; # trim ws
   }

   @conds or die "Expected at least one condition\n";

   return @conds;
}

sub deparse_cond_all
{
   my $self = shift;
   my ( @conds ) = @_;

   return join( " ", map { $self->deparse_cond( $_ ) } @conds );
}

sub eval_cond_all
{
   my $self = shift;
   my ( $event, $results, @conds ) = @_;

   # Construct sub-results because we don't want any results to apply if a
   # later failure causes us to fail after an earlier cond was successful and
   # stored results
   my $subresults = Circle::Rule::Resultset->new();

   foreach my $cond ( @conds ) {
      return 0 unless $self->eval_cond( $cond, $event, $subresults );
   }

   $results->merge_from( $subresults );
   return 1;
}

0x55AA;