NAME
Workflow::Condition - Evaluate a condition depending on the workflow state and environment
VERSION
This documentation describes version 2.00 of this package
SYNOPSIS
# First declare the condition in a 'workflow_condition.xml'...
<conditions>
<condition
name="IsAdminUser"
class="MyApp::Condition::IsAdminUser">
<param name="admin_group_id" value="5" />
<param name="admin_group_id" value="6" />
</condition>
...
# Reference the condition in an action of the state/workflow definition...
<workflow>
<state>
...
<action name="SomeAdminAction">
...
<condition name="IsAdminUser" />
</action>
<action name="AnotherAdminAction">
...
<condition name="IsAdminUser" />
</action>
<action name="AUserAction">
...
<condition name="!IsAdminUser" />
</action>
</state>
...
</workflow>
# Then implement the condition
package MyApp::Condition::IsAdminUser;
use strict;
use parent qw( Workflow::Condition );
use Workflow::Exception qw( configuration_error );
__PACKAGE__->mk_accessors( 'admin_group_id' );
sub init {
my ( $self, $params ) = @_;
$self->SUPER::init( $params );
unless ( $params->{admin_group_id} ) {
configuration_error
"You must define one or more values for 'admin_group_id' in ",
"declaration of condition ", $self->name;
}
my @admin_ids = $self->_normalize_array( $params->{admin_group_id} );
$self->admin_group_id( { map { $_ => 1 } @admin_ids } );
}
sub evaluate {
my ( $self, $wf ) = @_;
my $admin_ids = $self->admin_group_id;
my $current_user = $wf->context->param( 'current_user' );
unless ( $current_user ) {
return ''; # return false
}
foreach my $group ( @{ $current_user->get_groups } ) {
return 1 if ( $admin_ids->{ $group->id } ); # return true
}
return ''; # return false
}
DESCRIPTION
Conditions are used by the workflow to see whether actions are available in a particular context. So if user A asks the workflow for the available actions she might get a different answer than user B since they determine separate contexts.
NOTE: The condition is enforced by Workflow::State. This means that the condition name must be visible inside of the state definition. If you specify the reference to the condition only inside of the full action specification in a seperate file then nothing will happen. The reference to the condition must be defined inside of the state/workflow specification.
CONFIGURATION
While some conditions apply to all workflows, you may have a case where a condition has different implementations for different workflow types. For example, IsAdminUser may look in two different places for two different workflow types, but you want to use the same condition name for both.
You can accomplish this by adding a type in the condition configuration.
<conditions>
<type>Ticket</type>
<condition
name="IsAdminUser"
class="MyApp::Condition::IsAdminUser">
<param name="admin_group_id" value="5" />
<param name="admin_group_id" value="6" />
</condition>
...
The type must match a loaded workflow type, or the condition won't work. When the workflow looks for a condition, it will look for a typed condition first. If it doesn't find one, it will look for non-typed conditions.
SUBCLASSING
Strategy
The methods below specify an interface. Classes used as conditions need to implement these methods. The easiest way to achieve that is by inheriting from Workflow::Condition
. This is not required, though.
The idea behind conditions is that they are be stateless. So when the Workflow::Factory object reads in the condition configuration it creates the condition objects and initializes them with whatever information is passed in.
Then when the condition is evaluated we just call evaluate()
on the condition. Hopefully the operation can be done very quickly since the condition may be called many, many times during a workflow lifecycle -- they are typically used to show users what options they have given the current state of the workflow for things like menu options. So keep it short!
Interface methods
When implementing a condition in a class that doesn't have this class as a super-class, you must implement these methods. If you implement a condition that does have this class as a super-class, you may need to override these methods.
evaluate( $workflow )
Determine whether your condition fails by returning a false value or a true value upon success. You can get the application context information necessary to process your condition from the $workflow
object.
NOTE Callers wanting to evaluate a condition, should not call this method directly, but rather use the Workflow::Condition->evaluate_condition
class method described below.
Other methods
To create your own condition based on this class, these methods are available and can be overridden to do specific tasks.
init( \%params )
This is optional, but called when the condition is first initialized. It may contain information you will want to initialize your condition with in \%params
, which are all the declared parameters in the condition declaration except for 'class' and 'name'.
You may also do any initialization here -- you can fetch data from the database and store it in the class or object, whatever you need.
If you do not have sufficient information in \%params
you should throw an exception (preferably 'configuration_error' imported from Workflow::Exception).
Caching and inverting the result
If in one state, you ask for the same condition again, Workflow uses the cached result, so that within one list of available actions, you will get a consistent view. Note that if we would not use caching, this might not necessary be the case, as something external might change between the two evaluate() calls.
Caching is also used with an inverted condition, which you can specify in the definition using <condition name="!some_condition"
>. This condition returns the negation of the original one, i.e. if the original condition fails, this one does not and the other way round. As caching is used, you can model "yes/no" decisions using this feature - if you have both <condition name="some_condition"
> and <condition name="!some_condition"
> in your workflow state definition, exactly one of them will succeed and one will fail - which is particularly useful if you use "autorun" a lot.
Caching can be disabled by changing $Workflow::Condition::CACHE_RESULTS
to zero (0):
$Workflow::Condition::CACHE_RESULTS = 0;
All versions before 1.49 used a mechanism that effectively caused global state. To address the problems that resulted (see GitHub issues #9 and #7), 1.49 switched to a new mechanism with a cache per workflow instance.
$class->evaluate_condition( $WORKFLOW, $CONDITION_NAME )
Users call this method to evaluate a condition; subclasses call this method to evaluate a nested condition.
If the condition name starts with an '!', the result of the condition is negated. Note that a side-effect of this is that the return value of the condition is ignored. Only the negated boolean-ness is preserved.
This does implement a trick that is not a convention in the underlying Workflow library: by default, workflow conditions throw an error when the condition is false and just return when the condition is true. To allow for counting the true conditions, we also look at the return value here. If a condition returns zero or an undefined value, but did not throw an exception, we consider it to be '1'. Otherwise, we consider it to be the value returned.
COPYRIGHT
Copyright (c) 2003-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 LICENSE
AUTHORS
Please see Workflow