Treex/Core/Document.pm

filter: Statementstmt 88.5% 270 / 305 Branchbran 68.0% 64 / 94 Conditioncond 65.0% 13 / 20 MC/DCmcdc 41.6% 5 / 12 Subroutinesub 86.0% 37 / 43 Podpod 65.2% 15 / 23 totaltotal 81.2% 404 / 497 CC 76 SCAR 20.2
CC
76
Cov
83%
BUILD
20.1
retrieve_storable
13.5
_rebless_and_index
9.0
CRAP 7.5
« Treex/Block/Read/Treex.pm ↑ Summary Treex/Core/Log.pm »
1 package Treex::Core::Document;
2 33 $Treex::Core::Document::VERSION = '2.20210102';
3 33 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 33 ▶use Treex::Core::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 33 ▶use Treex::Core::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 33 ▶use Treex::Core::DocZone;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 33 ▶use Treex::Core::Bundle;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 33 ▶use Treex::PML;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 33 Treex::PML::UseBackends('PMLBackend');
11 33 Treex::PML::AddResourcePath( Treex::Core::Config->pml_schema_dir() );
12  
13 33 with 'Treex::Core::WildAttr';
14  
15 33 ▶use Scalar::Util qw( weaken reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 33 ▶use PerlIO::via::gzip;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 33 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 33 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 33 ▶use Lingua::Interset::FeatureStructure;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 33 has loaded_from => ( is => 'rw', isa => 'Str', default => '' );
23 33 has path => ( is => 'rw', isa => 'Str' );
24 33 has file_stem => ( is => 'rw', isa => 'Str', default => 'noname' );
25 33 has file_number => ( is => 'rw', isa => 'Str', builder => 'build_file_number' );
26 33 has compress => ( is => 'rw', isa => 'Bool', default => undef, documentation => 'compression to .gz' );
27 33 has storable => (
28 is => 'rw',
29 isa => 'Bool',
30 default => undef,
31 documentation => 'using Storable with gz compression instead of Treex::PML'
32 );
33  
34 33 has _hash => ( is => 'rw', isa => 'Str' );
35  
36 0 ▶sub get_hash {
Subroutine get_hash: not called CC=2 SCAR=17.9
Pod undocumented
37 0 my $self = shift;
38 0 ▶ if ( ! defined($self->_hash) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->_hash) 0 0 0
39 0 $Storable::canonical = 1;
40 0 $self->_set_hash(md5_hex(Storable::nfreeze($self)));
41 0 $Storable::canonical = 0;
42 }
43 0 return $self->_hash;
44 }
45  
46 0 ▶sub set_hash {
Subroutine set_hash: not called CC=1 SCAR=6.9
Pod undocumented
47 0 my ($self, $hash) = @_;
48  
49 0 $self->_set_hash($hash);
50  
51 0 return;
52 }
53  
54 has _pmldoc => (
55 isa => 'Treex::PML::Document',
56 is => 'rw',
57 init_arg => 'pml_doc',
58 writer => '_set_pmldoc',
59 handles => {
60 set_filename => 'changeFilename',
61 33 map { $_ => $_ }
62 qw( clone writeFile writeTo filename URL
63 changeFilename changeURL fileFormat changeFileFormat
64 backend changeBackend encoding changeEncoding userData
65 changeUserData metaData changeMetaData listMetaData
66 appData changeAppData listAppData
67  
68 documentRootData
69  
70 FS changeFS
71  
72 hint changeHint pattern_count pattern patterns
73 changePatterns tail changeTail
74  
75 trees changeTrees treeList tree delete_tree lastTreeNo notSaved
76 currentTreeNo currentNode nodes value_line value_line_list
77 determine_node_type )
78 },
79 builder => '_create_empty_pml_doc',
80 );
81  
82 has _index => (
83 is => 'rw',
84 32 ▶ default => sub { return {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
85 33 );
86  
87 has _backref => (
88 is => 'rw',
89 32 ▶ default => sub { return {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
90 33 );
91  
92 33 has _latest_node_number => ( # for generating document-unique IDs
93 is => 'rw',
94 default => 0,
95 );
96  
97 33 ▶use Treex::PML::Factory;
Subroutine BEGIN: called CC=1 SCAR=0.0
98 33 my $factory = Treex::PML::Factory->new();
99  
100 33 my $highest_file_number = 1;
101  
102 # the description attribute is stored inside the meta structures of pml documents,
103 # that is why it is not realized as a regular Moose attribute
104  
105 2 ▶sub set_description {
Subroutine set_description: called CC=1 SCAR=0.0
Pod undocumented
106 2 my ( $self, $attr_value ) = @_;
107  
108 return Treex::PML::Node::set_attr(
109 $self->metaData('pml_root')->{meta},
110 2 'description', $attr_value
111 );
112 }
113  
114 4 ▶sub description {
Subroutine description: called CC=1 SCAR=0.0
Pod documented
115 4 my $self = shift;
116 4 return Treex::PML::Node::attr( $self->metaData('pml_root')->{meta}, 'description' );
117 }
118  
119 31 ▶sub build_file_number {
Subroutine build_file_number: called CC=1 SCAR=0.0
Pod undocumented
120 31 return sprintf "%03d", $highest_file_number++;
121 }
122  
123 # Full filename without the extension
124 2 ▶sub full_filename {
Subroutine full_filename: called CC=5 SCAR=20.9
Pod documented
125 2 ▶ log_fatal 'Incorrect number of arguments' if @_ != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 2 2
126 2 my $self = shift;
127 2 my $path = '';
128 2 ▶ if (defined $self->path && $self->path ne ''){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->path and $self->path ne "") 0 2 2
Condition33%1/3
defined $self->path and $self->path ne ""
!ll&&!rl&&r
020
MC/DC0%0/2
defined $self->path and $self->path ne ""
defined $self->path $self->path ne ""
Truth table33%1/3
A and B
A= defined $self->pathB= $self->path ne ""
ABresult
0X0
100
111
129 0 $path = $self->path;
130 0 ▶ $path .= '/' if $path !~ m{/$};
Branch: 1 branch
BranchTrueFalseTotal
unless $path =~ m[/$] 0 0 0
131 }
132 2 return $path . $self->file_stem . $self->file_number;
133 }
134  
135 32 ▶sub BUILD {
Subroutine BUILD: called CC=18 SCAR=30.0
Pod undocumented
136 32 my $self = shift;
137 32 my ($params_rf) = @_;
138 32 my $pmldoc;
139  
140 32 ▶ if ( defined $params_rf ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params_rf) 32 0 32
141  
142 # creating Treex::Core::Document from an already existing Treex::PML::Document instance
143 32 ▶ if ( $params_rf->{pmldoc} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($params_rf->{'pmldoc'}) { } 0 32 32
elsif ($params_rf->{'filename'}) { } 10 22 32
144 0 $pmldoc = $params_rf->{pmldoc};
145 }
146  
147 # loading Treex::Core::Document from a file
148 elsif ( $params_rf->{filename} ) {
149  
150 10 ▶ if ( $params_rf->{filename} =~ /.streex$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params_rf->{'filename'} =~ /.streex$/) { } 0 10 10
151 0 log_fatal 'Storable (.streex) docs must be retrieved by Treex::Core::Document->retrieve_storable($filename)';
152 }
153  
154 else {
155  
156 # If the file contains invalid PML (e.g. unknown afun value)
157 # Treex::PML fails with die.
158 # TODO: we should rather catch the die message and report it via log_fatal
159 10 $pmldoc = eval {
160 # In r10421, ZŽ added here recover => 1:
161 # $factory->createDocumentFromFile( $params_rf->{filename}, { recover => 1 });
162 # However, if the file contains invalid PML (e.g. unknown afun value), the recover=>1 option
163 # results in returning a $pmldoc which seems to be OK, but it contains no bundles,
164 # so Treex crashes on subsequent blocks which is misleading for users.
165 # If we really want to be fault-tolerant, it seems we would need to set Treex::PML::Instance::Reader::STRICT=0,
166 # but I don't know enough about PML internals and I think it's better to make such errors fatal.
167 # Martin Popel
168 # 2018-03-05: Dan Zeman: adding recover=>1 again.
169 # Random strange errors happen with visually correct files on cluster in the PML backend ("extra content after document end").
170 # It is paralyzing my work.
171 10 $factory->createDocumentFromFile( $params_rf->{filename}, {'recover' => 1} );
172 };
173 10 ▶ if ($Treex::PML::FSError) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Treex::PML::FSError) 0 10 10
174 0 log_warn "Treex::PML::FSError: $Treex::PML::FSError";
175 0 log_warn "Trying to process the document anyway.";
176 }
177 10 ▶ log_fatal "Error while loading " . $params_rf->{filename} . ( $@ ? "\n$@" : '' )
Branch: 2 branches
BranchTrueFalseTotal
unless defined $pmldoc 0 10 10
$@ ? : 0 0 0
178 if !defined $pmldoc;
179 }
180 }
181 }
182  
183 # constructing treex document from an existing file
184 32 ▶ if ($pmldoc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pmldoc) 10 22 32
185 10 $self->_set_pmldoc($pmldoc);
186  
187 # ensuring Treex::Core types (partially copied from the factory)
188 # $doczone hashref will be reused as the blessed instance variable
189 10 for my $doczone ($self->get_all_zones()){
190 3 Treex::Core::DocZone->new($doczone);
191 }
192  
193 10 $self->_rebless_and_index();
194 }
195  
196 32 $self->deserialize_wild;
197 32 foreach my $bundle ( $self->get_bundles ) {
198 12 $bundle->deserialize_wild;
199 12 foreach my $bundlezone ( $bundle->get_all_zones ) {
200 14 foreach my $tree ( $bundlezone->get_all_trees ){
201 32 ▶ my $ordered = $tree->type->get_structure_name =~ /[at]-(root|node)/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$tree->type->get_structure_name =~ /[at]-(root|node)/ ? : 19 13 32
202 32 my $correct_ord = 0;
203 32 ▶ my @nodes = $tree->get_descendants( { add_self => 1, ($ordered ? (ordered => 1) : ()) } );
Branch: 1 branch
BranchTrueFalseTotal
$ordered ? : 19 13 32
204 32 foreach my $node (@nodes){
205 # normalize ord, so there are no gaps
206 42 ▶ if ($ordered){
Branch: 1 branch
BranchTrueFalseTotal
if ($ordered) 28 14 42
207 28 $node->_set_ord($correct_ord);
208 28 $correct_ord++;
209 }
210 42 $node->deserialize_wild;
211 # Interset, if present, must be deserialized after wild because it relies on wild to take care of the 'other' feature.
212 42 ▶ if ( $node->DOES('Treex::Core::Node::Interset') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->DOES("Treex::Core::Node::Interset")) 28 14 42
213 28 $node->deserialize_iset;
214 }
215 }
216 }
217 }
218 }
219  
220 32 return;
221 }
222  
223 10 ▶sub _rebless_and_index {
Subroutine _rebless_and_index: called CC=9 SCAR=22.0
224 10 my $self = shift;
225 10 foreach my $bundle ( $self->get_bundles ) {
226 12 bless $bundle, 'Treex::Core::Bundle';
227  
228 12 $bundle->_set_document($self);
229  
230 12 ▶ if ( defined $bundle->{zones} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bundle->{"zones"}) 8 4 12
231 8 foreach my $zone ( map { $_->value() } $bundle->{zones}->elements ) {
232  
233 # $zone hashref will be reused as the blessed instance variable
234 14 Treex::Core::BundleZone->new($zone);
235 14 $zone->_set_bundle($bundle);
236  
237 14 foreach my $tree ( $zone->get_all_trees ) {
238 32 my $layer;
239 32 ▶ if ( $tree->type->get_structure_name =~ /(\S)-(root|node|nonterminal|terminal)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tree->type->get_structure_name =~ /(\S)-(root|node|nonterminal|terminal)/) { } 32 0 32
240 32 $layer = uc($1);
241 }
242 else {
243 0 log_fatal "Unexpected member in zone structure: " . $tree->type->get_structure_name;
244 }
245 32 foreach my $node ( $tree, $tree->descendants ) { # must still call Treex::PML::Node's API
246 42 bless $node, "Treex::Core::Node::$layer";
247 42 $self->index_node_by_id( $node->get_id, $node );
248 42 ▶ if ($layer eq 'A' && $node->{iset}){
Branch: 1 branch
BranchTrueFalseTotal
if ($layer eq "A" and $node->{"iset"}) 3 39 42
Condition100%3/3
$layer eq "A" and $node->{"iset"}
!ll&&!rl&&r
3093
MC/DC100%2/2
$layer eq "A" and $node->{"iset"}
$layer eq "A" $node->{"iset"}
Truth table100%3/3
A and B
A= $layer eq "A"B= $node->{"iset"}
ABresult
0X0
100
111
249 3 $node->{iset} = Lingua::Interset::FeatureStructure->new(%{$node->{iset}});
250 }
251 }
252 32 $tree->_set_zone($zone);
253 }
254 }
255 }
256 }
257 10 return;
258 }
259  
260 0 ▶sub _pml_attribute_hash {
Subroutine _pml_attribute_hash: not called CC=1 SCAR=6.9
261 0 my $self = shift;
262 0 return $self->metaData('pml_root')->{meta};
263 }
264  
265 #my $_treex_schema_file = Treex::PML::ResolvePath( '.', 'treex_schema.xml', 1 );
266 33 my $_treex_schema_file = Treex::Core::Config->pml_schema_dir . "/" . 'treex_schema.xml';
267 33 ▶if ( not -f $_treex_schema_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $_treex_schema_file) 0 33 33
268 0 log_fatal "Can't find PML schema $_treex_schema_file";
269 }
270  
271 33 my $_treex_schema = Treex::PML::Schema->new( { filename => $_treex_schema_file } );
272  
273 32 ▶sub _create_empty_pml_doc { ## no critic (ProhibitUnusedPrivateSubroutines)
Subroutine _create_empty_pml_doc: called CC=1 SCAR=0.0
274 32 my $fsfile = Treex::PML::Document->create
275 (
276 name => "x", #$filename, ???
277 FS => Treex::PML::FSFormat->new(
278 {
279 'deepord' => ' N' # ???
280 }
281 ),
282 trees => [],
283 backend => 'PMLBackend',
284 encoding => "utf-8",
285 );
286  
287 32 $fsfile->changeMetaData( 'schema-url', 'treex_schema.xml' );
288 32 $fsfile->changeMetaData( 'schema', $_treex_schema );
289 32 $fsfile->changeMetaData( 'pml_root', { meta => {}, bundles => undef, } );
290 32 return $fsfile;
291 }
292  
293 # --- INDEXING
294  
295 414 ▶sub index_node_by_id {
Subroutine index_node_by_id: called CC=3 SCAR=11.0
Pod documented
296 414 my $self = shift;
297 414 my ( $id, $node ) = pos_validated_list(
298 \@_,
299 { isa => 'Treex::Type::Id' },
300 { isa => 'Maybe[Treex::Core::Node]' }, #jde to takhle?
301 );
302 414 my $index = $self->_index;
303 414 ▶ if ( defined $node ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $node) { } 303 111 414
304 303 $index->{$id} = $node;
305 303 weaken $index->{$id};
306  
307 303 my $refs = $node->_get_referenced_ids;
308 303 foreach my $type ( keys %{$refs} ) {
309 4 $self->index_backref( $type, $id, $refs->{$type} );
310 }
311 }
312 else {
313 111 delete $index->{$id};
314 }
315 414 return;
316 }
317  
318 # Add references to the reversed references list
319 24 ▶sub index_backref {
Subroutine index_backref: called CC=5 SCAR=16.1
Pod undocumented
320 24 my ( $self, $type, $source, $targets ) = @_;
321 24 my $backref = $self->_backref;
322  
323 24 foreach my $target ( @{$targets} ) {
324 30 ▶ next if ( !defined($target) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $target 5 25 30
325 25 ▶ my $target_backrefs = $backref->{$target} // {};
Condition100%2/2
$backref->{$target} // {}
l!l
1312
MC/DC100%1/1
$backref->{$target} // {}
$backref->{$target}
Truth table100%2/2
A
A= $backref->{$target}
Aresult
11
00
326 25 $backref->{$target} = $target_backrefs;
327  
328 25 ▶ $target_backrefs->{$type} = [] if ( !$target_backrefs->{$type} );
Branch: 1 branch
BranchTrueFalseTotal
unless $target_backrefs->{$type} 16 9 25
329 25 push @{ $target_backrefs->{$type} }, $source;
330 }
331 24 return;
332 }
333  
334 # Remove references from the reversed references list
335 10 ▶sub remove_backref {
Subroutine remove_backref: called CC=4 SCAR=14.0
Pod undocumented
336 10 my ( $self, $type, $source, $targets ) = @_;
337 10 my $backref = $self->_backref;
338  
339 10 foreach my $target ( @{$targets} ) {
340 16 ▶ next if ( !defined($target) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $target 0 16 16
341 16 my $target_backrefs = $backref->{$target};
342 16 ▶ next if ( !$target_backrefs );
Branch: 1 branch
BranchTrueFalseTotal
unless $target_backrefs 0 16 16
343  
344 16 $target_backrefs->{$type} = [ grep { $_ ne $source } @{ $target_backrefs->{$type} } ];
345 }
346 10 return;
347 }
348  
349 # Return a hash of references ( type->[nodes] ) leading to the node with the given id
350 24 ▶sub get_references_to_id {
Subroutine get_references_to_id: called CC=2 SCAR=6.9
Pod documented
351 24 my ( $self, $id ) = @_;
352 24 my $backref = $self->_backref;
353  
354 24 ▶ return if ( !$backref->{$id} );
Branch: 1 branch
BranchTrueFalseTotal
unless $backref->{$id} 2 22 24
355 22 return $backref->{$id}; # TODO clone ?
356 }
357  
358 # Remove all references and backreferences leading to the $node (calls remove_reference() on the source nodes)
359 69 ▶sub _remove_references_to_node {
Subroutine _remove_references_to_node: called CC=5 SCAR=16.1
360 69 my ( $self, $node ) = @_;
361 69 my $id = $node->id;
362 69 my $backref = $self->_backref;
363  
364 # First, delete backreferences to the $node
365 69 my $refs = $node->_get_referenced_ids();
366 69 foreach my $type ( keys %{$refs} ) {
367 1 $self->remove_backref( $type, $id, $refs->{$type} );
368 }
369  
370 # Second, delete references to the $node
371 69 ▶ return if ( !$backref->{$id} );
Branch: 1 branch
BranchTrueFalseTotal
unless $backref->{$id} 65 4 69
372 4 my $node_backref = $backref->{$id};
373  
374 4 foreach my $type ( keys %{$node_backref} ) {
375 6 foreach my $source ( @{ $node_backref->{$type} } ) {
376 6 $self->get_node_by_id($source)->remove_reference( $type, $id );
377 }
378 }
379  
380 # Third, delete backreferences from the $node
381 4 delete $backref->{$id};
382 4 return;
383 }
384  
385 183 ▶sub id_is_indexed {
Subroutine id_is_indexed: called CC=1 SCAR=0.0
Pod documented
386 183 my $self = shift;
387 183 my ($id) = pos_validated_list(
388 \@_,
389 { isa => 'Treex::Type::Id' },
390 );
391 183 return ( defined $self->_index->{$id} );
392 }
393  
394 71 ▶sub get_node_by_id {
Subroutine get_node_by_id: called CC=2 SCAR=7.9
Pod documented
395 71 my $self = shift;
396 71 my ($id) = pos_validated_list(
397 \@_,
398 { isa => 'Treex::Type::Id' },
399 );
400 71 ▶ if ( defined $self->_index->{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_index->{$id}) { } 71 0 71
401 71 return $self->_index->{$id};
402 }
403 else {
404 0 log_fatal "ID not indexed: id=\"$id\"";
405  
406 # This is something very fatal. Treex assumes every node ID to
407 # be valid and pointing to a node *in the given document*.
408 # (It is fine to have a node with no a/lex.rf
409 # attribute, but if the attribute is there, the value
410 # has to be an ID within the document.)
411 #
412 # If your data violates the requirement and your IDs point to
413 # a different document, the only hack we suggest is to drop such
414 # references...
415 }
416 0 return;
417 }
418  
419 0 ▶sub get_all_node_ids {
Subroutine get_all_node_ids: not called CC=2 SCAR=17.9
Pod documented
420 0 ▶ log_fatal('Incorrect number of arguments') if @_ != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 0 0
421 0 my $self = shift;
422 0 return ( keys %{ $self->_index } );
423 }
424  
425 # -------------------------------------- ACCESS TO BUNDLES -------------------
426  
427 98 ▶sub get_bundles {
Subroutine get_bundles: called CC=2 SCAR=7.1
Pod documented
428 98 ▶ log_fatal('Incorrect number of arguments') if @_ != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 98 98
429 98 my $self = shift;
430 98 return $self->trees;
431 }
432  
433 28 ▶sub create_bundle {
Subroutine create_bundle: called CC=6 SCAR=17.9
Pod documented
434 28 my ( $self, $arg_ref ) = @_;
435 28 my $fsfile = $self->_pmldoc();
436 28 my $new_bundle;
437 my $position_of_new;
438  
439 28 ▶ if ( $arg_ref and ( $arg_ref->{after} or $arg_ref->{before} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_ref and $arg_ref->{'after'} || $arg_ref->{'before'}) { } 2 26 28
Condition66%4/6
$arg_ref and $arg_ref->{"after"} || $arg_ref->{"before"}
!ll&&!rl&&r
2602
$arg_ref->{"after"} or $arg_ref->{"before"}
l!l&&r!l&&!r
110
MC/DC33%1/3
$arg_ref and $arg_ref->{"after"} || $arg_ref->{"before"}
$arg_ref $arg_ref->{"after"} $arg_ref->{"before"}
Truth table75%3/4
A and B or C
A= $arg_refB= $arg_ref->{"after"}C= $arg_ref->{"before"}
ABCresult
0XX0
1000
11X1
1011
440 2 ▶ my $reference_bundle = ( $arg_ref->{after} ) ? $arg_ref->{after} : $arg_ref->{before};
Branch: 1 branch
BranchTrueFalseTotal
$arg_ref->{'after'} ? : 1 1 2
441 2 my $position_of_reference = $reference_bundle->get_position;
442 2 ▶ $position_of_new = $position_of_reference + ( $arg_ref->{after} ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$arg_ref->{'after'} ? : 1 1 2
443 }
444  
445 else { # default: append at the end of the document
446 26 $position_of_new = scalar( $self->get_bundles() );
447 }
448  
449 28 $new_bundle = $fsfile->new_tree($position_of_new);
450 28 $new_bundle->set_type_by_name( $fsfile->metaData('schema'), 'bundle.type' );
451 28 bless $new_bundle, "Treex::Core::Bundle"; # is this correct/sufficient with Moose ????
452 28 $new_bundle->_set_document($self);
453  
454 28 $new_bundle->set_id( "s" . ( $fsfile->lastTreeNo + 1 ) );
455  
456 28 return $new_bundle;
457 }
458  
459 # ----------------------- ACCESS TO ZONES ------------------------------------
460  
461 4 ▶sub create_zone {
Subroutine create_zone: called CC=2 SCAR=6.9
Pod documented
462 4 my $self = shift;
463 4 my ( $language, $selector ) = pos_validated_list(
464 \@_,
465 { isa => 'Treex::Type::LangCode' },
466 { isa => 'Treex::Type::Selector', default => '' },
467 );
468  
469 4 my $new_zone = Treex::Core::DocZone->new(
470 {
471 'language' => $language,
472 'selector' => $selector
473 }
474 );
475  
476 4 my $new_element = Treex::PML::Seq::Element->new( 'zone', $new_zone );
477  
478 4 my $meta = $self->metaData('pml_root')->{meta};
479 4 ▶ if ( defined $meta->{zones} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $meta->{'zones'}) { } 2 2 4
480 2 $meta->{zones}->unshift_element_obj($new_element);
481 }
482 else {
483 2 $meta->{zones} = Treex::PML::Seq->new( [$new_element] );
484 }
485  
486 4 return $new_zone;
487 }
488  
489 15 ▶sub get_all_zones {
Subroutine get_all_zones: called CC=2 SCAR=6.9
Pod undocumented
490 15 my $self = shift;
491 15 my $meta = $self->metaData('pml_root')->{meta};
492 15 ▶ return if !$meta->{zones};
Branch: 1 branch
BranchTrueFalseTotal
unless $meta->{"zones"} 9 6 15
493  
494 # Each element is a pair [$name, $value]. We need just the values.
495 6 return map {$_->[1]} $meta->{zones}->elements;
496 }
497  
498 4 ▶sub get_zone {
Subroutine get_zone: called CC=4 SCAR=14.2
Pod documented
499 4 my $self = shift;
500 4 my ( $language, $selector ) = pos_validated_list(
501 \@_,
502 { isa => 'Treex::Type::LangCode' },
503 { isa => 'Treex::Type::Selector', default => '' },
504 );
505  
506 4 foreach my $zone ($self->get_all_zones()) {
507 12 ▶ return $zone if $zone->language eq $language && $zone->selector eq $selector;
Branch: 1 branch
BranchTrueFalseTotal
if $zone->language eq $language and $zone->selector eq $selector 4 8 12
Condition66%2/3
$zone->language eq $language and $zone->selector eq $selector
!ll&&!rl&&r
084
MC/DC50%1/2
$zone->language eq $language and $zone->selector eq $selector
$zone->language eq $language $zone->selector eq $selector
Truth table66%2/3
A and B
A= $zone->language eq $languageB= $zone->selector eq $selector
ABresult
0X0
100
111
508 }
509 0 return;
510 }
511  
512 0 ▶sub get_or_create_zone {
Subroutine get_or_create_zone: not called CC=2 SCAR=17.9
Pod documented
513 0 my $self = shift;
514 0 my ( $language, $selector ) = pos_validated_list(
515 \@_,
516 { isa => 'Treex::Type::LangCode' },
517 { isa => 'Treex::Type::Selector', default => '' },
518 );
519  
520 0 my $fs_zone = $self->get_zone( $language, $selector );
521 0 ▶ if ( not defined $fs_zone ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fs_zone) 0 0 0
522 0 $fs_zone = $self->create_zone( $language, $selector );
523 }
524 0 return $fs_zone;
525 }
526  
527 # -------------------- LOADING AND SAVING ------------------------------------
528  
529 0 ▶sub load {
Subroutine load: not called CC=1 SCAR=6.9
Pod documented
530 0 my $self = shift;
531 0 return $self->_pmldoc->load(@_);
532  
533 # TODO: this is unfinished: should be somehow connected with the code in BUILD
534 }
535  
536 11 ▶sub save {
Subroutine save: called CC=3 SCAR=11.0
Pod documented
537 11 my $self = shift;
538 11 my ($filename) = @_;
539  
540 11 ▶ if ( $filename =~ /\.streex$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ /\.streex$/) { } 1 10 11
541 1 ▶ open( my $F, ">:via(gzip)", $filename ) or log_fatal $!;
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
open my $F, ">:via(gzip)", $filename or log_fatal($!) 1 0 1
542 1 print $F Storable::nfreeze($self);
543 1 close $F;
544  
545 # using Storable::nstore_fd($self,*$F) emits 'Inappropriate ioctl for device'
546 }
547  
548 else {
549 10 $self->_serialize_all_wild();
550 10 return $self->_pmldoc->save(@_);
551 }
552  
553 1 return;
554 }
555  
556 10 ▶sub _serialize_all_wild {
Subroutine _serialize_all_wild: called CC=5 SCAR=16.1
557 10 my ($self) = @_;
558 10 $self->serialize_wild;
559 10 foreach my $bundle ( $self->get_bundles ) {
560 12 $bundle->serialize_wild;
561 12 foreach my $bundlezone ( $bundle->get_all_zones ) {
562 14 foreach my $node ( map { $_->get_descendants( { add_self => 1 } ) } $bundlezone->get_all_trees ) {
563 # Interset, if present, must be serialized before wild because it relies on wild to take care of the 'other' feature.
564 42 ▶ if ( $node->DOES('Treex::Core::Node::Interset') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->DOES("Treex::Core::Node::Interset")) 28 14 42
565 28 $node->serialize_iset;
566 }
567 42 $node->serialize_wild;
568 }
569 }
570 }
571 10 return;
572 }
573  
574 1 ▶sub retrieve_storable {
Subroutine retrieve_storable: called CC=12 SCAR=26.0
Pod documented
575 1 my ( $class, $file ) = @_; # $file stands for a file name, but it can be also file handle (needed by the TrEd backend for .streex)
576  
577 1 my $FILEHANDLE;
578 1 my $opened = 0;
579  
580 1 ▶ if ( ref($file) and reftype($file) eq 'GLOB' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file and builtin::reftype($file) eq 'GLOB') { } 0 1 1
Condition33%1/3
ref $file and builtin::reftype($file) eq "GLOB"
!ll&&!rl&&r
100
MC/DC0%0/2
ref $file and builtin::reftype($file) eq "GLOB"
ref $file builtin::reftype($file) eq "GLOB"
Truth table33%1/3
A and B
A= ref $fileB= builtin::reftype($file) eq "GLOB"
ABresult
0X0
100
111
581 0 $FILEHANDLE = $file;
582 }
583 else {
584 1 ▶ log_fatal "filename=$file, but Treex::Core::Document->retrieve_storable(\$filename) can be used only for .streex files"
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ /\.streex$/ 0 1 1
585 unless $file =~ /\.streex$/;
586 1 ▶ open $FILEHANDLE, "<:via(gzip)", $file or log_fatal($!);
Branch: 1 branch
BranchTrueFalseTotal
open $FILEHANDLE, "<:via(gzip)", $file or log_fatal($!) 1 0 1
587 1 $opened = 1;
588 }
589  
590 1 my $serialized;
591  
592 # reading it this way is silly, but both slurping the file or
593 # using Storable::retrieve_fd lead to errors when used with via(gzip)
594 1 ▶ while (<$FILEHANDLE>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $FILEHANDLE)) 5287 1 5288
595 5287 $serialized .= $_;
596 }
597  
598 1 ▶ if ( $opened ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opened) 1 0 1
599 1 close($FILEHANDLE);
600 }
601  
602 # my $retrieved_doc = Storable::retrieve_fd(*$FILEHANDLE) or log_fatal($!);
603 1 ▶ my $retrieved_doc = Storable::thaw($serialized) or log_fatal $!;
Branch: 1 branch
BranchTrueFalseTotal
my $retrieved_doc = Storable::thaw($serialized) or log_fatal($!) 1 0 1
604  
605 1 ▶ if ( not ref($file) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $file) 1 0 1
606 1 $retrieved_doc->set_loaded_from($file);
607 1 my ( $volume, $dirs, $file_name ) = File::Spec->splitpath($file);
608 1 $retrieved_doc->set_path( $volume . $dirs );
609  
610 # $retrieved_doc->changeFilename($file); # why this doesn't affect the name displayed in TrEd?
611 }
612  
613 # *.streex files saved before r8789 (2012-05-29) have no PML types with nodes, let's fix it
614 # TODO: delete this hack as soon as no such old streex files are needed.
615 1 foreach my $bundle ( $retrieved_doc->get_bundles() ) {
616 2 foreach my $bundlezone ( $bundle->get_all_zones() ) {
617 1 foreach my $node ( map { $_->get_descendants() } $bundlezone->get_all_trees() ) {
618  
619 # skip this hack if we are dealing with a new streex file
620 #return $retrieved_doc if $node->type;
621 # This shortcut does not work since old files have only *some* nodes without types
622 1 $node->fix_pml_type();
623 }
624 }
625 }
626  
627 1 return $retrieved_doc;
628 }
629  
630 33 __PACKAGE__->meta->make_immutable;
631  
632 33 1;
633  
634 __END__
« Treex/Block/Read/Treex.pm ↑ Summary Treex/Core/Log.pm »