| 1 | package Treex::Core::Scenario; | ||||||||||||||||||||||||||||
| 2 | { | ||||||||||||||||||||||||||||
| 3 | 41 | $Treex::Core::Scenario::VERSION = '0.08633_1'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | 60 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 43 | ▶ | use Treex::Core::Common; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 43 | ▶ | use File::Basename; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 42 | ▶ | use File::Slurp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 42 | ▶ | use File::chdir; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | #use Parse::RecDescent 1.967003; now using standalone version | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 41 | has from_file => ( | |||||||||||||||||||||||||||
| 13 | is => 'ro', | ||||||||||||||||||||||||||||
| 14 | isa => 'Str', | ||||||||||||||||||||||||||||
| 15 | predicate => '_has_from_file', | ||||||||||||||||||||||||||||
| 16 | documentation => q(Path to file with scenario), | ||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 41 | has from_string => ( | |||||||||||||||||||||||||||
| 20 | is => 'ro', | ||||||||||||||||||||||||||||
| 21 | isa => 'Str', | ||||||||||||||||||||||||||||
| 22 | predicate => '_has_from_string', | ||||||||||||||||||||||||||||
| 23 | documentation => q(String with scenario), | ||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 41 | has scenario_string => ( | |||||||||||||||||||||||||||
| 27 | is => 'ro', | ||||||||||||||||||||||||||||
| 28 | isa => 'Str', | ||||||||||||||||||||||||||||
| 29 | builder => '_build_scenario_string', | ||||||||||||||||||||||||||||
| 30 | lazy => 1, | ||||||||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 41 | has block_items => ( | |||||||||||||||||||||||||||
| 34 | is => 'ro', | ||||||||||||||||||||||||||||
| 35 | isa => 'ArrayRef[HashRef]', | ||||||||||||||||||||||||||||
| 36 | builder => 'parse_scenario_string', | ||||||||||||||||||||||||||||
| 37 | init_arg => undef, | ||||||||||||||||||||||||||||
| 38 | lazy => 1, | ||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 41 | has loaded_blocks => ( | |||||||||||||||||||||||||||
| 42 | is => 'ro', | ||||||||||||||||||||||||||||
| 43 | isa => 'ArrayRef[Treex::Core::Block]', | ||||||||||||||||||||||||||||
| 44 | builder => '_build_loaded_blocks', | ||||||||||||||||||||||||||||
| 45 | predicate => 'is_initialized', | ||||||||||||||||||||||||||||
| 46 | lazy => 1, | ||||||||||||||||||||||||||||
| 47 | init_arg => undef, | ||||||||||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 41 | has document_reader => ( | |||||||||||||||||||||||||||
| 51 | is => 'ro', | ||||||||||||||||||||||||||||
| 52 | does => 'Treex::Core::DocumentReader', | ||||||||||||||||||||||||||||
| 53 | predicate => '_has_document_reader', | ||||||||||||||||||||||||||||
| 54 | writer => '_set_document_reader', | ||||||||||||||||||||||||||||
| 55 | init_arg => undef, | ||||||||||||||||||||||||||||
| 56 | documentation => 'DocumentReader starts every scenario and reads a stream of documents.' | ||||||||||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | has _global_params => ( | ||||||||||||||||||||||||||||
| 60 | is => 'ro', | ||||||||||||||||||||||||||||
| 61 | isa => 'HashRef[Str]', | ||||||||||||||||||||||||||||
| 62 | traits => ['Hash'], | ||||||||||||||||||||||||||||
| 63 | 38 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 64 | 41 | handles => { | |||||||||||||||||||||||||||
| 65 | get_global_param => 'get', | ||||||||||||||||||||||||||||
| 66 | set_global_param => 'set', | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | #get_global_param_names => 'keys', | ||||||||||||||||||||||||||||
| 69 | #set_verbose => [ set => 'verbose' ], | ||||||||||||||||||||||||||||
| 70 | #get_verbose => [ get => 'verbose' ], | ||||||||||||||||||||||||||||
| 71 | #set_language => [ set => 'language' ], | ||||||||||||||||||||||||||||
| 72 | #get_language => [ get => 'language' ], | ||||||||||||||||||||||||||||
| 73 | #... ? | ||||||||||||||||||||||||||||
| 74 | }, | ||||||||||||||||||||||||||||
| 75 | ); | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 41 | has parser => ( | |||||||||||||||||||||||||||
| 78 | is => 'ro', | ||||||||||||||||||||||||||||
| 79 | isa => 'Parse::RecDescent::_Runtime', | ||||||||||||||||||||||||||||
| 80 | init_arg => undef, | ||||||||||||||||||||||||||||
| 81 | builder => '_build_parser', | ||||||||||||||||||||||||||||
| 82 | documentation => q{Parses treex scenarios} | ||||||||||||||||||||||||||||
| 83 | ); | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 41 | has runner => ( | |||||||||||||||||||||||||||
| 86 | is => 'ro', | ||||||||||||||||||||||||||||
| 87 | isa => 'Treex::Core::Run', | ||||||||||||||||||||||||||||
| 88 | writer => '_set_runner', | ||||||||||||||||||||||||||||
| 89 | weak_ref => 1, | ||||||||||||||||||||||||||||
| 90 | documentation => 'Treex::Core::Run instance in which the scenario is running', | ||||||||||||||||||||||||||||
| 91 | ); | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | sub _build_scenario_string { | ||||||||||||||||||||||||||||
| 94 | 38 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_build_scenario_string: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 95 | 38 | ▶ | if ( $self->_has_from_file ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 96 | 9 | return $self->_load_scenario_file( $self->from_file ); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | elsif ( $self->_has_from_string ) { | ||||||||||||||||||||||||||||
| 99 | 30 | return $self->from_string; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | 1 | log_fatal("You have to provide from_file or from_string attribute"); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | sub _build_loaded_blocks { | ||||||||||||||||||||||||||||
| 105 | 34 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_build_loaded_blocks: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 106 | 34 | my @block_items = @{ $self->block_items }; | |||||||||||||||||||||||||||
| 107 | 32 | my $block_count = scalar @block_items; | |||||||||||||||||||||||||||
| 108 | 32 | my $i = 0; | |||||||||||||||||||||||||||
| 109 | 32 | my @loaded_blocks; | |||||||||||||||||||||||||||
| 110 | 32 | foreach my $block_item (@block_items) { | |||||||||||||||||||||||||||
| 111 | 76 | $i++; | |||||||||||||||||||||||||||
| 112 | 76 | my $params = ''; | |||||||||||||||||||||||||||
| 113 | 76 | ▶ | if ( $block_item->{block_parameters} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 76 | $params = join ' ', @{ $block_item->{block_parameters} }; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | 76 | log_info("Loading block $block_item->{block_name} $params ($i/$block_count)"); | |||||||||||||||||||||||||||
| 117 | 76 | my $new_block = $self->_load_block($block_item); | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 75 | log_debug( $block_item->{block_name} ); | |||||||||||||||||||||||||||
| 120 | 75 | ▶ | if ( $new_block->does('Treex::Core::DocumentReader') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 31 | ▶ | log_fatal("Only one DocumentReader per scenario is permitted ($block_item->{block_name})") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | if $self->_has_document_reader; | ||||||||||||||||||||||||||||
| 123 | 31 | $self->_set_document_reader($new_block); | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | else { | ||||||||||||||||||||||||||||
| 126 | 44 | push @loaded_blocks, $new_block; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | 31 | log_info(''); | |||||||||||||||||||||||||||
| 130 | 31 | log_info(' ALL BLOCKS SUCCESSFULLY LOADED.'); | |||||||||||||||||||||||||||
| 131 | 31 | log_info(''); | |||||||||||||||||||||||||||
| 132 | 31 | return \@loaded_blocks; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | sub _load_parser { | ||||||||||||||||||||||||||||
| 136 | 37 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_load_parser: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 137 | 37 | require Treex::Core::ScenarioParser; | |||||||||||||||||||||||||||
| 138 | 37 | return Treex::Core::ScenarioParser->new(); | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | sub _my_dir { | ||||||||||||||||||||||||||||
| 142 | 0 | ▶ | return dirname( (caller)[1] ); | ||||||||||||||||||||||||||
|
Subroutine
_my_dir: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | sub _build_parser { | ||||||||||||||||||||||||||||
| 146 | 37 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_build_parser: called CC=5 SCAR=29.1
| |||||||||||||||||||||||||||||
| 147 | 37 | my $parser; | |||||||||||||||||||||||||||
| 148 | 37 | ▶ | eval { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 37 | $parser = $self->_load_parser(); | |||||||||||||||||||||||||||
| 150 | 37 | 1; | |||||||||||||||||||||||||||
| 151 | } and return $parser; | ||||||||||||||||||||||||||||
| 152 | 0 | log_info("Cannot find precompiled scenario parser, trying to build it from grammar"); | |||||||||||||||||||||||||||
| 153 | 0 | my $dir = $self->_my_dir(); #get module's directory | |||||||||||||||||||||||||||
| 154 | 0 | my $file = "$dir/ScenarioParser.rdg"; #find grammar file | |||||||||||||||||||||||||||
| 155 | 0 | ▶ | log_fatal("Cannot find grammar file") if !-e $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | #in fact we should never reach this | ||||||||||||||||||||||||||||
| 158 | 0 | log_warn('We should NOT reach this place. Treex distribution may be corrupted.'); | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 0 | my $grammar = read_file($file); #load it | |||||||||||||||||||||||||||
| 161 | eval { | ||||||||||||||||||||||||||||
| 162 | 0 | log_info("Trying to precompile it for you"); | |||||||||||||||||||||||||||
| 163 | 0 | require Parse::RecDescent; | |||||||||||||||||||||||||||
| 164 | 0 | local $CWD = $dir; | |||||||||||||||||||||||||||
| 165 | 0 | Parse::RecDescent->Precompile( {-standalone => 1 }, $grammar, 'Treex::Core::ScenarioParser' ); | |||||||||||||||||||||||||||
| 166 | 0 | $parser = $self->_load_parser(); | |||||||||||||||||||||||||||
| 167 | 0 | 1; | |||||||||||||||||||||||||||
| 168 | 0 | ▶ | } or eval { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
eval { or eval {
MC/DC0%0/2
eval {
do {
log_info("Trying to precompile it for you");
require Parse::RecDescent;
local $Treex::Core::Scenario::CWD = $dir;
"Parse::RecDescent"->Precompile({"-standalone", 1}, $grammar, "Treex::Core::ScenarioParser");
$parser = $self->_load_parser;
1
}
} or eval {
do {
log_info("Cannot precompile, loading directly from grammar. Consider precompiling it manually");
require Parse::RecDescent;
$parser = "Parse::RecDescent"->new($grammar);
1
}
}
eval {
do {
log_info("Trying to precompile it for you");
require Parse::RecDescent;
local $Treex::Core::Scenario::CWD = $dir;
"Parse::RecDescent"->Precompile({"-standalone", 1}, $grammar, "Treex::Core::ScenarioParser");
$parser = $self->_load_parser;
1
}
}
eval {
do {
log_info("Cannot precompile, loading directly from grammar. Consider precompiling it manually");
require Parse::RecDescent;
$parser = "Parse::RecDescent"->new($grammar);
1
}
}
Truth table0%0/3
A or B
A= eval {
do {
log_info("Trying to precompile it for you");
require Parse::RecDescent;
local $Treex::Core::Scenario::CWD = $dir;
"Parse::RecDescent"->Precompile({"-standalone", 1}, $grammar, "Treex::Core::ScenarioParser");
$parser = $self->_load_parser;
1
}
}B= eval {
do {
log_info("Cannot precompile, loading directly from grammar. Consider precompiling it manually");
require Parse::RecDescent;
$parser = "Parse::RecDescent"->new($grammar);
1
}
}
| |||||||||||||||||||||||||||||
| 169 | 0 | log_info("Cannot precompile, loading directly from grammar. Consider precompiling it manually"); | |||||||||||||||||||||||||||
| 170 | 0 | require Parse::RecDescent; | |||||||||||||||||||||||||||
| 171 | 0 | $parser = Parse::RecDescent->new($grammar); #create parser | |||||||||||||||||||||||||||
| 172 | 0 | 1; | |||||||||||||||||||||||||||
| 173 | } or log_fatal("Cannot create Scenario parser"); | ||||||||||||||||||||||||||||
| 174 | 0 | return $parser; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | sub _load_scenario_file { | ||||||||||||||||||||||||||||
| 178 | 8 | ▶ | my ( $self, $scenario_filename ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_load_scenario_file: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 179 | 8 | log_info "Loading scenario description $scenario_filename"; | |||||||||||||||||||||||||||
| 180 | 8 | ▶ | my $scenario_string = read_file( $scenario_filename, binmode => ':utf8', err_mode => 'quiet' ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | or log_fatal "Can't open scenario file $scenario_filename"; | ||||||||||||||||||||||||||||
| 182 | 8 | return $scenario_string; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | sub parse_scenario_string { | ||||||||||||||||||||||||||||
| 186 | 37 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
parse_scenario_string: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 187 | 37 | my $scenario_string = $self->scenario_string; | |||||||||||||||||||||||||||
| 188 | 37 | my $from_file = $self->from_file; | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 37 | my $parsed = $self->parser->startrule( $scenario_string, 1, $from_file ); | |||||||||||||||||||||||||||
| 191 | 37 | ▶ | log_fatal("Cannot parse the scenario: $scenario_string") if !defined $parsed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | 35 | return $parsed; | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | # reverse of parse_scenario_string, used in Treex::Core::Run for treex --dump | ||||||||||||||||||||||||||||
| 196 | sub construct_scenario_string { | ||||||||||||||||||||||||||||
| 197 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
construct_scenario_string: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 198 | 3 | my %args = @_; | |||||||||||||||||||||||||||
| 199 | 3 | my $multiline = $args{multiline}; | |||||||||||||||||||||||||||
| 200 | 3 | my @block_items = @{ $self->block_items }; | |||||||||||||||||||||||||||
| 201 | 3 | ▶ | my $delim = $multiline ? qq{\n} : q{ }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 202 | 3 | my @block_strings; | |||||||||||||||||||||||||||
| 203 | 3 | foreach my $block_item (@block_items) { | |||||||||||||||||||||||||||
| 204 | 7 | my $name = $block_item->{block_name}; | |||||||||||||||||||||||||||
| 205 | 7 | my @parameters = map { _add_quotes($_) } @{ $block_item->{block_parameters} }; | |||||||||||||||||||||||||||
| 206 | 7 | ▶ | $name =~ s{^Treex::Block::}{} or $name = "::$name"; #strip leading Treex::Block:: or add leading :: | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | 7 | my $params; | |||||||||||||||||||||||||||
| 208 | 7 | ▶ | if ( scalar @parameters ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | 2 | $params = q{ } . join q{ }, @parameters; | |||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | else { | ||||||||||||||||||||||||||||
| 212 | 5 | $params = q{}; | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | 7 | push @block_strings, $name . $params; | |||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | 3 | return join $delim, @block_strings; | |||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | # reverse of parse_scenario_string, used in Treex::Core::Run for treex --dump | ||||||||||||||||||||||||||||
| 221 | sub get_required_files { | ||||||||||||||||||||||||||||
| 222 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_required_files: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 223 | 0 | my @block_items = @{ $self->block_items }; | |||||||||||||||||||||||||||
| 224 | 0 | my @required_files; | |||||||||||||||||||||||||||
| 225 | 0 | foreach my $block_item (@block_items) { | |||||||||||||||||||||||||||
| 226 | 0 | my $block = $self->_load_block($block_item); | |||||||||||||||||||||||||||
| 227 | push @required_files, | ||||||||||||||||||||||||||||
| 228 | map { | ||||||||||||||||||||||||||||
| 229 | 0 | $block_item->{block_name} . "\t" . $_; | |||||||||||||||||||||||||||
| 230 | } $block->get_required_share_files(); | ||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||
| 232 | 0 | return @required_files; | |||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | sub _add_quotes { # adding quotes only if param. value contains a space | ||||||||||||||||||||||||||||
| 236 | 2 | ▶ | my ($block_parameter) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_add_quotes: called CC=2 SCAR=7.4
| |||||||||||||||||||||||||||||
| 237 | 2 | my ( $name, $value ) = split /=/, $block_parameter, 2; | |||||||||||||||||||||||||||
| 238 | 2 | ▶ | if ( $value =~ /\s/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 239 | 0 | return "$name=\"$value\""; | |||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | 2 | return $block_parameter; | |||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | sub load_blocks { | ||||||||||||||||||||||||||||
| 245 | 25 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
load_blocks: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 246 | 25 | $self->loaded_blocks; #just access lazy attribute | |||||||||||||||||||||||||||
| 247 | 25 | return; | |||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | sub init { | ||||||||||||||||||||||||||||
| 251 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
init: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 252 | 0 | $self->load_blocks(); | |||||||||||||||||||||||||||
| 253 | 0 | return; | |||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | sub _load_block { | ||||||||||||||||||||||||||||
| 257 | 76 | ▶ | my ( $self, $block_item ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_load_block: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 258 | 76 | my $block_name = $block_item->{block_name}; | |||||||||||||||||||||||||||
| 259 | 76 | my $new_block; | |||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | # Initialize with global (scenario) parameters | ||||||||||||||||||||||||||||
| 262 | 76 | my %params = ( %{ $self->_global_params }, scenario => $self ); | |||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | # which can be overriden by (local) block parameters. | ||||||||||||||||||||||||||||
| 265 | 76 | foreach my $param ( @{ $block_item->{block_parameters} } ) { | |||||||||||||||||||||||||||
| 266 | 61 | my ( $name, $value ) = split /=/, $param, 2; | |||||||||||||||||||||||||||
| 267 | 61 | $params{$name} = $value; | |||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | 76 | ▶ | eval "use $block_name; 1;" or log_fatal "Can't use block $block_name !\n$@\n"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 271 | 75 | ▶ | eval { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 272 | 75 | $new_block = $block_name->new( \%params ); | |||||||||||||||||||||||||||
| 273 | 75 | 1; | |||||||||||||||||||||||||||
| 274 | } or log_fatal "Treex::Core::Scenario->new: error when initializing block $block_name\n\nEVAL ERROR:\t$@"; | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | 75 | return $new_block; | |||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | sub run { | ||||||||||||||||||||||||||||
| 280 | 26 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
run: called CC=9 SCAR=22.1
Pod
documented
| |||||||||||||||||||||||||||||
| 281 | 26 | my $number_of_blocks = @{ $self->loaded_blocks }; | |||||||||||||||||||||||||||
| 282 | 26 | ▶ | log_fatal('No DocumentReader supplied') if !$self->_has_document_reader; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 283 | 26 | my $reader = $self->document_reader; | |||||||||||||||||||||||||||
| 284 | 26 | ▶ | my $number_of_documents = $reader->number_of_documents_per_this_job() || '?'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$reader->number_of_documents_per_this_job || '?'
MC/DC0%0/1
$reader->number_of_documents_per_this_job || '?'
$reader->number_of_documents_per_this_job
Truth table50%1/2
A
A= $reader->number_of_documents_per_this_job
| |||||||||||||||||||||||||||||
| 285 | 26 | my $document_number = 0; | |||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | 26 | log_info "Applying process_start"; | |||||||||||||||||||||||||||
| 288 | 26 | foreach my $block ( @{ $self->loaded_blocks } ) { | |||||||||||||||||||||||||||
| 289 | 39 | $block->process_start(); | |||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | 26 | ▶ | while ( my $document = $reader->next_document_for_this_job() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 293 | 29 | $document_number++; | |||||||||||||||||||||||||||
| 294 | 29 | my $doc_name = $document->full_filename; | |||||||||||||||||||||||||||
| 295 | 29 | my $doc_from = $document->loaded_from; | |||||||||||||||||||||||||||
| 296 | 29 | log_info "Document $document_number/$number_of_documents $doc_name loaded from $doc_from"; | |||||||||||||||||||||||||||
| 297 | 29 | my $block_number = 0; | |||||||||||||||||||||||||||
| 298 | 29 | foreach my $block ( @{ $self->loaded_blocks } ) { | |||||||||||||||||||||||||||
| 299 | 41 | $block_number++; | |||||||||||||||||||||||||||
| 300 | 41 | log_info "Applying block $block_number/$number_of_blocks " . ref($block); | |||||||||||||||||||||||||||
| 301 | 41 | $block->process_document($document); | |||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | # this actually marks the document as successfully done in parallel processing (if this line | ||||||||||||||||||||||||||||
| 305 | # does not appear in the output, the parallel process will fail -- it must appear at any errorlevel, | ||||||||||||||||||||||||||||
| 306 | # therefore not using log_info or similiar) | ||||||||||||||||||||||||||||
| 307 | 29 | ▶ | if ( $self->document_reader->jobindex ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 308 | 0 | print STDERR "Document $document_number/$number_of_documents $doc_name: [success].\n"; | |||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | 26 | log_info "Applying process_end"; | |||||||||||||||||||||||||||
| 313 | 26 | foreach my $block ( @{ $self->loaded_blocks } ) { | |||||||||||||||||||||||||||
| 314 | 39 | $block->process_end(); | |||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | 26 | ▶ | log_info "Processed $document_number document" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 318 | . ( $document_number == 1 ? '' : 's' ); | ||||||||||||||||||||||||||||
| 319 | 26 | return 1; | |||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | 42 | ▶ | use Module::Reload; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | sub restart { | ||||||||||||||||||||||||||||
| 325 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
restart: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 326 | 0 | my $changed_modules = Module::Reload->check; | |||||||||||||||||||||||||||
| 327 | 0 | log_info "Number of reloaded modules = $changed_modules"; | |||||||||||||||||||||||||||
| 328 | 0 | log_info "reseting the document reader\n"; | |||||||||||||||||||||||||||
| 329 | 0 | $self->document_reader->restart(); | |||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | # TODO rebuild the reloaded blocks | ||||||||||||||||||||||||||||
| 332 | 0 | return; | |||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | 41 | 1; | |||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | __END__ | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | =for Pod::Coverage BUILD | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | =encoding utf-8 | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | =head1 NAME | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | Treex::Core::Scenario - a larger Treex processing unit, composed of blocks | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | =head1 VERSION | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | version 0.08633_1 | ||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | use Treex::Core; | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | my $scenario = Treex::Core::Scenario->new(from_file => 'myscenario.scen' ); | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | $scenario->run; | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | A Treex scenario consists of a sequence of (possibly parametrized) Treex blocks. | ||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | Scenarios can be described by a simple textual format, which is either passed | ||||||||||||||||||||||||||||
| 367 | directly to the scenario construction, or is contained in a text file whose | ||||||||||||||||||||||||||||
| 368 | name is passed. | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | The string description of scenarios looks as follows. | ||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | 1) It contains a list of block names from which their 'C<Treex::Block::>' | ||||||||||||||||||||||||||||
| 373 | prefixes were removed. | ||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | 2) The block names are separated by one or more white spaces. | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | 3) The block names are listed in the same order in which they should be | ||||||||||||||||||||||||||||
| 378 | applied on data. | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | 4) For each block, there can be one or more parameters specified, using the | ||||||||||||||||||||||||||||
| 381 | C<attribute=value> form. | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | 5) Comments start with 'C<#>' and end with the nearest newline character. | ||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | Scenario example: | ||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | # morphological analysis of an English text | ||||||||||||||||||||||||||||
| 389 | Util::SetGlobal language=en selector=src | ||||||||||||||||||||||||||||
| 390 | Read::Text | ||||||||||||||||||||||||||||
| 391 | W2A::ResegmentSentences | ||||||||||||||||||||||||||||
| 392 | W2A::EN::Tokenize | ||||||||||||||||||||||||||||
| 393 | W2A::EN::NormalizeForms | ||||||||||||||||||||||||||||
| 394 | W2A::EN::FixTokenization | ||||||||||||||||||||||||||||
| 395 | W2A::EN::TagMorce | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | =head1 METHODS | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | =head2 Constructor | ||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | =over 4 | ||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | =item my $scenario = Treex::Core::Scenario->new(from_string => 'W2A::Tokenize language=en W2A::Lemmatize' ); | ||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | Constructor parameter C<from_string> specifies the names of blocks which are | ||||||||||||||||||||||||||||
| 407 | to be executed (in the specified order) when the scenario is applied on a | ||||||||||||||||||||||||||||
| 408 | L<Treex::Core::Document> object. | ||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | =item my $scenario = Treex::Core::Scenario->new(from_file => 'myscenario.scen' ); | ||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | The scenario description is loaded from the file. | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | =back | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | =head2 Running the scenario | ||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||
| 419 | =over 4 | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | =item $scenario->run(); | ||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | Run the scenario. | ||||||||||||||||||||||||||||
| 424 | One of the blocks (usually the first one) must be the document reader (see | ||||||||||||||||||||||||||||
| 425 | L<Treex::Core::DocumentReader>) that produces the | ||||||||||||||||||||||||||||
| 426 | documents on which this scenario is applied. | ||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | =back | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | =head2 Internal methods for loading scenarios | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | =over 4 | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | =item _load_scenario_file($filename) | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | loads a scenario description from a file | ||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | =item parse_scenario_string | ||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | parses a textual description of a scenario | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | =item construct_scenario_string | ||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | constructs a scenario textual description from an existing scenario instance | ||||||||||||||||||||||||||||
| 445 | accepts named parameter multiline - when set, blocks are separated by newline instead of space | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | =item load_blocks | ||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | use blocks and call their constructors | ||||||||||||||||||||||||||||
| 450 | can be used for preloading blocks for e.g. server applications | ||||||||||||||||||||||||||||
| 451 | when running scenario blocks are loaded automatically | ||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | =item init | ||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | do all initialization so after this method scenario is ready to run | ||||||||||||||||||||||||||||
| 456 | currently just load blocks | ||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | =item restart | ||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||
| 460 | resets the document reader, in future it will rebuild reloaded blocks | ||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | =back | ||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||
| 467 | L<Treex::Core::Block> | ||||||||||||||||||||||||||||
| 468 | L<Treex::Core> | ||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | =head1 AUTHORS | ||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||
| 472 | Zdeněk Žabokrtský <zabokrtsky@ufal.mff.cuni.cz> | ||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||
| 474 | Martin Popel <popel@ufal.mff.cuni.cz> | ||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||
| 476 | David Mareček <marecek@ufal.mff.cuni.cz> | ||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||
| 478 | Tomáš Kraut <kraut@ufal.mff.cuni.cz> | ||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||
| 482 | Copyright © 2011 by Institute of Formal and Applied Linguistics, Charles University in Prague | ||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||
| 484 | This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||