PML/Instance.pm

filter: Statementstmt 38.9% 223 / 573 Branchbran 13.9% 47 / 336 Conditioncond 11.1% 24 / 215 MC/DCmcdc 5.9% 7 / 118 Subroutinesub 48.6% 35 / 72 Podpod 90.7% 39 / 43 totaltotal 27.6% 375 / 1357 CC 265 SCAR 61.9
CC
265
Cov
26%
set_data
1806.0
__for_each_match
1056.0
get_all
930.0
CRAP 489.4
« PML/Backend/CSTS/Csts2fs.pm ↑ Summary PML/Backend/TEIXML.pm »
1 package Treex::PML::Instance;
2  
3 9 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 9 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 9 require Exporter;
12 9 import Exporter qw(import);
13  
14 }
15  
16 9 ▶use Scalar::Util qw(weaken blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 9 ▶use UNIVERSAL::DOES;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 9 ▶use Treex::PML::Instance::Common qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 9 ▶use Treex::PML::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 9 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 9 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 9 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 9 ▶use URI::file;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 9 our $DEFAULT_ENCODING = 'utf-8';
25  
26 9 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 =begin comment
29  
30 TODO
31  
32 Note: correct writing with XSLT requires XML::LibXML >= 1.59 (!!!)
33  
34 (GENERAL):
35  
36 - improve reading/writing trees (use the live object)
37 Postponing because:
38 1/ sequences of tree/no-tree objects are problematic
39 2/ changing this would break binary compatibility
40  
41 - Treex::PML:
42 find_role_in_data,
43 traverse_data($node, $decl, sub($data,$decl,$decl_resolved))
44  
45 - readas DOM => readas PML, where #KNITting means pointing to the same data (if possible).
46 test implementation: breaks old Undo/Redo, but ok with the new "object-preserving" one
47  
48 (XSLT):
49  
50 - support for external xslt processors (maybe a common wrapper)
51 - with LibXSLT, cache the parsed stylesheets
52  
53 DONE:
54  
55 - hash by #ID into appData('id-hash')/{'id-hash'} (knitted instances could be hashed with prefix#,
56 knitted-knitted instances with prefix1#prefix2#...)
57 (this is temporary)
58  
59 =end comment
60  
61 =cut
62  
63 }
64  
65 our %EXPORT_TAGS = (
66 'functions' => [ qw( get_data set_data count_matches for_each_match get_all_matches ) ],
67 'constants' => $Treex::PML::Instance::Common::EXPORT_TAGS{constants},
68 'diagnostics' => $Treex::PML::Instance::Common::EXPORT_TAGS{diagnostics},
69 9 );
70 9 $EXPORT_TAGS{'all'} = [ @{ $EXPORT_TAGS{'constants'} },
71 9 @{ $EXPORT_TAGS{'diagnostics'} },
72 9 @{ $EXPORT_TAGS{'functions'} },
73 qw( $DEBUG )
74 ];
75  
76 9 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
77 9 our @EXPORT = qw( );
78 9 our $VERSION = '2.29'; # version template
79  
80 9 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
81 9 require Treex::PML::IO;
82 9 require Treex::PML;
83  
84  
85 # FIELDS:
86 9 ▶use fields qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
87 _schema
88 _schema-url
89 _schema-inline
90 _types
91 _dom
92 _root
93 _parser
94 _writer
95 _filename
96 _transform_id
97 _status
98 _readas-trees
99 _references
100 _refnames
101 _ref
102 _ref-index
103 _pml_trees_type
104 _no_read_trees
105 _no_references
106 _no_knit
107 _selected_references
108 _selected_references_ids
109 _selected_knits
110 _selected_knits_ids
111 _trees
112 _pml_prolog
113 _pml_epilog
114 _id-hash
115 _log
116 _id_prefix
117 _trees_written
118 _refs_save
119 _save_flags
120 _pi
121 9 );
122  
123 } # BEGIN
124  
125 9 ▶use Treex::PML::Instance::Reader;
Subroutine BEGIN: called CC=1 SCAR=0.0
126 9 ▶use Treex::PML::Instance::Writer;
Subroutine BEGIN: called CC=1 SCAR=0.0
127  
128 # PML Instance File
129 0 ▶sub get_filename {
Subroutine get_filename: not called CC=4 SCAR=30.0
Pod documented
130 0 my $filename= $_[0]->{'_filename'};
131 0 ▶ if (blessed($filename) and $filename->isa('URI')
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($filename) and $filename->isa("URI") and $filename->scheme eq "file") 0 0 0
Condition0%0/6
builtin::blessed($filename) and $filename->isa("URI") and $filename->scheme eq "file"
!ll&&!rl&&r
000
builtin::blessed($filename) and $filename->isa("URI")
!ll&&!rl&&r
000
MC/DC0%0/3
builtin::blessed($filename) and $filename->isa("URI") and $filename->scheme eq "file"
builtin::blessed($filename) $filename->isa("URI") $filename->scheme eq "file"
Truth table0%0/4
A and B and C
A= builtin::blessed($filename)B= $filename->isa("URI")C= $filename->scheme eq "file"
ABCresult
0XX0
10X0
1100
1111
132 and $filename->scheme eq 'file') {
133 0 return $filename->file;
134 }
135 0 return $filename;
136 }
137 0 ▶sub get_url {
Subroutine get_url: not called CC=4 SCAR=30.0
Pod documented
138 0 my $filename= $_[0]->{'_filename'};
139 0 ▶ if ($filename and not (blessed($filename) and $filename->isa('URI'))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename and not builtin::blessed($filename) && $filename->isa("URI")) 0 0 0
Condition0%0/6
$filename and not builtin::blessed($filename) && $filename->isa("URI")
!ll&&!rl&&r
000
builtin::blessed($filename) && $filename->isa('URI')
!ll&&!rl&&r
000
MC/DC0%0/3
$filename and not builtin::blessed($filename) && $filename->isa("URI")
$filename builtin::blessed($filename) $filename->isa('URI')
Truth table0%0/4
A and not(B && C)
A= $filenameB= builtin::blessed($filename)C= $filename->isa('URI')
ABCresult
0XX0
1110
10X1
1101
140 0 return Treex::PML::IO::make_URI($filename);
141 }
142 0 return $filename;
143 }
144  
145 74 ▶sub set_filename {
Subroutine set_filename: called CC=1 SCAR=0.0
Pod documented
146 74 $_[0]->{'_filename'} = Treex::PML::IO::make_abs_URI($_[1]); # 1K faster than cwd
147 }
148 0 ▶sub get_transform_id { $_[0]->{'_transform_id'}; }
Subroutine get_transform_id: not called CC=1 SCAR=6.9
Pod documented
149 0 ▶sub set_transform_id { $_[0]->{'_transform_id'} = $_[1]; }
Subroutine set_transform_id: not called CC=1 SCAR=6.9
Pod documented
150  
151 # Schema
152 0 ▶sub schema { $_[0]->{'_schema'} }
Subroutine schema: not called CC=1 SCAR=6.9
Pod undocumented
153 9 *get_schema = \&schema;
154 0 ▶sub set_schema { $_[0]->{'_schema'} = $_[1] }
Subroutine set_schema: not called CC=1 SCAR=6.9
Pod documented
155 0 ▶sub get_schema_url { $_[0]->{'_schema-url'} }
Subroutine get_schema_url: not called CC=1 SCAR=6.9
Pod documented
156 0 ▶sub set_schema_url { $_[0]->{'_schema-url'} = $_[1]; }
Subroutine set_schema_url: not called CC=1 SCAR=6.9
Pod documented
157  
158 # Data
159 107 ▶sub get_root { $_[0]->{'_root'}; }
Subroutine get_root: called CC=1 SCAR=0.0
Pod documented
160 0 ▶sub set_root { $_[0]->{'_root'} = $_[1]; }
Subroutine set_root: not called CC=1 SCAR=6.9
Pod documented
161 0 ▶sub get_trees { $_[0]->{'_trees'}; }
Subroutine get_trees: not called CC=1 SCAR=6.9
Pod documented
162 #sub set_trees { $_[0]->{'_trees'} = $_[1]; }
163 0 ▶sub get_trees_prolog { $_[0]->{'_pml_prolog'}; }
Subroutine get_trees_prolog: not called CC=1 SCAR=6.9
Pod documented
164 #sub set_trees_prolog { $_[0]->{'_pml_prolog'} = $_[1]; }
165 0 ▶sub get_trees_epilog { $_[0]->{'_pml_epilog'}; }
Subroutine get_trees_epilog: not called CC=1 SCAR=6.9
Pod documented
166 #sub set_trees_epilog { $_[0]->{'_pml_epilog'} = $_[1]; }
167 0 ▶sub get_trees_type { $_[0]->{'_pml_trees_type'}; }
Subroutine get_trees_type: not called CC=1 SCAR=6.9
Pod documented
168 #sub set_trees_type { $_[0]->{'_pml_trees_type'} = $_[1]; }
169  
170 # References
171 18 ▶sub get_references_hash {
Subroutine get_references_hash: called CC=2 SCAR=7.6
Pod documented
172 18 ▶ return ($_[0]->{'_references'}||={});
Condition50%1/2
$_[0]{"_references"} ||= {}
l!l
180
MC/DC0%0/1
$_[0]{"_references"} ||= {}
$_[0]{"_references"}
Truth table50%1/2
A
A= $_[0]{"_references"}
Aresult
11
00
173 }
174 0 ▶sub set_references_hash { $_[0]->{'_references'} = $_[1]; }
Subroutine set_references_hash: not called CC=1 SCAR=6.9
Pod documented
175 0 ▶sub get_ref_ids_by_name {
Subroutine get_ref_ids_by_name: not called CC=2 SCAR=17.9
Pod documented
176 0 my ($self,$name)=@_;
177 0 my $refs = $self->get_refname_hash->{$name};
178 0 ▶ return ref($refs) ? @$refs : ($refs);
Branch: 1 branch
BranchTrueFalseTotal
ref $refs ? : 0 0 0
179 }
180 0 ▶sub get_refs_by_name {
Subroutine get_refs_by_name: not called CC=1 SCAR=6.9
Pod documented
181 0 my ($self,$name)=@_;
182 0 return map {$self->get_ref($_)} $self->get_ref_ids_by_name;
183 }
184 0 ▶sub get_refname_hash {
Subroutine get_refname_hash: not called CC=2 SCAR=17.9
Pod documented
185 0 ▶ return ($_[0]->{'_refnames'}||={});
Condition0%0/2
$_[0]{"_refnames"} ||= {}
l!l
00
MC/DC0%0/1
$_[0]{"_refnames"} ||= {}
$_[0]{"_refnames"}
Truth table0%0/2
A
A= $_[0]{"_refnames"}
Aresult
11
00
186 }
187 0 ▶sub set_refname_hash { $_[0]->{'_refnames'} = $_[1]; }
Subroutine set_refname_hash: not called CC=1 SCAR=6.9
Pod documented
188 0 ▶sub get_ref {
Subroutine get_ref: not called CC=2 SCAR=17.9
Pod documented
189 0 my ($self,$id)=@_;
190 0 my $refs = $self->{'_ref'};
191 0 ▶ return $refs ? $refs->{$id} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$refs ? : 0 0 0
192 }
193 0 ▶sub set_ref {
Subroutine set_ref: not called CC=2 SCAR=17.9
Pod documented
194 0 my ($self,$id,$obj)=@_;
195 0 my $refs = $self->{'_ref'};
196 0 ▶ $self->{'_ref'} = $refs = {} unless ($refs);
Branch: 1 branch
BranchTrueFalseTotal
unless $refs 0 0 0
197 0 return $refs->{$id}=$obj;
198 }
199  
200 # Status=1 (if parsed fine)
201 22 ▶sub get_status { $_[0]->{'_status'}; }
Subroutine get_status: called CC=1 SCAR=0.0
Pod documented
202 #sub set_status { $_[0]->{'_status'} = $_[1]; }
203  
204 107 ▶sub get_reffiles {
Subroutine get_reffiles: called CC=7 SCAR=20.6
Pod documented
205 107 my ($ctxt)=@_;
206 107 my $references = [$ctxt->{'_schema'}->get_named_references];
207 107 my @refs;
208 107 ▶ if ($references) {
Branch: 1 branch
BranchTrueFalseTotal
if ($references) 107 0 107
209 107 foreach my $reference (@$references) {
210 56 my $refids = $ctxt->{'_refnames'}->{$reference->{name}};
211 56 ▶ if ($refids) {
Branch: 1 branch
BranchTrueFalseTotal
if ($refids) { } 56 0 56
212 56 ▶ foreach my $refid (ref($refids) ? @$refids : ($refids)) {
Branch: 1 branch
BranchTrueFalseTotal
ref $refids ? : 0 56 56
213 56 my $href = $ctxt->{'_references'}->{$refid};
214 56 ▶ if ($href) {
Branch: 1 branch
BranchTrueFalseTotal
if ($href) { } 56 0 56
215 56 _debug("Found '$reference->{name}' as $refid# = '$href'");
216 push @refs,{
217 readas => $reference->{readas},
218 name => $reference->{name},
219 56 id => $refid,
220 href => $href
221 };
222 } else {
223 0 _die("No href for $refid# ($reference->{name})")
224 }
225 }
226 } else {
227 0 _warn("Didn't find any reference to '".$reference->{name}."'\n");
228 }
229 }
230 }
231 107 return @refs;
232 }
233  
234 65 ▶sub read_reffiles {
Subroutine read_reffiles: called CC=9 SCAR=23.8
Pod undocumented
235 65 my ($ctxt,$opts) = @_;
236 65 foreach my $ref ($ctxt->get_reffiles()) {
237 28 my $id = $ref->{id};
238 28 my $selected = $ctxt->{'_selected_references_ids'}{$id};
239 28 ▶ next if (defined($selected) ? $selected==0 : $ctxt->{'_no_references'});
Branch: 2 branches
BranchTrueFalseTotal
if defined $selected ? $selected == 0 : $ctxt->{"_no_references"} 0 28 28
defined $selected ? : 0 28 28
240 28 my $readas = $ref->{readas};
241 28 ▶ if (defined $readas) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $readas) 28 0 28
242 28 ▶ if ($readas eq 'dom') {
Branch: 4 branches
BranchTrueFalseTotal
if ($readas eq 'dom') { } 20 8 28
elsif ($readas eq 'trees') { } 2 6 8
elsif ($readas eq 'pml') { } 6 0 6
elsif (length $readas) { } 0 0 0
243 20 $ctxt->readas_dom($id,$ref->{href},$opts);
244 } elsif($readas eq 'trees') {
245 # when translating to Treex::PML::Document,
246 # push to fs-require [$id,$ref->{href}];
247 } elsif($readas eq 'pml') {
248 6 $ctxt->readas_pml($id,$ref->{href},$opts);
249 } elsif (length($readas)) {
250 0 _warn("Ignoring references with unknown readas method: '$readas' for reffile id='$id', href='$ref->{href}'\n");
251 }
252 }
253 }
254 }
255  
256 6 ▶sub readas_pml {
Subroutine readas_pml: called CC=4 SCAR=14.2
Pod undocumented
257 6 my ($ctxt,$refid,$href,$opts)=@_;
258 # embed PML documents
259 6 my $ref_data;
260 6 _debug("readas_pml: $refid => $href");
261 my $pml = Treex::PML::Instance->load({
262 filename => $href,
263 no_knit => $ctxt->{_no_knit},
264 selected_knits => $ctxt->{_selected_knits},
265 no_references => $ctxt->{_no_references},
266 selected_references => $ctxt->{_selected_references},
267 6 ▶ ($opts ? %$opts : ()),
Branch: 1 branch
BranchTrueFalseTotal
$opts ? : 6 0 6
268 });
269 6 ▶ $ctxt->{'_ref'} ||= {};
Condition50%1/2
$ctxt->{"_ref"} ||= {}
l!l
06
MC/DC0%0/1
$ctxt->{"_ref"} ||= {}
$ctxt->{"_ref"}
Truth table50%1/2
A
A= $ctxt->{"_ref"}
Aresult
11
00
270 6 $ctxt->{'_ref'}->{$refid}=$pml;
271 6 ▶ $ctxt->{'_ref-index'} ||= {};
Condition50%1/2
$ctxt->{"_ref-index"} ||= {}
l!l
06
MC/DC0%0/1
$ctxt->{"_ref-index"} ||= {}
$ctxt->{"_ref-index"}
Truth table50%1/2
A
A= $ctxt->{"_ref-index"}
Aresult
11
00
272 6 weaken( $ctxt->{'_ref-index'}->{$refid} = $pml->{'_id-hash'} );
273 6 1;
274 }
275  
276 # $ctxt, $refid, $href
277 32 ▶sub readas_dom {
Subroutine readas_dom: called CC=15 SCAR=32.0
Pod undocumented
278 32 my ($ctxt,$refid,$href,$opts)=@_;
279 # embed DOM documents
280 32 my $ref_data;
281 # if ($opts and $opts->{use_resources}) {
282 # $href = Treex::PML::FindInResourcePaths($href);
283 # }
284  
285 32 my ($local_file,$remove_file) = Treex::PML::IO::fetch_file($href);
286 32 my $ref_fh = Treex::PML::IO::open_uri($local_file);
287 32 ▶ _die("Cannot open $href for reading") unless $ref_fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $ref_fh 0 32 32
288 32 _debug("readas_dom: $refid => $href");
289 32 ▶ my $parser = $ctxt->{'_parser'} || $ctxt->_xml_parser();
Condition66%2/3
$ctxt->{'_parser'} || $ctxt->_xml_parser
l!l&&r!l&&!r
12200
MC/DC0%0/2
$ctxt->{'_parser'} || $ctxt->_xml_parser
$ctxt->{'_parser'} $ctxt->_xml_parser
Truth table66%2/3
A || B
A= $ctxt->{'_parser'}B= $ctxt->_xml_parser
ABresult
1X1
011
000
290 32 ▶ if ($ref_fh){
Branch: 1 branch
BranchTrueFalseTotal
if ($ref_fh) { } 32 0 32
291 32 eval {
292 32 $ref_data = $parser->parse_fh($ref_fh, $href);
293 };
294 32 ▶ _die("Error parsing $href $ref_fh $local_file ($@)") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 32 32
295 32 ▶ $ref_data->setBaseURI($href) if $ref_data and $ref_data->can('setBaseURI');;
Branch: 1 branch
BranchTrueFalseTotal
if $ref_data and $ref_data->can("setBaseURI") 32 0 32
Condition33%1/3
$ref_data and $ref_data->can("setBaseURI")
!ll&&!rl&&r
0032
MC/DC0%0/2
$ref_data and $ref_data->can("setBaseURI")
$ref_data $ref_data->can("setBaseURI")
Truth table33%1/3
A and B
A= $ref_dataB= $ref_data->can("setBaseURI")
ABresult
0X0
100
111
296 32 $parser->process_xincludes($ref_data);
297 32 Treex::PML::IO::close_uri($ref_fh);
298 32 ▶ $ctxt->{'_ref'} ||= {};
Condition100%2/2
$ctxt->{"_ref"} ||= {}
l!l
1418
MC/DC100%1/1
$ctxt->{"_ref"} ||= {}
$ctxt->{"_ref"}
Truth table100%2/2
A
A= $ctxt->{"_ref"}
Aresult
11
00
299 32 $ctxt->{'_ref'}->{$refid}=$ref_data;
300 32 ▶ $ctxt->{'_ref-index'} ||= {};
Condition100%2/2
$ctxt->{"_ref-index"} ||= {}
l!l
725
MC/DC100%1/1
$ctxt->{"_ref-index"} ||= {}
$ctxt->{"_ref-index"}
Truth table100%2/2
A
A= $ctxt->{"_ref-index"}
Aresult
11
00
301 32 $ctxt->{'_ref-index'}->{$refid}=_index_by_id($ref_data);
302 32 ▶ if ($href ne $local_file and $remove_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($href ne $local_file and $remove_file) 10 22 32
Condition100%3/3
$href ne $local_file and $remove_file
!ll&&!rl&&r
22010
MC/DC100%2/2
$href ne $local_file and $remove_file
$href ne $local_file $remove_file
Truth table100%3/3
A and B
A= $href ne $local_fileB= $remove_file
ABresult
0X0
100
111
303 10 local $!;
304 10 ▶ unlink $local_file || _warn("couldn't unlink tmp file $local_file: $!\n");
Condition33%1/3
$local_file || _warn("couldn't unlink tmp file ${local_file}: $!\n")
l!l&&r!l&&!r
1000
MC/DC0%0/2
$local_file || _warn("couldn't unlink tmp file ${local_file}: $!\n")
$local_file _warn("couldn't unlink tmp file ${local_file}: $!\n")
Truth table33%1/3
A || B
A= $local_fileB= _warn("couldn't unlink tmp file ${local_file}: $!\n")
ABresult
1X1
011
000
305 }
306 } else {
307 0 ▶ if ($href ne $local_file and $remove_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($href ne $local_file and $remove_file) 0 0 0
Condition0%0/3
$href ne $local_file and $remove_file
!ll&&!rl&&r
000
MC/DC0%0/2
$href ne $local_file and $remove_file
$href ne $local_file $remove_file
Truth table0%0/3
A and B
A= $href ne $local_fileB= $remove_file
ABresult
0X0
100
111
308 0 local $!;
309 0 ▶ unlink $local_file || _warn("couldn't unlink tmp file $local_file: $!\n");
Condition0%0/3
$local_file || _warn("couldn't unlink tmp file ${local_file}: $!\n")
l!l&&r!l&&!r
000
MC/DC0%0/2
$local_file || _warn("couldn't unlink tmp file ${local_file}: $!\n")
$local_file _warn("couldn't unlink tmp file ${local_file}: $!\n")
Truth table0%0/3
A || B
A= $local_fileB= _warn("couldn't unlink tmp file ${local_file}: $!\n")
ABresult
1X1
011
000
310 }
311 0 _die("Couldn't open '".$href."': $!");
312 }
313 32 1;
314 }
315  
316 40 ▶sub _xml_parser {
Subroutine _xml_parser: called CC=3 SCAR=11.7
317 40 my ($self,$opts) = @_;
318 40 my $parser = XML::LibXML->new();
319 40 $parser->keep_blanks(0);
320 40 $parser->line_numbers(1);
321 40 $parser->load_ext_dtd(0);
322 40 $parser->validation(0);
323 40 ▶ if (ref($opts) and $parser->can('set_options')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts and $parser->can("set_options")) 0 40 40
Condition33%1/3
ref $opts and $parser->can("set_options")
!ll&&!rl&&r
4000
MC/DC0%0/2
ref $opts and $parser->can("set_options")
ref $opts $parser->can("set_options")
Truth table33%1/3
A and B
A= ref $optsB= $parser->can("set_options")
ABresult
0X0
100
111
324 0 $parser->set_options($opts);
325 }
326 40 return $parser;
327 }
328  
329 ###################################
330 # CONSTRUCTOR
331 ####################################
332  
333 75 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
334 75 my $class = shift;
335 75 ▶ _die('Usage: ' . __PACKAGE__ . '->new()') if ref($class);
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 75 75
336 75 return fields::new($class);
337 }
338  
339  
340  
341 ###################################
342 # LOAD
343 ###################################
344  
345 65 ▶sub load {
Subroutine load: called CC=1 SCAR=0.0
Pod documented
346 65 return &Treex::PML::Instance::Reader::load;
347 }
348  
349 21 ▶sub save {
Subroutine save: called CC=1 SCAR=0.0
Pod documented
350 21 return &Treex::PML::Instance::Writer::save;
351 }
352  
353 0 ▶sub lookup_id {
Subroutine lookup_id: not called CC=2 SCAR=17.9
Pod documented
354 0 my ($ctxt,$id)=@_;
355 0 ▶ my $hash = $ctxt->{'_id-hash'} ||= {};
Condition0%0/2
$ctxt->{"_id-hash"} ||= {}
l!l
00
MC/DC0%0/1
$ctxt->{"_id-hash"} ||= {}
$ctxt->{"_id-hash"}
Truth table0%0/2
A
A= $ctxt->{"_id-hash"}
Aresult
11
00
356 0 return $hash->{ $id };
357 }
358  
359 0 ▶sub hash_id {
Subroutine hash_id: not called CC=9 SCAR=45.0
Pod documented
360 0 my ($ctxt,$id,$object,$check_uniq) = @_;
361 0 ▶ return unless defined($id) and length($id);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id and length $id 0 0 0
Condition0%0/3
defined $id and length $id
!ll&&!rl&&r
000
MC/DC0%0/2
defined $id and length $id
defined $id length $id
Truth table0%0/3
A and B
A= defined $idB= length $id
ABresult
0X0
100
111
362 0 ▶ my $prefix = $ctxt->{'_id_prefix'} || '';
Condition0%0/2
$ctxt->{'_id_prefix'} || ''
l!l
00
MC/DC0%0/1
$ctxt->{'_id_prefix'} || ''
$ctxt->{'_id_prefix'}
Truth table0%0/2
A
A= $ctxt->{'_id_prefix'}
Aresult
11
00
363 0 $id = $prefix . $id;
364 0 ▶ my $hash = $ctxt->{'_id-hash'} ||= {};
Condition0%0/2
$ctxt->{"_id-hash"} ||= {}
l!l
00
MC/DC0%0/1
$ctxt->{"_id-hash"} ||= {}
$ctxt->{"_id-hash"}
Truth table0%0/2
A
A= $ctxt->{"_id-hash"}
Aresult
11
00
365 0 ▶ if ($check_uniq) { # and $prefix eq ''
Branch: 1 branch
BranchTrueFalseTotal
if ($check_uniq) 0 0 0
366 0 my $current = $hash->{$id};
367 0 ▶ if (defined $current and $current != $object) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $current and $current != $object) 0 0 0
Condition0%0/3
defined $current and $current != $object
!ll&&!rl&&r
000
MC/DC0%0/2
defined $current and $current != $object
defined $current $current != $object
Truth table0%0/3
A and B
A= defined $currentB= $current != $object
ABresult
0X0
100
111
368 0 _warn("Duplicated ID '$id'");
369 }
370 }
371 0 ▶ if (ref($object)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $object) { } 0 0 0
372 0 weaken( $hash->{$id} = $object );
373 } else {
374 0 $hash->{$id} = $object;
375 }
376 }
377  
378 32 ▶sub _index_by_id {
Subroutine _index_by_id: called CC=2 SCAR=6.9
379 32 my ($dom) = @_;
380 32 my %index;
381 32 $dom->indexElements;
382 32 for my $node (@{$dom->findnodes('//*/@*[name()="id" or name()="xml:id"]')}) {
383 8322 $index{ $node->value }=$node->ownerElement;
384 }
385 32 return \%index;
386 }
387  
388 ##########################################
389 # Validation
390 #########################################
391  
392 0 ▶sub validate_object {
Subroutine validate_object: not called CC=1 SCAR=6.9
Pod documented
393 0 my ($ctxt, $object, $type, $opts)=@_;
394 0 $type->validate_object($object,$opts);
395 }
396  
397 ##########################################
398 # Data pulling
399 #########################################
400  
401  
402 757 ▶sub get_data {
Subroutine get_data: called CC=27 SCAR=53.3
Pod documented
403 757 my ($node,$path, $strict) = @_;
404 757 ▶ if (UNIVERSAL::DOES::does($node,'Treex::PML::Instance')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::DOES::does($node, "Treex::PML::Instance")) 54 703 757
405 54 $node = $node->get_root;
406 }
407 757 my $val = $node;
408 757 ▶ if (!defined $path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $path) 0 757 757
409 0 carp("Treex::PML::Instance::get_data : undefined attribute path!");
410 0 return;
411 }
412 757 for my $step (split /\//, $path) {
413 815 ▶ next if $step eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $step eq "." 0 815 815
414 815 my $is_list = UNIVERSAL::DOES::does($val,'Treex::PML::List');
415 815 ▶ if ($is_list or UNIVERSAL::DOES::does($val,'Treex::PML::Alt')) {
Branch: 4 branches
BranchTrueFalseTotal
if ($is_list or UNIVERSAL::DOES::does($val, 'Treex::PML::Alt')) { } 1 814 815
elsif (UNIVERSAL::DOES::does($val, 'Treex::PML::Seq')) { } 0 814 814
elsif (ref $val) { } 760 54 814
elsif (defined $val) { } 0 54 54
Condition66%2/3
$is_list or UNIVERSAL::DOES::does($val, "Treex::PML::Alt")
l!l&&r!l&&!r
10814
MC/DC50%1/2
$is_list or UNIVERSAL::DOES::does($val, "Treex::PML::Alt")
$is_list UNIVERSAL::DOES::does($val, "Treex::PML::Alt")
Truth table66%2/3
A or B
A= $is_listB= UNIVERSAL::DOES::does($val, "Treex::PML::Alt")
ABresult
1X1
011
000
416 1 ▶ if ($step =~ /^\[([-+]?\d+)\]/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($step =~ /^\[([-+]?\d+)\]/) { } 0 1 1
elsif ($strict) { } 0 1 1
417 0 ▶ $val =
Branch: 2 branches
BranchTrueFalseTotal
$1 > 0 ? : 0 0 0
$1 < 0 ? : 0 0 0
418 $1>0 ? $val->[$1-1] :
419 $1<0 ? $val->[$1] : undef;
420 } elsif ($strict) {
421 # warn "Can't follow attribute path '$path' (step '$step')\n";
422 0 return; # ERROR
423 } else {
424 1 $val = $val->[0];
425 1 ▶ redo unless $step eq ($is_list ? LM : AM);
Branch: 2 branches
BranchTrueFalseTotal
unless $step eq ($is_list ? "LM" : "AM") 1 0 1
$is_list ? : 1 0 1
426 }
427 } elsif (UNIVERSAL::DOES::does($val,'Treex::PML::Seq')) {
428 0 ▶ if ($step =~ /^\[([-+]?\d+)\](.*)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($step =~ /^\[([-+]?\d+)\](.*)/) { } 0 0 0
elsif ($step =~ /^([^\[]+)(?:\[([-+]?\d+)\])?/) { } 0 0 0
429 0 ▶ $val =
Branch: 2 branches
BranchTrueFalseTotal
$1 > 0 ? : 0 0 0
$1 < 0 ? : 0 0 0
430 $1>0 ? $val->elements_list->[$1-1] :
431 $1<0 ? $val->elements_list->[$1] : undef; # element
432 0 ▶ if ($val) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val) 0 0 0
433 0 ▶ if (defined $2 and length $2) { # optional name test
Branch: 1 branch
BranchTrueFalseTotal
if (defined $2 and length $2) 0 0 0
Condition0%0/3
defined $2 and length $2
!ll&&!rl&&r
000
MC/DC0%0/2
defined $2 and length $2
defined $2 length $2
Truth table0%0/3
A and B
A= defined $2B= length $2
ABresult
0X0
100
111
434 0 ▶ return if $val->[0] ne $2; # ERROR
Branch: 1 branch
BranchTrueFalseTotal
if $val->[0] ne $2 0 0 0
435 }
436 0 $val = $val->[1]; # value
437 }
438 } elsif ($step =~ /^([^\[]+)(?:\[([-+]?\d+)\])?/) {
439 0 my $i = $2;
440 0 $val = $val->values($1);
441 0 ▶ if ($i ne q{}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i ne "") 0 0 0
442 0 ▶ $val = $i>0 ? $val->[$i-1] :
Branch: 2 branches
BranchTrueFalseTotal
$i > 0 ? : 0 0 0
$i < 0 ? : 0 0 0
443 $i<0 ? $val->[$i] : undef;
444 }
445 } else {
446 0 return; # ERROR
447 }
448 } elsif (ref($val)) {
449 760 $val = $val->{$step};
450 } elsif (defined($val)) {
451 # warn "Can't follow attribute path '$path' (step '$step')\n";
452 0 return; # ERROR
453 } else {
454 54 return undef;
455 }
456 }
457 703 return $val;
458 }
459  
460 0 ▶sub get_all {
Subroutine get_all: not called CC=30 SCAR=68.4
Pod documented
461 0 my ($node,$path) = @_;
462 0 ▶ if (UNIVERSAL::DOES::does($node,'Treex::PML::Instance')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::DOES::does($node, "Treex::PML::Instance")) 0 0 0
463 0 $node = $node->get_root;
464 }
465 0 my @vals = ($node);
466 0 my $val;
467 0 my $redo=0;
468 0 my $dot;
469 0 ▶ for my $step (ref($path) ? @$path : (split /\//, $path)) {
Branch: 1 branch
BranchTrueFalseTotal
ref $path ? : 0 0 0
470 0 $dot= ($step eq '.');
471 0 ▶ next if $dot;
Branch: 1 branch
BranchTrueFalseTotal
if $dot 0 0 0
472 0 $redo=0;
473 @vals = map {
474 0 $val=$_;
475 0 my $is_list = UNIVERSAL::DOES::does($val,'Treex::PML::List');
476 0 ▶ if ($is_list or UNIVERSAL::DOES::does($val,'Treex::PML::Alt')) {
Branch: 3 branches
BranchTrueFalseTotal
if ($is_list or UNIVERSAL::DOES::does($val, 'Treex::PML::Alt')) { } 0 0 0
elsif (UNIVERSAL::DOES::does($val, 'Treex::PML::Seq')) { } 0 0 0
elsif (ref $val) { } 0 0 0
Condition0%0/3
$is_list or UNIVERSAL::DOES::does($val, "Treex::PML::Alt")
l!l&&r!l&&!r
000
MC/DC0%0/2
$is_list or UNIVERSAL::DOES::does($val, "Treex::PML::Alt")
$is_list UNIVERSAL::DOES::does($val, "Treex::PML::Alt")
Truth table0%0/3
A or B
A= $is_listB= UNIVERSAL::DOES::does($val, "Treex::PML::Alt")
ABresult
1X1
011
000
477 0 ▶ if ($step =~ /^\[([-+]?\d+)\]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($step =~ /^\[([-+]?\d+)\]/) { } 0 0 0
478 0 ▶ $1>0 ? $val->[$1-1] :
Branch: 2 branches
BranchTrueFalseTotal
$1 > 0 ? : 0 0 0
$1 < 0 ? : 0 0 0
479 $1<0 ? $val->[$1] : ();
480 } else {
481 0 ▶ $redo=1 unless $step eq ($is_list ? 'LM' : 'AM');
Branch: 2 branches
BranchTrueFalseTotal
unless $step eq ($is_list ? "LM" : "AM") 0 0 0
$is_list ? : 0 0 0
482 0 @$val
483 }
484 } elsif (UNIVERSAL::DOES::does($val,'Treex::PML::Seq')) {
485 # grep { $_->[0] eq $step } @{$val->[0]}
486 0 ▶ if ($step =~ /^\[([-+]?\d+)\](.*)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($step =~ /^\[([-+]?\d+)\](.*)/) { } 0 0 0
elsif ($step =~ /^([^\[]+)(?:\[([-+]?\d+)\])?/) { } 0 0 0
487 0 ▶ $val =
Branch: 2 branches
BranchTrueFalseTotal
$1 > 0 ? : 0 0 0
$1 < 0 ? : 0 0 0
488 $1>0 ? $val->elements_list->[$1-1] :
489 $1<0 ? $val->elements_list->[$1] : undef; # element
490 0 ▶ $val ?
Branch: 3 branches
BranchTrueFalseTotal
$val ? : 0 0 0
defined $2 && length $2 ? : 0 0 0
$val->[0] eq $2 ? : 0 0 0
Condition0%0/3
defined $2 and length $2
!ll&&!rl&&r
000
MC/DC0%0/2
defined $2 and length $2
defined $2 length $2
Truth table0%0/3
A and B
A= defined $2B= length $2
ABresult
0X0
100
111
491 (defined $2 and length $2) ?
492 ($val->[0] eq $2) ? ($val->[1]) : ()
493 : $val->[1]
494 :()
495 } elsif ($step =~ /^([^\[]+)(?:\[([-+]?\d+)\])?/) {
496 0 my $i = $2;
497 0 $val = $val->values($1);
498 0 ▶ if (defined $i and length $i) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $i and length $i) { } 0 0 0
Condition0%0/3
defined $i and length $i
!ll&&!rl&&r
000
MC/DC0%0/2
defined $i and length $i
defined $i length $i
Truth table0%0/3
A and B
A= defined $iB= length $i
ABresult
0X0
100
111
499 0 ▶ $i>0 ? $val->[$i-1] :
Branch: 2 branches
BranchTrueFalseTotal
$i > 0 ? : 0 0 0
$i < 0 ? : 0 0 0
500 $i<0 ? $val->[$i] : ();
501 } else {
502 0 @$val
503 }
504 0 } else { () }
505 } elsif (ref($val)) {
506 0 ($val->{$step});
507 } else {
508 ()
509 0 }
510 } @vals;
511 0 ▶ redo if $redo;
Branch: 1 branch
BranchTrueFalseTotal
if $redo 0 0 0
512 }
513 0 ▶ return @vals if $dot; # a path may end with a /. to prevent expanding trailing lists and alts
Branch: 1 branch
BranchTrueFalseTotal
if $dot 0 0 0
514 0 ▶ return map { (UNIVERSAL::DOES::does($_,'Treex::PML::List') or UNIVERSAL::DOES::does($_,'Treex::PML::Alt')) ? __expand_list_alt($_) : ($_) } @vals;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::DOES::does($_, 'Treex::PML::List') || UNIVERSAL::DOES::does($_, 'Treex::PML::Alt') ? : 0 0 0
Condition0%0/3
UNIVERSAL::DOES::does($_, "Treex::PML::List") or UNIVERSAL::DOES::does($_, "Treex::PML::Alt")
l!l&&r!l&&!r
000
MC/DC0%0/2
UNIVERSAL::DOES::does($_, "Treex::PML::List") or UNIVERSAL::DOES::does($_, "Treex::PML::Alt")
UNIVERSAL::DOES::does($_, "Treex::PML::List") UNIVERSAL::DOES::does($_, "Treex::PML::Alt")
Truth table0%0/3
A or B
A= UNIVERSAL::DOES::does($_, "Treex::PML::List")B= UNIVERSAL::DOES::does($_, "Treex::PML::Alt")
ABresult
1X1
011
000
515 }
516 0 ▶sub __expand_list_alt {
Subroutine __expand_list_alt: not called CC=3 SCAR=24.8
517 0 ▶ return map { (UNIVERSAL::DOES::does($_,'Treex::PML::List') or UNIVERSAL::DOES::does($_,'Treex::PML::Alt')) ? _expand_list_alt($_) : ($_) } @{$_[0]};
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::DOES::does($_, 'Treex::PML::List') || UNIVERSAL::DOES::does($_, 'Treex::PML::Alt') ? : 0 0 0
Condition0%0/3
UNIVERSAL::DOES::does($_, "Treex::PML::List") or UNIVERSAL::DOES::does($_, "Treex::PML::Alt")
l!l&&r!l&&!r
000
MC/DC0%0/2
UNIVERSAL::DOES::does($_, "Treex::PML::List") or UNIVERSAL::DOES::does($_, "Treex::PML::Alt")
UNIVERSAL::DOES::does($_, "Treex::PML::List") UNIVERSAL::DOES::does($_, "Treex::PML::Alt")
Truth table0%0/3
A or B
A= UNIVERSAL::DOES::does($_, "Treex::PML::List")B= UNIVERSAL::DOES::does($_, "Treex::PML::Alt")
ABresult
1X1
011
000
518 }
519  
520 0 ▶sub set_data {
Subroutine set_data: not called CC=42 SCAR=75.0
Pod documented
521 0 my ($node,$path, $value, $strict) = @_;
522 0 ▶ if (UNIVERSAL::DOES::does($node,'Treex::PML::Instance')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::DOES::does($node, "Treex::PML::Instance")) 0 0 0
523 0 $node = $node->get_root;
524 }
525 0 my $val = $node;
526 0 my @steps = split /\//, $path;
527 0 ▶ while (@steps) {
Branch: 1 branch
BranchTrueFalseTotal
if (@steps) 0 0 0
528 0 my $step = shift @steps;
529 0 ▶ if (UNIVERSAL::DOES::does($val,'Treex::PML::List') or UNIVERSAL::DOES::does($val,'Treex::PML::Alt')) {
Branch: 4 branches
BranchTrueFalseTotal
if (UNIVERSAL::DOES::does($val, 'Treex::PML::List') or UNIVERSAL::DOES::does($val, 'Treex::PML::Alt')) { } 0 0 0
elsif (UNIVERSAL::DOES::does($val, 'Treex::PML::Seq')) { } 0 0 0
elsif (ref $val) { } 0 0 0
elsif (defined $val) { } 0 0 0
Condition0%0/3
UNIVERSAL::DOES::does($val, "Treex::PML::List") or UNIVERSAL::DOES::does($val, "Treex::PML::Alt")
l!l&&r!l&&!r
000
MC/DC0%0/2
UNIVERSAL::DOES::does($val, "Treex::PML::List") or UNIVERSAL::DOES::does($val, "Treex::PML::Alt")
UNIVERSAL::DOES::does($val, "Treex::PML::List") UNIVERSAL::DOES::does($val, "Treex::PML::Alt")
Truth table0%0/3
A or B
A= UNIVERSAL::DOES::does($val, "Treex::PML::List")B= UNIVERSAL::DOES::does($val, "Treex::PML::Alt")
ABresult
1X1
011
000
530 0 ▶ if ($step =~ /^\[([-+]?\d+)\]/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($step =~ /^\[([-+]?\d+)\]/) { } 0 0 0
elsif ($strict) { } 0 0 0
531 0 ▶ if (@steps) {
Branch: 1 branch
BranchTrueFalseTotal
if (@steps) { } 0 0 0
532 0 ▶ $val =
Branch: 2 branches
BranchTrueFalseTotal
$1 > 0 ? : 0 0 0
$1 < 0 ? : 0 0 0
533 $1>0 ? $val->[$1-1] :
534 $1<0 ? $val->[$1] : undef;
535 } else {
536 return
537 0 ▶ $1>0 ? ($val->[$1-1]=$value) :
Branch: 2 branches
BranchTrueFalseTotal
$1 > 0 ? : 0 0 0
$1 < 0 ? : 0 0 0
538 $1<0 ? ($val->[$1]=$value) : undef;
539 }
540 } elsif ($strict) {
541 0 my $msg = "Can't follow attribute path '$path' (step '$step')";
542 0 ▶ croak $msg if ($strict==2);
Branch: 1 branch
BranchTrueFalseTotal
if $strict == 2 0 0 0
543 0 warn $msg."\n";
544 0 return; # ERROR
545 } else {
546 0 ▶ if (@steps) {
Branch: 1 branch
BranchTrueFalseTotal
if (@steps) { } 0 0 0
547 0 $val = $val->[0]{$step};
548 } else {
549 0 $val->[0]{$step} = $value;
550 0 return $value;
551 }
552 }
553 } elsif (UNIVERSAL::DOES::does($val,'Treex::PML::Seq')) {
554 0 ▶ if ($step =~ /^\[([-+]?\d+)\](.*)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($step =~ /^\[([-+]?\d+)\](.*)/) { } 0 0 0
elsif ($step =~ /^([^\[]+)(?:\[([-+]?\d+)\])?/) { } 0 0 0
555 0 my $i = $1;
556 0 ▶ my $el = $i>0 ? $val->elements_list->[$i-1] :
Branch: 2 branches
BranchTrueFalseTotal
$i > 0 ? : 0 0 0
$i < 0 ? : 0 0 0
557 $i<0 ? $val->elements_list->[$i] : undef; # element
558 0 ▶ if (defined $el and defined $2 and length $2 and $el->[0] ne $2) { # optional name test
Branch: 1 branch
BranchTrueFalseTotal
if (defined $el and defined $2 and length $2 and $el->[0] ne $2) 0 0 0
Condition0%0/9
defined $el and defined $2 and length $2 and $el->[0] ne $2
!ll&&!rl&&r
000
defined $el and defined $2 and length $2
!ll&&!rl&&r
000
defined $el and defined $2
!ll&&!rl&&r
000
MC/DC0%0/4
defined $el and defined $2 and length $2 and $el->[0] ne $2
defined $el defined $2 length $2 $el->[0] ne $2
Truth table0%0/5
A and B and C and D
A= defined $elB= defined $2C= length $2D= $el->[0] ne $2
ABCDresult
0XXX0
10XX0
110X0
11100
11111
559 0 my $msg = "Can't follow attribute path '$path' (step '$step')";
560 0 ▶ croak $msg if ($strict==2);
Branch: 1 branch
BranchTrueFalseTotal
if $strict == 2 0 0 0
561 0 warn $msg."\n";
562 0 return; # ERROR
563 }
564 0 ▶ if (@steps) {
Branch: 1 branch
BranchTrueFalseTotal
if (@steps) { } 0 0 0
565 0 $val = $el->[1];
566 } else {
567 0 ▶ if (UNIVERSAL::DOES::does($value,'Treex::PML::Seq::Element')) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::DOES::does($value, 'Treex::PML::Seq::Element')) { } 0 0 0
elsif (ref $$val[$i - 1]) { } 0 0 0
568 0 $val = $val->elements_list;
569 return
570 0 ▶ $i>0 ? ($val->[$i-1]=$value) :
Branch: 2 branches
BranchTrueFalseTotal
$i > 0 ? : 0 0 0
$i < 0 ? : 0 0 0
571 $i<0 ? ($val->[$i]=$value) : undef;
572 } elsif (ref $val->[$i-1]) {
573 0 $el->[1]=$value;
574 0 return $value;
575 } else {
576 0 my $msg = "Can't follow attribute path '$path' (no sequence element found at step '$step')";
577 0 ▶ croak $msg if ($strict==2);
Branch: 1 branch
BranchTrueFalseTotal
if $strict == 2 0 0 0
578 0 warn $msg."\n";
579 0 return; # ERROR
580 }
581 }
582 } elsif ($step =~ /^([^\[]+)(?:\[([-+]?\d+)\])?/) {
583 0 my $i = $2;
584 0 $val = $val->values($1);
585 0 ▶ unless (@steps) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@steps) 0 0 0
586 0 ▶ $val = $1>0 ? $val->[$1-1] :
Branch: 2 branches
BranchTrueFalseTotal
$1 > 0 ? : 0 0 0
$1 < 0 ? : 0 0 0
587 $1<0 ? $val->[$1] : undef;
588 0 ▶ if (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) { } 0 0 0
589 0 ▶ if (UNIVERSAL::DOES::does($value,'Treex::PML::Seq::Element')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::DOES::does($value, 'Treex::PML::Seq::Element')) { } 0 0 0
590 0 $val->[0]=$value->[0];
591 0 $val->[1]=$value->[1];
592 0 return $val;
593 } else {
594 0 $val->[1]=$value;
595 0 return $value;
596 }
597 } else {
598 0 my $msg = "Can't follow attribute path '$path' (no sequence element found at step '$step')";
599 0 ▶ croak $msg if ($strict==2);
Branch: 1 branch
BranchTrueFalseTotal
if $strict == 2 0 0 0
600 0 warn $msg."\n";
601 0 return; # ERROR
602 }
603 }
604 } else {
605 0 return; # ERROR
606 }
607 } elsif (ref($val)) {
608 0 ▶ if (@steps) {
Branch: 1 branch
BranchTrueFalseTotal
if (@steps) { } 0 0 0
609 0 ▶ if (!defined($val->{$step}) and $steps[0]!~/^\[/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val->{$step} or $steps[0] =~ /^\[/) 0 0 0
Condition0%0/3
defined $val->{$step} or $steps[0] =~ /^\[/
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $val->{$step} or $steps[0] =~ /^\[/
defined $val->{$step} $steps[0] =~ /^\[/
Truth table0%0/3
A or B
A= defined $val->{$step}B= $steps[0] =~ /^\[/
ABresult
1X1
011
000
610 0 $val->{$step}=Treex::PML::Factory->createStructure();
611 }
612 0 $val = $val->{$step};
613 } else {
614 0 $val->{$step} = $value;
615 0 return $value;
616 }
617 } elsif (defined($val)) {
618 0 my $msg = "Can't follow attribute path '$path' (step '$step')";
619 0 ▶ croak $msg if ($strict==2);
Branch: 1 branch
BranchTrueFalseTotal
if $strict == 2 0 0 0
620 0 warn $msg."\n";
621 0 return; # ERROR
622 } else {
623 0 return '';
624 }
625 }
626 0 return;
627 }
628  
629  
630  
631 0 ▶sub __match_path {
Subroutine __match_path: not called CC=9 SCAR=45.0
632 0 my ($match_paths, $step)=@_;
633 0 my @r;
634 0 my $s = $step;
635 0 $s =~ s/^\[\d+\]//;
636 0 foreach my $m (@$match_paths) {
637 0 my ($m_step,@rest) = @{$m->[0]};
638 0 ▶ if (defined $m_step and length($m_step)==0) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $m_step and length $m_step == 0) { } 0 0 0
elsif ($m_step eq $step or $m_step eq '*') { } 0 0 0
elsif (not $m_step =~ /^\[/) { } 0 0 0
Condition0%0/3
defined $m_step and length $m_step == 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $m_step and length $m_step == 0
defined $m_step length $m_step == 0
Truth table0%0/3
A and B
A= defined $m_stepB= length $m_step == 0
ABresult
0X0
100
111
639 # handle //
640 0 push @r,$m, [\@rest=>$m->[1]];
641 ▶ } elsif ($m_step eq $step or $m_step eq '*') {
Condition0%0/3
$m_step eq $step or $m_step eq "*"
l!l&&r!l&&!r
000
MC/DC0%0/2
$m_step eq $step or $m_step eq "*"
$m_step eq $step $m_step eq "*"
Truth table0%0/3
A or B
A= $m_step eq $stepB= $m_step eq "*"
ABresult
1X1
011
000
642 0 push @r,[\@rest=>$m->[1]];
643 } elsif ($m_step !~ /^\[/) {
644 0 ▶ if (!length($s)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not length $s) { } 0 0 0
elsif ($s eq $m_step) { } 0 0 0
645 0 push @r,$m;
646 } elsif ($s eq $m_step) {
647 0 push @r,[\@rest=>$m->[1]];
648 }
649 }
650 }
651 0 return \@r;
652 }
653  
654 0 ▶sub __split_path {
Subroutine __split_path: not called CC=3 SCAR=24.8
655 0 my @p = split m{/}, $_[0];
656 0 ▶ if (@p>0 and length($p[0])==0) { shift @p; }
Branch: 1 branch
BranchTrueFalseTotal
if (@p > 0 and length $p[0] == 0) 0 0 0
Condition0%0/3
@p > 0 and length $p[0] == 0
!ll&&!rl&&r
000
MC/DC0%0/2
@p > 0 and length $p[0] == 0
@p > 0 length $p[0] == 0
Truth table0%0/3
A and B
A= @p > 0B= length $p[0] == 0
ABresult
0X0
100
111
657 0 return \@p;
658 }
659  
660 0 ▶sub for_each_match {
Subroutine for_each_match: not called CC=7 SCAR=40.3
Pod documented
661 0 my ($obj,$paths,$opts) = @_;
662 0 ▶ $opts||={};
Condition0%0/2
$opts ||= {}
l!l
00
MC/DC0%0/1
$opts ||= {}
$opts
Truth table0%0/2
A
A= $opts
Aresult
11
00
663 0 my @match_paths;
664 0 ▶ if (UNIVERSAL::isa($paths,'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($paths, 'HASH')) { } 0 0 0
665 0 @match_paths = map { [ __split_path($_) => $paths->{$_} ] } keys %$paths;
666 } else {
667 0 croak("Usage: \$pml->for_each_match( { path1 => callback1, path2 => callback2,...} )\n".
668 " or: Treex::PML::Instance::for_each_match( \$obj, { path1 => callback1, ... } )\n");
669 }
670 0 my $type;
671 0 ▶ if (UNIVERSAL::DOES::does($obj,'Treex::PML::Instance')) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::DOES::does($obj, 'Treex::PML::Instance')) { } 0 0 0
elsif (exists $opts->{'type'}) { } 0 0 0
672 0 ▶ if (exists $opts->{type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{'type'}) { } 0 0 0
673 $type = $opts->{type}
674 0 } else {
675 0 $type = $obj->get_schema->get_root_type
676 }
677 0 $obj = $obj->get_root;
678 } elsif (exists $opts->{type}) {
679 0 $type = $opts->{type};
680 }
681 0 ▶ __for_each_match_dispatch('','',\@match_paths,$obj,$type) if @match_paths;
Branch: 1 branch
BranchTrueFalseTotal
if @match_paths 0 0 0
682 }
683  
684 0 ▶sub __for_each_match_dispatch {
Subroutine __for_each_match_dispatch: not called CC=10 SCAR=47.0
685 0 my ($path, $step, $match_paths, $v, $type)=@_;
686 0 ▶ $path .= $path eq '/' ? $step : '/'.$step;
Branch: 1 branch
BranchTrueFalseTotal
$path eq '/' ? : 0 0 0
687 0 my $match = __match_path($match_paths,$step);
688 0 my @m;
689 0 ▶ if (defined $type) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type) 0 0 0
690 0 my $dt = $type->get_decl_type;
691 0 ▶ if ($dt==PML_ATTRIBUTE_DECL ||
Branch: 1 branch
BranchTrueFalseTotal
if ($dt == 11 or $dt == 12 or $dt == 13) 0 0 0
Condition0%0/6
$dt == 11 or $dt == 12 or $dt == 13
l!l&&r!l&&!r
000
$dt == 11 or $dt == 12
l!l&&r!l&&!r
000
MC/DC0%0/3
$dt == 11 or $dt == 12 or $dt == 13
$dt == 11 $dt == 12 $dt == 13
Truth table0%0/4
A or B or C
A= $dt == 11B= $dt == 12C= $dt == 13
ABCresult
1XX1
01X1
0011
0000
692 $dt==PML_MEMBER_DECL ||
693 $dt==PML_ELEMENT_DECL) {
694 0 $type = $type->get_content_decl;
695 }
696 }
697 0 for my $m (@$match) {
698 0 ▶ if ( @{$m->[0]}>0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$m->[0];} > 0) { } 0 0 0
699 0 push @m, $m;
700 } else {
701 0 my $cb = $m->[1];
702 0 my @args;
703 0 ▶ if (UNIVERSAL::isa($cb,'ARRAY')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($cb, "ARRAY")) 0 0 0
704 0 ($cb,@args) = @$cb;
705 }
706 0 $cb->({path => $path, value => $v, type=>$type},@args);
707 }
708 }
709 0 ▶ __for_each_match($path,$v,\@m,$type) if (@m);
Branch: 1 branch
BranchTrueFalseTotal
if @m 0 0 0
710 }
711  
712 0 ▶sub __for_each_match {
Subroutine __for_each_match: not called CC=32 SCAR=69.6
713 0 my ($p, $val, $match_paths,$type)=@_;
714 0 ▶ if ($val) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val) 0 0 0
715 0 ▶ my $dt = (defined($type)||undef) && $type->get_decl_type;
Condition0%0/5
(defined $type || undef) && $type->get_decl_type
!ll&&!rl&&r
000
defined $type or undef
l!l
00
MC/DC0%0/2
(defined $type || undef) && $type->get_decl_type
defined $type $type->get_decl_type
Truth table0%0/3
A && B
A= defined $typeB= $type->get_decl_type
ABresult
0X0
100
111
716 0 ▶ if (defined($type) and $dt == PML_ALT_DECL and !UNIVERSAL::DOES::does($val, 'Treex::PML::Alt')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type and $dt == 7 and not UNIVERSAL::DOES::does($val, "Treex::PML::Alt")) 0 0 0
Condition0%0/6
defined $type and $dt == 7 and not UNIVERSAL::DOES::does($val, "Treex::PML::Alt")
!ll&&!rl&&r
000
defined $type and $dt == 7
!ll&&!rl&&r
000
MC/DC0%0/3
defined $type and $dt == 7 and not UNIVERSAL::DOES::does($val, "Treex::PML::Alt")
defined $type $dt == 7 not UNIVERSAL::DOES::does($val, "Treex::PML::Alt")
Truth table0%0/4
A and B and not(C)
A= defined $typeB= $dt == 7C= not UNIVERSAL::DOES::does($val, "Treex::PML::Alt")
ABCresult
0XX0
10X0
1100
1111
717 0 $type=$type->get_content_decl;
718 0 $dt=$type->get_decl_type;
719 }
720 0 ▶ if ((UNIVERSAL::DOES::does($val, 'Treex::PML::List') or UNIVERSAL::DOES::does($val, 'Treex::PML::Alt'))
Branch: 3 branches
BranchTrueFalseTotal
if (UNIVERSAL::DOES::does($val, 'Treex::PML::List') || UNIVERSAL::DOES::does($val, 'Treex::PML::Alt') and !defined($dt) || $dt == 6 || $dt == 7) { } 0 0 0
elsif (UNIVERSAL::DOES::does($val, 'Treex::PML::Seq') and !defined($dt) || $dt == 5) { } 0 0 0
elsif (UNIVERSAL::isa($val, 'HASH') and !defined($dt) || $dt == 3 || $dt == 4) { } 0 0 0
Condition0%0/12
UNIVERSAL::DOES::does($val, "Treex::PML::List") || UNIVERSAL::DOES::does($val, "Treex::PML::Alt") and !defined($dt) || $dt == 6 || $dt == 7
!ll&&!rl&&r
000
UNIVERSAL::DOES::does($val, "Treex::PML::List") or UNIVERSAL::DOES::does($val, "Treex::PML::Alt")
l!l&&r!l&&!r
000
not defined $dt or $dt == 6 or $dt == 7
l!l&&r!l&&!r
000
not defined $dt or $dt == 6
l!l&&r!l&&!r
000
MC/DC0%0/5
UNIVERSAL::DOES::does($val, "Treex::PML::List") || UNIVERSAL::DOES::does($val, "Treex::PML::Alt") and !defined($dt) || $dt == 6 || $dt == 7
UNIVERSAL::DOES::does($val, "Treex::PML::List") UNIVERSAL::DOES::does($val, "Treex::PML::Alt") not defined $dt $dt == 6 $dt == 7
Truth table0%0/9
A or B and not(C) or D or E
A= UNIVERSAL::DOES::does($val, "Treex::PML::List")B= UNIVERSAL::DOES::does($val, "Treex::PML::Alt")C= not defined $dtD= $dt == 6E= $dt == 7
ABCDEresult
00XXX0
1X0000
010000
1X1XX1
1X01X1
1X0011
011XX1
0101X1
010011
721 and (!defined($dt) ||
722 $dt == PML_LIST_DECL ||
723 $dt == PML_ALT_DECL)) {
724 0 my $no = 1;
725 0 ▶ my $content_type =(defined($type)||undef) && $type->get_content_decl;
Condition0%0/5
(defined $type || undef) && $type->get_content_decl
!ll&&!rl&&r
000
defined $type or undef
l!l
00
MC/DC0%0/2
(defined $type || undef) && $type->get_content_decl
defined $type $type->get_content_decl
Truth table0%0/3
A && B
A= defined $typeB= $type->get_content_decl
ABresult
0X0
100
111
726 0 foreach my $v (@$val) {
727 0 __for_each_match_dispatch($p,"[$no]",$match_paths,
728 $v,$content_type);
729 0 $no++;
730 }
731 ▶ } elsif ((UNIVERSAL::DOES::does($val, 'Treex::PML::Seq'))
Condition0%0/6
UNIVERSAL::DOES::does($val, "Treex::PML::Seq") and !defined($dt) || $dt == 5
!ll&&!rl&&r
000
not defined $dt or $dt == 5
l!l&&r!l&&!r
000
MC/DC0%0/3
UNIVERSAL::DOES::does($val, "Treex::PML::Seq") and !defined($dt) || $dt == 5
UNIVERSAL::DOES::does($val, "Treex::PML::Seq") not defined $dt $dt == 5
Truth table0%0/4
A and not(B) or C
A= UNIVERSAL::DOES::does($val, "Treex::PML::Seq")B= not defined $dtC= $dt == 5
ABCresult
0XX0
1000
11X1
1011
732 and (!defined($dt) || $dt == PML_SEQUENCE_DECL)) {
733 0 my $no = 1;
734 0 foreach my $e ($val->elements) {
735 0 my $name = $e->name;
736 0 ▶ my $content_type = (defined($type)||undef) && $type->get_element_by_name($name);
Condition0%0/5
(defined $type || undef) && $type->get_element_by_name($name)
!ll&&!rl&&r
000
defined $type or undef
l!l
00
MC/DC0%0/2
(defined $type || undef) && $type->get_element_by_name($name)
defined $type $type->get_element_by_name($name)
Truth table0%0/3
A && B
A= defined $typeB= $type->get_element_by_name($name)
ABresult
0X0
100
111
737 0 ▶ if (!defined($type) || defined($content_type)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $type or defined $content_type) 0 0 0
Condition0%0/3
not defined $type or defined $content_type
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $type or defined $content_type
not defined $type defined $content_type
Truth table0%0/3
not(A) or B
A= not defined $typeB= defined $content_type
ABresult
1X1
011
000
738 0 __for_each_match_dispatch($p,"[$no]$name",$match_paths,
739 $e->value, $content_type);
740 }
741 0 $no++;
742 }
743 ▶ } elsif (UNIVERSAL::isa($val,'HASH')
Condition0%0/9
UNIVERSAL::isa($val, "HASH") and !defined($dt) || $dt == 3 || $dt == 4
!ll&&!rl&&r
000
not defined $dt or $dt == 3 or $dt == 4
l!l&&r!l&&!r
000
not defined $dt or $dt == 3
l!l&&r!l&&!r
000
MC/DC0%0/4
UNIVERSAL::isa($val, "HASH") and !defined($dt) || $dt == 3 || $dt == 4
UNIVERSAL::isa($val, "HASH") not defined $dt $dt == 3 $dt == 4
Truth table0%0/5
A and not(B) or C or D
A= UNIVERSAL::isa($val, "HASH")B= not defined $dtC= $dt == 3D= $dt == 4
ABCDresult
0XXX0
10000
11XX1
101X1
10011
744 and (!defined($dt)
745 || $dt == PML_STRUCTURE_DECL
746 || $dt == PML_CONTAINER_DECL)) {
747 0 foreach my $name (keys %$val) {
748 0 ▶ my $content_type = (defined($type)||undef) && $type->get_member_by_name($name);
Condition0%0/5
(defined $type || undef) && $type->get_member_by_name($name)
!ll&&!rl&&r
000
defined $type or undef
l!l
00
MC/DC0%0/2
(defined $type || undef) && $type->get_member_by_name($name)
defined $type $type->get_member_by_name($name)
Truth table0%0/3
A && B
A= defined $typeB= $type->get_member_by_name($name)
ABresult
0X0
100
111
749 0 ▶ if (!defined($type) || defined($content_type)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $type or defined $content_type) 0 0 0
Condition0%0/3
not defined $type or defined $content_type
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $type or defined $content_type
not defined $type defined $content_type
Truth table0%0/3
not(A) or B
A= not defined $typeB= defined $content_type
ABresult
1X1
011
000
750 0 __for_each_match_dispatch($p,$name,$match_paths,$val->{$name},
751 $content_type);
752 }
753 }
754 }
755 }
756 }
757 0 ▶sub get_all_matches {
Subroutine get_all_matches: not called CC=4 SCAR=30.0
Pod documented
758 0 my ($obj,$path_list,$opts) = @_;
759  
760 0 ▶ unless (UNIVERSAL::isa($path_list,"ARRAY")) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($path_list, "ARRAY")) 0 0 0
761 0 ▶ if (ref($path_list)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $path_list) { } 0 0 0
762 0 die "Usage: ".__PACKAGE__."::get_all_matches: expected a string or a list, got $path_list";
763 } else {
764 0 $path_list = [$path_list];
765 }
766 }
767 0 my @matches;
768 0 ▶ my $sub = sub { push @matches, $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
769 0 for_each_match($obj, { map { $_=>$sub } @$path_list }, $opts);
770 0 ▶ return wantarray ? @matches : \@matches;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
771 }
772 0 ▶sub count_matches {
Subroutine count_matches: not called CC=3 SCAR=24.8
Pod documented
773 0 my ($obj,$path_list,$opts) = @_;
774  
775 0 ▶ unless (UNIVERSAL::isa($path_list,"ARRAY")) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($path_list, "ARRAY")) 0 0 0
776 0 ▶ if (ref($path_list)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $path_list) { } 0 0 0
777 0 die "Usage: ".__PACKAGE__."::get_all_matches: expected a string or a list, got $path_list";
778 } else {
779 0 $path_list = [$path_list];
780 }
781 }
782 0 my $matches;
783 0 ▶ my $sub = sub { $matches++ };
Subroutine __ANON__: not called CC=1 SCAR=6.9
784 0 for_each_match($obj, { map { $_=>$sub } @$path_list }, $opts);
785 0 return $matches;
786 }
787  
788  
789 0 ▶sub traverse_data {
Subroutine traverse_data: not called CC=6 SCAR=37.4
Pod documented
790 0 my ($value,$decl,$callback,$opts)=@_;
791 0 ▶ $opts||={};
Condition0%0/2
$opts ||= {}
l!l
00
MC/DC0%0/1
$opts ||= {}
$opts
Truth table0%0/2
A
A= $opts
Aresult
11
00
792 0 ▶ die "Usage: traverse_data(\$data,\$type_decl,\$callback,\$option_hash)"
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($value) and $decl->isa("Treex::PML::Schema::Decl") and ref $callback eq "CODE" and ref $opts eq "HASH" 0 0 0
Condition0%0/9
builtin::blessed($value) and $decl->isa("Treex::PML::Schema::Decl") and ref $callback eq "CODE" and ref $opts eq "HASH"
!ll&&!rl&&r
000
builtin::blessed($value) and $decl->isa("Treex::PML::Schema::Decl") and ref $callback eq "CODE"
!ll&&!rl&&r
000
builtin::blessed($value) and $decl->isa("Treex::PML::Schema::Decl")
!ll&&!rl&&r
000
MC/DC0%0/4
builtin::blessed($value) and $decl->isa("Treex::PML::Schema::Decl") and ref $callback eq "CODE" and ref $opts eq "HASH"
builtin::blessed($value) $decl->isa("Treex::PML::Schema::Decl") ref $callback eq "CODE" ref $opts eq "HASH"
Truth table0%0/5
A and B and C and D
A= builtin::blessed($value)B= $decl->isa("Treex::PML::Schema::Decl")C= ref $callback eq "CODE"D= ref $opts eq "HASH"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
793 unless blessed($value) and $decl->isa('Treex::PML::Schema::Decl')
794 and ref($callback) eq 'CODE'
795 and ref($opts) eq 'HASH';
796 0 return _traverse_data($value,$decl,$callback,$opts);
797 }
798  
799 0 ▶sub _traverse_data {
Subroutine _traverse_data: not called CC=25 SCAR=64.8
800 0 my ($value,$decl,$callback,$opts)=@_;
801 0 my $decl_is = $decl->get_decl_type;
802 0 my $desc;
803 0 $callback->($value,$decl,$opts->{data});
804 0 ▶ if ($decl_is == PML_STRUCTURE_DECL) {
Branch: 5 branches
BranchTrueFalseTotal
if ($decl_is == 3) { } 0 0 0
elsif ($decl_is == 4) { } 0 0 0
elsif ($decl_is == 5) { } 0 0 0
elsif ($decl_is == 6 or $decl_is == 7) { } 0 0 0
elsif ($decl_is == 9 or $decl_is == 10 or $decl_is == 8) { } 0 0 0
805 0 my @members = $decl->get_members;
806 0 ▶ if ($opts->{no_childnodes}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"no_childnodes"}) 0 0 0
807 @members = grep {
808 0 my $role = $_->get_role;
809 0 ▶ !defined($role) or $role ne '#CHILDNODES'
Condition0%0/3
!defined($role) || $role ne '#CHILDNODES'
l!l&&r!l&&!r
000
MC/DC0%0/2
!defined($role) || $role ne '#CHILDNODES'
!defined($role) $role ne '#CHILDNODES'
Truth table0%0/3
not(A) || B
A= !defined($role)B= $role ne '#CHILDNODES'
ABresult
1X1
011
000
810 } $decl->get_members;
811 }
812 0 ▶ if ($opts->{no_trees}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"no_trees"}) 0 0 0
813 @members = grep {
814 0 my $role = $_->get_role;
815 0 ▶ !defined($role) or $role ne '#TREES'
Condition0%0/3
!defined($role) || $role ne '#TREES'
l!l&&r!l&&!r
000
MC/DC0%0/2
!defined($role) || $role ne '#TREES'
!defined($role) $role ne '#TREES'
Truth table0%0/3
not(A) || B
A= !defined($role)B= $role ne '#TREES'
ABresult
1X1
011
000
816 } $decl->get_members;
817 }
818 0 for (@members) {
819 0 my $n = $_->get_knit_name;
820 0 my $v = $value->{$n};
821 0 ▶ _traverse_data($v,$_->get_knit_content_decl,$callback,$opts) if defined $v;
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 0 0 0
822 }
823 } elsif ($decl_is == PML_CONTAINER_DECL) {
824 0 my @attrs = $decl->get_attributes;
825 0 for (@attrs) {
826 0 my $n = $_->get_name;
827 0 my $v = $value->{$n};
828 0 ▶ _traverse_data($v,$_->get_content_decl,$callback,$opts) if defined $v;
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 0 0 0
829 }
830 0 my $content_decl = $decl->get_knit_content_decl;
831 0 my $v = $value->{'#content'};
832 0 ▶ _traverse_data($v,$content_decl,$callback,$opts) if $v;
Branch: 1 branch
BranchTrueFalseTotal
if $v 0 0 0
833 } elsif ($decl_is == PML_SEQUENCE_DECL) {
834 0 my @elems = $decl->get_elements;
835 0 for (@{$value->elements_list}) {
836 0 my $n = $_->name;
837 0 my $v = $_->value;
838 0 my $e = $decl->get_element_by_name($n);
839 0 ▶ _traverse_data($v,$e,$callback,$opts) if $v and $e;
Branch: 1 branch
BranchTrueFalseTotal
if $v and $e 0 0 0
Condition0%0/3
$v and $e
!ll&&!rl&&r
000
MC/DC0%0/2
$v and $e
$v $e
Truth table0%0/3
A and B
A= $vB= $e
ABresult
0X0
100
111
840 }
841 ▶ } elsif ($decl_is == PML_LIST_DECL || $decl_is == PML_ALT_DECL) {
Condition0%0/3
$decl_is == 6 or $decl_is == 7
l!l&&r!l&&!r
000
MC/DC0%0/2
$decl_is == 6 or $decl_is == 7
$decl_is == 6 $decl_is == 7
Truth table0%0/3
A or B
A= $decl_is == 6B= $decl_is == 7
ABresult
1X1
011
000
842 0 ▶ if ($decl_is == PML_ALT_DECL and not UNIVERSAL::DOES::does($value,'Treex::PML::Alt')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($decl_is == 7 and not UNIVERSAL::DOES::does($value, "Treex::PML::Alt")) 0 0 0
Condition0%0/3
$decl_is == 7 and not UNIVERSAL::DOES::does($value, "Treex::PML::Alt")
!ll&&!rl&&r
000
MC/DC0%0/2
$decl_is == 7 and not UNIVERSAL::DOES::does($value, "Treex::PML::Alt")
$decl_is == 7 not UNIVERSAL::DOES::does($value, "Treex::PML::Alt")
Truth table0%0/3
A and not(B)
A= $decl_is == 7B= not UNIVERSAL::DOES::does($value, "Treex::PML::Alt")
ABresult
0X0
100
111
843 0 $value=Treex::PML::Factory->createAlt([$value],1);
844 }
845 0 my $content_decl=$decl->get_knit_content_decl;
846 0 for my $v ($value->values) {
847 0 ▶ _traverse_data($v,$content_decl,$callback,$opts) if defined($v);
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 0 0 0
848 }
849 ▶ } elsif ($decl_is == PML_CHOICE_DECL || $decl_is == PML_CONSTANT_DECL || $decl_is == PML_CDATA_DECL) {
Condition0%0/6
$decl_is == 9 or $decl_is == 10 or $decl_is == 8
l!l&&r!l&&!r
000
$decl_is == 9 or $decl_is == 10
l!l&&r!l&&!r
000
MC/DC0%0/3
$decl_is == 9 or $decl_is == 10 or $decl_is == 8
$decl_is == 9 $decl_is == 10 $decl_is == 8
Truth table0%0/4
A or B or C
A= $decl_is == 9B= $decl_is == 10C= $decl_is == 8
ABCresult
1XX1
01X1
0011
0000
850 } else {
851 0 die "unhandled data type: $decl\n";
852 }
853 0 return;
854 }
855  
856  
857  
858 ##########################################
859 # Convert to Treex::PML::Document
860 #########################################
861  
862 22 ▶sub convert_to_fsfile {
Subroutine convert_to_fsfile: called CC=18 SCAR=35.8
Pod documented
863 22 my ($ctxt,$fsfile,$opts)=@_;
864  
865 22 my $schema = $ctxt->{'_schema'};
866 22 ▶ $opts||={};
Condition50%1/2
$opts ||= {}
l!l
022
MC/DC0%0/1
$opts ||= {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
867  
868 22 ▶ unless (ref($fsfile)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $fsfile) 0 22 22
869 0 $fsfile = Treex::PML::Factory->createDocument({ backend => 'PML' } );
870 }
871  
872 22 $fsfile->changeURL( $ctxt->{'_filename'} );
873 22 $fsfile->changeEncoding($DEFAULT_ENCODING);
874  
875 22 ▶ if ($schema->isa('Treex::PML::Schema') and not UNIVERSAL::DOES::does($schema, 'Treex::PML::Schema')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->isa("Treex::PML::Schema") and not UNIVERSAL::DOES::does($schema, "Treex::PML::Schema")) 0 22 22
Condition33%1/3
$schema->isa("Treex::PML::Schema") and not UNIVERSAL::DOES::does($schema, "Treex::PML::Schema")
!ll&&!rl&&r
0220
MC/DC0%0/2
$schema->isa("Treex::PML::Schema") and not UNIVERSAL::DOES::does($schema, "Treex::PML::Schema")
$schema->isa("Treex::PML::Schema") not UNIVERSAL::DOES::does($schema, "Treex::PML::Schema")
Truth table33%1/3
A and not(B)
A= $schema->isa("Treex::PML::Schema")B= not UNIVERSAL::DOES::does($schema, "Treex::PML::Schema")
ABresult
0X0
100
111
876 # rebless
877 0 require Treex::PML::Schema;
878 0 bless $schema, 'Treex::PML::Schema';
879 }
880 22 $fsfile->changeMetaData( 'schema', $schema );
881 22 $fsfile->changeMetaData( 'schema-url', $ctxt->{'_schema-url'} );
882 22 $fsfile->changeMetaData( 'schema-inline', $ctxt->{'_schema-inline'} );
883 22 $fsfile->changeMetaData( 'pml_transform', $ctxt->{'_transform_id'} );
884 22 $fsfile->changeMetaData( 'references', $ctxt->{'_references'} );
885 22 $fsfile->changeMetaData( 'refnames', $ctxt->{'_refnames'} );
886 $fsfile->changeMetaData( 'fs-require',
887 2 [ map { [$_->{id},$_->{href}] }
888 22 grep { $_->{readas} eq 'trees' } $ctxt->get_reffiles() ]
889 );
890  
891 22 ▶ $fsfile->changeAppData( 'ref', $ctxt->{'_ref'} || {} );
Condition100%2/2
$ctxt->{'_ref'} || {}
l!l
148
MC/DC100%1/1
$ctxt->{'_ref'} || {}
$ctxt->{'_ref'}
Truth table100%2/2
A
A= $ctxt->{'_ref'}
Aresult
11
00
892 # $fsfile->changeAppData( 'ref-index', $ctxt->{'_ref-index'} || {} );
893 22 $fsfile->changeAppData( 'id-hash', $ctxt->{'_id-hash'} );
894  
895 22 $fsfile->changeMetaData( 'pml_root', $ctxt->{'_root'} );
896 22 $fsfile->changeMetaData( 'pml_trees_type', $ctxt->{'_pml_trees_type'} );
897 22 $fsfile->changeMetaData( 'pml_prolog', $ctxt->{'_pml_prolog'} );
898 22 $fsfile->changeMetaData( 'pml_epilog', $ctxt->{'_pml_epilog'} );
899
900 22 ▶ if ($ctxt->{'_pi'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctxt->{"_pi"}) 0 22 22
901 0 my @patterns = map { $_->[1] } grep { $_->[0] eq 'tred-pattern' } @{$ctxt->{'_pi'}};
902 0 my ($hint) = map { $_->[1] } grep { $_->[0] eq 'tred-hint' } @{$ctxt->{'_pi'}} ;
903 0 for (@patterns, $hint) {
904 0 ▶ next unless defined;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 0 0
905 0 s/&lt;/</g;
906 0 s/&gt;/>/g;
907 0 s/&amp;/&/g;
908 }
909 0 $fsfile->changePatterns( @patterns );
910 0 $fsfile->changeHint( $hint );
911 }
912  
913 22 ▶ $fsfile->changeTrees( @{$ctxt->{'_trees'}} ) if $ctxt->{'_trees'};
Branch: 1 branch
BranchTrueFalseTotal
if $ctxt->{"_trees"} 22 0 22
914  
915 22 my @nodes = $ctxt->{'_schema'}->find_role('#NODE');
916 22 my (@order,@hide);
917 22 for my $path (@nodes) {
918 97 my $node_decl = $schema->find_type_by_path($path);
919 97 ▶ $node_decl or die "Type-path $path does not lead to anything\n";
Branch: 1 branch
BranchTrueFalseTotal
$node_decl or die "Type-path $path does not lead to anything\n" 97 0 97
920  
921 97 push @order, map { $_->get_name } $node_decl->find_members_by_role('#ORDER');
922 0 push @hide, map { $_->get_name } $node_decl->find_members_by_role('#HIDE' );
923 }
924 22 my %uniq;
925 22 ▶ @order = grep { !$uniq{$_} && ($uniq{$_}=1) } @order;
Condition100%2/2
!$uniq{$_} && ($uniq{$_} = 1)
!ll
127
MC/DC100%1/1
!$uniq{$_} && ($uniq{$_} = 1)
!$uniq{$_}
Truth table100%2/2
not(A)
A= !$uniq{$_}
Aresult
00
11
926 22 %uniq=();
927 0 ▶ @hide = grep { !$uniq{$_} && ($uniq{$_}=1) } @hide;
Condition0%0/2
!$uniq{$_} && ($uniq{$_} = 1)
!ll
00
MC/DC0%0/1
!$uniq{$_} && ($uniq{$_} = 1)
!$uniq{$_}
Truth table0%0/2
not(A)
A= !$uniq{$_}
Aresult
00
11
928 22 ▶ if (@order>1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@order > 1) 0 22 22
929 0 _warn("Treex::PML::Document only supports #ORDER members/attributes with a same name: found {",
930 join(',',@order),"}, using $order[0]!");
931 }
932 22 ▶ if (@hide>1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@hide > 1) 0 22 22
933 0 _warn("Treex::PML::Document only supports #HIDE members/attributes with a same name: found {",
934 join(',',@hide),"} $hide[0]!");
935 }
936 22 my $defs = $fsfile->FS->defs;
937 22 ▶ $defs->{$order[0]} = ' N' if @order;
Branch: 1 branch
BranchTrueFalseTotal
if @order 7 15 22
938 22 ▶ $defs->{$hide[0]} = ' H' if @hide;
Branch: 1 branch
BranchTrueFalseTotal
if @hide 0 22 22
939  
940 22 return $fsfile;
941 }
942  
943 ##########################################
944 # Convert from Treex::PML::Document
945 ##########################################
946  
947 10 ▶sub convert_from_fsfile {
Subroutine convert_from_fsfile: called CC=5 SCAR=17.6
Pod documented
948 10 my ($ctxt,$fsfile)=@_;
949  
950 10 ▶ unless (ref($ctxt)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $ctxt) 10 0 10
951 10 $ctxt = $ctxt->new();
952 }
953  
954 10 $ctxt->{'_transform_id'} = $fsfile->metaData('pml_transform');
955 10 $ctxt->{'_filename'} = $fsfile->filename;
956 10 $ctxt->{'_schema'} = $fsfile->metaData('schema');
957 10 $ctxt->{'_root'} = $fsfile->metaData('pml_root');
958 10 $ctxt->{'_schema-inline'} = $fsfile->metaData('schema-inline'); # not used anymore
959 10 $ctxt->{'_schema-url'} = $fsfile->metaData('schema-url');
960 10 $ctxt->{'_references'} = $fsfile->metaData('references');
961 10 $ctxt->{'_refnames'} = $fsfile->metaData('refnames');
962 10 $ctxt->{'_pml_trees_type'} = $fsfile->metaData('pml_trees_type');
963 10 $ctxt->{'_pml_prolog'} = $fsfile->metaData('pml_prolog');
964 10 $ctxt->{'_pml_epilog'} = $fsfile->metaData('pml_epilog');
965 10 $ctxt->{'_trees'} = Treex::PML::Factory->createList( $fsfile->treeList );
966  
967 10 $ctxt->{'_refs_save'} = $fsfile->appData('refs_save');
968  
969 10 $ctxt->{'_ref'} = $fsfile->appData('ref');
970 # $ctxt->{'_ref-index'} = $fsfile->appData('ref-index');
971 10 $ctxt->{'_id-hash'} = $fsfile->appData('id-hash');
972  
973 10 my $PIs = $ctxt->{'_pi'} = [];
974 10 for my $pattern ($fsfile->patterns) {
975 0 $pattern =~ s/&/&amp;/g;
976 0 $pattern =~ s/</&lt;/g;
977 0 $pattern =~ s/>/&gt;/g;
978 0 push @$PIs, ['tred-pattern', $pattern];
979 }
980 10 my $hint = $fsfile->hint;
981 10 ▶ if (defined $hint and length $hint) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hint and length $hint) 0 10 10
Condition33%1/3
defined $hint and length $hint
!ll&&!rl&&r
1000
MC/DC0%0/2
defined $hint and length $hint
defined $hint length $hint
Truth table33%1/3
A and B
A= defined $hintB= length $hint
ABresult
0X0
100
111
982 0 $hint =~ s/&/&amp;/g;
983 0 $hint =~ s/</&lt;/g;
984 0 $hint =~ s/>/&gt;/g;
985 0 push @$PIs, [ 'tred-hint', $hint ];
986 }
987
988 10 return $ctxt;
989 }
990  
991  
992 9 1;
993 __END__
« PML/Backend/CSTS/Csts2fs.pm ↑ Summary PML/Backend/TEIXML.pm »