Treex/Core/Bundle.pm

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