带验证器的Perl工作流模块

带验证器的Perl工作流模块,perl,workflow,validation,Perl,Workflow,Validation,我正在尝试使用perl工作流模块进行工作- 我设法弄清楚了它是如何与工作流、操作、条件以及所有这些一起工作的,但我无法让它将validator类应用于该操作 来自validator的我的_init方法加载并打印出我放在那里进行测试的行,但validate方法从未被触发。另外,当从action类转储$self->get_validators()时,我会得到一个空列表 我创建了一个简短的示例,因此如果您看到问题,请尝试并帮助解决。Tnx 链接至代码- 运行程序 节目以 Answer: London

我正在尝试使用perl工作流模块进行工作-

我设法弄清楚了它是如何与工作流、操作、条件以及所有这些一起工作的,但我无法让它将validator类应用于该操作

来自validator的我的_init方法加载并打印出我放在那里进行测试的行,但validate方法从未被触发。另外,当从action类转储$self->get_validators()时,我会得到一个空列表

我创建了一个简短的示例,因此如果您看到问题,请尝试并帮助解决。Tnx

链接至代码-

运行程序

节目以

Answer: London
If you answer right the action should change state to 'finished'. Try answering wrong first.
Capital city of England:
如果你回答“伯明翰”,应该写上

Your answer is being validated!
再问一次这个问题

当你回答“伦敦”时,它应该

Correct! Current state of workflow is - finished
编辑现在,无论您的答案是什么,它都会写出“正确!工作流的当前状态已完成”

工作流_test.pl

使用严格;
使用Log::log4perlqw(get_logger);
使用工作流::工厂qw(工厂);
Log4perl::init('Log4perl.conf');
系统(“清除”);
#将配置存放在工厂中;如果需要,我们可以稍后添加更多
工厂->从\u文件添加\u配置\u(
workflow=>workflow.xml,
action=>action.xml,
persister=>'persister.xml',
validator=>“validator.xml”
);
my$workflow=工厂->创建_工作流(“工作流1”);
我的$context=$workflow->context;
while($workflow->state eq“INITIAL”){
打印“如果回答正确,则操作的状态应更改为“已完成”。请先尝试回答错误。\n”;
my$city=get_response(“英格兰首都:”);
打印“您的回答-$city\n”;
$workflow->execute_action('action1');
如果($workflow->state eq“INITIAL”){
打印“您的答案错误!请重试!\n\n”;
}
}
打印“\n更正!工作流的当前状态为-”$workflow->state。“\n\n”;
#从命令行读取响应的通用例程(默认值,
#请注意,返回值的结尾/开头有空格
#例行公事被削减了。
sub-get_响应{
我的($msg)=@;
打印$msg;
我的$response=;
chomp$response;
$response=~s/^\s+/;
$response=~s/\s+$/;
返回$response;
}
workflow.xml


工作流程1
地方的
这是我的工作流程。
坚持者1
action.xml


$city
validator.xml


App::Action::Action1.pm

package-App::Action::Action1;
严格使用;
使用基本qw(工作流::操作);
使用工作流::异常qw(验证错误配置错误);
使用数据::转储程序;
次新{
我的$class=shift;
我的$self={};
祝福($self,$class);
返回$self;
}
副执行{
我的$self=shift;
我的$wf=班次;
打印“App::Action::Action1::Execute\n”;
打印“Validators:”.Dumper($self->get\u Validators())。“\n”;
}
1.
App::Validator::Validator1.pm

package-App::Validator::Validator1;
严格使用;
使用基本qw(工作流::验证器);
使用工作流::异常qw(验证错误配置错误);
使用数据::转储程序;
使用鲤鱼qw(Carp);
sub_init{
我的($self,$params)=@;
除非($params->{answer}){
配置错误
“您必须在中定义'answer'的值”,
“验证器声明”,$self->name;
}
if(ref$params->{answer}){
配置错误
“应答”的值必须是中的简单标量”,
“验证器声明”,$self->name;
}
打印“应答:.$params->{Answer}.\n”;
$self->{answer=>$params->{answer};
}
子验证{
我的($self,$wf,$city)=@;
打印“您的答案正在验证!\n”;
打印“您的答案是-”$city。“\n”;
我的美元支票;
if($city eq$self->{answer}){
$check=1;
}否则{
$check=0;
}
除非($支票){
验证错误“验证错误!”;
}
}
1.
编辑: 如果我在创建之后和执行任何操作之前转储工作流对象,我会得到以下结果:

工作流:$VAR1=bless({
'美国'=>{
'首字母'=>祝福({
...,
“\u操作”=>{
“操作1”=>{
'结果_状态'=>'完成',
“名称”=>“操作1”
}
},
“_工厂”=>祝福({
...,
“\u操作\u配置”=>{
“默认值”=>{
“操作1”=>{
'name'=>'action1',
'class'=>'App::Action::Action1',
“验证程序”=>[
{
“arg”=>[
“$city”
],
“名称”=>“验证程序1”
}
]
}
}
},
“\u验证程序”=>{
'验证程序1'=>祝福({
'name'=>'validator1',
'class'=>'App::Validator::Validator1',
'PARAMS'=>{}
},'App::Validator::Validator1')
},
“\u验证程序\u配置”=>{
'验证程序1'=>{
“答案”=>“伦敦”,
'name'=>'validator1',
“类”=>“应用程序::验证程序::验证程序1”
}
},
...
},“工作流::工厂”),
'type'=>'Workflow1',
'PARAMS'=>{}
},“工作流::状态”),
'finished'=>$VAR1->{'u states'}{'INITIAL'}{'u factory'}{{'u Workflow1'}[1]
},
...
}“工作FL
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;
$context->param( answer => $city );
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;
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;
}