Workflow/State.pm

filter: Statementstmt 87.0% 154 / 177 Branchbran 55.5% 30 / 54 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 96.3% 26 / 27 Podpod 100% 13 / 13 totaltotal 78.6% 225 / 286 CC 36 SCAR 17.3
CC
36
Cov
78%
get_next_state
21.9
get_available_action_names
8.8
_assign_next_state_from_array
7.5
CRAP 5.6
« Workflow.pm ↑ Summary Workflow/Factory.pm »
1 package Workflow::State;
2  
3 28 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 28 ▶use v5.14.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 28 ▶use parent qw( Workflow::Base );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 28 ▶use Workflow::Condition;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 28 ▶use Workflow::Condition::Evaluate;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 28 ▶use Workflow::Exception qw( workflow_error );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 28 ▶use Exception::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 28 ▶use Workflow::Factory qw( FACTORY );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 28 $Workflow::State::VERSION = '2.12';
14  
15 28 my @FIELDS = qw( state description type );
16 28 my @INTERNAL = qw( _test_condition_count _factory _actions _conditions
17 _next_state );
18 28 __PACKAGE__->mk_accessors( @FIELDS, @INTERNAL );
19  
20  
21 ########################################
22 # PUBLIC
23  
24 113 ▶sub get_conditions {
Subroutine get_conditions: called CC=1 SCAR=0.0
Pod documented
25 113 my ( $self, $action_name ) = @_;
26 113 $self->_contains_action_check($action_name);
27 113 return @{ $self->_conditions->{$action_name} };
28 }
29  
30 43 ▶sub get_action {
Subroutine get_action: called CC=1 SCAR=0.0
Pod documented
31 43 my ( $self, $wf, $action_name ) = @_;
32 43 my $common_config =
33 $self->_factory->get_action_config($wf, $action_name);
34 43 my $state_config = $self->_actions->{$action_name};
35 43 my $config = { %{$common_config}, %{$state_config} };
36 43 my $action_class = $common_config->{class};
37  
38 43 return $action_class->new( $wf, $config );
39 }
40  
41 192 ▶sub contains_action {
Subroutine contains_action: called CC=1 SCAR=0.0
Pod documented
42 192 my ( $self, $action_name ) = @_;
43 192 return $self->_actions->{$action_name};
44 }
45  
46 25 ▶sub get_all_action_names {
Subroutine get_all_action_names: called CC=1 SCAR=0.0
Pod documented
47 25 my ($self) = @_;
48 25 return keys %{ $self->_actions };
49 }
50  
51 25 ▶sub get_available_action_names {
Subroutine get_available_action_names: called CC=6 SCAR=21.8
Pod documented
52 25 my ( $self, $wf, $group ) = @_;
53 25 my @all_actions = $self->get_all_action_names;
54 25 my @available_actions = ();
55  
56 # assuming that the user wants the _fresh_ list of available actions,
57 # we clear the condition cache before checking which ones are available
58 25 local $wf->{'_condition_result_cache'} = {};
59  
60 25 foreach my $action_name (@all_actions) {
61  
62 67 ▶ if ( $group ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($group) 0 67 67
63 0 my $action_config =
64 $self->_factory()->get_action_config( $wf, $action_name );
65 0 ▶ if ( defined $action_config->{group}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $action_config->{"group"} and $action_config->{"group"} ne $group) 0 0 0
Condition0%0/3
defined $action_config->{"group"} and $action_config->{"group"} ne $group
!ll&&!rl&&r
000
MC/DC0%0/2
defined $action_config->{"group"} and $action_config->{"group"} ne $group
defined $action_config->{"group"} $action_config->{"group"} ne $group
Truth table0%0/3
A and B
A= defined $action_config->{"group"}B= $action_config->{"group"} ne $group
ABresult
0X0
100
111
66 and $action_config->{group} ne $group ) {
67 0 next;
68 }
69 }
70  
71 67 ▶ if ( $self->is_action_available( $wf, $action_name ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_action_available($wf, $action_name)) 39 28 67
72 39 push @available_actions, $action_name;
73 }
74 }
75 25 return @available_actions;
76 }
77  
78 67 ▶sub is_action_available {
Subroutine is_action_available: called CC=1 SCAR=0.0
Pod documented
79 67 my ( $self, $wf, $action_name ) = @_;
80 67 return $self->evaluate_action( $wf, $action_name );
81 }
82  
83 0 ▶sub clear_condition_cache {
Subroutine clear_condition_cache: not called CC=1 SCAR=6.9
Pod documented
84 0 my ($self) = @_;
85 0 return; # left for backward compatibility with 1.49
86 }
87  
88 110 ▶sub evaluate_action {
Subroutine evaluate_action: called CC=4 SCAR=13.9
Pod documented
89 110 my ( $self, $wf, $action_name ) = @_;
90 110 my $state = $self->state;
91  
92 # NOTE: this will throw an exception if C<$action_name> is not
93 # contained in this state, so there's no need to do it explicitly
94  
95 110 my @conditions = $self->get_conditions($action_name);
96 110 foreach my $condition (@conditions) {
97 69 my $condition_name = $condition->name;
98 69 my $rv = Workflow::Condition->evaluate_condition($wf, $condition);
99 69 ▶ if (! $rv) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rv) 28 41 69
100  
101 28 ▶ $self->log->is_debug
Branch: 1 branch
BranchTrueFalseTotal
$self->log->is_debug and $self->log->debug("No access to action '${action_name}' in ", "state '${state}' because condition '${condition_name}' failed") 0 28 28
102 && $self->log->debug(
103 "No access to action '$action_name' in ",
104 "state '$state' because condition '$condition_name' failed");
105  
106 28 return $rv;
107 }
108 }
109  
110 82 return 1;
111 }
112  
113 36 ▶sub get_next_state {
Subroutine get_next_state: called CC=6 SCAR=30.9
Pod documented
114 36 my ( $self, $action_name, $action_return ) = @_;
115 36 $self->_contains_action_check($action_name);
116 36 my $resulting_state = $self->_next_state->{$action_name};
117 36 ▶ return $resulting_state unless ( ref($resulting_state) eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $resulting_state eq "HASH" 36 0 36
118  
119 0 ▶ return %{$resulting_state} unless(defined $action_return);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $action_return 0 0 0
120  
121 0 my $state = $self->state;
122 0 ▶ workflow_error "State->get_next_state was called with a non-scalar ",
Branch: 1 branch
BranchTrueFalseTotal
if ref $action_return ne "" 0 0 0
123 "return value in state '$state' on action '$action_name'" if (ref $action_return ne '');
124  
125 0 ▶ return $resulting_state->{$action_return} if ($resulting_state->{$action_return});
Branch: 1 branch
BranchTrueFalseTotal
if $resulting_state->{$action_return} 0 0 0
126  
127 0 ▶ return $resulting_state->{'*'} if ($resulting_state->{'*'});
Branch: 1 branch
BranchTrueFalseTotal
if $resulting_state->{"*"} 0 0 0
128  
129 0 workflow_error "State '$state' does not define a next state ",
130 "for a return value of '$action_return' and there is ",
131 "also no default state set.";
132  
133 }
134  
135 5 ▶sub get_autorun_action_name {
Subroutine get_autorun_action_name: called CC=4 SCAR=15.2
Pod documented
136 5 my ( $self, $wf ) = @_;
137 5 my $state = $self->state;
138 5 ▶ unless ( $self->autorun ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->autorun) 0 5 5
139 0 workflow_error "State '$state' is not marked for automatic ",
140 "execution. If you want it to be run automatically ",
141 "set the 'autorun' property to 'yes'.";
142 }
143  
144 5 my @actions = $self->get_available_action_names($wf);
145 5 my $pre_error = "State '$state' should be automatically executed but";
146 5 ▶ if ( scalar @actions > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @actions > 1) 0 5 5
147 0 workflow_error "$pre_error there are multiple actions available ",
148 "for execution. Actions are: ", join ', ', @actions;
149 }
150 5 ▶ if ( scalar @actions == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @actions == 0) 0 5 5
151 0 workflow_error
152 "$pre_error there are no actions available for execution.";
153 }
154 5 $self->log->debug("Auto-running state '$state' with action '$actions[0]'");
155 5 return $actions[0];
156 }
157  
158 231 ▶sub autorun {
Subroutine autorun: called CC=3 SCAR=11.0
Pod documented
159 231 my ( $self, $setting ) = @_;
160 231 ▶ if ( defined $setting ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $setting) 162 69 231
161 162 ▶ if ( $setting =~ /^(true|1|yes)$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($setting =~ /^(true|1|yes)$/iu) { } 5 157 162
162 5 $self->{autorun} = 'yes';
163 } else {
164 157 $self->{autorun} = 'no';
165 }
166 }
167 231 return ( $self->{autorun} eq 'yes' );
168 }
169  
170 167 ▶sub may_stop {
Subroutine may_stop: called CC=3 SCAR=11.2
Pod documented
171 167 my ( $self, $setting ) = @_;
172 167 ▶ if ( defined $setting ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $setting) 162 5 167
173 162 ▶ if ( $setting =~ /^(true|1|yes)$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($setting =~ /^(true|1|yes)$/iu) { } 0 162 162
174 0 $self->{may_stop} = 'yes';
175 } else {
176 162 $self->{may_stop} = 'no';
177 }
178 }
179 167 return ( $self->{may_stop} eq 'yes' );
180 }
181  
182 ########################################
183 # INTERNAL
184  
185 162 ▶sub init {
Subroutine init: called CC=5 SCAR=16.2
Pod documented
186 162 my ( $self, $config, $factory ) = @_;
187  
188 # Fallback for old style
189 162 ▶ $factory ||= FACTORY;
Condition33%1/3
$factory ||= FACTORY()
l!l&&r!l&&!r
16200
MC/DC0%0/2
$factory ||= FACTORY()
$factory FACTORY()
Truth table33%1/3
A ||= B
A= $factoryB= FACTORY()
ABresult
1X1
011
000
190 162 my $name = $config->{name};
191  
192 162 my $class = ref $self;
193  
194 162 $self->log->debug("Constructing '$class' object for state $name");
195  
196 162 $self->state($name);
197 162 $self->_factory($factory);
198 162 $self->_actions( {} );
199 162 $self->_conditions( {} );
200 162 $self->_next_state( {} );
201  
202 # Note this is the workflow type.
203 162 $self->type( $config->{type} );
204 162 $self->description( $config->{description} );
205  
206 162 ▶ if ( $config->{autorun} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{'autorun'}) { } 5 157 162
207 5 $self->autorun( $config->{autorun} );
208 } else {
209 157 $self->autorun('no');
210 }
211 162 ▶ if ( $config->{may_stop} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{'may_stop'}) { } 0 162 162
212 0 $self->may_stop( $config->{may_stop} );
213 } else {
214 162 $self->may_stop('no');
215 }
216 162 foreach my $state_action_config ( @{ $config->{action} } ) {
217 165 my $action_name = $state_action_config->{name};
218 165 $self->log->debug("Adding action '$action_name' to '$class' '$name'");
219 165 $self->_add_action_config( $action_name, $state_action_config );
220 }
221 }
222  
223 14 ▶sub _assign_next_state_from_array {
Subroutine _assign_next_state_from_array: called CC=6 SCAR=20.2
224 14 my ( $self, $action_name, $resulting ) = @_;
225 14 my $name = $self->state;
226 14 my @errors = ();
227 14 my %new_resulting = ();
228 14 foreach my $map ( @{$resulting} ) {
229 28 ▶ if ( not $map->{state} or not defined $map->{return} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $map->{'state'} && defined $map->{'return'}) { } 0 28 28
elsif ($new_resulting{$map->{'return'}}) { } 0 28 28
Condition33%1/3
$map->{'state'} && defined $map->{'return'}
!ll&&!rl&&r
0028
MC/DC0%0/2
$map->{'state'} && defined $map->{'return'}
$map->{'state'} defined $map->{'return'}
Truth table33%1/3
A && B
A= $map->{'state'}B= defined $map->{'return'}
ABresult
0X0
100
111
230 0 push @errors,
231 "Must have both 'state' ($map->{state}) and 'return' "
232 . "($map->{return}) keys defined.";
233 } elsif ( $new_resulting{ $map->{return} } ) {
234 0 push @errors, "The 'return' value ($map->{return}) must be "
235 . "unique among the resulting states.";
236 } else {
237 28 $new_resulting{ $map->{return} } = $map->{state};
238 }
239 }
240 14 ▶ if ( scalar @errors ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @errors) 0 14 14
241 0 workflow_error "Errors found assigning 'resulting_state' to ",
242 "action '$action_name' in state '$name': ", join '; ', @errors;
243 }
244 14 $self->log->debug( "Assigned multiple resulting states in '$name' and ",
245 "action '$action_name' from array ok" );
246 14 return \%new_resulting;
247 }
248  
249 165 ▶sub _create_next_state {
Subroutine _create_next_state: called CC=3 SCAR=11.0
250 165 my ( $self, $action_name, $resulting ) = @_;
251  
252 165 ▶ if ( my $resulting_type = ref $resulting ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $resulting_type = ref $resulting) 14 151 165
253 14 ▶ if ( $resulting_type eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resulting_type eq "ARRAY") 14 0 14
254 14 $resulting
255 = $self->_assign_next_state_from_array( $action_name,
256 $resulting );
257 }
258 }
259  
260 165 return $resulting;
261 }
262  
263 165 ▶sub _add_action_config {
Subroutine _add_action_config: called CC=2 SCAR=7.2
264 165 my ( $self, $action_name, $action_config ) = @_;
265 165 my $state = $self->state;
266 165 ▶ unless ( $action_config->{resulting_state} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($action_config->{"resulting_state"}) 0 165 165
267 0 my $no_change_value = Workflow->NO_CHANGE_VALUE;
268 0 workflow_error "Action '$action_name' in state '$state' does not ",
269 "have the key 'resulting_state' defined. This key ",
270 "is required -- if you do not want the state to ",
271 "change, use the value '$no_change_value'.";
272 }
273 # Copy the action config,
274 # so we can delete keys consumed by the state below
275 165 my $copied_config = { %$action_config };
276 165 my $resulting_state = delete $copied_config->{resulting_state};
277 165 my $condition = delete $copied_config->{condition};
278  
279 # Removes 'resulting_state' key from action_config
280 165 $self->_next_state->{$action_name} =
281 $self->_create_next_state( $action_name, $resulting_state );
282  
283 # Removes 'condition' key from action_config
284 165 $self->_conditions->{$action_name} = [
285 $self->_create_condition_objects( $action_name, $condition )
286 ];
287  
288 165 $self->_actions->{$action_name} = $copied_config;
289 }
290  
291 165 ▶sub _create_condition_objects {
Subroutine _create_condition_objects: called CC=3 SCAR=11.0
292 165 my ( $self, $action_name, $action_conditions ) = @_;
293 165 my @conditions = $self->normalize_array( $action_conditions );
294 165 my @condition_objects = ();
295 165 my $count = 1;
296 165 foreach my $condition_info (@conditions) {
297  
298 # Special case: a 'test' denotes our 'evaluate' condition
299 98 ▶ if ( $condition_info->{test} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($condition_info->{'test'}) { } 20 78 98
300 20 my $state = $self->state();
301 push @condition_objects,
302 Workflow::Condition::Evaluate->new(
303 { name => "_$state\_$action_name\_condition\_$count",
304 class => 'Workflow::Condition::Evaluate',
305 test => $condition_info->{test},
306 }
307 20 );
308 20 $count++;
309 } else {
310 78 $self->log->info(
311 "Fetching condition '$condition_info->{name}'");
312 push @condition_objects,
313 $self->_factory()
314 78 ->get_condition( $condition_info->{name}, $self->type() );
315 }
316 }
317 165 return @condition_objects;
318 }
319  
320 149 ▶sub _contains_action_check {
Subroutine _contains_action_check: called CC=2 SCAR=8.1
321 149 my ( $self, $action_name ) = @_;
322 149 ▶ unless ( $self->contains_action($action_name) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->contains_action($action_name)) 0 149 149
323 0 workflow_error "State '", $self->state, "' does not contain ",
324 "action '$action_name'";
325 }
326 }
327  
328 28 1;
329  
330 __END__
« Workflow.pm ↑ Summary Workflow/Factory.pm »