Treex/Core/Scenario.pm

filter: Statementstmt 56.1% 177 / 315 Branchbran 41.6% 35 / 84 Conditioncond 11.5% 3 / 26 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 77.1% 27 / 35 Podpod 90.0% 9 / 10 totaltotal 51.7% 251 / 485 CC 63 SCAR 42.4
CC
63
Cov
51%
_run_with_cache
380.0
_build_parser
18.4
_build_loaded_blocks
12.4
CRAP 69.4
« Treex/Core/DocumentReader/Base.pm ↑ Summary Treex/Block/Util/DefinedAttr.pm »
1 package Treex::Core::Scenario;
2 15 $Treex::Core::Scenario::VERSION = '2.20210102';
3 16 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use Treex::Core::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use File::Slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use File::chdir;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 #use Parse::RecDescent 1.967003; now using standalone version
11  
12 15 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 15 has from_string => (
20 is => 'ro',
21 isa => 'Str',
22 predicate => '_has_from_string',
23 documentation => q(String with scenario),
24 );
25  
26 15 has scenario_string => (
27 is => 'ro',
28 isa => 'Str',
29 builder => '_build_scenario_string',
30 lazy => 1,
31 );
32  
33 15 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 15 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 15 has document_reader => (
51 is => 'rw',
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 writers => (
60 is => 'rw',
61 does => 'ArrayRef[Treex::Block::Write::BaseWriter]',
62 15 ▶ default => sub { [] }
Subroutine __ANON__: called CC=1 SCAR=0.0
63 15 );
64  
65 has _global_params => (
66 is => 'ro',
67 isa => 'HashRef[Str]',
68 traits => ['Hash'],
69 15 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
70 15 handles => {
71 get_global_param => 'get',
72 set_global_param => 'set',
73  
74 #get_global_param_names => 'keys',
75 #set_verbose => [ set => 'verbose' ],
76 #get_verbose => [ get => 'verbose' ],
77 #set_language => [ set => 'language' ],
78 #get_language => [ get => 'language' ],
79 #... ?
80 },
81 );
82  
83 15 has parser => (
84 is => 'ro',
85 isa => 'Parse::RecDescent::_Runtime',
86 init_arg => undef,
87 builder => '_build_parser',
88 documentation => q{Parses treex scenarios}
89 );
90  
91 15 has runner => (
92 is => 'ro',
93 isa => 'Treex::Core::Run',
94 writer => '_set_runner',
95 weak_ref => 1,
96 documentation => 'Treex::Core::Run instance in which the scenario is running',
97 );
98  
99 15 has cache => (
100 is => 'rw',
101 isa => 'Maybe[Cache::Memcached]',
102 builder => '_build_cache',
103 );
104  
105 15 ▶sub _build_scenario_string {
Subroutine _build_scenario_string: called CC=3 SCAR=11.0
106 15 my $self = shift;
107 15 ▶ if ( $self->_has_from_file ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_has_from_file) { } 10 4 14
elsif ($self->_has_from_string) { } 4 0 4
108 11 return $self->_load_scenario_file( $self->from_file );
109 }
110 elsif ( $self->_has_from_string ) {
111 5 return $self->from_string;
112 }
113 1 log_fatal("You have to provide from_file or from_string attribute");
114 }
115  
116 15 my %sequence = ();
117  
118 12 ▶sub _build_loaded_blocks {
Subroutine _build_loaded_blocks: called CC=9 SCAR=25.1
119 12 my $self = shift;
120 12 my @block_items = @{ $self->block_items };
121 10 my $block_count = scalar @block_items;
122 10 my $i = 0;
123 10 my @loaded_blocks;
124  
125 10 my $sequence_from = 0;
126 10 my $sequence_hash = "";
127 10 foreach my $block_item (@block_items) {
128 21 $i++;
129 21 my $params = '';
130 21 ▶ if ( $block_item->{block_parameters} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block_item->{"block_parameters"}) 20 0 20
131 21 $params = join ' ', @{ $block_item->{block_parameters} };
132 }
133 20 log_info("Loading block $block_item->{block_name} $params ($i/$block_count)");
134 20 my $new_block = $self->_load_block($block_item);
135  
136 19 ▶ if ( $new_block->does('Treex::Core::DocumentReader') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($new_block->does('Treex::Core::DocumentReader')) { } 8 11 19
elsif ($new_block->isa('Treex::Block::Write::BaseWriter')) { } 1 10 11
137 8 ▶ log_fatal("Only one DocumentReader per scenario is permitted ($block_item->{block_name})")
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_document_reader 0 8 8
138 if $self->_has_document_reader;
139 8 $self->_set_document_reader($new_block);
140 }
141 elsif ( $new_block->isa('Treex::Block::Write::BaseWriter') ) {
142 1 push( @{ $self->writers }, $new_block );
143 1 push @loaded_blocks, $new_block; # duplicity
144 }
145 else {
146 10 ▶ if ( ref($new_block) eq "Treex::Core::CacheBlock" ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $new_block eq 'Treex::Core::CacheBlock') { } 0 10 10
elsif ($self->cache) { } 0 10 10
147 0 $sequence{$sequence_from}{from} = $sequence_from;
148 0 $sequence{$sequence_from}{to} = $i;
149 0 $sequence{$sequence_from}{hash} = $sequence_hash;
150  
151 0 $sequence{$i}{_from} = $sequence_from;
152 0 $sequence_from = $i;
153 0 push( @{ $sequence{$sequence_from}{block} }, $new_block->get_hash() );
154 0 $sequence_hash = $new_block->get_hash();
155 }
156 elsif ($self->cache) {
157 0 $sequence_hash = md5_hex( $sequence_hash . $new_block->get_hash() );
158 0 ▶ if ( defined( $sequence{$sequence_from} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sequence{$sequence_from}) 0 0 0
159 0 push( @{ $sequence{$sequence_from}{block} }, $new_block->get_hash() );
160 }
161 }
162  
163 10 push @loaded_blocks, $new_block;
164 }
165 }
166  
167 8 log_info('ALL BLOCKS SUCCESSFULLY LOADED.');
168 8 return \@loaded_blocks;
169 }
170  
171 15 ▶sub _load_parser {
Subroutine _load_parser: called CC=1 SCAR=0.0
172 14 my $self = shift;
173 14 require Treex::Core::ScenarioParser;
174 14 return Treex::Core::ScenarioParser->new();
175 }
176  
177 1 ▶sub _my_dir {
Subroutine _my_dir: called CC=1 SCAR=6.9
178 0 return dirname( (caller)[1] );
179 }
180  
181 15 ▶sub _build_parser {
Subroutine _build_parser: called CC=5 SCAR=29.1
182 14 my $self = shift;
183 14 my $parser;
184 14 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $parser = $self->_load_parser; 1 } } and return $parser 14 0 14
185 14 $parser = $self->_load_parser();
186 14 1;
187 } and return $parser;
188 0 log_info("Cannot find precompiled scenario parser, trying to build it from grammar");
189 0 my $dir = $self->_my_dir(); #get module's directory
190 0 my $file = "$dir/ScenarioParser.rdg"; #find grammar file
191 0 ▶ log_fatal("Cannot find grammar file") if !-e $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 0 0 0
192  
193 #in fact we should never reach this
194 0 log_warn('We should NOT reach this place. Treex distribution may be corrupted.');
195  
196 0 my $grammar = read_file($file); #load it
197 eval {
198 0 log_info("Trying to precompile it for you");
199 0 require Parse::RecDescent;
200 0 local $CWD = $dir;
201 0 Parse::RecDescent->Precompile( { -standalone => 1 }, $grammar, 'Treex::Core::ScenarioParser' );
202 0 $parser = $self->_load_parser();
203 0 1;
204 0 ▶ } or eval {
Branch: 1 branch
BranchTrueFalseTotal
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 } } or log_fatal("Cannot create Scenario parser") 0 0 0
Condition0%0/3
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 } }
l!l&&r!l&&!r
000
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 } }
ABresult
1X1
011
000
205 0 log_info("Cannot precompile, loading directly from grammar. Consider precompiling it manually");
206 0 require Parse::RecDescent;
207 0 $parser = Parse::RecDescent->new($grammar); #create parser
208 0 1;
209 } or log_fatal("Cannot create Scenario parser");
210 0 return $parser;
211 }
212  
213 14 ▶sub _build_cache {
Subroutine _build_cache: called CC=3 SCAR=15.0
214 14 my $self = shift;
215  
216 14 ▶ if ( $self->runner && $self->runner->cache ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->runner and $self->runner->cache) 0 14 14
Condition33%1/3
$self->runner and $self->runner->cache
!ll&&!rl&&r
1400
MC/DC0%0/2
$self->runner and $self->runner->cache
$self->runner $self->runner->cache
Truth table33%1/3
A and B
A= $self->runnerB= $self->runner->cache
ABresult
0X0
100
111
217  
218 0 require Treex::Core::CacheBlock;
219 0 require Treex::Tool::Memcached::Memcached;
220  
221 0 return Treex::Tool::Memcached::Memcached::get_connection(
222 "documents-cache"
223 );
224 }
225  
226 14 return;
227 }
228  
229 10 ▶sub _load_scenario_file {
Subroutine _load_scenario_file: called CC=2 SCAR=7.0
230 10 my ( $self, $scenario_filename ) = @_;
231 10 log_info "Loading scenario description $scenario_filename";
232 10 ▶ my $scenario_string = read_file( $scenario_filename, binmode => ':utf8', err_mode => 'quiet' )
Branch: 1 branch
BranchTrueFalseTotal
my $scenario_string = read_file($scenario_filename, "binmode", ":utf8", "err_mode", "quiet") or log_fatal("Can't open scenario file $scenario_filename") 10 0 10
233 or log_fatal "Can't open scenario file $scenario_filename";
234 10 return $scenario_string;
235 }
236  
237 14 ▶sub parse_scenario_string {
Subroutine parse_scenario_string: called CC=2 SCAR=6.9
Pod documented
238 14 my $self = shift;
239 14 my $scenario_string = $self->scenario_string;
240 14 my $from_file = $self->from_file;
241  
242 14 my $parsed = $self->parser->startrule( $scenario_string, 1, $from_file );
243 14 ▶ log_fatal("Cannot parse the scenario: $scenario_string") if !defined $parsed;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parsed 2 12 14
244 12 return $parsed;
245 }
246  
247 # reverse of parse_scenario_string, used in Treex::Core::Run for treex --dump
248 3 ▶sub construct_scenario_string {
Subroutine construct_scenario_string: called CC=5 SCAR=16.1
Pod documented
249 3 my $self = shift;
250 3 my %args = @_;
251 3 my $multiline = $args{multiline};
252 3 my @block_items = @{ $self->block_items };
253 3 ▶ my $delim = $multiline ? qq{\n} : q{ };
Branch: 1 branch
BranchTrueFalseTotal
$multiline ? : 1 2 3
254 3 my @block_strings;
255 3 foreach my $block_item (@block_items) {
256 7 my $name = $block_item->{block_name};
257 7 my @parameters = map { _add_quotes($_) } @{ $block_item->{block_parameters} };
258 7 ▶ $name =~ s{^Treex::Block::}{} or $name = "::$name"; #strip leading Treex::Block:: or add leading ::
Branch: 1 branch
BranchTrueFalseTotal
$name =~ s/^Treex::Block::// or $name = "::$name" 6 1 7
259 7 my $params;
260 7 ▶ if ( scalar @parameters ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @parameters) { } 2 5 7
261 2 $params = q{ } . join q{ }, @parameters;
262 }
263 else {
264 5 $params = q{};
265 }
266 7 push @block_strings, $name . $params;
267 }
268 3 return join $delim, @block_strings;
269 }
270  
271 0 ▶sub get_required_files {
Subroutine get_required_files: not called CC=2 SCAR=17.9
Pod undocumented
272 0 my $self = shift;
273 0 my @block_items = @{ $self->block_items };
274 0 my @required_files;
275 0 foreach my $block_item (@block_items) {
276 0 my $block = $self->_load_block($block_item);
277 push @required_files,
278 map {
279 0 $block_item->{block_name} . "\t" . $_;
280 } $block->get_required_share_files();
281 }
282 0 return @required_files;
283 }
284  
285 2 ▶sub _add_quotes { # adding quotes only if param. value contains a space
Subroutine _add_quotes: called CC=4 SCAR=22.6
286 2 my ($block_parameter) = @_;
287 2 my ( $name, $value ) = split /=/, $block_parameter, 2;
288 2 ▶ if ( $value =~ /\s/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /\s/) 0 2 2
289 0 my $res_string = "$name=";
290  
291 0 ▶ if ( $value =~ /'/ && $value !~ /"/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /'/ and not $value =~ /"/) { } 0 0 0
Condition0%0/3
$value =~ /'/ and not $value =~ /"/
!ll&&!rl&&r
000
MC/DC0%0/2
$value =~ /'/ and not $value =~ /"/
$value =~ /'/ not $value =~ /"/
Truth table0%0/3
A and not(B)
A= $value =~ /'/B= not $value =~ /"/
ABresult
0X0
100
111
292 0 $res_string .= '"' . $value . '"';
293 } else {
294 0 $value =~ s/'/\\'/g;
295 0 $res_string .= "'" . $value . "'";
296 }
297 0 return $res_string;
298 }
299 2 return $block_parameter;
300 }
301  
302 0 ▶sub load_blocks {
Subroutine load_blocks: not called CC=1 SCAR=6.9
Pod documented
303 0 my $self = shift;
304 0 $self->loaded_blocks; #just access lazy attribute
305 0 return;
306 }
307  
308 0 ▶sub init {
Subroutine init: not called CC=1 SCAR=6.9
Pod documented
309 0 my $self = shift;
310 0 $self->load_blocks();
311 0 return;
312 }
313  
314 20 ▶sub _load_block {
Subroutine _load_block: called CC=6 SCAR=18.1
315 20 my ( $self, $block_item ) = @_;
316 20 my $block_name = $block_item->{block_name};
317 20 my $new_block;
318  
319 # Initialize with global (scenario) parameters
320 20 my %params = ( %{ $self->_global_params }, scenario => $self );
321  
322 # which can be overriden by (local) block parameters.
323 20 foreach my $param ( @{ $block_item->{block_parameters} } ) {
324 12 my ( $name, $value ) = split /=/, $param, 2;
325 12 $params{$name} = $value;
326 }
327  
328 20 ▶ 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
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
eval "use $block_name; 1;" or log_fatal("Can't use block $block_name !\n$@\n") 19 1 20
329 19 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $new_block = $block_name->new(\%params); 1 } } or log_fatal("Treex::Core::Scenario->new: error when initializing block $block_name\n\nEVAL ERROR:\t$@") 19 0 19
330 19 $new_block = $block_name->new( \%params );
331 19 1;
332 } or log_fatal "Treex::Core::Scenario->new: error when initializing block $block_name\n\nEVAL ERROR:\t$@";
333  
334 19 ▶ if ( $self->cache && $params{'use_cache'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cache and $params{"use_cache"}) 0 19 19
Condition33%1/3
$self->cache and $params{"use_cache"}
!ll&&!rl&&r
1900
MC/DC0%0/2
$self->cache and $params{"use_cache"}
$self->cache $params{"use_cache"}
Truth table33%1/3
A and B
A= $self->cacheB= $params{"use_cache"}
ABresult
0X0
100
111
335 0 $new_block = Treex::Core::CacheBlock->new( { block => $new_block, cache => $self->cache } );
336 }
337  
338 19 return $new_block;
339 }
340  
341 1 ▶sub run {
Subroutine run: called CC=4 SCAR=14.1
Pod documented
342 1 my ($self) = @_;
343 1 my $number_of_blocks = @{ $self->loaded_blocks };
344 1 ▶ log_fatal('No DocumentReader supplied') if !$self->_has_document_reader;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_has_document_reader 0 1 1
345 1 my $reader = $self->document_reader;
346 1 ▶ my $number_of_documents = $reader->number_of_documents_per_this_job() || '?';
Condition50%1/2
$reader->number_of_documents_per_this_job || '?'
l!l
10
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
Aresult
11
00
347 1 my $document_number = 0;
348  
349 #if ( $self->cache ) {
350 # $document_number = $self->_run_with_cache( $reader, $number_of_blocks, $number_of_documents );
351 #}
352 #else {
353 1 $document_number = $self->_run_without_cache( $reader, $number_of_blocks, $number_of_documents );
354 #}
355  
356 1 ▶ log_info "Processed $document_number document"
Branch: 1 branch
BranchTrueFalseTotal
$document_number == 1 ? : 1 0 1
357 . ( $document_number == 1 ? '' : 's' );
358 1 return 1;
359 }
360  
361 0 ▶sub _run_with_cache {
Subroutine _run_with_cache: not called CC=19 SCAR=59.4
362  
363 0 my ( $self, $reader, $number_of_blocks, $number_of_documents ) = @_;
364 0 my $document_number = 0;
365  
366 0 ▶ while ( my $document = $reader->next_document_for_this_job() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $document = $reader->next_document_for_this_job) 0 0 0
367 0 $document_number++;
368 0 my $doc_name = $document->full_filename;
369 0 my $doc_from = $document->loaded_from;
370 0 log_info "Document $document_number/$number_of_documents $doc_name loaded from $doc_from";
371 0 my $block_number = 0;
372 0 my $skip_to = 0;
373 0 my $process = 0;
374 0 my $skip_from = 0;
375 0 my $from_hash = "";
376 0 my $document_last_hash = "";
377 0 foreach my $block ( @{ $self->loaded_blocks } ) {
378 0 $block_number++;
379 0 $process = 1;
380 0 ▶ if ( $block_number < $skip_to ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($block_number < $skip_to) { } 0 0 0
elsif ($block_number == $skip_to) { } 0 0 0
381  
382 # we know that there are identical, so we can skip them
383 0 log_info "Skipping block $block_number/$number_of_blocks " . ref($block);
384 0 $process = 0;
385 }
386 elsif ( $block_number == $skip_to ) {
387  
388 # this is border Cache block -> we have to check whether next sequence is also same
389 0 $skip_from = $block_number + 1;
390  
391 # following sequence is same => we can continue with skipping
392 0 ▶ if ($sequence{$skip_from}{'to'}
Branch: 1 branch
BranchTrueFalseTotal
if ($sequence{$skip_from}{'to'} and $self->_is_known_sequence($sequence{$skip_from}{'hash'}, $document->get_hash)) { } 0 0 0
Condition0%0/3
$sequence{$skip_from}{"to"} and $self->_is_known_sequence($sequence{$skip_from}{"hash"}, $document->get_hash)
!ll&&!rl&&r
000
MC/DC0%0/2
$sequence{$skip_from}{"to"} and $self->_is_known_sequence($sequence{$skip_from}{"hash"}, $document->get_hash)
$sequence{$skip_from}{"to"} $self->_is_known_sequence($sequence{$skip_from}{"hash"}, $document->get_hash)
Truth table0%0/3
A and B
A= $sequence{$skip_from}{"to"}B= $self->_is_known_sequence($sequence{$skip_from}{"hash"}, $document->get_hash)
ABresult
0X0
100
111
393 &&
394 $self->_is_known_sequence( $sequence{$skip_from}{'hash'}, $document->get_hash() )
395 )
396 {
397  
398 #log_warn("\tskip from " . $sequence{$skip_from}{from} . ' to ' . $sequence{$skip_from}{to});
399 0 $skip_to = $sequence{$skip_from}{to} - 1;
400 0 $from_hash = $document->get_hash();
401 0 $process = 0;
402 }
403 else {
404 0 $document_last_hash = $document->get_hash();
405  
406 #$document->set_hash(md5_hex($document->get_hash() . $block->get_hash()));
407 0 my $full_hash = $document->get_hash();
408 0 $document = $self->cache->get($full_hash);
409  
410 0 ▶ if ( !$document ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($document) 0 0 0
411 0 log_fatal("Document - $full_hash is missing!!!");
412 }
413 0 $process = 2;
414 }
415 }
416  
417 0 ▶ if ( $process == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($process == 1) 0 0 0
418 0 log_info "Applying block $block_number/$number_of_blocks " . ref($block);
419  
420 0 ▶ $block->process_start if !$block->is_started;
Branch: 1 branch
BranchTrueFalseTotal
unless $block->is_started 0 0 0
421 0 $block->_set_is_started(1);
422  
423 #log_info("Document-hash: " . $document->get_hash());
424 0 $skip_from = $block_number + 1;
425 0 my $status = $block->process_document($document);
426 0 ▶ if (defined($status)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $status and $status == $Treex::Core::Block::DOCUMENT_FROM_CACHE and $sequence{$skip_from}{"to"} and $self->_is_known_sequence($sequence{$skip_from}{"hash"}, $document->get_hash)) 0 0 0
Condition0%0/9
defined $status and $status == $Treex::Core::Block::DOCUMENT_FROM_CACHE and $sequence{$skip_from}{"to"} and $self->_is_known_sequence($sequence{$skip_from}{"hash"}, $document->get_hash)
!ll&&!rl&&r
000
defined $status and $status == $Treex::Core::Block::DOCUMENT_FROM_CACHE and $sequence{$skip_from}{"to"}
!ll&&!rl&&r
000
defined $status and $status == $Treex::Core::Block::DOCUMENT_FROM_CACHE
!ll&&!rl&&r
000
MC/DC0%0/4
defined $status and $status == $Treex::Core::Block::DOCUMENT_FROM_CACHE and $sequence{$skip_from}{"to"} and $self->_is_known_sequence($sequence{$skip_from}{"hash"}, $document->get_hash)
defined $status $status == $Treex::Core::Block::DOCUMENT_FROM_CACHE $sequence{$skip_from}{"to"} $self->_is_known_sequence($sequence{$skip_from}{"hash"}, $document->get_hash)
Truth table0%0/5
A and B and C and D
A= defined $statusB= $status == $Treex::Core::Block::DOCUMENT_FROM_CACHEC= $sequence{$skip_from}{"to"}D= $self->_is_known_sequence($sequence{$skip_from}{"hash"}, $document->get_hash)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
427 &&
428 $status == $Treex::Core::Block::DOCUMENT_FROM_CACHE &&
429 $sequence{$skip_from}{'to'} &&
430 $self->_is_known_sequence( $sequence{$skip_from}{'hash'}, $document->get_hash() )
431 )
432 {
433  
434 #log_warn("\tskip from " . $sequence{$skip_from}{from} . ' to ' . $sequence{$skip_from}{to});
435 0 $skip_to = $sequence{$skip_from}{to} - 1;
436 0 $skip_from = $block_number + 1;
437 0 $from_hash = $document->get_hash();
438 }
439 }
440  
441 0 $document_last_hash = $document->get_hash();
442 0 $document->set_hash( md5_hex( $document->get_hash() . $block->get_hash() ) );
443  
444 0 ▶ if ( ref($block) eq "Treex::Core::CacheBlock" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $block eq "Treex::Core::CacheBlock") 0 0 0
445  
446 # cache block => mark this path as known
447 0 my $id = $block_number + 1;
448 0 my $from = $sequence{$id}{'_from'};
449  
450 # the first sequence has no document
451 0 ▶ if ( defined( $sequence{$from}{'document'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sequence{$from}{"document"}) 0 0 0
452 0 $self->_set_known_sequence( $sequence{$from}{'hash'}, $sequence{$from}{'document'} );
453 }
454  
455 0 $sequence{$id}{'document'} = $document_last_hash;
456 }
457 }
458  
459 # this actually marks the document as successfully done in parallel processing (if this line
460 # does not appear in the output, the parallel process will fail -- it must appear at any errorlevel,
461 # therefore not using log_info or similiar)
462 0 ▶ if ( $self->document_reader->jobindex ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->document_reader->jobindex) 0 0 0
463 0 print STDERR "Document $document_number/$number_of_documents $doc_name: [success].\n";
464 }
465 }
466  
467 0 log_info "Applying process_end";
468  
469 0 foreach my $block ( @{ $self->loaded_blocks } ) {
470 0 ▶ $block->process_end() if ( $block->is_started );
Branch: 1 branch
BranchTrueFalseTotal
if $block->is_started 0 0 0
471 }
472  
473 0 return $document_number;
474 }
475  
476 0 ▶sub _is_known_sequence {
Subroutine _is_known_sequence: not called CC=1 SCAR=6.9
477 0 my ( $self, $sequence_hash, $document_hash ) = @_;
478 0 my $hash = md5_hex( $sequence_hash, $document_hash );
479 0 return $self->cache->get($hash);
480 }
481  
482 0 ▶sub _set_known_sequence {
Subroutine _set_known_sequence: not called CC=1 SCAR=6.9
483 0 my ( $self, $sequence_hash, $document_hash ) = @_;
484 0 my $hash = md5_hex( $sequence_hash, $document_hash );
485 0 $self->cache->set( $hash, 1 );
486  
487 0 return;
488 }
489  
490 1 ▶sub _run_without_cache {
Subroutine _run_without_cache: called CC=5 SCAR=16.2
491  
492 1 my ( $self, $reader, $number_of_blocks, $number_of_documents ) = @_;
493 1 my $document_number = 0;
494  
495 1 $self->start();
496  
497 1 ▶ while ( my $document = $reader->next_document_for_this_job() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $document = $reader->next_document_for_this_job) 1 1 2
498 1 $document_number++;
499 1 my $doc_name = $document->full_filename;
500 1 my $doc_from = $document->loaded_from;
501 1 log_info "Document $document_number/$number_of_documents $doc_name loaded from $doc_from";
502 1 my $block_number = 0;
503 1 foreach my $block ( @{ $self->loaded_blocks } ) {
504 2 $block_number++;
505 2 log_info "Applying block $block_number/$number_of_blocks " . ref($block);
506 2 $block->process_document($document);
507 }
508  
509 # this actually marks the document as successfully done in parallel processing (if this line
510 # does not appear in the output, the parallel process will fail -- it must appear at any errorlevel,
511 # therefore not using log_info or similiar)
512 1 ▶ if ( $self->document_reader->jobindex ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->document_reader->jobindex) 0 1 1
513 0 print STDERR "Document $document_number/$number_of_documents $doc_name: [success].\n";
514 }
515 }
516  
517 1 $self->end();
518  
519 1 ▶ log_info "Processed $document_number document"
Branch: 1 branch
BranchTrueFalseTotal
$document_number == 1 ? : 1 0 1
520 . ( $document_number == 1 ? '' : 's' );
521  
522 1 return $document_number;
523 }
524  
525 # Apply process_start to all blocks for which this has not yet been applied
526 1 ▶sub start {
Subroutine start: called CC=3 SCAR=11.0
Pod documented
527 1 my ($self) = @_;
528  
529 1 log_info "Applying process_start";
530 1 foreach my $block ( @{ $self->loaded_blocks } ) {
531 2 ▶ $block->process_start() if !$block->is_started;
Branch: 1 branch
BranchTrueFalseTotal
unless $block->is_started 2 0 2
532 2 $block->_set_is_started(1);
533 }
534  
535 1 return;
536 }
537  
538 # Apply the scenario to documents given in parameter
539 0 ▶sub apply_to_documents {
Subroutine apply_to_documents: not called CC=3 SCAR=24.8
Pod documented
540  
541 0 my ( $self, @documents ) = @_;
542  
543 0 my $number_of_blocks = @{ $self->loaded_blocks };
544 0 my $block_number = 0;
545  
546 0 foreach my $document (@documents){
547 0 log_info "Processing document" . $document->full_filename;
548  
549 0 foreach my $block ( @{ $self->loaded_blocks } ) {
550 0 $block_number++;
551 0 log_info "Applying block $block_number/$number_of_blocks " . ref($block);
552 0 $block->process_document($document);
553 }
554 }
555  
556 0 return;
557 }
558  
559 # Apply process_end to all blocks for which this has not yet been applied
560 1 ▶sub end {
Subroutine end: called CC=3 SCAR=11.0
Pod documented
561 1 my ($self) = @_;
562  
563 1 log_info "Applying process_end";
564 1 foreach my $block ( @{ $self->loaded_blocks } ) {
565 2 ▶ $block->process_end() if ( $block->is_started );
Branch: 1 branch
BranchTrueFalseTotal
if $block->is_started 2 0 2
566 }
567  
568 1 return;
569 }
570  
571 16 ▶use Module::Reload;
Subroutine BEGIN: called CC=1 SCAR=0.0
572  
573 0 ▶sub restart {
Subroutine restart: not called CC=1 SCAR=6.9
Pod documented
574 0 my ($self) = @_;
575 0 my $changed_modules = Module::Reload->check;
576 0 log_info "Number of reloaded modules = $changed_modules";
577 0 log_info "reseting the document reader\n";
578 0 $self->document_reader->restart();
579  
580 # TODO rebuild the reloaded blocks
581 0 return;
582 }
583  
584 15 1;
585  
586 __END__
« Treex/Core/DocumentReader/Base.pm ↑ Summary Treex/Block/Util/DefinedAttr.pm »