-
Notifications
You must be signed in to change notification settings - Fork 11
/
CheckReturn.pm
182 lines (126 loc) · 4.31 KB
/
CheckReturn.pm
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
package Workflow::Condition::CheckReturn;
use warnings;
use strict;
use v5.14.0;
our $VERSION = '1.57';
use parent qw( Workflow::Condition );
use Workflow::Exception qw( configuration_error workflow_error );
__PACKAGE__->mk_accessors( 'condition', 'operator', 'argument' );
my %supported_ops = (
eq => '==',
lt => '<',
gt => '>',
le => '<=',
ge => '>=',
ne => '!=',
);
sub _init {
my ( $self, $params ) = @_;
unless ( defined $params->{condition} ) {
configuration_error
"You must specify the name of the nested condition in the parameter 'condition' for ",
$self->name;
}
$self->condition( $params->{condition} );
unless ( defined $params->{operator} ) {
configuration_error "You must define the value for 'operator' in ",
"declaration of condition ", $self->name;
}
$self->operator( $params->{operator} );
unless ( defined $params->{argument} ) {
configuration_error "You must define the value for 'argument' in ",
"declaration of condition ", $self->name;
}
$self->argument( $params->{argument} );
}
sub evaluate {
my ( $self, $wf ) = @_;
my $cond = $self->condition;
my $op = $self->operator;
my $arg = $self->argument;
# warn "DEBUG: evaluating operator '$op'";
my $numop = $supported_ops{$op};
if ( not $numop ) {
configuration_error "Unsupported operator '$op'";
}
# Fetch argument from context or eval, if necessary
my $argval;
if ( $arg =~ /^[-]?\d+$/ ) { # numeric
$argval = $arg;
} elsif ( $arg =~ /^[a-zA-Z0-9_]+$/ ) { # alpha-numeric, plus '_'
$argval = $wf->context->param($arg);
} else {
my $error;
my $success = do {
local $@;
my $rv = eval "\$argval = do { $arg }; 1;";
$error = $@;
$rv;
};
if (not $success) {
workflow_error
"Unable to evaluate condition expression '$arg': $error";
}
}
my $condval = $self->evaluate_condition( $wf, $cond );
local $@;
return eval "\$condval $op \$argval";
}
1;
__END__
=pod
=head1 NAME
Workflow::Condition::CheckReturn
=head1 VERSION
This documentation describes version 1.57 of this package
=head1 DESCRIPTION
Using nested conditions (See Workflow::Condition::Nested), this evaluates
a given condition and compares the value returned with a given argument.
=head1 SYNOPSIS
In condition.xml:
<condition name="check_approvals" class="Workflow::Condition::CheckReturn">
<param name="condition" value="count_approvals" />
<!-- operator "ge" means: greater than or equal to -->
<param name="operator" value="ge" />
<param name="argument" value="$context->{approvals_needed}" />
</condition>
In workflow.xml:
<state name="CHECK_APPROVALS" autorun="yes">
<action name="null_1" resulting_state="APPROVED">
<condition name="check_approvals" />
</action>
<action name="null_2" resulting_state="REJECTED">
<condition name="!check_approvals" />
</action>
</state>
=cut
=head1 PARAMETERS
The following parameters may be configured in the C<param> entity of the
condition in the XML configuration:
=head2 condition
The name of the condition to be evaluated.
=head2 argument
The value to compare with the given condition. This can be one of the following:
=over
=item Integer
The integer value is compared with the return value of the condition.
=item String [a-zA-Z0-9_]
The string is interpreted as the name of a workflow context parameter. The current
value of that parmeter is used in the comparison.
=item String
Any other string is evaluated in an C<eval> block. The result should be numeric.
=back
=head2 operator
The name of the comparison operator to use. Supported values are:
'eq', 'lt', 'gt', 'le', 'ge', 'ne'
The string names are used to simplify the notation in the XML files. The
above strings map to the following numeric operators internally:
'==', '<', '>', '<=', '>=', !=
=head1 COPYRIGHT
Copyright (c) 2004-2021 Chris Winters. All rights reserved.
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
Please see the F<LICENSE>
=head1 AUTHORS
Please see L<Workflow>
=cut