Perl Workflow module with validator

最后都变了- 提交于 2019-12-05 06:03:05

Looks like we will have to wait for this a bit or we can participate in a project to get this functionaity done.

Scroll to the "get_validators" heading and you will se a "#TODO" mark. I'm not sure whether this means the documentation needs to be done or code but i did look at the code a bit and it looks like the code needs to be done for this functionality. http://search.cpan.org/~jonasbn/Workflow-1.41/lib/Workflow/Factory.pm

Correct me if i'm wrong.

I located several issues in your example.

Action1.pm has a constructor, this interferes with the inheritance, So this should be deleted leaving your action class as follows

package App::Action::Action1;

use strict;
use base qw( Workflow::Action );
use Workflow::Exception qw( validation_error configuration_error );
use Data::Dumper;

sub execute {
    my $self = shift;
    my $wf = shift;
    print "App::Action::Action1::Execute\n";
    print "Validators: ".Dumper($self->get_validators())."\n";
}

1;

The main issue that your application receives the input from the user, but you never feed it to the workflow. This has be done using context, like so:

$context->param( answer => $city );

The validator should looks as follows:

package App::Validator::Validator1;

use strict;
use base qw( Workflow::Validator );
use Workflow::Exception qw( validation_error configuration_error );
use Data::Dumper;
use Carp qw(carp);

sub _init {
    my ( $self, $params ) = @_;
     unless ( $params->{answer} ) {
         configuration_error
             "You must define a value for 'answer' in ",
             "declaration of validator ", $self->name;
     }
     if ( ref $params->{answer} ) {
         configuration_error
             "The value for 'answer' must be a simple scalar in ",
             "declaration of validator ", $self->name;
     }
     print "Answer: ".$params->{answer}."\n";
     $self->{answer} = $params->{answer};
}

sub validate {
    my ( $self, $wf ) = @_;

    my $city = $wf->context->param('answer');

    print "Your answer is being validated!\n";
    print "Your answer is - ".$city."\n";

    my $check;

    if ( $city eq $self->{answer} ){
        $check = 1;
    } else {
        $check = 0;
    }
    unless ( $check ) {
        validation_error "Validation error!";
    }
}

1;

And your main application should resemble the following:

use strict;
use Log::Log4perl     qw( get_logger );
use Workflow::Factory qw( FACTORY );
use lib qw(lib);

Log::Log4perl::init( 'log4perl.conf' );
system('clear');

# Stock the factory with the configurations; we can add more later if we want
FACTORY->add_config_from_file(
    workflow   => 'workflow.xml',
    action     => 'action.xml',
    persister  => 'persister.xml',
    validator  => 'validator.xml'
    );

my $workflow = FACTORY->create_workflow( "Workflow1" );
my $context = $workflow->context;

while ( $workflow->state eq "INITIAL" ) {
    print "If you answer right the action should change state to 'finished'. Try answering wrong first.\n";
    my $city = get_response( "Capital city of England: " );
    print "You answered - $city\n";
    $context->param( answer => $city );
    $workflow->execute_action( 'action1' );

    if( $workflow->state eq "INITIAL" ) {
        print "Your answer is wrong! try again!\n\n";
    }
}

print "\nCorrect! Current state of workflow is - ".$workflow->state."\n\n";


# Generic routine to read a response from the command-line (defaults,
# etc.) Note that return value has whitespace at the end/beginning of
# the routine trimmed.

sub get_response {
    my ( $msg ) = @_;
    print $msg;
    my $response = <STDIN>;
    chomp $response;
    $response =~ s/^\s+//;
    $response =~ s/\s+$//;
    return $response;
}

I understand your confusion, since the documentation does not properly reflect this fact and it cannot be read from the example application in the distribution. I will update the documentation accordingly.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!