lib/Treex/Core/Node.pm

filter: Statementstmt 65.4% 277 / 423 Branchbran 51.5% 102 / 198 Conditioncond 40.7% 31 / 76 MC/DCmcdc 16.2% 7 / 43 Subroutinesub 66.6% 44 / 66 Podpod 95.2% 40 / 42 totaltotal 59.0% 501 / 848 CC 133 SCAR 21.6
CC
133
Cov
59%
delete_aligned_node
30.0
get_deref_attr
20.0
get_aligned_nodes_of_type
20.0
CRAP 8.7
« lib/Treex/Block/Write/BaseWriter.pm ↑ Summary lib/Treex/Block/Read/BaseReader.pm »
1 package Treex::Core::Node;
2 {
3 64 $Treex::Core::Node::VERSION = '0.08633_1';
4 }
5  
6 64 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 64 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 64 ▶use MooseX::NonMoose;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 64 ▶use Treex::Core::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 64 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 64 ▶use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 64 ▶use Treex::PML;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 64 extends 'Treex::PML::Node';
16 64 with 'Treex::Core::WildAttr';
17  
18 # overloading does not work with namespace::autoclean
19 # see https://rt.cpan.org/Public/Bug/Display.html?id=50938
20 # We may want to use https://metacpan.org/module/namespace::sweep instead.
21 #
22 # use overload
23 # '""' => 'to_string',
24 # '==' => 'equals',
25 # '!=' => '_not_equals',
26 # 'eq' => 'equals', # deprecated
27 # 'ne' => '_not_equals', # deprecated
28 # 'bool' => sub{1},
29 #
30 # # We can A) let Magic Autogeneration to build "derived" overloadings,
31 # # or B) we can disable this feature (via fallback=>0)
32 # # and define only the needed overloadings
33 # # (so all other overloadings will result in fatal errors).
34 # # See perldoc overload.
35 # # I decided for A, but uncommenting the following lines can catch some misuses.
36 # #'!' => sub{0},
37 # #'.' => sub{$_[2] ? $_[1] . $_[0]->to_string : $_[0]->to_string . $_[1]},
38 # #fallback => 0,
39 # ;
40 # # TODO: '<' => 'precedes' (or better '<=>' => ...)
41 # # 'eq' => sub {log_warn 'You should use ==' && return $_[0]==$_[1]} # similarly for 'ne'
42  
43 64 Readonly my $_SWITCHES_REGEX => qr/^(ordered|add_self|(preceding|following|first|last)_only)$/x;
44 64 my $CHECK_FOR_CYCLES = 1;
45  
46 64 has _zone => (
47 is => 'rw',
48 writer => '_set_zone',
49 reader => '_get_zone',
50 weak_ref => 1,
51 );
52  
53 64 has id => (
54 is => 'rw',
55 trigger => \&_index_my_id,
56 );
57  
58 sub BUILD {
59 163 ▶ my ( $self, $arg_ref ) = @_;
Subroutine BUILD: called CC=4 SCAR=17.9
Pod undocumented
60  
61 163 ▶ if (( not defined $arg_ref or not defined $arg_ref->{_called_from_core_} )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $arg_ref and defined $arg_ref->{"_called_from_core_"} or $Treex::Core::Config::running_in_tred) 0 163 163
Condition33%2/6
defined $arg_ref and defined $arg_ref->{"_called_from_core_"} or $Treex::Core::Config::running_in_tred
l!l&&r!l&&!r
16300
defined $arg_ref and defined $arg_ref->{"_called_from_core_"}
!ll&&!rl&&r
00163
MC/DC0%0/3
defined $arg_ref and defined $arg_ref->{"_called_from_core_"} or $Treex::Core::Config::running_in_tred
defined $arg_ref defined $arg_ref->{"_called_from_core_"} $Treex::Core::Config::running_in_tred
Truth table20%1/5
A and B or C
A= defined $arg_refB= defined $arg_ref->{"_called_from_core_"}C= $Treex::Core::Config::running_in_tred
ABCresult
11X1
0X11
1011
0X00
1000
62 and not $Treex::Core::Config::running_in_tred
63 )
64 {
65 0 log_fatal 'Because of node indexing, no nodes can be created outside of documents. '
66 . 'You have to use $zone->create_tree(...) or $node->create_child() '
67 . 'instead of Treex::Core::Node...->new().';
68 }
69 163 return;
70 }
71  
72 sub to_string {
73 0 ▶ my ($self) = @_;
Subroutine to_string: not called CC=2 SCAR=17.9
Pod documented
74 0 ▶ return $self->id // 'node_without_id(addr=' . refaddr($self) . ')';
Condition0%0/2
$self->id // 'node_without_id(addr=' . builtin::refaddr($self) . ')'
l!l
00
MC/DC0%0/1
$self->id // 'node_without_id(addr=' . builtin::refaddr($self) . ')'
$self->id
Truth table0%0/2
A
A= $self->id
Aresult
11
00
75 }
76  
77 # Since we have overloaded stringification, we must overload == as well,
78 # so you can use "if ($nodeA == $nodeB){...}".
79 sub equals {
80 0 ▶ my ($self, $node) = @_;
Subroutine equals: not called CC=2 SCAR=17.9
Pod documented
81 #return ref($node) && $node->id eq $self->id;
82 0 ▶ return ref($node) && refaddr($node) == refaddr($self);
Condition0%0/3
ref $node && builtin::refaddr($node) == builtin::refaddr($self)
!ll&&!rl&&r
000
MC/DC0%0/2
ref $node && builtin::refaddr($node) == builtin::refaddr($self)
ref $node builtin::refaddr($node) == builtin::refaddr($self)
Truth table0%0/3
A && B
A= ref $nodeB= builtin::refaddr($node) == builtin::refaddr($self)
ABresult
0X0
100
111
83 }
84  
85 sub _not_equals {
86 0 ▶ my ($self, $node) = @_;
Subroutine _not_equals: not called CC=1 SCAR=6.9
87 0 return !$self->equals($node);
88 }
89  
90 sub _index_my_id {
91 163 ▶ my $self = shift;
Subroutine _index_my_id: called CC=1 SCAR=0.0
92 163 pos_validated_list( \@_, { isa => 'Any', optional => 1 } ); #TODO
93 163 $self->get_document->index_node_by_id( $self->id, $self );
94 163 return;
95 }
96  
97 # ---- access to attributes ----
98  
99 # unlike attr (implemented in Treex::PML::Instance::get_data)
100 # get_attr implements only "plain" and "nested hash" attribute names,
101 # i.e. no XPath-like expressions (a/aux.rf[3]) are allowed.
102 # This results in much faster code.
103 sub get_attr {
104 928 ▶ my ( $self, $attr_name ) = @_;
Subroutine get_attr: called CC=5 SCAR=16.2
Pod documented
105 928 ▶ log_fatal('Incorrect number of arguments') if @_ != 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 928 928
106 928 my $val = $self;
107 928 for my $step ( split /\//, $attr_name ) {
108 1079 ▶ if ( !defined $val ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 127 952 1079
109 127 ▶ log_fatal "Attribute '$attr_name' contains strange symbols."
Branch: 1 branch
BranchTrueFalseTotal
if $attr_name =~ m[[^-\w/.]] 0 127 127
110 . " For XPath like constructs (e.g. 'a/aux.rf[3]') use the 'attr' method."
111 if $attr_name =~ /[^-\w\/.]/;
112 }
113 1079 $val = $val->{$step};
114 }
115 928 return $val;
116 }
117  
118 64 ▶use Treex::PML::Factory;
Subroutine BEGIN: called CC=1 SCAR=0.0
119  
120 sub set_attr {
121 50 ▶ my ( $self, $attr_name, $attr_value ) = @_;
Subroutine set_attr: called CC=15 SCAR=28.6
Pod documented
122 50 ▶ log_fatal('Incorrect number of arguments') if @_ != 3;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 3 0 50 50
123 50 ▶ if ( $attr_name eq 'id' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($attr_name eq 'id') { } 0 50 50
elsif (ref $attr_value eq 'ARRAY') { } 3 47 50
124 0 ▶ if ( not defined $attr_value or $attr_value eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $attr_value or $attr_value eq "") 0 0 0
Condition0%0/3
not defined $attr_value or $attr_value eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $attr_value or $attr_value eq ""
not defined $attr_value $attr_value eq ""
Truth table0%0/3
not(A) or B
A= not defined $attr_valueB= $attr_value eq ""
ABresult
1X1
011
000
125 0 log_fatal 'Setting undefined or empty ID is not allowed';
126 }
127 0 $self->get_document->index_node_by_id( $attr_value, $self );
128 }
129 elsif ( ref($attr_value) eq 'ARRAY' ) {
130 3 $attr_value = Treex::PML::List->new( @{$attr_value} );
131 }
132  
133 50 ▶ if ($attr_name =~ /\.rf$/){
Branch: 1 branch
BranchTrueFalseTotal
if ($attr_name =~ /\.rf$/) 12 38 50
134 12 my $document = $self->get_document();
135  
136 # Delete previous back references
137 12 my $old_value = $self->get_attr($attr_name);
138 12 ▶ if ($old_value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_value) 5 7 12
139 5 ▶ if ( ref $old_value eq 'Treex::PML::List' && @$old_value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $old_value eq 'Treex::PML::List' and @$old_value) { } 2 3 5
Condition66%2/3
ref $old_value eq "Treex::PML::List" and @$old_value
!ll&&!rl&&r
302
MC/DC50%1/2
ref $old_value eq "Treex::PML::List" and @$old_value
ref $old_value eq "Treex::PML::List" @$old_value
Truth table66%2/3
A and B
A= ref $old_value eq "Treex::PML::List"B= @$old_value
ABresult
0X0
100
111
140 2 $document->remove_backref( $attr_name, $self->id, $old_value );
141 }
142 else {
143 3 $document->remove_backref( $attr_name, $self->id, [$old_value] );
144 }
145 }
146  
147 # Set new back references
148 12 ▶ my $ids = ref($attr_value) eq 'Treex::PML::List' ? $attr_value : [$attr_value];
Branch: 1 branch
BranchTrueFalseTotal
ref $attr_value eq 'Treex::PML::List' ? : 3 9 12
149 12 $document->index_backref( $attr_name, $self->id, $ids );
150 }
151  
152 #simple attributes can be accessed directly
153 50 ▶ return $self->{$attr_name} = $attr_value if $attr_name =~ /^[\w\.]+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $attr_name =~ /^[\w\.]+$/ 42 8 50
154 8 ▶ log_fatal "Attribute '$attr_name' contains strange symbols."
Branch: 1 branch
BranchTrueFalseTotal
if $attr_name =~ m[[^-\w/.]] 0 8 8
155 . " No XPath like constructs (e.g. 'a/aux.rf[3]') are allowed."
156 if $attr_name =~ /[^-\w\/.]/;
157  
158 8 my $val = $self;
159 8 my @steps = split /\//, $attr_name;
160 8 while (1) {
161 16 my $step = shift @steps;
162 16 ▶ if (@steps) {
Branch: 1 branch
BranchTrueFalseTotal
if (@steps) { } 8 8 16
163 8 ▶ if ( !defined( $val->{$step} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val->{$step}) 4 4 8
164 4 $val->{$step} = Treex::PML::Factory->createStructure();
165 }
166 8 $val = $val->{$step};
167 }
168 else {
169 8 return $val->{$step} = $attr_value;
170 }
171 }
172 0 return;
173 }
174  
175 sub get_deref_attr {
176 0 ▶ my ( $self, $attr_name ) = @_;
Subroutine get_deref_attr: not called CC=4 SCAR=30.0
Pod documented
177 0 ▶ log_fatal('Incorrect number of arguments') if @_ != 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 0 0
178 0 my $attr_value = $self->get_attr($attr_name);
179  
180 0 ▶ return if !$attr_value;
Branch: 1 branch
BranchTrueFalseTotal
unless $attr_value 0 0 0
181 0 my $document = $self->get_document();
182 0 ▶ return [ map { $document->get_node_by_id($_) } @{$attr_value} ]
Branch: 1 branch
BranchTrueFalseTotal
if ref $attr_value eq "Treex::PML::List" 0 0 0
183 if ref($attr_value) eq 'Treex::PML::List';
184 0 return $document->get_node_by_id($attr_value);
185 }
186  
187 sub set_deref_attr {
188 0 ▶ my ( $self, $attr_name, $attr_value ) = @_;
Subroutine set_deref_attr: not called CC=3 SCAR=24.8
Pod documented
189 0 ▶ log_fatal('Incorrect number of arguments') if @_ != 3;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 3 0 0 0
190  
191 # If $attr_value is an array of nodes
192 0 ▶ if ( ref($attr_value) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attr_value eq 'ARRAY') { } 0 0 0
193 0 my @list = map { $_->id } @{$attr_value};
194 0 $attr_value = Treex::PML::List->new(@list);
195 }
196  
197 # If $attr_value is just one node
198 else {
199 0 $attr_value = $attr_value->id;
200 }
201  
202 # Set the new reference(s)
203 0 $self->set_attr( $attr_name, $attr_value );
204 0 return;
205 }
206  
207 sub get_bundle {
208 580 ▶ log_fatal 'Incorrect number of arguments' if @_ != 1;
Subroutine get_bundle: called CC=2 SCAR=7.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 580 580
209 580 my $self = shift;
210 580 return $self->get_zone->get_bundle;
211 }
212  
213 # reference to embedding zone is stored only with tree root, not with nodes
214 sub get_zone {
215 585 ▶ log_fatal 'Incorrect number of arguments' if @_ != 1;
Subroutine get_zone: called CC=5 SCAR=16.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 585 585
216 585 my $self = shift;
217 585 my $zone;
218 585 ▶ if ( $self->is_root ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_root) { } 199 386 585
219 199 $zone = $self->_get_zone;
220 }
221 else {
222 386 $zone = $self->get_root->_get_zone; ## no critic (ProtectPrivateSubs)
223 }
224  
225 585 ▶ if (!$zone) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($zone) 0 585 585
226 # $self->get_document->save("pokus2.treex.gz");
227 }
228  
229 585 ▶ log_fatal "a node (" . $self->id . ") can't reveal its zone" if !$zone;
Branch: 1 branch
BranchTrueFalseTotal
unless $zone 0 585 585
230 585 return $zone;
231  
232 }
233  
234 sub remove {
235 15 ▶ log_fatal 'Incorrect number of arguments' if @_ != 1;
Subroutine remove: called CC=6 SCAR=18.3
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 15 15
236 15 my $self = shift;
237 15 ▶ if ( $self->is_root ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_root) 0 15 15
238 0 log_fatal 'Tree root cannot be removed using $root->remove().'
239 . ' Use $zone->remove_tree($layer) instead';
240 }
241 15 my $root = $self->get_root();
242 15 my $document = $self->get_document();
243  
244 # Remove the subtree from the document's indexing table
245 15 my @to_remove = ( $self, $self->get_descendants );
246 15 foreach my $node ( @to_remove) {
247 19 ▶ if ( defined $node->id ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $node->id) 19 0 19
248 19 $document->_remove_references_to_node( $node );
249 19 $document->index_node_by_id( $node->id, undef );
250 }
251 }
252  
253 # Disconnect the node from its parent (& siblings) and delete all attributes
254 # It actually does: $self->cut(); undef %$_ for ($self->descendants(), $self);
255 15 $self->destroy;
256  
257 # TODO: order normalizing can be done in a more efficient way
258 # (update just the following ords)
259 15 $root->_normalize_node_ordering();
260  
261 # By reblessing we make sure that
262 # all methods called on removed nodes will result in fatal errors.
263 15 foreach my $node (@to_remove){
264 19 bless $node, 'Treex::Core::Node::Deleted';
265 }
266 15 return;
267 }
268  
269 # Return all nodes that have a reference of the given type (e.g. 'alignment', 'a/lex.rf') to this node
270 sub get_referencing_nodes {
271  
272 12 ▶ my ( $self, $type ) = @_;
Subroutine get_referencing_nodes: called CC=3 SCAR=11.4
Pod documented
273 12 my $doc = $self->get_document;
274 12 my $refs = $doc->get_references_to_id( $self->id );
275  
276 12 ▶ return if ( !$refs || !$refs->{$type} );
Branch: 1 branch
BranchTrueFalseTotal
unless $refs and $refs->{$type} 0 12 12
Condition33%1/3
$refs and $refs->{$type}
!ll&&!rl&&r
0012
MC/DC0%0/2
$refs and $refs->{$type}
$refs $refs->{$type}
Truth table33%1/3
A and B
A= $refsB= $refs->{$type}
ABresult
0X0
100
111
277 12 return map { $doc->get_node_by_id($_) } @{ $refs->{$type} };
278 }
279  
280 # Remove a reference of the given type to the given node. This will not remove a reverse reference from document
281 # index, since it is itself called from when removing reverse references; use the API methods for the individual
282 # references if you want to keep reverse references up-to-date.
283 sub remove_reference {
284 6 ▶ my ( $self, $type, $id ) = @_;
Subroutine remove_reference: called CC=6 SCAR=20.9
Pod undocumented
285  
286 6 ▶ if ( $type eq 'alignment' ) { # handle alignment links separately
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'alignment') { } 0 6 6
287  
288 0 my $links = $self->get_attr('alignment');
289  
290 0 ▶ if ($links) {
Branch: 1 branch
BranchTrueFalseTotal
if ($links) 0 0 0
291 0 my $document = $self->get_document;
292 0 $self->set_attr( 'alignment', [ grep { $_->{'counterpart.rf'} ne $id } @{$links} ] );
293 }
294 }
295 else {
296 6 my $attr = $self->get_attr($type);
297 6 ▶ log_fatal "undefined attr $type (id=$id)" if !defined $attr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attr 0 6 6
298  
299 6 ▶ if ( $attr eq $id || scalar( @{$attr} ) <= 1 ) { # single-value attributes
Branch: 1 branch
BranchTrueFalseTotal
if ($attr eq $id or scalar @{$attr;} <= 1) { } 5 1 6
Condition100%3/3
$attr eq $id or scalar @{$attr;} <= 1
l!l&&r!l&&!r
321
MC/DC100%2/2
$attr eq $id or scalar @{$attr;} <= 1
$attr eq $id scalar @{$attr;} <= 1
Truth table100%3/3
A or B
A= $attr eq $idB= scalar @{$attr;} <= 1
ABresult
1X1
011
000
300 5 $self->set_attr( $type, undef );
301 }
302 else {
303 1 $attr->delete_value($id); # TODO : will it be always a Treex::PML::List? Looks like it works.
304 }
305 }
306 6 return;
307 }
308  
309 sub get_pml_type_name {
310 0 ▶ log_fatal 'Incorrect number of arguments' if @_ != 1;
Subroutine get_pml_type_name: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 0 0
311 0 my $self = shift;
312 0 return;
313 }
314  
315 sub get_layer {
316 19 ▶ log_fatal 'Incorrect number of arguments' if @_ != 1;
Subroutine get_layer: called CC=3 SCAR=12.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 19 19
317 19 my $self = shift;
318 19 ▶ if ( ref($self) =~ /Node::(\w)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($self) =~ /Node::(\w)$/) { } 19 0 19
319 19 return lc($1);
320 }
321 else {
322 0 log_fatal "Cannot recognize node's layer: $self";
323 }
324 }
325  
326 sub language {
327 0 ▶ log_fatal 'Incorrect number of arguments' if @_ != 1;
Subroutine language: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 0 0
328 0 my $self = shift;
329 0 return $self->get_zone()->language;
330 }
331  
332 sub selector {
333 0 ▶ log_fatal 'Incorrect number of arguments' if @_ != 1;
Subroutine selector: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 0 0
334 0 my $self = shift;
335 0 return $self->get_zone()->selector;
336 }
337  
338 sub create_child {
339 106 ▶ my $self = shift;
Subroutine create_child: called CC=9 SCAR=22.1
Pod documented
340  
341 #NOT VALIDATED INTENTIONALLY - passing args to to new (and it's also black magic, so I'm not touching it)
342  
343 # TODO:
344 #my $new_node = ( ref $self )->new(@_);
345 # Previous line is very strange and causes errors which are hard to debug.
346 # Magically, it works on UFAL machines, but nowhere else - I don't know why.
347 # Substituting the hash by hashref is a workaround,
348 # but the black magic is still there.
349 106 my $arg_ref;
350 106 ▶ if ( scalar @_ == 1 && ref $_[0] eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ == 1 and ref $_[0] eq 'HASH') { } 15 91 106
elsif (@_ % 2) { } 0 91 91
Condition66%2/3
scalar @_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
91015
MC/DC50%1/2
scalar @_ == 1 and ref $_[0] eq "HASH"
scalar @_ == 1 ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= scalar @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
351 15 $arg_ref = $_[0];
352 }
353 elsif ( @_ % 2 ) {
354 0 log_fatal "Odd number of elements for create_child";
355 }
356 else {
357 91 $arg_ref = {@_};
358 }
359  
360 # Structured attributes (e.g. morphcat/pos) must be handled separately
361 # TODO: And also attributes which don't have accessors (those are not Moose attributes).
362 # Note: mlayer_pos was not added to Treex::Core::Node::T because it goes
363 # against the "tectogrammatical ideology" and we use it as a temporary hack.
364 106 my %structured_attrs;
365 106 foreach my $attr ( keys %{$arg_ref} ) {
366 55 ▶ if ( $attr =~ m{/} || $attr eq 'mlayer_pos' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr =~ m[/] or $attr eq "mlayer_pos") 1 54 55
Condition66%2/3
$attr =~ m[/] or $attr eq "mlayer_pos"
l!l&&r!l&&!r
1054
MC/DC50%1/2
$attr =~ m[/] or $attr eq "mlayer_pos"
$attr =~ m[/] $attr eq "mlayer_pos"
Truth table66%2/3
A or B
A= $attr =~ m[/]B= $attr eq "mlayer_pos"
ABresult
1X1
011
000
367 1 $structured_attrs{$attr} = delete $arg_ref->{$attr};
368 }
369 }
370  
371 106 $arg_ref->{_called_from_core_} = 1;
372 106 my $new_node = ( ref $self )->new($arg_ref);
373 106 $new_node->set_parent($self);
374  
375 106 my $new_id = $self->generate_new_id();
376 106 $new_node->set_id($new_id);
377  
378 106 foreach my $attr ( keys %structured_attrs ) {
379 1 $new_node->set_attr( $attr, $structured_attrs{$attr} );
380 }
381  
382 106 my $type = $new_node->get_pml_type_name();
383 106 ▶ return $new_node if !defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 38 68 106
384 68 my $fs_file = $self->get_bundle->get_document()->_pmldoc;
385 68 $self->set_type_by_name( $fs_file->metaData('schema'), $type );
386 68 return $new_node;
387 }
388  
389 #************************************
390 #---- TREE NAVIGATION ------
391  
392 sub get_document {
393 429 ▶ log_fatal 'Incorrect number of arguments' if @_ != 1;
Subroutine get_document: called CC=3 SCAR=11.3
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 429 429
394 429 my $self = shift;
395 429 my $bundle = $self->get_bundle();
396 429 ▶ log_fatal('Cannot call get_document on a node which is in no bundle') if !defined $bundle;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bundle 0 429 429
397 429 return $bundle->get_document();
398 }
399  
400 sub get_root {
401 628 ▶ log_fatal 'Incorrect number of arguments' if @_ != 1;
Subroutine get_root: called CC=2 SCAR=7.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 628 628
402 628 my $self = shift;
403 628 return $self->root();
404 }
405  
406 sub is_root {
407 771 ▶ log_fatal 'Incorrect number of arguments' if @_ != 1;
Subroutine is_root: called CC=2 SCAR=7.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 771 771
408 771 my $self = shift;
409 771 return !$self->parent;
410 }
411  
412 sub is_leaf {
413 0 ▶ log_fatal 'Incorrect number of arguments' if @_ != 1;
Subroutine is_leaf: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 0 0
414 0 my $self = shift;
415 0 return not $self->firstson;
416 }
417  
418 sub get_parent {
419 393 ▶ log_fatal 'Incorrect number of arguments' if @_ != 1;
Subroutine get_parent: called CC=2 SCAR=7.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 393 393
420 393 my $self = shift;
421 393 return $self->parent;
422 }
423  
424 sub set_parent {
425 106 ▶ my $self = shift;
Subroutine set_parent: called CC=6 SCAR=20.1
Pod documented
426 106 my ($parent) = pos_validated_list(
427 \@_,
428 { isa => 'Treex::Core::Node' },
429 );
430  
431 # TODO check for this (but set_parent is called also from create_child)
432 #if ($self->get_document() != $parent->get_document()) {
433 # log_fatal("Cannot move a node from one document to another");
434 #}
435  
436 # We cannot detach a node by setting an undefined parent. The if statement below will die.
437 # Let's inform the user where the bad call is.
438 106 ▶ log_fatal( 'Cannot attach the node ' . $self->id . ' to an undefined parent' ) if ( !defined($parent) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parent 0 106 106
439 106 ▶ if ( $self == $parent || $CHECK_FOR_CYCLES && $parent->is_descendant_of($self) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self == $parent or $CHECK_FOR_CYCLES and $parent->is_descendant_of($self)) 0 106 106
Condition33%2/6
$self == $parent or $CHECK_FOR_CYCLES and $parent->is_descendant_of($self)
l!l&&r!l&&!r
00106
$CHECK_FOR_CYCLES and $parent->is_descendant_of($self)
!ll&&!rl&&r
01060
MC/DC0%0/3
$self == $parent or $CHECK_FOR_CYCLES and $parent->is_descendant_of($self)
$self == $parent $CHECK_FOR_CYCLES $parent->is_descendant_of($self)
Truth table25%1/4
A or B and C
A= $self == $parentB= $CHECK_FOR_CYCLESC= $parent->is_descendant_of($self)
ABCresult
1XX1
0111
00X0
0100
440 0 my $id = $self->id;
441 0 my $p_id = $parent->id;
442 0 log_fatal("Attempt to set parent of $id to the node $p_id, which would lead to a cycle.");
443 }
444  
445 # TODO: Too much FSlib (aka Treex::PML) here
446 106 $self->cut();
447 106 my $fsfile = $parent->get_document()->_pmldoc;
448 106 my @fschildren = $parent->children();
449 106 ▶ if (@fschildren) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fschildren) { } 51 55 106
450 51 Treex::PML::PasteAfter( $self, $fschildren[-1] );
451 }
452 else {
453 55 Treex::PML::Paste( $self, $parent, $fsfile->FS() );
454 }
455  
456 106 return;
457 }
458  
459 sub _check_switches {
460  
461 #This method may be replaced by subtype and checked as parameter
462 122 ▶ my $self = shift;
Subroutine _check_switches: called CC=11 SCAR=27.0
463 122 my ($arg_ref) = pos_validated_list(
464 \@_,
465 { isa => 'Maybe[HashRef]' },
466 );
467  
468 # Check for role Ordered
469 log_fatal('This type of node does not support ordering')
470 if (
471 122 ▶ ( $arg_ref->{ordered} || any { $arg_ref->{ $_ . '_only' } } qw(first last preceding following) )
Subroutine __ANON__: called CC=1 SCAR=0.3
Branch: 1 branch
BranchTrueFalseTotal
if $arg_ref->{"ordered"} || &any(sub { $$arg_ref{$_ . '_only'}; } , "first", "last", "preceding", "following") and not $self->does("Treex::Core::Node::Ordered") 0 122 122
Condition66%4/6
$arg_ref->{"ordered"} || &any(sub { and not $self->does("Treex::Core::Node::Ordered")
!ll&&!rl&&r
68540
$arg_ref->{"ordered"} or &any(sub {
l!l&&r!l&&!r
54068
MC/DC0%0/3
$arg_ref->{"ordered"} || &any(sub { $$arg_ref{$_ . '_only'}; } , "first", "last", "preceding", "following") and not $self->does("Treex::Core::Node::Ordered")
$arg_ref->{"ordered"} &any(sub { $$arg_ref{$_ . '_only'}; } , "first", "last", "preceding", "following") not $self->does("Treex::Core::Node::Ordered")
Truth table40%2/5
A or B and not(C)
A= $arg_ref->{"ordered"}B= &any(sub { $$arg_ref{$_ . '_only'}; } , "first", "last", "preceding", "following")C= not $self->does("Treex::Core::Node::Ordered")
ABCresult
00X0
1X00
0100
1X11
0111
472 &&
473 !$self->does('Treex::Core::Node::Ordered')
474 );
475  
476 # Check switches for not allowed combinations
477 log_fatal('Specified both preceding_only and following_only.')
478 122 ▶ if $arg_ref->{preceding_only} && $arg_ref->{following_only};
Branch: 1 branch
BranchTrueFalseTotal
if $arg_ref->{"preceding_only"} and $arg_ref->{"following_only"} 0 122 122
Condition33%1/3
$arg_ref->{"preceding_only"} and $arg_ref->{"following_only"}
!ll&&!rl&&r
12200
MC/DC0%0/2
$arg_ref->{"preceding_only"} and $arg_ref->{"following_only"}
$arg_ref->{"preceding_only"} $arg_ref->{"following_only"}
Truth table33%1/3
A and B
A= $arg_ref->{"preceding_only"}B= $arg_ref->{"following_only"}
ABresult
0X0
100
111
479 log_fatal('Specified both first_only and last_only.')
480 122 ▶ if $arg_ref->{first_only} && $arg_ref->{last_only};
Branch: 1 branch
BranchTrueFalseTotal
if $arg_ref->{"first_only"} and $arg_ref->{"last_only"} 0 122 122
Condition33%1/3
$arg_ref->{"first_only"} and $arg_ref->{"last_only"}
!ll&&!rl&&r
12200
MC/DC0%0/2
$arg_ref->{"first_only"} and $arg_ref->{"last_only"}
$arg_ref->{"first_only"} $arg_ref->{"last_only"}
Truth table33%1/3
A and B
A= $arg_ref->{"first_only"}B= $arg_ref->{"last_only"}
ABresult
0X0
100
111
481  
482 # Check for explicit "ordered" when not needed (possible typo)
483 log_warn('Specifying (first|last|preceding|following)_only implies ordered.')
484 if $arg_ref->{ordered}
485 122 ▶ && any { $arg_ref->{ $_ . '_only' } } qw(first last preceding following);
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if $arg_ref->{"ordered"} and &any(sub { $$arg_ref{$_ . '_only'}; } , "first", "last", "preceding", "following") 0 122 122
Condition66%2/3
$arg_ref->{"ordered"} and &any(sub {
!ll&&!rl&&r
68540
MC/DC0%0/2
$arg_ref->{"ordered"} and &any(sub { $$arg_ref{$_ . '_only'}; } , "first", "last", "preceding", "following")
$arg_ref->{"ordered"} &any(sub { $$arg_ref{$_ . '_only'}; } , "first", "last", "preceding", "following")
Truth table66%2/3
A and B
A= $arg_ref->{"ordered"}B= &any(sub { $$arg_ref{$_ . '_only'}; } , "first", "last", "preceding", "following")
ABresult
0X0
100
111
486  
487 # Check for unknown switches
488 122 ▶ my $unknown = first { $_ !~ $_SWITCHES_REGEX } keys %{$arg_ref};
Subroutine __ANON__: called CC=1 SCAR=0.0
489 122 ▶ log_warn("Unknown switch $unknown") if defined $unknown;
Branch: 1 branch
BranchTrueFalseTotal
if defined $unknown 0 122 122
490  
491 122 return;
492 }
493  
494 # Shared processing of switches: ordered, (preceding|following|first|last)_only
495 # for subs get_children, get_descendants and get_siblings.
496 # This is quite an uneffective implementation in case of e.g. first_only
497 sub _process_switches {
498 122 ▶ my $self = shift;
Subroutine _process_switches: called CC=10 SCAR=24.7
499 122 my ( $arg_ref, @nodes ) = pos_validated_list(
500 \@_,
501 { isa => 'Maybe[HashRef]' },
502 MX_PARAMS_VALIDATE_ALLOW_EXTRA => 1,
503 );
504  
505 # Check for unknown switches and not allowed combinations
506 122 $self->_check_switches($arg_ref);
507  
508 # Add this node if add_self
509 122 ▶ if ( $arg_ref->{add_self} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_ref->{"add_self"}) 102 20 122
510 102 push @nodes, $self;
511 }
512  
513 # Sort nodes if needed
514 122 ▶ if (( $arg_ref->{ordered} || any { $arg_ref->{ $_ . '_only' } } qw(first last preceding following) )
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_ref->{"ordered"} || &any(sub { $$arg_ref{$_ . '_only'}; } , "first", "last", "preceding", "following") and @nodes and defined $nodes[0]->ord) 34 88 122
Condition77%7/9
$arg_ref->{"ordered"} || &any(sub { and defined $nodes[0]->ord
!ll&&!rl&&r
682034
$arg_ref->{"ordered"} || &any(sub { and @nodes
!ll&&!rl&&r
68054
$arg_ref->{"ordered"} or &any(sub {
l!l&&r!l&&!r
54068
MC/DC50%2/4
$arg_ref->{"ordered"} || &any(sub { $$arg_ref{$_ . '_only'}; } , "first", "last", "preceding", "following") and @nodes and defined $nodes[0]->ord
$arg_ref->{"ordered"} &any(sub { $$arg_ref{$_ . '_only'}; } , "first", "last", "preceding", "following") @nodes defined $nodes[0]->ord
Truth table42%3/7
A or B and C and D
A= $arg_ref->{"ordered"}B= &any(sub { $$arg_ref{$_ . '_only'}; } , "first", "last", "preceding", "following")C= @nodesD= defined $nodes[0]->ord
ABCDresult
00XX0
1X0X0
010X0
1X100
01100
1X111
01111
515 && @nodes && defined $nodes[0]->ord
516 )
517 {
518 34 @nodes = sort { $a->ord() <=> $b->ord() } @nodes;
519 }
520  
521 # Leave preceding/following only if needed
522 122 ▶ if ( $arg_ref->{preceding_only} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($arg_ref->{'preceding_only'}) { } 0 122 122
elsif ($arg_ref->{'following_only'}) { } 0 122 122
523 0 @nodes = grep { $_->ord() <= $self->ord } @nodes;
524 }
525 elsif ( $arg_ref->{following_only} ) {
526 0 @nodes = grep { $_->ord() >= $self->ord } @nodes;
527 }
528  
529 # first_only / last_only
530 122 ▶ return $nodes[0] if $arg_ref->{first_only};
Branch: 1 branch
BranchTrueFalseTotal
if $arg_ref->{"first_only"} 0 122 122
531 122 ▶ return $nodes[-1] if $arg_ref->{last_only};
Branch: 1 branch
BranchTrueFalseTotal
if $arg_ref->{"last_only"} 0 122 122
532 122 return @nodes;
533 }
534  
535 sub get_children {
536 136 ▶ my $self = shift;
Subroutine get_children: called CC=2 SCAR=6.9
Pod documented
537 136 my ($arg_ref) = pos_validated_list(
538 \@_,
539 { isa => 'Maybe[HashRef]', optional => 1 },
540 );
541  
542 136 my @children = $self->children();
543 136 ▶ return @children if !$arg_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $arg_ref 135 1 136
544 1 return $self->_process_switches( $arg_ref, @children );
545 }
546  
547 sub get_descendants {
548 160 ▶ my $self = shift;
Subroutine get_descendants: called CC=5 SCAR=18.9
Pod documented
549 160 my ($arg_ref) = pos_validated_list(
550 \@_,
551 { isa => 'Maybe[HashRef]', optional => 1 },
552 );
553  
554 160 my @descendants;
555 160 ▶ if ( $arg_ref && $arg_ref->{except} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_ref and $arg_ref->{'except'}) { } 0 160 160
Condition66%2/3
$arg_ref and $arg_ref->{"except"}
!ll&&!rl&&r
581020
MC/DC0%0/2
$arg_ref and $arg_ref->{"except"}
$arg_ref $arg_ref->{"except"}
Truth table66%2/3
A and B
A= $arg_refB= $arg_ref->{"except"}
ABresult
0X0
100
111
556 0 my $except_node = delete $arg_ref->{except};
557 0 ▶ return () if $self == $except_node;
Branch: 1 branch
BranchTrueFalseTotal
if $self == $except_node 0 0 0
558 @descendants = map {
559 0 $_->get_descendants( { except => $except_node, add_self => 1 } )
560 } $self->get_children();
561 }
562 else {
563 160 @descendants = $self->descendants();
564 }
565 160 ▶ return @descendants if !$arg_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $arg_ref 58 102 160
566 102 return $self->_process_switches( $arg_ref, @descendants );
567 }
568  
569 sub get_siblings {
570 16 ▶ my $self = shift;
Subroutine get_siblings: called CC=3 SCAR=11.1
Pod documented
571 16 my ($arg_ref) = pos_validated_list(
572 \@_,
573 { isa => 'Maybe[HashRef]', optional => 1 },
574 );
575 16 my $parent = $self->get_parent();
576 16 ▶ return () if !$parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 4 12 16
577 12 my @siblings = grep { $_ ne $self } $parent->get_children();
578 12 ▶ return @siblings if !$arg_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $arg_ref 12 0 12
579 0 return $self->_process_switches( $arg_ref, @siblings );
580 }
581  
582 0 ▶sub get_left_neighbor { return $_[0]->get_siblings( { preceding_only => 1, last_only => 1 } ); }
Subroutine get_left_neighbor: not called CC=1 SCAR=6.9
Pod documented
583 0 ▶sub get_right_neighbor { return $_[0]->get_siblings( { following_only => 1, first_only => 1 } ); }
Subroutine get_right_neighbor: not called CC=1 SCAR=6.9
Pod documented
584  
585 sub is_descendant_of {
586 132 ▶ my $self = shift;
Subroutine is_descendant_of: called CC=3 SCAR=11.0
Pod documented
587 132 my ($another_node) = pos_validated_list(
588 \@_,
589 { isa => 'Treex::Core::Node' },
590 );
591  
592 132 my $parent = $self->get_parent();
593 132 ▶ while ($parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent) 148 128 276
594 148 ▶ return 1 if $parent == $another_node;
Branch: 1 branch
BranchTrueFalseTotal
if $parent == $another_node 4 144 148
595 144 $parent = $parent->get_parent();
596 }
597 128 return 0;
598 }
599  
600 #----------- alignment -------------
601  
602 sub get_aligned_nodes {
603 0 ▶ my ($self) = @_;
Subroutine get_aligned_nodes: not called CC=2 SCAR=17.9
Pod documented
604 0 my $links_rf = $self->get_attr('alignment');
605 0 ▶ if ($links_rf) {
Branch: 1 branch
BranchTrueFalseTotal
if ($links_rf) 0 0 0
606 0 my $document = $self->get_document;
607 0 my @nodes = map { $document->get_node_by_id( $_->{'counterpart.rf'} ) } @$links_rf;
608 0 my @types = map { $_->{'type'} } @$links_rf;
609 0 return ( \@nodes, \@types );
610 }
611 0 return ( undef, undef );
612 }
613  
614 sub get_aligned_nodes_of_type {
615 0 ▶ my ( $self, $type_regex ) = @_;
Subroutine get_aligned_nodes_of_type: not called CC=4 SCAR=30.0
Pod documented
616 0 my @nodes;
617 0 my ( $n_rf, $t_rf ) = $self->get_aligned_nodes();
618 0 ▶ return if !$n_rf;
Branch: 1 branch
BranchTrueFalseTotal
unless $n_rf 0 0 0
619 0 my $iterator = List::MoreUtils::each_arrayref( $n_rf, $t_rf );
620 0 ▶ while ( my ( $node, $type ) = $iterator->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$iterator()) 0 0 0
621 0 ▶ if ( $type =~ /$type_regex/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /$type_regex/) 0 0 0
622 0 push @nodes, $node;
623 }
624 }
625 0 return @nodes;
626 }
627  
628 sub is_aligned_to {
629 0 ▶ my ( $self, $node, $type ) = @_;
Subroutine is_aligned_to: not called CC=3 SCAR=24.8
Pod documented
630 0 ▶ log_fatal 'Incorrect number of parameters' if @_ != 3;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 3 0 0 0
631 0 ▶ return any { $_ eq $node } $self->get_aligned_nodes_of_type( $node, $type ) ? 1 : 0;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$self->get_aligned_nodes_of_type($node, $type) ? : 0 0 0
632 }
633  
634 sub delete_aligned_node {
635 0 ▶ my ( $self, $node, $type ) = @_;
Subroutine delete_aligned_node: not called CC=5 SCAR=34.0
Pod documented
636 0 my $links_rf = $self->get_attr('alignment');
637 0 my @links = ();
638 0 ▶ if ($links_rf) {
Branch: 1 branch
BranchTrueFalseTotal
if ($links_rf) 0 0 0
639 @links = grep {
640 0 $_->{'counterpart.rf'} ne $node->id
641 0 ▶ || ( defined($type) && defined( $_->{'type'} ) && $_->{'type'} ne $type )
Branch: 1 branch
BranchTrueFalseTotal
$_->{"counterpart.rf"} ne $node->id or defined $type and defined $_->{"type"} and $_->{"type"} ne $type 0 0 0
Condition0%0/9
$_->{'counterpart.rf'} ne $node->id || defined $type && defined $_->{'type'} && $_->{'type'} ne $type
l!l&&r!l&&!r
000
defined $type and defined $_->{"type"} and $_->{"type"} ne $type
!ll&&!rl&&r
000
defined $type and defined $_->{"type"}
!ll&&!rl&&r
000
MC/DC0%0/4
$_->{'counterpart.rf'} ne $node->id || defined $type && defined $_->{'type'} && $_->{'type'} ne $type
$_->{'counterpart.rf'} ne $node->id defined $type defined $_->{"type"} $_->{"type"} ne $type
Truth table0%0/5
A || B and C and D
A= $_->{'counterpart.rf'} ne $node->idB= defined $typeC= defined $_->{"type"}D= $_->{"type"} ne $type
ABCDresult
1XXX1
01111
00XX0
010X0
01100
642 }
643 @$links_rf;
644 }
645 0 $self->set_attr( 'alignment', \@links );
646 0 return;
647 }
648  
649 sub add_aligned_node {
650 0 ▶ my ( $self, $node, $type ) = @_;
Subroutine add_aligned_node: not called CC=1 SCAR=6.9
Pod documented
651 0 my $links_rf = $self->get_attr('alignment');
652 0 my %new_link = ( 'counterpart.rf' => $node->id, 'type' => $type );
653 0 push( @$links_rf, \%new_link );
654 0 $self->set_attr( 'alignment', $links_rf );
655 0 return;
656 }
657  
658 # remove invalid alignment links (leading to unindexed nodes)
659 sub update_aligned_nodes {
660  
661 0 ▶ my ($self) = @_;
Subroutine update_aligned_nodes: not called CC=3 SCAR=24.8
Pod documented
662 0 my $doc = $self->get_document();
663 0 my $links_rf = $self->get_attr('alignment');
664 0 my @new_links;
665  
666 0 foreach my $link ( @{$links_rf} ) {
667 0 ▶ push @new_links, $link if ( $doc->id_is_indexed( $link->{'counterpart.rf'} ) );
Branch: 1 branch
BranchTrueFalseTotal
if $doc->id_is_indexed($link->{"counterpart.rf"}) 0 0 0
668 }
669 0 $self->set_attr( 'alignment', \@new_links );
670 0 return;
671 }
672  
673 #************************************
674 #---- OTHER ------
675  
676 sub get_depth {
677 12 ▶ log_fatal 'Incorrect number of arguments' if @_ != 1;
Subroutine get_depth: called CC=3 SCAR=11.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 12 12
678 12 my $self = shift;
679 12 my $depth = 0;
680 12 ▶ while ( $self = $self->get_parent() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self = $self->get_parent) 12 12 24
681 12 $depth++;
682 }
683 12 return $depth;
684 }
685  
686 # This is called from $node->remove()
687 # so it must be defined in this class,
688 # but it is overriden in Treex::Core::Node::Ordered.
689 ▶sub _normalize_node_ordering {
Subroutine _normalize_node_ordering: called CC=1 SCAR=0.0
690 }
691  
692 sub get_address {
693 0 ▶ log_fatal 'Incorrect number of arguments' if @_ != 1;
Subroutine get_address: not called CC=3 SCAR=24.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 0 0
694 0 my $self = shift;
695 0 my $id = $self->id;
696 0 my $bundle = $self->get_bundle();
697 0 my $doc = $bundle->get_document();
698 0 ▶ my $file = $doc->loaded_from || ( $doc->full_filename . '.treex' );
Condition0%0/2
$doc->loaded_from || $doc->full_filename . '.treex'
l!l
00
MC/DC0%0/1
$doc->loaded_from || $doc->full_filename . '.treex'
$doc->loaded_from
Truth table0%0/2
A
A= $doc->loaded_from
Aresult
11
00
699 0 my $position = $bundle->get_position() + 1;
700  
701 #my $filename = Cwd::abs_path($file);
702 0 return "$file##$position.$id";
703 }
704  
705 # Empty DESTROY method is a hack to get rid of the "Deep recursion warning"
706 # in Treex::PML::Node::DESTROY and MooseX::NonMoose::Meta::Role::Class::_check_superclass_destructor.
707 # Without this hack, you get the warning after creating a node with 99 or more children.
708 # Deep recursion on subroutine "Class::MOP::Method::execute" at .../5.12.2/MooseX/NonMoose/Meta/Role/Class.pm line 183.
709 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=0.0
710 }
711  
712 #*************************************
713 #---- DEPRECATED & QUESTIONABLE ------
714  
715 sub generate_new_id { #TODO move to Core::Document?
716 106 ▶ log_fatal 'Incorrect number of arguments' if @_ != 1;
Subroutine generate_new_id: called CC=4 SCAR=14.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 106 106
717 106 my $self = shift;
718 106 my $doc = $self->get_document;
719  
720 106 my $latest_node_number = $doc->_latest_node_number;
721  
722 106 my $new_id;
723  
724 #$self->get_root->id =~ /(.+)root/;
725 #my $id_base = $1 || "";
726 my $id_base;
727 106 ▶ if ( $self->get_root->id =~ /(.+)root/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_root->id =~ /(.+)root/) { } 106 0 106
728 106 $id_base = $1;
729 }
730 else {
731 0 $id_base = q();
732 }
733  
734 106 while (1) {
735 106 $latest_node_number++;
736 106 $new_id = "${id_base}n$latest_node_number";
737 106 ▶ last if !$doc->id_is_indexed($new_id);
Branch: 1 branch
BranchTrueFalseTotal
unless $doc->id_is_indexed($new_id) 106 0 106
738  
739 }
740  
741 106 $doc->_set_latest_node_number($latest_node_number);
742  
743 106 return $new_id;
744 }
745  
746 sub add_to_listattr {
747 0 ▶ my $self = shift;
Subroutine add_to_listattr: not called CC=4 SCAR=30.0
Pod documented
748 0 my ( $attr_name, $attr_value ) = pos_validated_list(
749 \@_,
750 { isa => 'Str' },
751 { isa => 'Any' },
752 );
753  
754 0 my $list = $self->attr($attr_name);
755 0 ▶ log_fatal("Attribute $attr_name is not a list!")
Branch: 1 branch
BranchTrueFalseTotal
if not defined $list or ref $list ne "Treex::PML::List" 0 0 0
Condition0%0/3
not defined $list or ref $list ne "Treex::PML::List"
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $list or ref $list ne "Treex::PML::List"
not defined $list ref $list ne "Treex::PML::List"
Truth table0%0/3
not(A) or B
A= not defined $listB= ref $list ne "Treex::PML::List"
ABresult
1X1
011
000
756 if !defined $list || ref($list) ne 'Treex::PML::List';
757 0 my @new_list = @{$list};
758 0 ▶ if ( ref($attr_value) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attr_value eq 'ARRAY') { } 0 0 0
759 0 push @new_list, @{$attr_value};
760 }
761 else {
762 0 push @new_list, $attr_value;
763 }
764 0 return $self->set_attr( $attr_name, Treex::PML::List->new(@new_list) );
765 }
766  
767 # Get more attributes at once
768 sub get_attrs {
769 0 ▶ my $self = shift;
Subroutine get_attrs: not called CC=3 SCAR=24.8
Pod documented
770 0 my @attr_names = pos_validated_list(
771 \@_,
772 { isa => 'Any' }, #at least one parameter
773 MX_PARAMS_VALIDATE_ALLOW_EXTRA => 1,
774 );
775  
776 0 my @attr_values;
777 0 ▶ if ( ref $attr_names[-1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attr_names[-1]) { } 0 0 0
778 0 my $arg_ref = pop @attr_names;
779 0 my $change_undefs_to = $arg_ref->{undefs};
780 @attr_values = map {
781 0 ▶ defined $self->get_attr($_) ? $self->get_attr($_) : $change_undefs_to
Branch: 1 branch
BranchTrueFalseTotal
defined $self->get_attr($_) ? : 0 0 0
782 } @attr_names;
783 }
784 else {
785 0 @attr_values = map { $self->get_attr($_) } @attr_names;
786 }
787  
788 0 return @attr_values;
789 }
790  
791 # Return all attributes of the given node (sub)type that contain references
792 sub _get_reference_attrs {
793 60 ▶ my ($self) = @_;
Subroutine _get_reference_attrs: called CC=1 SCAR=0.0
794 60 return ();
795 }
796  
797 # Return IDs of all nodes to which there are reference links from this node (must be overridden in
798 # the respective node types)
799 sub _get_referenced_ids {
800 224 ▶ my ($self) = @_;
Subroutine _get_referenced_ids: called CC=5 SCAR=16.3
801 224 my $ret = {};
802  
803 # handle alignment separately
804 224 my $links_rf = $self->get_attr('alignment');
805 224 ▶ $ret->{alignment} = [ map { $_->{'counterpart.rf'} } @{$links_rf} ] if ($links_rf);
Branch: 1 branch
BranchTrueFalseTotal
if $links_rf 0 224 224
806  
807 # all other references
808 224 foreach my $ref_attr ( $self->_get_reference_attrs() ) {
809 566 ▶ my $val = $self->get_attr($ref_attr) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $val = $self->get_attr($ref_attr) or next 561 5 566
810 5 ▶ if ( !ref $val ) { # single-valued
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $val) { } 3 2 5
811 3 $ret->{$ref_attr} = [$val];
812 }
813 else {
814 2 $ret->{$ref_attr} = $val;
815 }
816 }
817 224 return $ret;
818 }
819  
820  
821 # ---------------------
822  
823 # changing the functionality of Treex::PML::Node's following() so that it traverses all
824 # nodes in all trees in all zones (needed for search in TrEd)
825  
826 sub following {
827 186 ▶ my ( $self ) = @_;
Subroutine following: called CC=7 SCAR=19.5
Pod documented
828  
829 186 my $pml_following = Treex::PML::Node::following(@_);
830  
831 186 ▶ if ( $pml_following ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pml_following) { } 98 88 186
832 98 return $pml_following;
833 }
834  
835 else {
836 88 ▶ my $bundle = ( ref($self) eq 'Treex::Core::Bundle' ) ? $self : $self->get_bundle;
Branch: 1 branch
BranchTrueFalseTotal
ref $self eq 'Treex::Core::Bundle' ? : 37 51 88
837  
838 my @all_trees = map {
839 88 ▶ ref($_) ne 'Treex::PML::Struct'
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ne 'Treex::PML::Struct' ? : 128 0 128
840 ? $_->get_all_trees
841 : ()
842 } $bundle->get_all_zones;
843  
844 88 ▶ if ( ref($self) eq 'Treex::Core::Bundle' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self eq 'Treex::Core::Bundle') { } 37 51 88
845 37 return $all_trees[0];
846 }
847  
848 else {
849 51 my $my_root = $self->get_root;
850 51 foreach my $index ( 0..$#all_trees ) {
851 168 ▶ if ( $all_trees[$index] eq $my_root ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($all_trees[$index] eq $my_root) 51 117 168
852 51 return $all_trees[$index+1];
853 }
854 }
855 0 log_fatal "Node belongs to no tree: this should never happen";
856 }
857 }
858 }
859  
860 sub descendants {
861 175 ▶ my ( $self ) = @_;
Subroutine descendants: called CC=1 SCAR=0.0
Pod documented
862 175 return ( map { $_->_descendants_and_self() } $self->children );
863 }
864  
865 sub _descendants_and_self {
866 384 ▶ my ( $self ) = @_;
Subroutine _descendants_and_self: called CC=1 SCAR=0.0
867 384 return ( $self, map { $_->_descendants_and_self() } $self->children );
868 }
869  
870  
871 # TODO: How to do this in an elegant way?
872 # Unless we find a better way, we must disable two perlcritics
873 package Treex::Core::Node::Removed;
874 {
875 64 $Treex::Core::Node::Removed::VERSION = '0.08633_1';
876 } ## no critic (ProhibitMultiplePackages)
877 64 ▶use Treex::Core::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
878  
879 sub AUTOLOAD { ## no critic (ProhibitAutoloading)
880 3 ▶ our $AUTOLOAD;
Subroutine AUTOLOAD: called CC=2 SCAR=8.1
881 3 ▶ if ( $AUTOLOAD !~ /DESTROY$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $Treex::Core::Node::Removed::AUTOLOAD =~ /DESTROY$/) 0 3 3
882 0 log_fatal("You cannot call any methods on removed nodes, but have called $AUTOLOAD");
883 }
884 }
885  
886 package Treex::Core::Node; ## no critic (ProhibitMultiplePackages)
887  
888 64 __PACKAGE__->meta->make_immutable;
889  
890 64 1;
891  
892 __END__
893  
894 ##-- begin proposal
895 # Example usage:
896 # Treex::Core::Node::T methods get_lex_anode and get_aux_anodes could use:
897 # my $a_lex = $t_node->get_r_attr('a/lex.rf'); # returns the node or undef
898 # my @a_aux = $t_node->get_r_attr('a/aux.rf'); # returns the nodes or ()
899 sub get_r_attr {
900 my ( $self, $attr_name ) = @_;
901 log_fatal('Incorrect number of arguments') if @_ != 2;
902 my $attr_value = $self->get_attr($attr_name);
903  
904 return if !$attr_value;
905 my $document = $self->get_document();
906 if (wantarray) {
907 log_fatal("Attribute '$attr_name' is not a list, but get_r_attr() called in a list context.")
908 if ref($attr_value) ne 'Treex::PML::List';
909 return map { $document->get_node_by_id($_) } @{$attr_value};
910 }
911  
912 log_fatal("Attribute $attr_name is a list, but get_r_attr() not called in a list context.")
913 if ref($attr_value) eq 'Treex::PML::List';
914 return $document->get_node_by_id($attr_value);
915 }
916  
917 # Example usage:
918 # $t_node->set_r_attr('a/lex.rf', $a_lex);
919 # $t_node->set_r_attr('a/aux.rf', @a_aux);
920 sub set_r_attr {
921 my ( $self, $attr_name, @attr_values ) = @_;
922 log_fatal('Incorrect number of arguments') if @_ < 3;
923 my $fs = $self;
924  
925 # TODO $fs->type nefunguje - asi protoze se v konstruktorech nenastavuje typ
926 if ( $fs->type($attr_name) eq 'Treex::PML::List' ) {
927 my @list = map { $_->id } @attr_values;
928  
929 # TODO: overriden Node::N::set_attr is bypassed by this call
930 return $fs->set_attr( $attr_name, Treex::PML::List->new(@list) );
931 }
932 log_fatal("Attribute '$attr_name' is not a list, but set_r_attr() called with @attr_values values.")
933 if @attr_values > 1;
934  
935 # TODO: overriden Node::N::set_attr is bypassed by this call
936 return $fs->set_attr( $attr_name, $attr_values[0]->id );
937 }
938  
939  
940  
941 =for Pod::Coverage BUILD
942  
943  
944 =encoding utf-8
945  
946 =head1 NAME
947  
948 Treex::Core::Node - smallest unit that holds information in Treex
949  
950 =head1 VERSION
951  
952 version 0.08633_1
953  
954 =head1 DESCRIPTION
955  
956 This class represents a Treex node.
957 Treex trees (contained in bundles) are formed by nodes and edges.
958 Attributes can be attached only to nodes. Edge's attributes must
959 be stored as the lower node's attributes.
960 Tree's attributes must be stored as attributes of the root node.
961  
962 =head1 METHODS
963  
964 =head2 Construction
965  
966 =over 4
967  
968 =item my $new_node = $existing_node->create_child({lemma=>'house', tag=>'NN' });
969  
970 Creates a new node as a child of an existing node. Some of its attribute
971 can be filled. Direct calls of node constructors (C<< ->new >>) should be avoided.
972  
973  
974 =back
975  
976  
977  
978 =head2 Access to the containers
979  
980 =over 4
981  
982 =item my $bundle = $node->get_bundle();
983  
984 Returns the L<Treex::Core::Bundle> object in which the node's tree is contained.
985  
986 =item my $document = $node->get_document();
987  
988 Returns the L<Treex::Core::Document> object in which the node's tree is contained.
989  
990 =item get_layer
991  
992 Return the layer of this node (I<a>, I<t>, I<n> or I<p>).
993  
994 =item get_zone
995  
996 Return the zone (L<Treex::Core::BundleZone>) to which this node
997 (and the whole tree) belongs.
998  
999 =item $lang_code = $node->language
1000  
1001 shortcut for C<< $lang_code = $node->get_zone()->language >>
1002  
1003 =item $selector = $node->selector
1004  
1005 shortcut for C<< $selector = $node->get_zone()->selector >>
1006  
1007 =back
1008  
1009  
1010 =head2 Access to attributes
1011  
1012 =over 4
1013  
1014 =item my $value = $node->get_attr($name);
1015  
1016 Returns the value of the node attribute of the given name.
1017  
1018 =item my $node->set_attr($name,$value);
1019  
1020 Sets the given attribute of the node with the given value.
1021 If the attribute name is C<id>, then the document's indexing table
1022 is updated. If value of the type C<List> is to be filled,
1023 then C<$value> must be a reference to the array of values.
1024  
1025 =item my $node2 = $node1->get_deref_attr($name);
1026  
1027 If value of the given attribute is reference (or list of references),
1028 it returns the appropriate node (or a reference to the list of nodes).
1029  
1030 =item my $node1->set_deref_attr($name, $node2);
1031  
1032 Sets the given attribute with C<id> (list of C<id>s) of the given node (list of nodes).
1033  
1034 =item my $node->add_to_listattr($name, $value);
1035  
1036 If the given attribute is list, the given value is appended to it.
1037  
1038 =item my $node->get_attrs(qw(name_of_attr1 name_of_attr2 ...));
1039  
1040 Get more attributes at once.
1041 If the last argument is C<{undefs=E<gt>$value}>, all undefs are substituted
1042 by a C<$value> (typically the value is an empty string).
1043  
1044 =back
1045  
1046  
1047  
1048  
1049 =head2 Access to tree topology
1050  
1051 =over 4
1052  
1053 =item my $parent_node = $node->get_parent();
1054  
1055 Returns the parent node, or C<undef> if there is none (if C<$node> itself is the root)
1056  
1057 =item $node->set_parent($parent_node);
1058  
1059 Makes C<$node> a child of C<$parent_node>.
1060  
1061 =item $node->remove();
1062  
1063 Deletes a node and the subtree rooted by the given node.
1064 Node identifier is removed from the document indexing table.
1065 The removed node cannot be further used.
1066  
1067 =item my $root_node = $node->get_root();
1068  
1069 Returns the root of the node's tree.
1070  
1071 =item $node->is_root();
1072  
1073 Returns C<true> if the node has no parent.
1074  
1075 =item $node->is_leaf();
1076  
1077 Returns C<true> if the node has no children.
1078  
1079 =item $node1->is_descendant_of($node2);
1080  
1081 Tests whether C<$node1> is among transitive descendants of C<$node2>;
1082  
1083 =back
1084  
1085 Next three methods (for access to children / descendants / siblings)
1086 have an optional argument C<$arg_ref> for specifying switches.
1087 By adding some switches, you can modify the behavior of these methods.
1088 See L<"Switches"> for examples.
1089  
1090 =over
1091  
1092 =item my @child_nodes = $node->get_children($arg_ref);
1093  
1094 Returns an array of child nodes.
1095  
1096 =item my @descendant_nodes = $node->get_descendants($arg_ref);
1097  
1098 Returns an array of descendant nodes ('transitive children').
1099  
1100 =item my @sibling_nodes = $node->get_siblings($arg_ref);
1101  
1102 Returns an array of nodes sharing the parent with the current node.
1103  
1104 =back
1105  
1106 =head3 Switches
1107  
1108 Currently there are 6 switches:
1109  
1110 =over
1111  
1112 =item * ordered
1113  
1114 =item * preceding_only, following_only
1115  
1116 =item * first_only, last_only
1117  
1118 =item * add_self
1119  
1120 =back
1121  
1122 =head4 Examples of usage
1123  
1124 Names of variables in the examples suppose a language with left-to-right script.
1125  
1126 my @ordered_descendants = $node->get_descendants({ordered=>1});
1127 my @self_and_left_children = $node->get_children({preceding_only=>1, add_self=>1});
1128 my @ordered_self_and_children = $node->get_children({ordered=>1, add_self=>1});
1129 my $leftmost_child = $node->get_children({first_only=>1});
1130 my @ordered_siblings = $node->get_siblings({ordered=>1});
1131 my $left_neighbor = $node->get_siblings({preceding_only=>1, last_only=>1});
1132 my $right_neighbor = $node->get_siblings({following_only=>1, first_only=>1});
1133 my $leftmost_sibling_or_self = $node->get_siblings({add_self=>1, first_only=>1});
1134  
1135 =head4 Restrictions
1136  
1137 =over
1138  
1139 =item *
1140  
1141 C<first_only> and C<last_only> switches makes the method return just one item -
1142 a scalar, even if combined with the C<add_self> switch.
1143  
1144 =item *
1145  
1146 Specifying C<(first|last|preceding|following)_only> implies C<ordered>,
1147 so explicit addition of C<ordered> gives a warning.
1148  
1149 =item *
1150  
1151 Specifying both C<preceding_only> and C<following_only> gives an error
1152 (same for combining C<first_only> and C<last_only>).
1153  
1154 =back
1155  
1156 =head4 Shortcuts
1157  
1158 There are shortcuts for comfort of those who use B<left-to-right> scripts:
1159  
1160 =over
1161  
1162 =item my $left_neighbor_node = $node->get_left_neighbor();
1163  
1164 Returns the rightmost node from the set of left siblings (the nearest left sibling).
1165 Actually, this is shortcut for C<$node-E<gt>get_siblings({preceding_only=E<gt>1, last_only=E<gt>1})>.
1166  
1167 =item my $right_neighbor_node = $node->get_right_neighbor();
1168  
1169 Returns the leftmost node from the set of right siblings (the nearest right sibling).
1170 Actually, this is shortcut for C<$node-E<gt>get_siblings({following_only=E<gt>1, first_only=E<gt>1})>.
1171  
1172 =back
1173  
1174 =head2 PML-related methods
1175  
1176 =over
1177  
1178 =item my $type = $node->get_pml_type_name;
1179  
1180 =back
1181  
1182  
1183 =head2 Access to alignment
1184  
1185 =over
1186  
1187 =item add_aligned_node
1188  
1189 =item my ($nodes_rf, $types_rf) = $node->get_aligned_nodes();
1190  
1191 Returns an array containing two array references. The first array contains the nodes aligned to this node, the second array contains types of the links.
1192  
1193 =item my @nodes = $node->get_aligned_nodes_of_type($regex_constraint_on_type);
1194  
1195 =item delete_aligned_node
1196  
1197 =item is_aligned_to
1198  
1199 =item update_aligned_nodes()
1200  
1201 Removes all alignment links leading to nodes which have been deleted.
1202  
1203 =back
1204  
1205 =head2 References (alignment and other references depending on node subtype)
1206  
1207 =over
1208  
1209 =item my @refnodes = $node->get_referencing_nodes($ref_type);
1210  
1211 Returns an array of nodes referencing this node with the given reference type (e.g. 'alignment', 'a/lex.rf' etc.).
1212  
1213 =back
1214  
1215 =head2 Other methods
1216  
1217 =over 4
1218  
1219 =item $node->generate_new_id();
1220  
1221 Generate new (= so far unindexed) identifier (to be used when creating new
1222 nodes). The new identifier is derived from the identifier of the root
1223 (C<< $node->root >>), by adding suffix C<x1> (or C<x2>, if C<...x1> has already
1224 been indexed, etc.) to the root's C<id>.
1225  
1226 =item my $levels = $node->get_depth();
1227  
1228 Return the depth of the node. The root has depth = 0, its children have depth = 1 etc.
1229  
1230 =item my $address = $node->get_address();
1231  
1232 Return the node address, i.e. file name and node's position within the file,
1233 similarly to TrEd's C<FPosition()> (but the value is only returned, not printed).
1234  
1235 =item $node->equals($another_node)
1236  
1237 This is the internal implementation of overloaded C<==> operator,
1238 which checks whether C<$node == $another_node> (the object instance must be identical).
1239  
1240 =item my $string = $node->to_string()
1241  
1242 This is the internal implementation of overloaded stringification,
1243 so you can use e.g. C<print "There is a node $node.">.
1244 It returns the id (C<$node->id>), but the behavior may be overridden in subclasses.
1245 See L<overload> pragma for details about overloading operators in Perl.
1246  
1247 =back
1248  
1249  
1250 =head1 AUTHORS
1251  
1252 Zdeněk Žabokrtský <zabokrtsky@ufal.mff.cuni.cz>
1253  
1254 Martin Popel <popel@ufal.mff.cuni.cz>
1255  
1256 David Mareček <marecek@ufal.mff.cuni.cz>
1257  
1258 Daniel Zeman <zeman@ufal.mff.cuni.cz>
1259  
1260 Ondřej Dušek <odusek@ufal.mff.cuni.cz>
1261  
1262 =head1 COPYRIGHT AND LICENSE
1263  
1264 Copyright © 2011-2012 by Institute of Formal and Applied Linguistics, Charles University in Prague
1265  
1266 This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
« lib/Treex/Block/Write/BaseWriter.pm ↑ Summary lib/Treex/Block/Read/BaseReader.pm »