2015-01-13 26 views
6

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.

+0

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

+0

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

+0

Siamo spiacenti, ho perso l'aggiunta dell'esempio di Birmingham. La domanda è davvero completa ora, grazie. – ikegami

risposta

0

Sembra che dovremo aspettare un po 'o possiamo partecipare a un progetto per ottenere questa funzionalità.

Scorrere fino all'intestazione "get_validators" e verrà visualizzato il segno "#TODO". Non sono sicuro se questo significa che la documentazione deve essere fatta o codice, ma ho guardato il codice un po 'e sembra che il codice debba essere fatto per questa funzionalità. http://search.cpan.org/~jonasbn/Workflow-1.41/lib/Workflow/Factory.pm

Correggimi se sbaglio.

-1

Ho individuato diversi problemi nell'esempio.

Action1.pm ha un costruttore, questo interferisce con l'eredità, quindi questo dovrebbe essere eliminato lasciando la classe azione come segue

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; 

Il problema principale che l'applicazione riceve l'input da parte dell'utente, ma si mai nutrirlo per il flusso di lavoro. Questo è essere fatto utilizzando contesto, in questo modo:

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

Il validatore dovrebbe apparire come segue:

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; 

e la vostra applicazione principale dovrebbe essere simile al seguente:

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; 
} 

Capisco la tua confusione, dal momento che la documentazione non riflette correttamente questo fatto e non può essere letta dall'applicazione di esempio nella distribuzione. Aggiornerò la documentazione di conseguenza.

+0

Questo non fornisce una risposta alla domanda. Per criticare o richiedere chiarimenti da un autore, lascia un commento sotto il loro post. - [Dalla recensione] (/ recensione/post di bassa qualità/18067782) –

+0

Ho creato [a PR] (https://github.com/vmcooper/perl_workflow_test/pull/1) per il tuo repository Github con tutti i file , Mi dispiace di non averlo notato prima di crearne uno mio e i file che mancavano nella tua domanda erano ovviamente disponibili nel repository. La mia risposta manca delle modifiche apportate al validatore, lo aggiornerò. – jonasbn

+0

@ guillaume-darmont sarebbe possibile rivedere la mia risposta, l'ho aggiornata - e mi piacerebbe ottenere l'indirizzo -1, se la risposta ovviamente lo giustifica - grazie – jonasbn