RuleEngine/Runloop.pm

filter: Statementstmt 99.2% 129 / 130 Branchbran 84.3% 27 / 32 Conditioncond 88.8% 8 / 9 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 100% 18 / 18 Podpod 100% 5 / 5 totaltotal 95.5% 191 / 200 CC 27 SCAR 19.1
CC
27
Cov
96%
__ANON__
15.0
add_engine
5.0
step
4.0
CRAP 6.8
« RuleEngine/Declare.pm ↑ Summary RuleEngine/Rule/InputUnderThreshold.pm »
1 package VS::RuleEngine::Runloop;
2  
3 33 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 33 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 33 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 33 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 33 ▶use Scalar::Util qw(refaddr blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 33 ▶use VS::RuleEngine::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 33 my %Engine;
13 my %Runloop;
14 33 my %Initialized;
15  
16 25 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
17 25 my ($pkg) = @_;
18 25 my $self = bless \do { my $v; }, $pkg;
19 25 $$self = refaddr $self;
20  
21 25 $Engine{$$self} = [];
22 25 $Runloop{$$self} = [];
23 25 $Initialized{$$self} = 0;
24  
25 25 return $self;
26 }
27  
28 25 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
29 25 my $self = shift;
30 25 delete $Engine{$$self};
31 25 delete $Initialized{$$self};
32 25 delete $Runloop{$$self};
33 }
34  
35 30 ▶sub add_engine {
Subroutine add_engine: called CC=5 SCAR=16.1
Pod documented
36 30 my ($self, $engine, $global) = @_;
37
38 30 ▶ croak "Engine is undefined" if !defined $engine;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $engine 1 29 30
39 29 ▶ croak "Engine is not a VS::RuleEngine::Engine instance" if !(blessed $engine && $engine->isa("VS::RuleEngine::Engine"));
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($engine) and $engine->isa("VS::RuleEngine::Engine") 2 27 29
Condition100%3/3
builtin::blessed($engine) and $engine->isa("VS::RuleEngine::Engine")
!ll&&!rl&&r
1127
MC/DC100%2/2
builtin::blessed($engine) and $engine->isa("VS::RuleEngine::Engine")
builtin::blessed($engine) $engine->isa("VS::RuleEngine::Engine")
Truth table100%3/3
A and B
A= builtin::blessed($engine)B= $engine->isa("VS::RuleEngine::Engine")
ABresult
0X0
100
111
40
41 27 my $engines = $Engine{$$self};
42 27 ▶ if (!first { $_->[0] == $engine } @$engines) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (not &first(sub { $_->[0] == $engine; } , @$engines)) { } 26 1 27
43 26 push @$engines, [$engine, $global];
44 }
45 else {
46 1 croak "Engine already exists";
47 }
48 }
49  
50 24 ▶sub init {
Subroutine init: called CC=3 SCAR=11.0
Pod documented
51 24 my ($self) = @_;
52
53 24 ▶ return if $Initialized{$$self};
Branch: 1 branch
BranchTrueFalseTotal
if $Initialized{$$self} 0 24 24
54
55 24 for my $engine (@{$Engine{$$self}}) {
56 25 my $runloop = _mk_runloop($engine->[0], $engine->[1]);
57 25 $self->_register_runloop($runloop);
58 }
59 }
60  
61 34 ▶sub _mk_runloop {
Subroutine _mk_runloop: called CC=2 SCAR=6.9
62 34 my $engine = shift;
63 34 my $global = shift;
64
65 34 my @rules = @{$engine->_rule_order};
66  
67 my %action_map = map {
68 34 my $actions = $engine->_get_rule_actions($_);
69 48 $_ => [@{$actions}];
70 } @rules;
71
72 my %rules = map {
73 34 my $rule = $engine->_get_rule($_);
74 48 my $rule_obj = $rule->instantiate($engine);
75 48 $_ => $rule_obj;
76 } @rules;
77
78 my %actions = map {
79 34 my $action = $engine->_get_action($_);
80 43 my $action_obj = $action->instantiate($engine);
81 43 $_ => $action_obj;
82 } $engine->actions;
83
84 34 my @pre_hooks = map { $_->instantiate($engine); } map { $engine->_get_hook($_) } @{$engine->_pre_hooks};
85 34 my @post_hooks = map { $_->instantiate($engine); } map { $engine->_get_hook($_) } @{$engine->_post_hooks};
86  
87 34 my $inputs = $engine->_input_handler;
88 34 my @outputs = map { $_->instantiate($engine); } map { $engine->_get_output($_) } sort $engine->outputs;
89  
90 34 ▶ $global = $global || VS::RuleEngine::Data->new();
Condition66%2/3
$global || 'VS::RuleEngine::Data'->new
l!l&&r!l&&!r
1330
MC/DC0%0/2
$global || 'VS::RuleEngine::Data'->new
$global 'VS::RuleEngine::Data'->new
Truth table66%2/3
A || B
A= $globalB= 'VS::RuleEngine::Data'->new
ABresult
1X1
011
000
91
92 34 $inputs->set_global($global);
93  
94 34 ▶ my $runloop = sub {
Subroutine __ANON__: called CC=15 SCAR=27.1
95 34 $inputs->_clear();
96
97 34 my $local = VS::RuleEngine::Data->new();
98
99 34 $inputs->set_local($local);
100  
101 34 my $skip = 0;
102
103 # Process all pre hooks
104 34 for my $hook (@pre_hooks) {
105 7 my $result = $hook->invoke($inputs, $global, $local);
106 7 ▶ $skip = 1 if $result == KV_SKIP;
Branch: 1 branch
BranchTrueFalseTotal
if $result == 2 0 7 7
107 7 ▶ return KV_ABORT if $result == KV_ABORT;
Branch: 1 branch
BranchTrueFalseTotal
if $result == 0 4 3 7
108 }
109
110 # Run rules until we find a matching rule
111 30 ▶ if (!$skip) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($skip) 30 0 30
112 30 my $match;
113
114 30 PROCESS_RULES: for (@rules) {
115 37 my $rule = $rules{$_};
116 37 my $result = $rule->evaluate($inputs, $global, $local);
117 37 ▶ $skip = 1, last PROCESS_RULES if $result == KV_SKIP;
Branch: 1 branch
BranchTrueFalseTotal
if $result == 2 1 36 37
118 36 ▶ $match = $_, last PROCESS_RULES if $result == KV_MATCH;
Branch: 1 branch
BranchTrueFalseTotal
if $result == 1 24 12 36
119 }
120
121 # Run all actions
122 30 ▶ if (!$skip && $match) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $skip and $match) 24 6 30
Condition100%3/3
not $skip and $match
!ll&&!rl&&r
1524
MC/DC100%2/2
not $skip and $match
not $skip $match
Truth table100%3/3
not(A) and B
A= not $skipB= $match
ABresult
0X0
100
111
123 24 $local->set('VS::RuleEngine/matchingRule' => $match);
124 24 my $actions = $action_map{$match};
125 24 for (@$actions) {
126 23 my $action = $actions{$_};
127 23 $action->perform($inputs, $global, $local);
128 }
129 }
130 }
131
132 # All outputs are always called if somethings decides not to skip
133 30 ▶ if (!$skip) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($skip) 29 1 30
134 29 PROCESS_OUTPUT: for my $output (@outputs) {
135 2 $output->process($inputs, $global, $local);
136 }
137 }
138
139 # Process all post hooks
140 30 for my $hook (@post_hooks) {
141 24 my $result = $hook->invoke($inputs, $global, $local);
142 24 ▶ return KV_ABORT if $result == KV_ABORT;
Branch: 1 branch
BranchTrueFalseTotal
if $result == 0 21 3 24
143 }
144
145 9 return KV_CONTINUE;
146 34 };
147
148 34 return $runloop;
149 }
150  
151 25 ▶sub _register_runloop {
Subroutine _register_runloop: called CC=2 SCAR=7.2
152 25 my ($self, $runloop) = @_;
153
154 25 my $runloops = $Runloop{$$self};
155 25 ▶ if (!first { $_ == $runloop } @$runloops) {
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if (not &first(sub { $_ == $runloop; } , @$runloops)) { } 25 0 25
156 25 push @$runloops, $runloop;
157 }
158 else {
159 0 croak "Runloop already registered";
160 }
161 }
162  
163 25 ▶sub _unregister_runloop {
Subroutine _unregister_runloop: called CC=1 SCAR=0.0
164 25 my ($self, $runloop) = @_;
165
166 25 my $runloops = $Runloop{$$self};
167 25 my @runloops = grep { $_ != $runloop } @$runloops;
168 25 $Runloop{$$self} = \@runloops;
169
170 }
171  
172 48 ▶sub step {
Subroutine step: called CC=4 SCAR=13.9
Pod documented
173 48 my $self = shift;
174
175 48 my $runloops = $Runloop{$$self};
176
177 48 ▶ if (@$runloops) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$runloops) 24 24 48
178 24 for my $runloop (@$runloops) {
179 25 my $status = $runloop->();
180 25 ▶ if ($status == KV_ABORT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == 0) 25 0 25
181 25 $self->_unregister_runloop($runloop);
182 }
183 }
184 }
185
186 48 return scalar @$runloops;
187 }
188  
189 24 ▶sub run {
Subroutine run: called CC=2 SCAR=6.9
Pod documented
190 24 my $self = shift;
191
192 24 $self->init();
193
194 24 RUNLOOP: while(1) {
195 48 ▶ last RUNLOOP if $self->step() == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->step == 0 24 24 48
196 }
197 }
198  
199 33 1;
200 __END__
« RuleEngine/Declare.pm ↑ Summary RuleEngine/Rule/InputUnderThreshold.pm »