Treex/Core/Node.pm

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