sto cercando di ottenere le cose funzionano con il modulo Workflow perl - http://search.cpan.org/~jonasbn/Workflow/modulo Perl flusso di lavoro con validatore
sono riuscito a capire come funziona con flussi di lavoro, le azioni, le condizioni e tutti, ma io non riesco a farlo applica la classe del validatore all'azione.
Il mio metodo _init dal validatore carica e stampa la riga che ho inserito per il test ma il metodo di convalida non viene attivato mai. Inoltre, quando si esegue il download di $ self-> get_validators() dalla classe action, viene visualizzato un elenco vuoto.
Ho creato un breve esempio quindi, per favore provalo e aiutalo se vedi il problema. Tnx!
link per il codice - https://github.com/vmcooper/perl_workflow_test
Esecuzione del programma
programma inizia con
Answer: London
If you answer right the action should change state to 'finished'. Try answering wrong first.
Capital city of England:
se si risponde "Birmingham" si dovrebbe scrivere
Your answer is being validated!
e chiedere il qu ancora una volta
Quando si risponde "London" dovrebbe
Correct! Current state of workflow is - finished
modifica Ora si scrive fuori "corretto stato attuale del flusso di lavoro è - finito" qualunque sia la vostra risposta è.
workflow_test.pl
use strict;
use Log::Log4perl qw(get_logger);
use Workflow::Factory qw(FACTORY);
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";
$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;
}
workflow.xml
<workflow>
<type>Workflow1</type>
<time_zone>local</time_zone>
<description>This is my workflow.</description>
<persister>Persister1</persister>
<state name="INITIAL">
<action name="action1" resulting_state="finished"/>
</state>
<state name="finished" />
</workflow>
action.xml
<actions>
<action name="action1" class="App::Action::Action1" >
<validator name="validator1">
<arg>$city</arg>
</validator>
</action>
</actions>
validator.xml
<validators>
<validator name="validator1" class="App::Validator::Validator1">
<param name="answer" value="London" />
</validator>
</validators>
App :: Azione :: Action1.pm
package App::Action::Action1;
use strict;
use base qw(Workflow::Action);
use Workflow::Exception qw(validation_error configuration_error);
use Data::Dumper;
sub new {
my $class = shift;
my $self = {};
bless ($self, $class);
return $self;
}
sub execute {
my $self = shift;
my $wf = shift;
print "App::Action::Action1::Execute\n";
print "Validators: ".Dumper($self->get_validators())."\n";
}
1;
App :: :: Validator Validator1.pm
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, $city) = @_;
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;
Edit: Se ho discarica oggetto del flusso di lavoro subito dopo la creazione e prima di ogni azione è eseguito ottengo questo:
Workflow: $VAR1 = bless({
'_states' => {
'INITIAL' => bless({
...,
'_actions' => {
'action1' => {
'resulting_state' => 'finished',
'name' => 'action1'
}
},
'_factory' => bless({
...,
'_action_config' => {
'default' => {
'action1' => {
'name' => 'action1',
'class' => 'App::Action::Action1',
'validator' => [
{
'arg' => [
'$city'
],
'name' => 'validator1'
}
]
}
}
},
'_validators' => {
'validator1' => bless({
'name' => 'validator1',
'class' => 'App::Validator::Validator1',
'PARAMS' => {}
}, 'App::Validator::Validator1')
},
'_validator_config' => {
'validator1' => {
'answer' => 'London',
'name' => 'validator1',
'class' => 'App::Validator::Validator1'
}
},
...
}, 'Workflow::Factory'),
'type' => 'Workflow1',
'PARAMS' => {}
}, 'Workflow::State'),
'finished' => $VAR1->{'_states'}{'INITIAL'}{'_factory'}{'_workflow_state'}{'Workflow1'}[1]
},
...
}, 'Workflow');
come si può vedere, validatore è qui e tutto è impostato su e sembra che sia ok, ma il validatore non viene applicato.
hm ... posso riassumerlo per essere più chiaro e minimo come questo: Il validatore non viene applicato prima che l'azione venga eseguita. Semplice come quello. Ho anche incluso il codice e un repository Github quindi non so davvero come essere più preciso. – Vedran
Il codice è semplice in quanto può essere perché spero solo di capire come funziona questo modulo. Puoi vedere cosa dovrebbe fare, sotto "Esecuzione del programma". È audace. Permettetemi di darvi un avviso che sto usando il modulo perl qui che non ho scritto io stesso. C'è un link in cima al cpan dove puoi vedere il modulo di cui sto parlando. Questo è il motivo per cui chiedo se qualcuno sa come usarlo e cosa ho fatto di sbagliato nei file di configurazione o nelle classi? – Vedran
Siamo spiacenti, ho perso l'aggiunta dell'esempio di Birmingham. La domanda è davvero completa ora, grazie. – ikegami