lib/Treex/Core/Bundle.pm

filter: Statementstmt 86.3% 114 / 132 Branchbran 65.6% 21 / 32 Conditioncond 71.4% 10 / 14 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 88.0% 22 / 25 Podpod 78.5% 11 / 14 totaltotal 80.8% 181 / 224 CC 29 SCAR 20.4
CC
29
Cov
81%
get_all_trees
42.0
get_zone
7.0
create_zone
5.1
CRAP 7.7
« lib/Treex/Block/Read/BaseReader.pm ↑ Summary lib/Treex/Core/Node/EffectiveRelations.pm »
1 package Treex::Core::Bundle;
2 {
3 58 $Treex::Core::Bundle::VERSION = '0.08633_1';
4 }
5  
6 58 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 58 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 58 ▶use Treex::Core::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 58 ▶use MooseX::NonMoose;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 58 extends 'Treex::PML::Node';
13 58 with 'Treex::Core::WildAttr';
14  
15 58 has document => (
16 is => 'ro',
17 writer => '_set_document',
18 reader => 'get_document',
19 weak_ref => 1,
20 );
21  
22 58 has id => ( is => 'rw' );
23  
24 58 ▶use Treex::Core::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 58 ▶use Treex::Core::Node::A;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 58 ▶use Treex::Core::Node::T;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 58 ▶use Treex::Core::Node::N;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 58 ▶use Treex::Core::Node::P;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 58 ▶use Treex::Core::BundleZone;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 58 ▶use Treex::Core::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 # --------- ACCESS TO ZONES ------------
34  
35 sub BUILD {
36 0 ▶ log_fatal 'Because of node indexing, no bundles can be created outside of documents. '
Subroutine BUILD: not called CC=1 SCAR=6.9
Pod undocumented
37 . 'You have to use $document->create_bundle() instead of $bundle->new().';
38  
39 }
40  
41 sub get_zone {
42 60 ▶ my $self = shift;
Subroutine get_zone: called CC=7 SCAR=19.5
Pod documented
43 60 my ( $language, $selector ) = pos_validated_list(
44 \@_,
45 { isa => 'Treex::Type::LangCode' },
46 { isa => 'Treex::Type::Selector', default => '' },
47 );
48 60 ▶ if ( defined $self->{zones} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"zones"}) 41 19 60
49 41 foreach my $element ( $self->{zones}->elements ) {
50 54 my ( undef, $value ) = @$element; # $name is not needed
51 54 ▶ if (( $value->{language} eq $language or $language eq 'mul' )
Branch: 1 branch
BranchTrueFalseTotal
if ($value->{"language"} eq $language || $language eq "mul" and ($value->{"selector"} || "") eq $selector) 35 19 54
Condition87%7/8
$value->{"language"} eq $language || $language eq "mul" and ($value->{"selector"} || "") eq $selector
!ll&&!rl&&r
11835
$value->{"language"} eq $language or $language eq "mul"
l!l&&r!l&&!r
5301
$value->{'selector'} || ''
l!l
2132
MC/DC66%2/3
$value->{"language"} eq $language || $language eq "mul" and ($value->{"selector"} || "") eq $selector
$value->{"language"} eq $language $language eq "mul" $value->{'selector'}
Truth table60%3/5
A or B and C
A= $value->{"language"} eq $languageB= $language eq "mul"C= $value->{'selector'}
ABCresult
00X0
1X00
0100
1X11
0111
52 and ( $value->{selector} || '' ) eq $selector
53 )
54 {
55 35 return $value;
56 }
57 }
58 }
59 25 return;
60 }
61  
62 sub create_zone {
63 23 ▶ my $self = shift;
Subroutine create_zone: called CC=5 SCAR=16.2
Pod documented
64 23 my ( $language, $selector, $params_rf ) = pos_validated_list(
65 \@_,
66 { isa => 'Treex::Type::LangCode' },
67 { isa => 'Treex::Type::Selector', default => '' },
68 { isa => 'Ref' },
69 );
70  
71 21 ▶ if ( $self->get_zone( $language, $selector ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_zone($language, $selector)) 1 20 21
72 1 ▶ if (defined $params_rf and $params_rf->{overwrite}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params_rf and $params_rf->{'overwrite'}) { } 0 1 1
Condition33%1/3
defined $params_rf and $params_rf->{"overwrite"}
!ll&&!rl&&r
100
MC/DC0%0/2
defined $params_rf and $params_rf->{"overwrite"}
defined $params_rf $params_rf->{"overwrite"}
Truth table33%1/3
A and B
A= defined $params_rfB= $params_rf->{"overwrite"}
ABresult
0X0
100
111
73  
74 }
75 else {
76 1 log_fatal("Bundle already contains a zone with language='$language' and selector='$selector'. "
77 . "Use create_zone(...,{overwrite=>1}) to remove it first.")
78 }
79 }
80  
81  
82  
83 20 my $new_zone = Treex::Core::BundleZone->new(
84 {
85 'language' => $language,
86 'selector' => $selector,
87 }
88 );
89  
90 20 my $new_element = Treex::PML::Seq::Element->new( 'zone', $new_zone );
91  
92 20 $new_zone->_set_bundle($self);
93  
94 # $new_subbundle->set_type_by_name( $self->get_document->metaData('schema'), 'zone' );
95  
96 20 ▶ if ( defined $self->{zones} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'zones'}) { } 2 18 20
97 2 $self->{zones}->unshift_element_obj($new_element);
98 }
99 else {
100 18 $self->{zones} = Treex::PML::Seq->new( [$new_element] );
101 }
102  
103 20 return $new_zone;
104 }
105  
106 sub get_or_create_zone {
107 4 ▶ my $self = shift;
Subroutine get_or_create_zone: called CC=2 SCAR=6.9
Pod documented
108 4 my ( $language, $selector ) = pos_validated_list(
109 \@_,
110 { isa => 'Treex::Type::LangCode' },
111 { isa => 'Treex::Type::Selector', default => '' },
112 );
113 4 my $zone = $self->get_zone( $language, $selector );
114 4 ▶ if ( !defined $zone ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $zone) 1 3 4
115 1 $zone = $self->create_zone( $language, $selector );
116 }
117 4 return $zone;
118 }
119  
120 sub get_all_zones {
121 115 ▶ my $self = shift;
Subroutine get_all_zones: called CC=2 SCAR=6.9
Pod documented
122 115 ▶ if ( $self->{zones} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'zones'}) { } 94 21 115
123 94 return map { $_->value() } $self->{zones}->elements;
124 }
125 else {
126 21 return ();
127 }
128 }
129  
130 sub remove_zone {
131 6 ▶ my ( $self, $language, $selector ) = @_;
Subroutine remove_zone: called CC=4 SCAR=14.5
Pod documented
132  
133 6 my $zone = $self->get_zone( $language, $selector );
134 6 ▶ if ( !$zone ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($zone) 0 6 6
135 0 log_fatal "Non-existing zone cannot be removed";
136 }
137  
138 # remove all trees first, so that their nodes are correctly removed from the index
139 6 foreach my $tree ( $zone->get_all_trees ) {
140 15 $zone->remove_tree( $tree->get_layer );
141 }
142  
143 6 ▶ $self->{zones}->delete_value($zone)
Branch: 1 branch
BranchTrueFalseTotal
$self->{"zones"}->delete_value($zone) or log_fatal("Zone to be deleted was not found. This should never happen") 0 6 6
144 or log_fatal "Zone to be deleted was not found. This should never happen";
145 6 return;
146 }
147  
148 sub remove {
149 3 ▶ my ( $self ) = @_;
Subroutine remove: called CC=4 SCAR=13.9
Pod undocumented
150  
151 # clean the bundle's content first (to ensure de-indexing)
152 3 foreach my $zone ( $self->get_all_zones ) {
153 2 $self->remove_zone( $zone->language, $zone->selector );
154 }
155  
156 3 my $position = 0;
157  
158 # find the bundle's position (this is quite inefficient, as the info about
159 # bundle's position is stored nowhere), and delete the bundle using Treex::PML API
160 BUNDLE:
161 3 foreach my $bundle ( $self->get_document->get_bundles ) {
162 6 ▶ if ( $bundle eq $self ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bundle eq $self) { } 3 3 6
163 3 last BUNDLE;
164 }
165 else {
166 3 $position++;
167 }
168 }
169  
170 3 $self->get_document->delete_tree($position);
171 3 bless $self, 'Treex::Core::Node::Removed';
172 3 return;
173 }
174  
175  
176 # --------- ACCESS TO TREES ------------
177  
178 sub get_all_trees {
179 0 ▶ my $self = shift;
Subroutine get_all_trees: not called CC=6 SCAR=37.4
Pod documented
180 0 ▶ if ($Treex::Core::Config::params_validate) { ## no critic (ProhibitPackageVars)
Branch: 1 branch
BranchTrueFalseTotal
if ($Treex::Core::Config::params_validate) 0 0 0
181 0 pos_validated_list( \@_ );
182 }
183  
184 0 ▶ return () if !$self->{zones};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"zones"} 0 0 0
185  
186 0 my @trees;
187 0 foreach my $zone ( $self->{zones}->elements ) {
188 0 my $structure = $zone->value;
189 0 foreach my $layer (Treex::Core::Types::layers()) {
190 0 $layer = lc $layer;
191 0 ▶ if ( exists $structure->{trees}->{"${layer}_tree"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $structure->{"trees"}{"${layer}_tree"}) 0 0 0
192 0 push @trees, $structure->{trees}->{"${layer}_tree"};
193 }
194 }
195 }
196 0 return @trees;
197  
198 }
199  
200 sub create_tree {
201 4 ▶ my $self = shift;
Subroutine create_tree: called CC=1 SCAR=0.0
Pod documented
202 4 my ( $language, $layer, $selector ) = pos_validated_list(
203 \@_,
204 { isa => 'Treex::Type::LangCode' },
205 { isa => 'Treex::Type::Layer' },
206 { isa => 'Treex::Type::Selector', default => '' }
207 );
208  
209 4 my $zone = $self->get_or_create_zone( $language, $selector );
210 4 my $tree_root = $zone->create_tree($layer);
211 4 return $tree_root;
212 }
213  
214 sub get_tree {
215 5 ▶ my $self = shift;
Subroutine get_tree: called CC=2 SCAR=7.0
Pod documented
216 5 my ( $language, $layer, $selector ) = pos_validated_list(
217 \@_,
218 { isa => 'Treex::Type::LangCode' },
219 { isa => 'Treex::Type::Layer' },
220 { isa => 'Treex::Type::Selector', default => '' }
221 );
222  
223 5 my $zone = $self->get_zone( $language, $selector );
224 5 ▶ log_fatal "Unavailable zone for selector=$selector language=$language\n" if !$zone;
Branch: 1 branch
BranchTrueFalseTotal
unless $zone 0 5 5
225 5 return $zone->get_tree($layer);
226 }
227  
228 sub has_tree {
229 5 ▶ my $self = shift;
Subroutine has_tree: called CC=2 SCAR=7.0
Pod documented
230 5 my ( $language, $layer, $selector ) = pos_validated_list(
231 \@_,
232 { isa => 'Treex::Type::LangCode' },
233 { isa => 'Treex::Type::Layer' },
234 { isa => 'Treex::Type::Selector', default => '' }
235 );
236 5 my $zone = $self->get_zone( $language, $selector );
237 5 ▶ return defined $zone && $zone->has_tree($layer);
Condition66%2/3
defined $zone && $zone->has_tree($layer)
!ll&&!rl&&r
104
MC/DC50%1/2
defined $zone && $zone->has_tree($layer)
defined $zone $zone->has_tree($layer)
Truth table66%2/3
A && B
A= defined $zoneB= $zone->has_tree($layer)
ABresult
0X0
100
111
238 }
239  
240 sub get_position {
241 2 ▶ my ($self) = @_;
Subroutine get_position: called CC=4 SCAR=14.0
Pod documented
242  
243 # search for position of the bundle
244 # (ineffective, because there's no caching of positions of bundles so far)
245 2 my $position_of_reference;
246 2 my $fsfile = $self->get_document->_pmldoc;
247 2 foreach my $position ( 0 .. $fsfile->lastTreeNo ) {
248 3 ▶ if ( $fsfile->tree($position) eq $self ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fsfile->tree($position) eq $self) 2 1 3
249 2 $position_of_reference = $position;
250 2 last;
251 }
252 }
253  
254 2 ▶ if ( !defined $position_of_reference ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $position_of_reference) 0 2 2
255 0 log_fatal "document structure inconsistency: can't detect position of bundle $self";
256 }
257  
258 2 return $position_of_reference;
259 }
260  
261 # --------- ACCESS TO ATTRIBUTES ------------
262  
263 sub get_attr {
264 0 ▶ my $self = shift;
Subroutine get_attr: not called CC=1 SCAR=6.9
Pod undocumented
265 0 my ($attr_name) = pos_validated_list(
266 \@_,
267 { isa => 'Str' },
268 );
269 0 return $self->{$attr_name};
270 }
271  
272 # ------- other -------------
273  
274 sub following {
275 37 ▶ Treex::Core::Node::following(@_);
Subroutine following: called CC=1 SCAR=0.0
Pod documented
276 }
277  
278 58 __PACKAGE__->meta->make_immutable;
279  
280 58 1;
281  
282 __END__
283  
284  
285 =for Pod::Coverage BUILD set_attr get_attr
286  
287 =encoding utf-8
288  
289 =head1 NAME
290  
291 Treex::Core::Bundle - a set of equivalent sentences in the Treex framework
292  
293 =head1 VERSION
294  
295 version 0.08633_1
296  
297 =head1 DESCRIPTION
298  
299 A set of equivalent sentences (translations, or variants) and their linguistic representations in the Treex framework
300 A bundle in Treex corresponds to one sentence or more sentences, typically
301 translations or variants of each other, with all their linguistic
302 representations. Each bundle is divided into zones (instances of
303 L<Treex::Core::BundleZone>), each of them containing
304 exactly one sentence and its representations.
305  
306 =head1 ATTRIBUTES
307  
308 Each bundle has two attributes:
309  
310 =over 4
311  
312 =item id
313  
314 identifier accessible by the getter method C<id()> and by the setter method
315 C<set_id($id)>
316  
317 =item document
318  
319 the document (an instance of L<Treex::Core::Document>)
320 which this bundle belongs to; accessible only by the getter method C<document()>
321  
322 =back
323  
324  
325  
326 =head1 METHODS
327  
328 =head2 Construction
329  
330 You cannot create a bundle by a constructor from scratch. You can create a
331 bundle only within an existing documents, using the following methods of
332 L<Treex::Core::Document>:
333  
334 =over 4
335  
336 =item create_bundle
337  
338 =item new_bundle_before
339  
340 =item new_bundle_after
341  
342 =back
343  
344  
345 =head2 Access to zones
346  
347 Bundle zones are instances of
348 L<Treex::Core::BundleZone>, parametrized by language
349 code and possibly also by another free label called selector, whose purpose is
350 to distinguish zones for the same language but from a different source.
351  
352 =over 4
353  
354 =item my $zone = $bundle->create_zone( $langcode, ?$selector, ?$params_rf );
355  
356 If the third argument is {overwrite=>1}, then the newly created empty zone
357 overwrites the previously existing one (if any). Fatal error appears if
358 the zone to be created already exists and this switch is not used.
359  
360 =item my $zone = $bundle->get_zone( $langcode, ?$selector );
361  
362 =item my $zone = $bundle->get_or_create_zone( $langcode, ?$selector );
363  
364 =item my @zones = $bundle->get_all_zones();
365  
366 =back
367  
368  
369 =head2 Access to trees
370  
371 Even if trees are not contained directly in bundle (there is the intermediate
372 zone level), they can be accessed using the following shortcut methods:
373  
374 =over 4
375  
376 =item my $tree_root = $bundle->get_tree( $language, $layer, ?$selector);
377  
378  
379 =item my $tree_root = $bundle->create_tree( $language, $layer, ?$selector );
380  
381  
382 =item $bundle->has_tree( $language, $layer, ?$selector );
383  
384  
385 =item my @tree_roots = $bundle->get_all_trees();
386  
387 =back
388  
389  
390  
391 =head2 Other
392  
393 =over 4
394  
395 =item $bundle->remove_zone( $language, $selector );
396  
397 delete all zone's trees and remove the zone from the bundle
398  
399 =item my $position = $bundle->get_position();
400  
401 position of the bundle within the document (number, starting from 0)
402  
403 =back
404  
405  
406 =head1 AUTHOR
407  
408 Zdeněk Žabokrtský <zabokrtsky@ufal.mff.cuni.cz>
409  
410 =head1 COPYRIGHT AND LICENSE
411  
412 Copyright © 2011 by Institute of Formal and Applied Linguistics, Charles University in Prague
413  
414 This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
« lib/Treex/Block/Read/BaseReader.pm ↑ Summary lib/Treex/Core/Node/EffectiveRelations.pm »