Treex/Core/Block.pm

filter: Statementstmt 38.1% 93 / 244 Branchbran 12.8% 10 / 78 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 66.6% 20 / 30 Podpod 61.5% 8 / 13 totaltotal 33.1% 131 / 395 CC 59 SCAR 36.9
CC
59
Cov
30%
_try_process_layer
110.0
process_bundle
90.0
_apply_function_on_each_zone
72.0
CRAP 40.2
« Treex/Core/DocumentReader/ZoneReader.pm ↑ Summary Treex/Core/RememberArgs.pm »
1 package Treex::Core::Block;
2 15 $Treex::Core::Block::VERSION = '2.20210102';
3 15 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use Treex::Core::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use Treex::Core::Resource;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use App::whichpm 'which_pm';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use List::MoreUtils qw(uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 15 has selector => ( is => 'ro', isa => 'Str', default => '' );
14 15 has language => ( is => 'ro', isa => 'Str', default => 'all' );
15  
16 15 has scenario => (
17 is => 'ro',
18 isa => 'Treex::Core::Scenario',
19 writer => '_set_scenario',
20 weak_ref => 1,
21 );
22  
23 15 has select_bundles => (
24 is => 'ro',
25 default => 0,
26 documentation => 'apply process_bundle only on the specified bundles,'
27 . ' e.g. "1-4,6,8-12". The default is 0 which means all bundles. Useful for debugging.',
28 );
29  
30 15 has if_missing_zone => (
31 is => 'ro',
32 isa => enum( [qw(fatal warn ignore create)] ),
33 default => 'fatal',
34 documentation => 'What to do if process_zone is to be called on a zone'
35 . ' (specified by parameters language and selector) that is missing in a given bundle?',
36 );
37  
38 15 has if_missing_tree => (
39 is => 'ro',
40 isa => enum( [qw(fatal warn ignore create)] ),
41 default => 'fatal',
42 documentation => 'What to do if process_[atnp]tree is to be called on a tree'
43 . ' that is missing in a given zone?',
44 );
45  
46 15 has if_missing_bundles => (
47 is => 'ro',
48 isa => enum( [qw(fatal warn ignore)] ),
49 default => 'fatal',
50 documentation => 'What to do if process_document is to be called on a document'
51 . ' with no bundles?',
52 );
53  
54  
55  
56 15 has report_progress => (
57 is => 'ro',
58 isa => 'Str',
59 default => 0,
60 documentation => 'Report which bundle (TODO: zone,tree,node) is being processed via log_info. Useful for debugging.',
61 );
62  
63 15 has [qw(_is_bundle_selected _is_language_selected _is_selector_selected)] => ( is => 'rw' );
64  
65 15 has _hash => ( is => 'rw', isa => 'Str' );
66  
67 15 has is_started => ( is => 'ro', isa => 'Bool', writer => '_set_is_started', default => 0 );
68  
69 15 Readonly our $DOCUMENT_PROCESSED => 1;
70 15 Readonly our $DOCUMENT_FROM_CACHE => 2;
71  
72  
73 # For load_other_block() and get_or_load_other_block()
74 # TODO this could also be in Scenario instead of Block...
75  
76 # new other block of same name replaces old one here
77 12 ▶has _loaded_other_blocks => ( is => 'rw', isa => 'HashRef', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
78 # all loaded other blocks, no replacing
79 12 ▶has _loaded_other_blocks_array => ( is => 'rw', isa => 'ArrayRef', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
80  
81 0 ▶sub zone_label {
Subroutine zone_label: not called CC=4 SCAR=30.0
Pod documented
82 0 my ($self) = @_;
83 0 ▶ my $label = $self->language or return;
Branch: 1 branch
BranchTrueFalseTotal
my $label = $self->language or return 0 0 0
84 0 ▶ if ( defined $self->selector && $self->selector ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->selector and $self->selector ne "") 0 0 0
Condition0%0/3
defined $self->selector and $self->selector ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->selector and $self->selector ne ""
defined $self->selector $self->selector ne ""
Truth table0%0/3
A and B
A= defined $self->selectorB= $self->selector ne ""
ABresult
0X0
100
111
85 0 $label .= '_' . $self->selector;
86 }
87 0 return $label;
88 }
89  
90 # TODO
91 # has robust => ( is=> 'ro', isa=>'Bool', default=>0,
92 # documentation=>'no fatal errors in robust mode');
93  
94 12 ▶sub BUILD {
Subroutine BUILD: called CC=12 SCAR=29.2
Pod undocumented
95 12 my $self = shift;
96  
97 12 ▶ if ( $self->select_bundles ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->select_bundles) 0 12 12
98 0 ▶ log_fatal 'select_bundles=' . $self->select_bundles . ' does not match /^\d+(-\d+)?(,\d+(-\d+)?)*$/'
Branch: 1 branch
BranchTrueFalseTotal
unless $self->select_bundles =~ /^\d+(-\d+)?(,\d+(-\d+)?)*$/ 0 0 0
99 if $self->select_bundles !~ /^\d+(-\d+)?(,\d+(-\d+)?)*$/;
100 0 my %selected;
101 0 foreach my $span ( split /,/, $self->select_bundles ) {
102 0 ▶ if ( $span =~ /(\d+)-(\d+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($span =~ /(\d+)-(\d+)/) { } 0 0 0
103 0 @selected{ $1 .. $2 } = ( $1 .. $2 );
104 }
105 else {
106 0 $selected{$span} = 1;
107 }
108 }
109 0 $self->_set_is_bundle_selected( \%selected );
110 }
111  
112 12 ▶ if ( $self->language ne 'all' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->language ne "all") 11 1 12
113 11 my @codes = split /,/, $self->language;
114 11 my %selected;
115 11 for my $code (@codes) {
116 11 ▶ log_fatal "'$code' is not a valid ISO 639 language code"
Branch: 1 branch
BranchTrueFalseTotal
unless Treex::Core::Types::is_lang_code($code) 0 11 11
117 if !Treex::Core::Types::is_lang_code($code);
118 11 $selected{$code} = 1;
119 }
120 11 $self->_set_is_language_selected( \%selected );
121 }
122  
123 12 ▶ if ( $self->selector ne 'all' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->selector ne "all") 12 0 12
124 12 ▶ if ( $self->selector eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->selector eq '') { } 11 1 12
125 11 $self->_set_is_selector_selected( { q{} => 1 } );
126 }
127 else {
128 1 my @selectors = split /,/, $self->selector;
129 1 my %selected;
130 1 for my $selector (@selectors) {
131 1 ▶ log_fatal "'$selector' is not a valid selector name"
Branch: 1 branch
BranchTrueFalseTotal
unless $selector =~ /^[a-z\d]*$/i 0 1 1
132 if $selector !~ /^[a-z\d]*$/i;
133 1 $selected{$selector} = 1;
134 }
135 1 $self->_set_is_selector_selected( \%selected );
136 }
137 }
138  
139 12 return;
140 }
141  
142 0 ▶sub _compute_hash {
Subroutine _compute_hash: not called CC=2 SCAR=15.2
143 0 my $self = shift;
144  
145 0 my $md5 = Digest::MD5->new();
146  
147 # compute block parameters hash
148 0 my $params_str = "";
149 map {
150 0 $params_str .= $_ . "=" . $self->{$_};
151  
152 # log_warn("\t\t" . $_ . "=" . $self->{$_} . " - " . ref($self->{$_}));
153 }
154 sort # in canonical form
155 0 grep { !ref( $self->{$_} ) } # no references
156 0 grep { defined( $self->{$_} ) } # value has to be defined
157 0 grep { !/(scenario|block)/ }
158 0 keys %{$self};
159  
160 # Digest::MD5 cannot handle Unicode strings (it dies with "Wide character in subroutine entry")
161 15 ▶ use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
162 0 $md5->add(Encode::encode_utf8($params_str));
163  
164 # compute block source code hash
165 0 my ( $block_filename, $block_version ) = which_pm( $self->get_block_name() );
166 0 ▶ open( my $block_fh, "<", $block_filename ) or log_fatal("Can't open '$block_filename': $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $block_fh, "<", $block_filename or log_fatal("Can't open '${block_filename}': $!") 0 0 0
167 0 binmode($block_fh);
168 0 $md5->addfile($block_fh);
169 0 close($block_fh);
170  
171 0 $self->_set_hash( $md5->hexdigest );
172  
173 0 return;
174 }
175  
176 0 ▶sub get_hash {
Subroutine get_hash: not called CC=2 SCAR=17.9
Pod undocumented
177 0 my $self = shift;
178 0 ▶ if (!$self->_hash){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_hash) 0 0 0
179 0 $self->_compute_hash();
180 }
181 0 return $self->_hash;
182 }
183  
184 2 ▶sub require_files_from_share {
Subroutine require_files_from_share: called CC=1 SCAR=0.6
Pod documented
185 2 my ( $self, @rel_paths ) = @_;
186 2 my $my_name = 'the block ' . $self->get_block_name();
187 return map {
188 0 log_info $self->get_block_name() . " requires file " . $_;
189 0 Treex::Core::Resource::require_file_from_share( $_, $my_name )
190 } @rel_paths;
191 }
192  
193 2 ▶sub get_required_share_files {
Subroutine get_required_share_files: called CC=1 SCAR=0.0
Pod documented
194 2 my ($self) = @_;
195  
196 # By default there are no required share files.
197 # The purpose of this method is to be overriden if needed.
198 2 return ();
199 }
200  
201 0 ▶sub process_document {
Subroutine process_document: not called CC=7 SCAR=40.3
Pod documented
202 0 my $self = shift;
203 0 my ($document) = pos_validated_list(
204 \@_,
205 { isa => 'Treex::Core::Document' },
206 );
207  
208 0 ▶ if ( !$document->get_bundles() && $self->if_missing_bundles =~ /fatal|warn/){
Branch: 1 branch
BranchTrueFalseTotal
if (not $document->get_bundles and $self->if_missing_bundles =~ /fatal|warn/) 0 0 0
Condition0%0/3
not $document->get_bundles and $self->if_missing_bundles =~ /fatal|warn/
!ll&&!rl&&r
000
MC/DC0%0/2
not $document->get_bundles and $self->if_missing_bundles =~ /fatal|warn/
not $document->get_bundles $self->if_missing_bundles =~ /fatal|warn/
Truth table0%0/3
not(A) and B
A= not $document->get_bundlesB= $self->if_missing_bundles =~ /fatal|warn/
ABresult
0X0
100
111
209 0 my $message = "There are no bundles in the document and block " . $self->get_block_name() .
210 " doesn't override the method process_document. You can use prepend 'Util::SetGlobal if_missing_bundles=ignore' to allow processing empty documents. ";
211 0 ▶ log_fatal($message) if $self->if_missing_bundles eq 'fatal';
Branch: 1 branch
BranchTrueFalseTotal
if $self->if_missing_bundles eq "fatal" 0 0 0
212 0 log_warn($message);
213 }
214  
215 0 my $bundleNo = 1;
216 0 foreach my $bundle ( $document->get_bundles() ) {
217 0 ▶ if ( !$self->select_bundles || $self->_is_bundle_selected->{$bundleNo} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->select_bundles or $self->_is_bundle_selected->{$bundleNo}) 0 0 0
Condition0%0/3
not $self->select_bundles or $self->_is_bundle_selected->{$bundleNo}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $self->select_bundles or $self->_is_bundle_selected->{$bundleNo}
not $self->select_bundles $self->_is_bundle_selected->{$bundleNo}
Truth table0%0/3
not(A) or B
A= not $self->select_bundlesB= $self->_is_bundle_selected->{$bundleNo}
ABresult
1X1
011
000
218 0 $self->process_bundle( $bundle, $bundleNo );
219 }
220 0 $bundleNo++;
221 }
222 0 return 1;
223 }
224  
225 0 ▶sub _apply_function_on_each_zone {
Subroutine _apply_function_on_each_zone: not called CC=8 SCAR=42.8
226 0 my ($self, $doc, $function, @function_params) = @_;
227 0 my %zones;
228  
229 # When using "all", we must collect the zones used in the whole document.
230 0 ▶ if ($self->language eq 'all' || $self->selector eq 'all'){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->language eq 'all' or $self->selector eq 'all') { } 0 0 0
Condition0%0/3
$self->language eq "all" or $self->selector eq "all"
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->language eq "all" or $self->selector eq "all"
$self->language eq "all" $self->selector eq "all"
Truth table0%0/3
A or B
A= $self->language eq "all"B= $self->selector eq "all"
ABresult
1X1
011
000
231 0 foreach my $bundle ($doc->get_bundles){
232 0 foreach my $zone ($bundle->get_all_zones()){
233 0 $zones{$zone->get_label()} = 1;
234 }
235 }
236 }
237 # Otherwise, we can make a Cartesian product of lang(uage)s and sel(ector)s
238 else {
239 0 foreach my $lang (keys %{$self->_is_language_selected}){
240 0 foreach my $sel (keys %{$self->_is_selector_selected}){
241 0 $zones{$lang . '_' . $sel} = 1;
242 }
243 }
244 }
245  
246 0 my $orig_language = $self->language;
247 0 my $orig_selector = $self->selector;
248 0 foreach my $label (keys %zones){
249 0 my ($lang, $sel) = split /_/, $label;
250  
251 # pretend this block was called with only this one language and selector
252 0 $self->{language} = $lang;
253 0 $self->{selector} = $sel;
254 0 $function->(@function_params);
255 }
256 0 $self->{language} = $orig_language;
257 0 $self->{selector} = $orig_selector;
258 0 return;
259 }
260  
261 0 ▶sub process_bundle {
Subroutine process_bundle: not called CC=9 SCAR=45.0
Pod documented
262 0 my ( $self, $bundle, $bundleNo ) = @_;
263 0 ▶ if ($self->report_progress){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->report_progress) 0 0 0
264 0 log_info "Processing bundle $bundleNo";
265 }
266  
267 0 my @zones = $bundle->get_all_zones();
268  
269 0 ▶ if ($self->if_missing_zone eq 'create') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->if_missing_zone eq 'create') { } 0 0 0
270 0 my (@langs, @sels);
271 0 ▶ if ($self->language eq 'all') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->language eq 'all') { } 0 0 0
272 0 @langs = uniq map{$_->language} @zones;
273 } else {
274 0 @langs = keys %{$self->_is_language_selected};
275 }
276 0 ▶ if ($self->selector eq 'all') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->selector eq 'all') { } 0 0 0
277 0 @sels = uniq map{$_->selector} @zones;
278 } else {
279 0 @sels = keys %{$self->_is_selector_selected};
280 }
281  
282 # Cartesian product of lang(uage)s and sel(ector)s
283 0 @zones = map {my $l = $_; map{$bundle->get_or_create_zone($l, $_)} @sels} @langs;
284 } else {
285 0 @zones = $self->get_selected_zones(@zones);
286 }
287  
288 0 ▶ if (!@zones && $self->if_missing_zone =~ /fatal|warn/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @zones and $self->if_missing_zone =~ /fatal|warn/) 0 0 0
Condition0%0/3
not @zones and $self->if_missing_zone =~ /fatal|warn/
!ll&&!rl&&r
000
MC/DC0%0/2
not @zones and $self->if_missing_zone =~ /fatal|warn/
not @zones $self->if_missing_zone =~ /fatal|warn/
Truth table0%0/3
not(A) and B
A= not @zonesB= $self->if_missing_zone =~ /fatal|warn/
ABresult
0X0
100
111
289 0 my $message = "No zone (language="
290 . $self->language
291 . ", selector="
292 . $self->selector
293 . ") was found in a bundle and block " . $self->get_block_name()
294 . " doesn't override the method process_bundle";
295 0 ▶ log_fatal($message) if $self->if_missing_zone eq 'fatal';
Branch: 1 branch
BranchTrueFalseTotal
if $self->if_missing_zone eq "fatal" 0 0 0
296 0 log_warn($message);
297 }
298  
299 0 foreach my $zone (@zones) {
300 0 $self->process_zone( $zone, $bundleNo );
301 }
302 0 return;
303 }
304  
305 1 ▶sub get_selected_zones {
Subroutine get_selected_zones: called CC=3 SCAR=11.1
Pod undocumented
306 1 my ( $self, @zones ) = @_;
307 1 ▶ if ( $self->language ne 'all') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->language ne "all") 1 0 1
308 1 @zones = grep { $self->_is_language_selected->{ $_->language } } @zones;
309 }
310 1 ▶ if ( $self->selector ne 'all') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->selector ne "all") 1 0 1
311 1 @zones = grep { $self->_is_selector_selected->{ $_->selector } } @zones;
312 }
313  
314 1 return @zones;
315 }
316  
317 0 ▶sub _try_process_layer {
Subroutine _try_process_layer: not called CC=10 SCAR=47.0
318 0 my ( $self, $zone, $layer, $bundleNo ) = @_;
319 0 my $meta = $self->meta;
320  
321 0 ▶ if ( my $m = $meta->find_method_by_name("process_${layer}tree") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $m = $meta->find_method_by_name("process_${layer}tree")) 0 0 0
322 0 ▶ if (!$zone->has_tree($layer)){
Branch: 1 branch
BranchTrueFalseTotal
unless ($zone->has_tree($layer)) 0 0 0
323 0 ▶ if ($self->if_missing_tree eq 'create'){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->if_missing_tree eq 'create') { } 0 0 0
324 0 $zone->create_tree($layer);
325 } else {
326 0 return 0;
327 }
328 }
329  
330 #$self->process_atree($tree, $bundleNo);
331 0 $m->execute( $self, $zone->get_tree($layer), $bundleNo );
332 0 return 1;
333 }
334  
335 0 ▶ if ( my $m = $meta->find_method_by_name("process_${layer}node") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $m = $meta->find_method_by_name("process_${layer}node")) 0 0 0
336 0 ▶ if (!$zone->has_tree($layer)){
Branch: 1 branch
BranchTrueFalseTotal
unless ($zone->has_tree($layer)) 0 0 0
337 0 ▶ if ($self->if_missing_tree eq 'create'){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->if_missing_tree eq 'create') { } 0 0 0
338 0 $zone->create_tree($layer);
339 } else {
340 0 return 0;
341 }
342 }
343 0 my $tree = $zone->get_tree($layer);
344  
345 # process_ptree should be executed also on the root node (usually the S phrase)
346 0 ▶ my @opts = $layer eq 'p' ? ( { add_self => 1 } ) : ();
Branch: 1 branch
BranchTrueFalseTotal
$layer eq 'p' ? : 0 0 0
347 0 foreach my $node ( $tree->get_descendants(@opts) ) {
348 # Skip nodes deleted by previous process_Xnode() call.
349 0 ▶ next if ref $node eq 'Treex::Core::Node::Deleted';
Branch: 1 branch
BranchTrueFalseTotal
if ref $node eq "Treex::Core::Node::Deleted" 0 0 0
350  
351 #$self->process_anode($node, $bundleNo);
352 0 $m->execute( $self, $node, $bundleNo );
353 }
354 0 return 1;
355 }
356  
357 0 return 0;
358 }
359  
360 0 ▶sub process_zone {
Subroutine process_zone: not called CC=6 SCAR=37.4
Pod documented
361 0 my ( $self, $zone, $bundleNo ) = @_;
362 0 my $overriden = 0;
363  
364 0 for my $layer (qw(a t n p)) {
365 0 ▶ if ($self->_try_process_layer( $zone, $layer, $bundleNo )){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_try_process_layer($zone, $layer, $bundleNo)) 0 0 0
366 0 $overriden++;
367 }
368 }
369  
370 0 ▶ if (!$overriden && $self->if_missing_tree =~ /fatal|warn/){
Branch: 1 branch
BranchTrueFalseTotal
if (not $overriden and $self->if_missing_tree =~ /fatal|warn/) 0 0 0
Condition0%0/3
not $overriden and $self->if_missing_tree =~ /fatal|warn/
!ll&&!rl&&r
000
MC/DC0%0/2
not $overriden and $self->if_missing_tree =~ /fatal|warn/
not $overriden $self->if_missing_tree =~ /fatal|warn/
Truth table0%0/3
not(A) and B
A= not $overridenB= $self->if_missing_tree =~ /fatal|warn/
ABresult
0X0
100
111
371 my $message = "At least one of the methods /process_(document|bundle|zone|[atnp](tree|node))/ "
372 . "must be overriden and the corresponding [atnp] trees must be present in bundles.\n"
373 . "The zone '" . $zone->get_label() . "' contains trees ( "
374 0 . ( join ',', map { $_->get_layer() } $zone->get_all_trees() ) . ").";
375 0 ▶ log_fatal($message) if $self->if_missing_tree eq 'fatal';
Branch: 1 branch
BranchTrueFalseTotal
if $self->if_missing_tree eq "fatal" 0 0 0
376 0 log_warn($message);
377 }
378  
379 0 return;
380 }
381  
382 2 ▶sub process_start {
Subroutine process_start: called CC=1 SCAR=0.0
Pod documented
383 2 my ($self) = @_;
384  
385 2 $self->require_files_from_share( $self->get_required_share_files() );
386  
387 2 return;
388 }
389  
390 1 ▶sub process_end {
Subroutine process_end: called CC=1 SCAR=0.0
391 1 my ($self) = @_;
392  
393 # default implementation is empty, but can be overriden
394 1 return;
395 }
396  
397 1 ▶after 'process_end' => sub {
Subroutine __ANON__: called CC=3 SCAR=14.2
398 1 my ($self) = @_;
399 1 foreach my $other_block (@{$self->_loaded_other_blocks_array}) {
400 0 ▶ if ( $other_block->is_started ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($other_block->is_started) 0 0 0
401 0 $other_block->process_end();
402 }
403 }
404 1 $self->_set_is_started(0);
405 15 };
406  
407 2 ▶sub get_block_name {
Subroutine get_block_name: called CC=1 SCAR=0.0
Pod documented
408 2 my $self = shift;
409 2 return ref($self);
410 }
411  
412 0 ▶sub load_other_block {
Subroutine load_other_block: not called CC=3 SCAR=24.8
Pod undocumented
413 0 my ($self, $other_block_name, $params_hash_ref) = @_;
414  
415 0 my $other_block_full_name = "Treex::Block::$other_block_name";
416  
417 # CONSTRUCT PARAMETERS HASH
418 # global params (TODO: do that?)
419 0 my %params = %{$self->scenario->_global_params};
420 # overridden by selected (TODO: all?) block params
421 0 $params{language} = $self->language;
422 0 $params{selector} = $self->selector;
423 0 $params{scenario} = $self->scenario;
424 # overridden by locally set params
425 0 @params{ keys %$params_hash_ref } = values %$params_hash_ref;
426  
427 # CREATE IT and start it
428 0 ▶ eval "use $other_block_full_name; 1;" or
Branch: 1 branch
BranchTrueFalseTotal
eval "use $other_block_full_name; 1;" or log_fatal('Treex::Core::Block->get_other_block: ' . "Can't use block $other_block_name!\n$@\n") 0 0 0
429 log_fatal "Treex::Core::Block->get_other_block: " .
430 "Can't use block $other_block_name!\n$@\n";
431 0 my $other_block;
432 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $other_block = $other_block_full_name->new(\%params); 1 } } or log_fatal('Treex::Core::Block->get_other_block: ' . "Can't initialize block $other_block_name!\n$@\n") 0 0 0
433 0 $other_block = $other_block_full_name->new( \%params );
434 0 1;
435 } or log_fatal "Treex::Core::Block->get_other_block: " .
436 "Can't initialize block $other_block_name!\n$@\n";
437 0 $other_block->process_start();
438  
439 # this may replace older block with same name
440 0 $self->_loaded_other_blocks->{$other_block_name} = $other_block;
441 # this not
442 0 push @{$self->_loaded_other_blocks_array}, $other_block;
443  
444 0 return $other_block;
445 }
446  
447 0 ▶sub get_or_load_other_block {
Subroutine get_or_load_other_block: not called CC=2 SCAR=17.9
Pod undocumented
448 0 my ($self, $other_block_name, $params_hash_ref) = @_;
449  
450 my $other_block =
451 exists ($self->_loaded_other_blocks->{$other_block_name})
452 ?
453 0 ▶ $self->_loaded_other_blocks->{$other_block_name}
Branch: 1 branch
BranchTrueFalseTotal
exists $self->_loaded_other_blocks->{$other_block_name} ? : 0 0 0
454 :
455 $self->load_other_block($other_block_name, $params_hash_ref)
456 ;
457  
458 0 return $other_block;
459 }
460  
461 15 1;
462  
463 __END__
« Treex/Core/DocumentReader/ZoneReader.pm ↑ Summary Treex/Core/RememberArgs.pm »