PML/Node.pm

filter: Statementstmt 24.3% 85 / 349 Branchbran 9.4% 19 / 202 Conditioncond 6.6% 4 / 60 MC/DCmcdc 2.8% 1 / 35 Subroutinesub 38.9% 23 / 59 Podpod 88.8% 40 / 45 totaltotal 22.9% 172 / 750 CC 124 SCAR 37.4
CC
124
Cov
18%
test_child_type
182.0
paste_on
132.0
set_type_by_name
90.0
CRAP 41.9
« PML/Schema/Reader.pm ↑ Summary PML/Backend/CSTS.pm »
1 package Treex::PML::Node;
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  
7 9 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 $VERSION='2.29'; # version template
10 }
11 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 9 ▶use base qw(Treex::PML::Struct);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 9 ▶use Treex::PML::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 require Treex::PML::Instance;
17 9 ▶use UNIVERSAL::DOES;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 9 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 9 our ($parent, $firstson, $lbrother, $rbrother, $TYPE) = qw(_P_ _S_ _L_ _R_ _T_);
21  
22 =pod
23  
24 =head1 NAME
25  
26 Treex::PML::Node - Treex::PML class representing a node.
27  
28 =head1 DESCRIPTION
29  
30 This class implements a node in a tree. The node has zero or one
31 parent node (C<parent()>) (if it has no parent, it is a root of a
32 tree), zero or more child nodes (the left-most of them returned by
33 C<firstson()>) and zero or more siblings (C<lbrother()> is the
34 immediate sibling the left and C<rbrother()> is the immediate sibling
35 the right).
36  
37 A node can also be associated with a PML type (contianer or structure)
38 and may carry named attributes (with atomic or complex values).
39  
40 =head2 Representation of trees
41  
42 L<Treex::PML> provides representation for oriented rooted trees (such as
43 dependency trees or constituency trees).
44  
45 In L<Treex::PML>, each tree is represented by its root-node. A node is a
46 Treex::PML::Node object, which is underlined by a usual Perl hash
47 reference whose hash keys represent node attributes (name-value
48 pairs).
49  
50 The set of available attributes at each node is specified in the data
51 format (which, depending on I/O backend, is represented either by a
52 L<Treex::PML::FSFormat> or L<Treex::PML::Schema> object; whereas
53 L<Treex::PML::FSFormat> uses a fixed set of attributes for all nodes
54 with text values (or alternating text values), in
55 L<Treex::PML::Schema> the set of attributes may depend on the type of
56 the node and a wide range of data-structures is supported for
57 attribute values. In particular, attribute values may be plain
58 scalars or L<Treex::PML> data objects (L<Treex::PML::List>,
59 L<Treex::PML::Alt>, L<Treex::PML::Struct>, L<Treex::PML::Container>,
60 L<Treex::PML::Seq>.
61  
62 FS format also allows to declare some attributes as representants of
63 extra features, such as total ordering on a tree, text value of a
64 node, indicator for "hidden" nodes, etc. Similarly, in PML schema,
65 some attributes may be associated with roles, e.g. the role '#ID' for
66 an attribute carrying a unique identifier of the node, or '#ORDER' for
67 an integer attribute representing the order of the node in the
68 horizontal ordering of the tree.
69  
70 The tree structure can be modified and traversed by various
71 Treex::PML::Node object methods, such as C<parent>, C<firstson>,
72 C<rbrother>, C<lbrother>, C<following>, C<previous>, C<cut>,
73 C<paste_on>, C<paste_after>, and C<paste_before>.
74  
75 Four special hash keys are reserved for representing the tree
76 structure in the Treex::PML::Node hash. These keys are defined in
77 global variables: C<$Treex::PML::Node::parent>, C<$Treex::PML::Node::firstson>,
78 C<$Treex::PML::Node::rbrother>, and C<$Treex::PML::Node::lbrother>. Another
79 special key C<$Treex::PML::Node::type> is reserved for storing data type
80 information. It is highly recommended to use Treex::PML::Node
81 object methods instead of accessing these hash keys directly. By
82 default, the values of these special keys in order are C<_P_>, C<_S_>,
83 C<_R_>, C<_L_>, C<_T_>.
84  
85 Although arbitrary non-attribute non-special keys may be added to the
86 node hashes at run-time, such keys are not normally preserved via I/O
87 backends and extreme care must be taken to aviod conflicts with
88 attribute names or the special hash keys described above.
89  
90 =head1 METHODS
91  
92 =over 4
93  
94 =item Treex::PML::Node->new($hash?,$reuse?)
95  
96 NOTE: Don't call this constructor directly, use Treex::PML::Factory->createTypedNode() or
97 Treex::PML::Factory->createNode() instead!
98  
99 Create a new Treex::PML::Node object. Treex::PML::Node is basically a hash reference. This
100 means that node's attributes can be accessed simply as
101 C<< $node->{attribute} >>.
102  
103 If a hash-reference is passed as the 1st argument, all its keys and
104 values are are copied to the new Treex::PML::Node.
105  
106 An optional 2nd argument $reuse can be set to a true value to indicate
107 that the passed hash-reference can be used directly as the underlying
108 hash-reference for the new Treex::PML::Node object (which avoids copying). It
109 is, however, not guaranteed that the hash-reference will be reused;
110 the caller thus must even in this case work with the object returned
111 by the constructor rather that the hash-reference passed.
112  
113 Returns the newly created Treex::PML::Node object.
114  
115 =cut
116  
117  
118 11235 ▶sub new {
Subroutine new: called CC=6 SCAR=18.8
Pod documented
119 11235 my $self = shift;
120 11235 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
0112350
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
121 11235 my $new = shift;
122 11235 ▶ if (ref($new)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $new) { } 11207 28 11235
123 11207 my $reuse=shift;
124 11207 ▶ unless ($reuse) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($reuse) 0 11207 11207
125 0 $new={%$new};
126 }
127 } else {
128 28 my $size=$new;
129 28 ▶ croak("Usage: ".__PACKAGE__."->new(key=>value, ...) - got ",join(', ',map ref($_).qq{= '$_'},@_)) if scalar(@_)/2!=0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar(@_) / 2 != 0 0 28 28
130 28 $new = {@_};
131 28 ▶ keys (%$new) = $size + 5 if defined($size);
Branch: 1 branch
BranchTrueFalseTotal
if defined $size 0 28 28
132 }
133 11235 bless $new, $class;
134 11235 return $new;
135 }
136  
137 =pod
138  
139 =item $node->destroy
140  
141 This function destroys a Treex::PML::Node (and all its descendants).
142 If the node has a parent, it is cut from it first.
143  
144 =cut
145  
146 568 ▶sub destroy {
Subroutine destroy: called CC=3 SCAR=11.1
Pod documented
147 568 my ($top) = @_;
148 568 ▶ $top->cut() if $top->{$parent};
Branch: 1 branch
BranchTrueFalseTotal
if $top->{$Treex::PML::Node::parent} 0 568 568
149 568 undef %$_ for ($top->descendants,$top);
150 568 return;
151 }
152  
153 =item $node->destroy_leaf
154  
155 This function destroys a leaf Treex::PML::Node and fails if the node is not a leaf (has children).
156 If the node has a parent, it is cut from it first.
157  
158 =cut
159  
160 0 ▶sub destroy_leaf {
Subroutine destroy_leaf: not called CC=2 SCAR=17.9
Pod documented
161 0 my ($node) = @_;
162 0 ▶ unless ($node->firstson) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->firstson) { } 0 0 0
163 0 $node->cut;
164 0 undef %$node;
165 0 undef $node;
166 0 return 1;
167 } else {
168 0 croak(ref($node)."->destroy_leaf: Not a leaf node");
169 }
170 }
171  
172 {
173 9 ▶no warnings qw(recursion); # disable deep recursion warnings in Treex::PML::Node::DESTROY (btw, no recursion there:-))
Subroutine BEGIN: called CC=1 SCAR=0.0
174 11235 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=7.0
175 11235 my ($self) = @_;
176 11235 ▶ return unless ref($self);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 11235 11235
177 11235 %{$self}=(); # this should not be needed, but
178 # without it, perl 5.10 leaks on weakened
179 # structures, try:
180 # Scalar::Util::weaken({}) while 1
181 11235 return 1;
182 }
183 }
184  
185 =pod
186  
187 =item $node->parent
188  
189 Return node's parent node (C<undef> if none).
190  
191 =cut
192  
193 9 ▶sub parent {
Subroutine parent: not called CC=1 SCAR=1.2
Pod documented
194 0 return shift()->{$parent};
195 }
196  
197 =pod
198  
199 =item $node->type (attr-path?)
200  
201 If called without an argument or if C<attr-path> is empty, return
202 node's data-type declaration (C<undef> if none). If C<attr-path> is
203 non-empty, return the data-type declaration of the value reachable
204 from C<$node> under the attribute-path C<attr-path>.
205  
206 =cut
207  
208  
209 0 ▶sub type {
Subroutine type: not called CC=4 SCAR=30.0
Pod documented
210 0 my ($self,$attr) = @_;
211 0 my $type = $self->{$TYPE};
212 0 ▶ if (defined $attr and length $attr) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $attr and length $attr) { } 0 0 0
Condition0%0/3
defined $attr and length $attr
!ll&&!rl&&r
000
MC/DC0%0/2
defined $attr and length $attr
defined $attr length $attr
Truth table0%0/3
A and B
A= defined $attrB= length $attr
ABresult
0X0
100
111
213 0 ▶ return $type ? $type->find($attr,1) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$type ? : 0 0 0
214 } else {
215 0 return $type;
216 }
217 }
218  
219 =item $node->root
220  
221 Find and return the root of the node's tree.
222  
223 =cut
224  
225  
226 0 ▶sub root {
Subroutine root: not called CC=2 SCAR=17.9
Pod documented
227 0 my ($node) = @_;
228 0 ▶ while (my $p = $node->{$parent}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $p = $node->{$Treex::PML::Node::parent}) 0 0 0
229 0 $node=$p;
230 }
231 0 return $node;
232 }
233  
234 =item $node->level
235  
236 Calculate node's level (root-level is 0).
237  
238 =cut
239  
240 0 ▶sub level {
Subroutine level: not called CC=2 SCAR=17.9
Pod documented
241 0 my ($node) = @_;
242 0 my $level=-1;
243 0 ▶ while ($node) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node) 0 0 0
244 0 $node=$node->parent;
245 0 $level++;
246 0 } return $level;
247 }
248  
249  
250 =pod
251  
252 =item $node->lbrother
253  
254 Return node's left brother node (C<undef> if none).
255  
256 =cut
257  
258 0 ▶sub lbrother {
Subroutine lbrother: not called CC=1 SCAR=6.9
Pod documented
259 0 return shift()->{$lbrother};
260 }
261  
262 =pod
263  
264 =item $node->rbrother
265  
266 Return node's right brother node (C<undef> if none).
267  
268 =cut
269  
270 4150 ▶sub rbrother {
Subroutine rbrother: called CC=1 SCAR=0.0
Pod documented
271 4150 return shift()->{$rbrother};
272 }
273  
274 =pod
275  
276 =item $node->firstson
277  
278 Return node's first dependent node (C<undef> if none).
279  
280 =cut
281  
282 672 ▶sub firstson {
Subroutine firstson: called CC=1 SCAR=0.0
Pod documented
283 672 return shift()->{$firstson};
284 }
285  
286 8 ▶sub set_parent {
Subroutine set_parent: called CC=2 SCAR=7.4
Pod undocumented
287 8 my ($node,$p) = @_;
288 8 ▶ if (ref( $p )) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $p) { } 8 0 8
289 8 weaken( $node->{$parent} = $p );
290 } else {
291 0 $node->{$parent} = undef;
292 }
293 8 return $p;
294 }
295  
296 8 ▶sub set_lbrother {
Subroutine set_lbrother: called CC=2 SCAR=7.4
Pod undocumented
297 8 my ($node,$p) = @_;
298 8 ▶ if (ref( $p )) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $p) { } 8 0 8
299 8 weaken( $node->{$lbrother} = $p );
300 } else {
301 0 $node->{$lbrother} = undef;
302 }
303 8 return $p;
304 }
305  
306 0 ▶sub set_rbrother {
Subroutine set_rbrother: not called CC=2 SCAR=17.9
Pod undocumented
307 0 my ($node,$p) = @_;
308 0 ▶ $node->{$rbrother}= ref($p) ? $p : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $p ? : 0 0 0
309 }
310  
311 0 ▶sub set_firstson {
Subroutine set_firstson: not called CC=2 SCAR=17.9
Pod undocumented
312 0 my ($node,$p) = @_;
313 0 ▶ $node->{$firstson}=ref($p) ? $p : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $p ? : 0 0 0
314 }
315  
316 =item $node->set_type (type)
317  
318 Wherever possible, avoid using this method directly; instead
319 create a typed nodes using Treex::PML::Factory->createTypedNode().
320  
321 Associate Treex::PML::Node object with a type declaration-object (see
322 L<Treex::PML::Schema> class).
323  
324 =cut
325  
326 11235 ▶sub set_type {
Subroutine set_type: called CC=1 SCAR=0.0
Pod documented
327 11235 my ($node,$t) = @_;
328 11235 $node->{$TYPE}=$t;
329 }
330  
331 =item $node->set_type_by_name (schema,type-name)
332  
333 Lookup a structure or container declaration in the given Treex::PML::Schema
334 by its type name and associate the corresponding type-declaration
335 object with the Treex::PML::Node.
336  
337 =cut
338  
339 0 ▶sub set_type_by_name {
Subroutine set_type_by_name: not called CC=9 SCAR=45.0
Pod documented
340 0 ▶ if (@_!=3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ != 3) 0 0 0
341 0 croak('Usage: $node->set_type_by_name($schema, $type_name)');
342 }
343 0 my ($node,$schema,$name) = @_;
344 0 my $type = $schema->get_type_by_name($name);
345 0 ▶ if (ref($type)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $type) { } 0 0 0
346 0 my $decl_type = $type->get_decl_type;
347 0 ▶ if ($decl_type == PML_MEMBER_DECL() ||
Branch: 1 branch
BranchTrueFalseTotal
if ($decl_type == 12 or $decl_type == 13 or $decl_type == 1 or $decl_type == 2) 0 0 0
Condition0%0/9
$decl_type == 12 or $decl_type == 13 or $decl_type == 1 or $decl_type == 2
l!l&&r!l&&!r
000
$decl_type == 12 or $decl_type == 13 or $decl_type == 1
l!l&&r!l&&!r
000
$decl_type == 12 or $decl_type == 13
l!l&&r!l&&!r
000
MC/DC0%0/4
$decl_type == 12 or $decl_type == 13 or $decl_type == 1 or $decl_type == 2
$decl_type == 12 $decl_type == 13 $decl_type == 1 $decl_type == 2
Truth table0%0/5
A or B or C or D
A= $decl_type == 12B= $decl_type == 13C= $decl_type == 1D= $decl_type == 2
ABCDresult
1XXX1
01XX1
001X1
00011
00000
348 $decl_type == PML_ELEMENT_DECL() ||
349 $decl_type == PML_TYPE_DECL() ||
350 $decl_type == PML_ROOT_DECL() ) {
351 0 $type = $type->get_content_decl;
352 }
353 0 $decl_type = $type->get_decl_type;
354 0 ▶ if ($decl_type == PML_CONTAINER_DECL() ||
Branch: 1 branch
BranchTrueFalseTotal
if ($decl_type == 4 or $decl_type == 3) { } 0 0 0
Condition0%0/3
$decl_type == 4 or $decl_type == 3
l!l&&r!l&&!r
000
MC/DC0%0/2
$decl_type == 4 or $decl_type == 3
$decl_type == 4 $decl_type == 3
Truth table0%0/3
A or B
A= $decl_type == 4B= $decl_type == 3
ABresult
1X1
011
000
355 $decl_type == PML_STRUCTURE_DECL()) {
356 0 $node->set_type($type);
357 } else {
358 0 croak __PACKAGE__."::set_type_by_name: Incompatible type '$name' (neither a structure nor a container)";
359 }
360 } else {
361 0 croak __PACKAGE__."::set_type_by_name: Type not found '$name'";
362 }
363 }
364  
365 =item $node->validate (attr-path?,log?)
366  
367 This method requires C<$node> to be associated with a type declaration.
368  
369 Validates the content of the node according to the associated type and
370 schema. If attr-path is non-empty, validate only attribute selected by
371 the attribute path. An array reference may be passed as the 2nd
372 argument C<log> to obtain a detailed report of all validation errors.
373  
374 Note: this method does not validate descendants of the node. Use e.g.
375  
376 $node->validate_subtree(\@log);
377  
378 to validate the complete subtree.
379  
380 Returns: 1 if the content validates, 0 otherwise.
381  
382 =cut
383  
384 0 ▶sub validate {
Subroutine validate: not called CC=6 SCAR=37.4
Pod documented
385 0 my ($node, $path, $log) = @_;
386 0 ▶ if (defined $log and UNIVERSAL::isa($log,'ARRAY')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $log and UNIVERSAL::isa($log, "ARRAY")) 0 0 0
Condition0%0/3
defined $log and UNIVERSAL::isa($log, "ARRAY")
!ll&&!rl&&r
000
MC/DC0%0/2
defined $log and UNIVERSAL::isa($log, "ARRAY")
defined $log UNIVERSAL::isa($log, "ARRAY")
Truth table0%0/3
A and B
A= defined $logB= UNIVERSAL::isa($log, "ARRAY")
ABresult
0X0
100
111
387 0 croak __PACKAGE__."::validate: log must be an ARRAY reference";
388 }
389 0 my $type = $node->type;
390 0 ▶ if (!ref($type)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $type) 0 0 0
391 0 croak __PACKAGE__."::validate: Cannot determine node data type!";
392 }
393 0 ▶ if ($path eq q{}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq '') { } 0 0 0
394 0 $type->validate_object($node,{ log=>$log, no_childnodes => 1 });
395 } else {
396 0 my $mtype = $type->find($path);
397 0 ▶ if ($mtype) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mtype) { } 0 0 0
398 0 $mtype->validate_object($node->attr($path),
399 {
400 path => $path,
401 log=>$log
402 });
403 } else {
404 0 croak __PACKAGE__."::validate: can't determine data type from attribute-path '$path'!";
405 }
406 }
407 }
408  
409 =item $node->validate_subtree (log?)
410  
411 This method requires C<$node> to be associated with a type declaration.
412  
413 Validates the content of the node and all its descendants according to
414 the associated type and schema. An array reference C<log> may be
415 passed as an argument to obtain a detailed report of all validation
416 errors.
417  
418 Returns: 1 if the subtree validates, 0 otherwise.
419  
420 =cut
421  
422 0 ▶sub validate_subtree {
Subroutine validate_subtree: not called CC=4 SCAR=30.0
Pod documented
423 0 my ($node, $log) = @_;
424 0 ▶ if (defined $log and ! UNIVERSAL::isa($log,'ARRAY')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $log and not UNIVERSAL::isa($log, "ARRAY")) 0 0 0
Condition0%0/3
defined $log and not UNIVERSAL::isa($log, "ARRAY")
!ll&&!rl&&r
000
MC/DC0%0/2
defined $log and not UNIVERSAL::isa($log, "ARRAY")
defined $log not UNIVERSAL::isa($log, "ARRAY")
Truth table0%0/3
A and not(B)
A= defined $logB= not UNIVERSAL::isa($log, "ARRAY")
ABresult
0X0
100
111
425 0 croak __PACKAGE__."::validate: log must be an ARRAY reference";
426 }
427 0 my $type = $node->type;
428 0 ▶ if (!ref($type)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $type) 0 0 0
429 0 croak __PACKAGE__."::validate: Cannot determine node data type!";
430 }
431 0 $type->validate_object($node,{ log=>$log });
432 }
433  
434 =item $node->attribute_paths
435  
436 This method requires C<$node> to be associated with a type declaration.
437  
438 This method is similar to Treex::PML::Schema->attributes but for a single
439 node. It returns attribute paths valid for the current node. That is,
440 it returns paths to all atomic subtypes of the type of the current
441 node.
442  
443  
444 =cut
445  
446 0 ▶sub attribute_paths {
Subroutine attribute_paths: not called CC=2 SCAR=17.9
Pod documented
447 0 my ($node) = @_;
448 0 my $type = $node->type;
449 0 ▶ return unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 0 0
450 0 return $type->schema->get_paths_to_atoms([$type],{ no_childnodes => 1 });
451 }
452  
453  
454 =pod
455  
456 =item $node->following (top?)
457  
458 Return the next node of the subtree in the order given by structure
459 (C<undef> if none). If any descendant exists, the first one is
460 returned. Otherwise, right brother is returned, if any. If the given
461 node has neither a descendant nor a right brother, the right brother
462 of the first (lowest) ancestor for which right brother exists, is
463 returned.
464  
465 =cut
466  
467 8484 ▶sub following {
Subroutine following: called CC=7 SCAR=19.9
Pod documented
468 8484 my ($node,$top) = @_;
469 8484 ▶ if ($node->{$firstson}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{$Treex::PML::Node::firstson}) 2524 5960 8484
470 2524 return $node->{$firstson};
471 }
472 5960 ▶ $top||=0; # for ==
Condition50%1/2
$top ||= 0
l!l
59600
MC/DC0%0/1
$top ||= 0
$top
Truth table50%1/2
A
A= $top
Aresult
11
00
473 5960 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$node and do { do { return if $node == $top or not $node->{$Treex::PML::Node::parent}; return $node->{$Treex::PML::Node::rbrother} if $node->{$Treex::PML::Node::rbrother}; $node = $node->{$Treex::PML::Node::parent} } } 2524 0 2524
474 8484 ▶ return if ($node==$top or !$node->{$parent});
Branch: 1 branch
BranchTrueFalseTotal
if $node == $top or not $node->{$Treex::PML::Node::parent} 568 7916 8484
Condition66%2/3
$node == $top or not $node->{$Treex::PML::Node::parent}
l!l&&r!l&&!r
56807916
MC/DC50%1/2
$node == $top or not $node->{$Treex::PML::Node::parent}
$node == $top not $node->{$Treex::PML::Node::parent}
Truth table66%2/3
A or not(B)
A= $node == $topB= not $node->{$Treex::PML::Node::parent}
ABresult
1X1
011
000
475 7916 ▶ return $node->{$rbrother} if $node->{$rbrother};
Branch: 1 branch
BranchTrueFalseTotal
if $node->{$Treex::PML::Node::rbrother} 5392 2524 7916
476 2524 $node = $node->{$parent};
477 } while ($node);
478 0 return;
479 }
480  
481 =pod
482  
483 =item $node->following_visible (FSFormat_object,top?)
484  
485 Return the next visible node of the subtree in the order given by
486 structure (C<undef> if none). A node is considered visible if it has
487 no hidden ancestor. Requires FSFormat object as the first parameter.
488  
489 =cut
490  
491 0 ▶sub following_visible {
Subroutine following_visible: not called CC=5 SCAR=34.0
Pod documented
492 0 my ($self,$fsformat,$top) = @_;
493 0 ▶ return unless ref($self);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
494 0 my $node=$self->following($top);
495 0 ▶ return $node unless ref($fsformat);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fsformat 0 0 0
496 0 my $hiding;
497 0 ▶ while ($node) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node) 0 0 0
498 0 ▶ return $node unless ($hiding=$fsformat->isHidden($node));
Branch: 1 branch
BranchTrueFalseTotal
unless $hiding = $fsformat->isHidden($node) 0 0 0
499 0 $node=$hiding->following_right_or_up($top);
500 }
501 }
502  
503 =pod
504  
505 =item $node->following_right_or_up (top?)
506  
507 Return the next node of the subtree in the order given by
508 structure (C<undef> if none), but not descending.
509  
510 =cut
511  
512 0 ▶sub following_right_or_up {
Subroutine following_right_or_up: not called CC=7 SCAR=40.3
Pod documented
513 0 my ($self,$top) = @_;
514 0 ▶ return unless ref($self);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
515  
516 0 my $node=$self;
517 0 ▶ while ($node) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node) 0 0 0
518 0 ▶ return 0 if (defined($top) and $node==$top or !$node->parent);
Branch: 1 branch
BranchTrueFalseTotal
if defined $top and $node == $top or not $node->parent 0 0 0
Condition0%0/6
defined $top and $node == $top or not $node->parent
l!l&&r!l&&!r
000
defined $top and $node == $top
!ll&&!rl&&r
000
MC/DC0%0/3
defined $top and $node == $top or not $node->parent
defined $top $node == $top not $node->parent
Truth table0%0/5
A and B or not(C)
A= defined $topB= $node == $topC= not $node->parent
ABCresult
11X1
0X11
1011
0X00
1000
519 0 ▶ return $node->rbrother if $node->rbrother;
Branch: 1 branch
BranchTrueFalseTotal
if $node->rbrother 0 0 0
520 0 $node = $node->parent;
521 }
522 }
523  
524  
525 =pod
526  
527 =item $node->previous (top?)
528  
529 Return the previous node of the subtree in the order given by
530 structure (C<undef> if none). The way of searching described in
531 C<following> is used here in reversed order.
532  
533 =cut
534  
535 0 ▶sub previous {
Subroutine previous: not called CC=8 SCAR=42.8
Pod documented
536 0 my ($node,$top) = @_;
537 0 ▶ return unless ref $node;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node 0 0 0
538 0 ▶ $top||=0;
Condition0%0/2
$top ||= 0
l!l
00
MC/DC0%0/1
$top ||= 0
$top
Truth table0%0/2
A
A= $top
Aresult
11
00
539 0 ▶ if ($node->{$lbrother}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{$Treex::PML::Node::lbrother}) 0 0 0
540 0 $node = $node->{$lbrother};
541 0 ▶ DIGDOWN: while ($node->{$firstson}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{$Treex::PML::Node::firstson}) 0 0 0
542 0 $node = $node->{$firstson};
543 0 ▶ LASTBROTHER: while ($node->{$rbrother}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{$Treex::PML::Node::rbrother}) 0 0 0
544 0 $node = $node->{$rbrother};
545 0 next LASTBROTHER;
546 }
547 0 next DIGDOWN;
548 }
549 0 return $node;
550 }
551 0 ▶ return if ($node == $top or !$node->{$parent});
Branch: 1 branch
BranchTrueFalseTotal
if $node == $top or not $node->{$Treex::PML::Node::parent} 0 0 0
Condition0%0/3
$node == $top or not $node->{$Treex::PML::Node::parent}
l!l&&r!l&&!r
000
MC/DC0%0/2
$node == $top or not $node->{$Treex::PML::Node::parent}
$node == $top not $node->{$Treex::PML::Node::parent}
Truth table0%0/3
A or not(B)
A= $node == $topB= not $node->{$Treex::PML::Node::parent}
ABresult
1X1
011
000
552 0 return $node->{$parent};
553 }
554  
555  
556 =pod
557  
558 =item $node->previous_visible (FSFormat_object,top?)
559  
560 Return the next visible node of the subtree in the order given by
561 structure (C<undef> if none). A node is considered visible if it has
562 no hidden ancestor. Requires FSFormat object as the first parameter.
563  
564 =cut
565  
566 0 ▶sub previous_visible {
Subroutine previous_visible: not called CC=5 SCAR=34.0
Pod documented
567 0 my ($self,$fsformat,$top) = @_;
568 0 ▶ return unless ref($self);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
569 0 my $node=$self->previous($top);
570 0 my $hiding;
571 0 ▶ return $node unless ref($fsformat);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fsformat 0 0 0
572 0 ▶ while ($node) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node) 0 0 0
573 0 ▶ return $node unless ($hiding=$fsformat->isHidden($node));
Branch: 1 branch
BranchTrueFalseTotal
unless $hiding = $fsformat->isHidden($node) 0 0 0
574 0 $node=$hiding->previous($top);
575 }
576 }
577  
578  
579 =pod
580  
581 =item $node->rightmost_descendant (node)
582  
583 Return the rightmost lowest descendant of the node (or
584 the node itself if the node is a leaf).
585  
586 =cut
587  
588 0 ▶sub rightmost_descendant {
Subroutine rightmost_descendant: not called CC=4 SCAR=30.0
Pod documented
589 0 my ($self) = @_;
590 0 ▶ return unless ref($self);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
591 0 my $node=$self;
592 0 ▶ DIGDOWN: while ($node->firstson) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->firstson) 0 0 0
593 0 $node = $node->firstson;
594 0 ▶ LASTBROTHER: while ($node->rbrother) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->rbrother) 0 0 0
595 0 $node = $node->rbrother;
596 0 next LASTBROTHER;
597 }
598 0 next DIGDOWN;
599 }
600 0 return $node;
601 }
602  
603  
604 =pod
605  
606 =item $node->leftmost_descendant (node)
607  
608 Return the leftmost lowest descendant of the node (or
609 the node itself if the node is a leaf).
610  
611 =cut
612  
613 0 ▶sub leftmost_descendant {
Subroutine leftmost_descendant: not called CC=3 SCAR=24.8
Pod documented
614 0 my ($self) = @_;
615 0 ▶ return unless ref($self);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
616 0 my $node=$self;
617 0 ▶ $node=$node->firstson while ($node->firstson);
Branch: 1 branch
BranchTrueFalseTotal
$node->firstson and do { $node = $node->firstson } 0 0 0
618 0 return $node;
619 }
620  
621 =pod
622  
623 =item $node->getAttribute (attr_name)
624  
625 Return value of the given attribute.
626  
627 =cut
628  
629 # compatibility
630 0 ▶sub getAttribute { shift()->get_member(@_) }
Subroutine getAttribute: not called CC=1 SCAR=6.9
Pod documented
631  
632 =item $node->attr (path)
633  
634 Retrieve first value matching a given attribute path.
635  
636 $node->attr($path)
637  
638 is an alias for
639  
640 Treex::PML::Instance::get_data($node,$path);
641  
642 See L<Treex::PML::Instance::get_data|Treex::PML::Instance/get_data> for details.
643  
644 =cut
645  
646 703 ▶sub attr {
Subroutine attr: called CC=1 SCAR=0.0
Pod documented
647 703 &Treex::PML::Instance::get_data;
648 }
649  
650 =item $node->all (path)
651  
652 Retrieve all values matching a given attribute path.
653  
654 $node->all($path)
655  
656 is an alias for
657  
658 Treex::PML::Instance::get_all($node,$path);
659  
660 See L<Treex::PML::Instance::get_all|Treex::PML::Instance/get_all> for details.
661  
662 =cut
663  
664 0 ▶sub all {
Subroutine all: not called CC=1 SCAR=6.9
Pod documented
665 0 &Treex::PML::Instance::get_all;
666 }
667  
668 0 ▶sub flat_attr {
Subroutine flat_attr: not called CC=5 SCAR=34.0
Pod undocumented
669 0 my ($node,$path) = @_;
670 0 ▶ return "$node" unless ref($node);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node 0 0 0
671 0 my ($step,$rest) = split /\//, $path,2;
672 0 ▶ if (UNIVERSAL::DOES::does($node,'Treex::PML::List') or
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::DOES::does($node, 'Treex::PML::List') or UNIVERSAL::DOES::does($node, 'Treex::PML::Alt')) { } 0 0 0
Condition0%0/3
UNIVERSAL::DOES::does($node, "Treex::PML::List") or UNIVERSAL::DOES::does($node, "Treex::PML::Alt")
l!l&&r!l&&!r
000
MC/DC0%0/2
UNIVERSAL::DOES::does($node, "Treex::PML::List") or UNIVERSAL::DOES::does($node, "Treex::PML::Alt")
UNIVERSAL::DOES::does($node, "Treex::PML::List") UNIVERSAL::DOES::does($node, "Treex::PML::Alt")
Truth table0%0/3
A or B
A= UNIVERSAL::DOES::does($node, "Treex::PML::List")B= UNIVERSAL::DOES::does($node, "Treex::PML::Alt")
ABresult
1X1
011
000
673 UNIVERSAL::DOES::does($node,'Treex::PML::Alt')) {
674 0 ▶ if ($step =~ /^\[(\d+)\]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($step =~ /^\[(\d+)\]$/) { } 0 0 0
675 0 return flat_attr($node->[$1-1],$rest);
676 } else {
677 0 return join "|",map { flat_attr($_,$rest) } @$node;
678 }
679 } else {
680 0 return flat_attr($node->{$step},$rest);
681 }
682 }
683  
684 =item $node->set_attr (path,value,strict?)
685  
686 Store a given value to a possibly nested attribute of $node specified
687 by path. The path argument uses the XPath-like syntax described in
688 L<Treex::PML::Instance::set_data|Treex::PML::Instance/set_data>.
689  
690 =cut
691  
692 0 ▶sub set_attr {
Subroutine set_attr: not called CC=1 SCAR=6.9
Pod documented
693 0 &Treex::PML::Instance::set_data;
694 }
695  
696 =pod
697  
698 =item $node->setAttribute (name,value)
699  
700 Set value of the given attribute.
701  
702 =cut
703  
704 # compatibility
705 9 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
706 9 *setAttribute = \&set_member;
707 }
708  
709 =pod
710  
711 =item $node->children
712  
713 Return a list of dependent nodes.
714  
715 =cut
716  
717 0 ▶sub children {
Subroutine children: not called CC=2 SCAR=17.9
Pod documented
718 0 my $self = $_[0];
719 0 my @children=();
720 0 my $child=$self->firstson;
721 0 ▶ while ($child) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child) 0 0 0
722 0 push @children, $child;
723 0 $child=$child->rbrother;
724 }
725 0 return @children;
726 }
727  
728 =pod
729  
730 =item $node->visible_children (fsformat)
731  
732 Return a list of visible dependent nodes.
733  
734 =cut
735  
736 0 ▶sub visible_children {
Subroutine visible_children: not called CC=6 SCAR=37.4
Pod documented
737 0 my ($self,$fsformat) = @_;
738 0 ▶ croak "required parameter missing for visible_children(fsformat)" unless $fsformat;
Branch: 1 branch
BranchTrueFalseTotal
unless $fsformat 0 0 0
739 0 my @children=();
740 0 ▶ unless ($fsformat->isHidden($self)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fsformat->isHidden($self)) 0 0 0
741 0 my $hid=$fsformat->hide;
742 0 my $child=$self->firstson;
743 0 ▶ while ($child) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child) 0 0 0
744 0 my $hidden = $child->getAttribute($hid);
745 0 ▶ push @children, $child unless defined($hidden) and length($hidden);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hidden and length $hidden 0 0 0
Condition0%0/3
defined $hidden and length $hidden
!ll&&!rl&&r
000
MC/DC0%0/2
defined $hidden and length $hidden
defined $hidden length $hidden
Truth table0%0/3
A and B
A= defined $hiddenB= length $hidden
ABresult
0X0
100
111
746 0 $child=$child->rbrother;
747 }
748 }
749 0 return @children;
750 }
751  
752  
753 =item $node->descendants
754  
755 Return a list recursively dependent nodes.
756  
757 =cut
758  
759 568 ▶sub descendants {
Subroutine descendants: called CC=2 SCAR=6.9
Pod documented
760 568 my $self = $_[0];
761 568 my @kin=();
762 568 my $desc=$self->following($self);
763 568 ▶ while ($desc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($desc) 7916 568 8484
764 7916 push @kin, $desc;
765 7916 $desc=$desc->following($self);
766 }
767 568 return @kin;
768 }
769  
770 =item $node->visible_descendants (fsformat)
771  
772 Return a list recursively dependent visible nodes.
773  
774 =cut
775  
776 0 ▶sub visible_descendants {
Subroutine visible_descendants: not called CC=3 SCAR=24.8
Pod documented
777 0 my ($self,$fsformat) = @_;
778 0 ▶ croak "required parameter missing for visible_descendants(fsfile)" unless $fsformat;
Branch: 1 branch
BranchTrueFalseTotal
unless $fsformat 0 0 0
779 0 my @kin=();
780 0 my $desc=$self->following_visible($fsformat,$self);
781 0 ▶ while ($desc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($desc) 0 0 0
782 0 push @kin, $desc;
783 0 $desc=$desc->following_visible($fsformat,$self);
784 }
785 0 return @kin;
786 }
787  
788 =item $node->ancestors
789  
790 Return a list of ancestor nodes of $node, e.g. the list of nodes on
791 the path from the node's parent to the root of the tree.
792  
793 =cut
794  
795 0 ▶sub ancestors {
Subroutine ancestors: not called CC=2 SCAR=17.9
Pod documented
796 0 my ($self)=@_;
797 0 $self = $self->parent;
798 0 my @ancestors;
799 0 ▶ while ($self) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self) 0 0 0
800 0 push @ancestors,$self;
801 0 $self = $self->parent;
802 }
803 0 return @ancestors;
804 }
805  
806  
807 =item $node->cut ()
808  
809 Disconnect the node from its parent and siblings. Returns the node
810 itself.
811  
812 =cut
813  
814 0 ▶sub cut {
Subroutine cut: not called CC=5 SCAR=34.0
Pod documented
815 0 my ($node)=@_;
816 0 my $p = $node->{$parent};
817 0 ▶ if ($p and $node==$p->{$firstson}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p and $node == $p->{$Treex::PML::Node::firstson}) 0 0 0
Condition0%0/3
$p and $node == $p->{$Treex::PML::Node::firstson}
!ll&&!rl&&r
000
MC/DC0%0/2
$p and $node == $p->{$Treex::PML::Node::firstson}
$p $node == $p->{$Treex::PML::Node::firstson}
Truth table0%0/3
A and B
A= $pB= $node == $p->{$Treex::PML::Node::firstson}
ABresult
0X0
100
111
818 0 $p->{$firstson}=$node->{$rbrother};
819 }
820 0 ▶ $node->{$lbrother}->set_rbrother($node->{$rbrother}) if ($node->{$lbrother});
Branch: 1 branch
BranchTrueFalseTotal
if $node->{$Treex::PML::Node::lbrother} 0 0 0
821 0 ▶ $node->{$rbrother}->set_lbrother($node->{$lbrother}) if ($node->{$rbrother});
Branch: 1 branch
BranchTrueFalseTotal
if $node->{$Treex::PML::Node::rbrother} 0 0 0
822 0 $node->{$parent}=$node->{$lbrother}=$node->{$rbrother}=undef;
823 0 return $node;
824 }
825  
826  
827 =item $node->paste_on (new-parent,ord-attr)
828  
829 Attach a new or previously disconnected node to a new parent, placing
830 it to the position among the other child nodes corresponding to a
831 numerical value obtained from the ordering attribute specified in
832 C<ord-attr>. If C<ord-attr> is not given, the node becomes the
833 left-most child of its parent.
834  
835 This method does not check node types, but one can use
836 C<$parent-E<gt>test_child_type($node)> before using this method to verify
837 that the node is of a permitted child-type for the parent node.
838  
839 Returns the node itself.
840  
841 =cut
842  
843 0 ▶sub paste_on {
Subroutine paste_on: not called CC=11 SCAR=48.8
Pod documented
844 0 my ($node,$p,$fsformat)=@_;
845 0 ▶ my $aord = ref($fsformat) ? $fsformat->order : $fsformat;
Branch: 1 branch
BranchTrueFalseTotal
ref $fsformat ? : 0 0 0
846 0 ▶ my $ordnum = defined($aord) ? $node->{$aord} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $aord ? : 0 0 0
847 0 my $b=$p->{$firstson};
848 0 ▶ if ($b and defined($ordnum) and $ordnum>($b->{$aord}||0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b and defined $ordnum and $ordnum > ($b->{$aord} || 0)) { } 0 0 0
Condition0%0/8
$b and defined $ordnum and $ordnum > ($b->{$aord} || 0)
!ll&&!rl&&r
000
$b and defined $ordnum
!ll&&!rl&&r
000
$b->{$aord} || 0
l!l
00
MC/DC0%0/3
$b and defined $ordnum and $ordnum > ($b->{$aord} || 0)
$b defined $ordnum $ordnum > ($b->{$aord} || 0)
MC/DC0%0/1
$b->{$aord} || 0
$b->{$aord}
Truth table0%0/4
A and B and C
A= $bB= defined $ordnumC= $ordnum > ($b->{$aord} || 0)
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/2
A
A= $b->{$aord}
Aresult
11
00
849 0 ▶ $b=$b->{$rbrother} while ($b->{$rbrother} and $ordnum>$b->{$rbrother}->{$aord});
Branch: 1 branch
BranchTrueFalseTotal
$b->{$Treex::PML::Node::rbrother} and $ordnum > $b->{$Treex::PML::Node::rbrother}{$aord} and do { $b = $b->{$Treex::PML::Node::rbrother} } 0 0 0
Condition0%0/3
$b->{$Treex::PML::Node::rbrother} and $ordnum > $b->{$Treex::PML::Node::rbrother}{$aord}
!ll&&!rl&&r
000
MC/DC0%0/2
$b->{$Treex::PML::Node::rbrother} and $ordnum > $b->{$Treex::PML::Node::rbrother}{$aord}
$b->{$Treex::PML::Node::rbrother} $ordnum > $b->{$Treex::PML::Node::rbrother}{$aord}
Truth table0%0/3
A and B
A= $b->{$Treex::PML::Node::rbrother}B= $ordnum > $b->{$Treex::PML::Node::rbrother}{$aord}
ABresult
0X0
100
111
850 0 my $rb = $b->{$rbrother};
851 0 $node->{$rbrother}=$rb;
852 # $rb->set_lbrother( $node ) if $rb;
853 0 ▶ weaken( $rb->{$lbrother} = $node ) if $rb;
Branch: 1 branch
BranchTrueFalseTotal
if $rb 0 0 0
854 0 $b->{$rbrother}=$node;
855 #$node->set_lbrother( $b );
856 0 weaken( $node->{$lbrother} = $b );
857 #$node->set_parent( $p );
858 0 weaken( $node->{$parent} = $p );
859 } else {
860 0 $node->{$rbrother}=$b;
861 0 $p->{$firstson}=$node;
862 0 $node->{$lbrother}=undef;
863 #$b->set_lbrother( $node ) if ($b);
864 0 ▶ weaken( $b->{$lbrother} = $node ) if $b;
Branch: 1 branch
BranchTrueFalseTotal
if $b 0 0 0
865 #$node->set_parent( $p );
866 0 weaken( $node->{$parent} = $p );
867 }
868 0 return $node;
869 }
870  
871 =item $node->paste_after (ref-node)
872  
873 Attach a new or previously disconnected node to ref-node's parent node
874 as a closest right sibling of ref-node in the structural order of
875 sibling nodes.
876  
877 This method does not check node types, but one can use
878 C<$ref_node-E<gt>parent->test_child_type($node)> before using this method
879 to verify that the node is of a permitted child-type for the parent
880 node.
881  
882 Returns the node itself.
883  
884 =cut
885  
886 0 ▶sub paste_after {
Subroutine paste_after: not called CC=4 SCAR=30.0
Pod documented
887 0 my ($node,$ref_node)=@_;
888 0 ▶ croak(__PACKAGE__."->paste_after: ref_node undefined") unless $ref_node;
Branch: 1 branch
BranchTrueFalseTotal
unless $ref_node 0 0 0
889 0 my $p = $ref_node->{$parent};
890 0 ▶ croak(__PACKAGE__."->paste_after: ref_node has no parent") unless $p;
Branch: 1 branch
BranchTrueFalseTotal
unless $p 0 0 0
891  
892 0 my $rb = $ref_node->{$rbrother};
893 0 $node->{$rbrother}=$rb;
894 # $rb->set_lbrother( $node ) if $rb;
895 0 ▶ weaken( $rb->{$lbrother} = $node ) if $rb;
Branch: 1 branch
BranchTrueFalseTotal
if $rb 0 0 0
896 0 $ref_node->{$rbrother}=$node;
897 #$node->set_lbrother( $ref_node );
898 0 weaken( $node->{$lbrother} = $ref_node );
899 #$node->set_parent( $p );
900 0 weaken( $node->{$parent} = $p );
901 0 return $node;
902 }
903  
904 =item $node->paste_before (ref-node)
905  
906 Attach a new or previously disconnected node to ref-node's parent node
907 as a closest left sibling of ref-node in the structural order of
908 sibling nodes.
909  
910 This method does not check node types, but one can use
911 C<$ref_node-E<gt>parent->test_child_type($node)> before using this method
912 to verify that the node is of a permitted child-type for the parent
913 node.
914  
915 Returns the node itself.
916  
917 =cut
918  
919 0 ▶sub paste_before {
Subroutine paste_before: not called CC=4 SCAR=30.0
Pod documented
920 0 my ($node,$ref_node)=@_;
921  
922 0 ▶ croak(__PACKAGE__."->paste_before: ref_node undefined") unless $ref_node;
Branch: 1 branch
BranchTrueFalseTotal
unless $ref_node 0 0 0
923 0 my $p = $ref_node->{$parent};
924 0 ▶ croak(__PACKAGE__."->paste_before: ref_node has no parent") unless $p;
Branch: 1 branch
BranchTrueFalseTotal
unless $p 0 0 0
925  
926 0 my $lb = $ref_node->{$lbrother};
927 # $node->set_lbrother( $lb );
928 0 ▶ if ($lb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lb) { } 0 0 0
929 0 weaken( $node->{$lbrother} = $lb );
930 0 $lb->{$rbrother}=$node;
931 } else {
932 0 $node->{$lbrother}=undef;
933 0 $p->{$firstson}=$node;
934 }
935 # $ref_node->set_lbrother( $node );
936 0 weaken( $ref_node->{$lbrother} = $node );
937 0 $node->{$rbrother}=$ref_node;
938 0 weaken( $node->{$parent} = $p );
939 0 return $node;
940 }
941  
942 =item $node->test_child_type ( test_node )
943  
944 This method can be used before a C<paste_on> or a similar operation to
945 test if the node provided as an argument is of a type that is valid
946 for children of the current node. More specifically, return 1 if the
947 current node is not associated with a type declaration or if it has
948 a #CHILDNODES member which is of a list or sequence type and the list
949 or sequence can contain members of the type of C<test_node>.
950 Otherwise return 0.
951  
952 A type-declaration object can be passed directly instead of
953 C<test_node>.
954  
955 =cut
956  
957 0 ▶sub test_child_type {
Subroutine test_child_type: not called CC=13 SCAR=52.0
Pod documented
958 0 my ($self, $obj) = @_;
959 0 ▶ die 'Usage: $node->test_child_type($node_or_decl)' unless ref($obj);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj 0 0 0
960 0 my $type = $self->type;
961 0 ▶ return 1 unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 0 0
962 0 ▶ if (UNIVERSAL::DOES::does($obj,'Treex::PML::Schema::Decl')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::DOES::does($obj, 'Treex::PML::Schema::Decl')) { } 0 0 0
963 0 ▶ if ($obj->get_decl_type == PML_TYPE_DECL) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->get_decl_type == 1) 0 0 0
964 # a named type decl passed, no problem
965 0 $obj = $obj->get_content_decl;
966 }
967 } else {
968 # assume it's a node
969 0 $obj = $obj->type;
970 0 ▶ return 0 unless $obj;
Branch: 1 branch
BranchTrueFalseTotal
unless $obj 0 0 0
971 }
972 0 ▶ if ($type->get_decl_type == PML_ELEMENT_DECL) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->get_decl_type == 13) 0 0 0
973 0 $type = $type->get_content_decl;
974 }
975 0 my ($ch) = $type->find_members_by_role('#CHILDNODES');
976 0 ▶ if ($ch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch) { } 0 0 0
977 0 my $ch_is = $ch->get_decl_type;
978 0 ▶ if ($ch_is == PML_MEMBER_DECL) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch_is == 12) 0 0 0
979 0 $ch = $ch->get_content_decl;
980 0 $ch_is = $ch->get_decl_type;
981 }
982 0 ▶ if ($ch_is == PML_SEQUENCE_DECL) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ch_is == 5) { } 0 0 0
elsif ($ch_is == 6) { } 0 0 0
983 0 ▶ return 1 if $ch->find_elements_by_content_decl($obj);
Branch: 1 branch
BranchTrueFalseTotal
if $ch->find_elements_by_content_decl($obj) 0 0 0
984 } elsif ($ch_is == PML_LIST_DECL) {
985 0 ▶ return 1 if $ch->get_content_decl == $obj;
Branch: 1 branch
BranchTrueFalseTotal
if $ch->get_content_decl == $obj 0 0 0
986 }
987 } else {
988 0 return 0;
989 }
990 }
991  
992 =item $node->get_order
993  
994 For a typed node return value of the ordering attribute on the node
995 (i.e. the one with role #ORDER). Returns undef for untyped nodes (for
996 untyped nodes the name of the ordering attribute can be obtained
997 from the FSFormat object).
998  
999 =cut
1000  
1001 0 ▶sub get_order {
Subroutine get_order: not called CC=2 SCAR=17.9
Pod documented
1002 0 my $self = $_[0];
1003 0 my $oattr = $self->get_ordering_member_name;
1004 0 ▶ return defined $oattr ? $self->{$oattr} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $oattr ? : 0 0 0
1005 }
1006  
1007 =item $node->get_ordering_member_name
1008  
1009 For a typed node return name of the ordering attribute on the node
1010 (i.e. the one with role #ORDER). Returns undef for untyped nodes (for
1011 untyped nodes the name of the ordering attribute can be obtained
1012 from the FSFormat object).
1013  
1014 =cut
1015  
1016 0 ▶sub get_ordering_member_name {
Subroutine get_ordering_member_name: not called CC=4 SCAR=30.0
Pod documented
1017 0 my $self = $_[0];
1018 0 my $type = $self->type;
1019 0 ▶ return undef unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 0 0
1020 0 ▶ if ($type->get_decl_type == PML_ELEMENT_DECL) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->get_decl_type == 13) 0 0 0
1021 0 $type = $type->get_content_decl;
1022 }
1023 0 my ($omember) = $type->find_members_by_role('#ORDER');
1024 0 ▶ if ($omember) {
Branch: 1 branch
BranchTrueFalseTotal
if ($omember) 0 0 0
1025 0 return ($omember->get_name);
1026 }
1027 0 return undef; # we want this undef
1028 }
1029  
1030 =item $node->get_id
1031  
1032 For a typed node return value of the ID attribute on the node
1033 (i.e. the one with role #ID). Returns undef for untyped nodes (for
1034 untyped nodes the name of the ID attribute can be obtained
1035 from the FSFormat object).
1036  
1037 =cut
1038  
1039 0 ▶sub get_id {
Subroutine get_id: not called CC=2 SCAR=17.9
Pod documented
1040 0 my $self = $_[0];
1041 0 my $oattr = $self->get_id_member_name;
1042 0 ▶ return defined $oattr ? $self->{$oattr} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $oattr ? : 0 0 0
1043 }
1044  
1045 =item $node->get_id_member_name
1046  
1047 For a typed node return name of the ID attribute on the node
1048 (i.e. the one with role #ID). Returns undef for untyped nodes (for
1049 untyped nodes the name of the ID attribute can be obtained
1050 from the FSFormat object).
1051  
1052 =cut
1053  
1054 0 ▶sub get_id_member_name {
Subroutine get_id_member_name: not called CC=4 SCAR=30.0
Pod documented
1055 0 my $self = $_[0];
1056 0 my $type = $self->type;
1057 0 ▶ return undef unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 0 0
1058 0 ▶ if ($type->get_decl_type == PML_ELEMENT_DECL) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->get_decl_type == 13) 0 0 0
1059 0 $type = $type->get_content_decl;
1060 }
1061 0 my ($omember) = $type->find_members_by_role('#ID');
1062 0 ▶ if ($omember) {
Branch: 1 branch
BranchTrueFalseTotal
if ($omember) 0 0 0
1063 0 return ($omember->get_name);
1064 }
1065 0 return undef; # we want this undef
1066 }
1067  
1068 0 ▶sub _weakenLinks {
Subroutine _weakenLinks: not called CC=3 SCAR=24.8
1069 0 my ($node)=@_;
1070 0 for ($node->{$lbrother}, $node->{$parent}) {
1071 0 ▶ weaken( $_ ) if $_
Branch: 1 branch
BranchTrueFalseTotal
if $_ 0 0 0
1072 }
1073 }
1074  
1075 ######################################################################
1076  
1077 9 ▶eval << 'EO_XPATH' if ($ENV{'TREEX_PML_ENABLE_XPATH_EXTENSION'});
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TREEX_PML_ENABLE_XPATH_EXTENSION"} 0 9 9
1078 *getRootNode = *root;
1079 *getParentNode = *parent;
1080 *getNextSibling = *rbrother;
1081 *getPreviousSibling = *lbrother;
1082 *getChildNodes = sub { wantarray ? $_[0]->children : [ $_[0]->children ] };
1083  
1084 sub getElementById { }
1085 sub isElementNode { 1 }
1086 sub get_global_pos { 0 }
1087 sub getNamespaces { return wantarray ? () : []; }
1088 sub isTextNode { 0 }
1089 sub isPINode { 0 }
1090 sub isCommentNode { 0 }
1091 sub getNamespace { undef }
1092 sub getValue { undef }
1093 sub getName { "node" }
1094 *getLocalName = *getName;
1095 *string_value = *getValue;
1096  
1097 sub getAttributes {
1098 my ($self) = @_;
1099 my @attribs = map {
1100 Treex::PML::Attribute->new($self,$_,$self->{$_})
1101 } keys %$self;
1102 return wantarray ? @attribs : \@attribs;
1103 }
1104  
1105 sub find {
1106 my ($node,$path) = @_;
1107 require XML::XPath;
1108 local $_; # XML::XPath isn't $_-safe
1109 my $xp = XML::XPath->new(); # new is v. lightweight
1110 return $xp->find($path, $node);
1111 }
1112  
1113 sub findvalue {
1114 my ($node,$path) = @_;
1115 require XML::XPath;
1116 local $_; # XML::XPath isn't $_-safe
1117 my $xp = XML::XPath->new();
1118 return $xp->findvalue($path, $node);
1119 }
1120  
1121 sub findnodes {
1122 my ($node,$path) = @_;
1123 require XML::XPath;
1124 local $_; # XML::XPath isn't $_-safe
1125 my $xp = XML::XPath->new();
1126 return $xp->findnodes($path, $node);
1127 }
1128  
1129 sub matches {
1130 my ($node,$path,$context) = @_;
1131 require XML::XPath;
1132 local $_; # XML::XPath isn't $_-safe
1133 my $xp = XML::XPath->new();
1134 return $xp->matches($node, $path, $context);
1135 }
1136  
1137 package Treex::PML::Attribute;
1138 use Carp;
1139  
1140 sub new { # node, name, value
1141 my $class = shift;
1142 return bless [@_],$class;
1143 }
1144 sub getElementById { $_[0]->getElementById($_[1]) }
1145 sub getLocalName { $_[0][1] }
1146 BEGIN { *getName = \&getLocalName; }
1147 sub string_value { $_[0][2] }
1148 BEGIN { *getValue = \&string_value; }
1149 sub getRootNode { $_[0][0]->getRootNode() }
1150 sub getParentNode { $_[0][0] }
1151 sub getNamespace { undef }
1152  
1153 EO_XPATH
1154  
1155  
1156 9 1;
1157  
1158 =back
1159  
1160 =cut
1161  
1162 __END__
« PML/Schema/Reader.pm ↑ Summary PML/Backend/CSTS.pm »