Treex/Core/Node.pm

filter: Statementstmt 66.0% 303 / 459 Branchbran 53.6% 119 / 222 Conditioncond 44.4% 40 / 90 MC/DCmcdc 22.6% 12 / 53 Subroutinesub 69.8% 44 / 63 Podpod 85.3% 35 / 41 totaltotal 59.5% 553 / 928 CC 151 SCAR 25.6
CC
151
Cov
60%
set_misc_attr
72.0
set_attr
36.5
get_deref_attr
20.0
CRAP 12.9
« Treex/Core/DocumentReader.pm ↑ Summary Treex/Block/Read/BaseReader.pm »
1 package Treex::Core::Node;
2 47 $Treex::Core::Node::VERSION = '2.20210102';
3 47 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 47 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 47 ▶use MooseX::NonMoose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 47 ▶use Treex::Core::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 47 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 47 ▶use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 47 ▶use Treex::PML;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 47 extends 'Treex::PML::Node';
13 47 with 'Treex::Core::WildAttr';
14 47 with 'Treex::Core::Node::Aligned';
15  
16 # overloading does not work with namespace::autoclean
17 # see https://rt.cpan.org/Public/Bug/Display.html?id=50938
18 # We may want to use https://metacpan.org/module/namespace::sweep instead.
19 #
20 # use overload
21 # '""' => 'to_string',
22 # '==' => 'equals',
23 # '!=' => '_not_equals',
24 # 'eq' => 'equals', # deprecated
25 # 'ne' => '_not_equals', # deprecated
26 # 'bool' => sub{1},
27 #
28 # # We can A) let Magic Autogeneration to build "derived" overloadings,
29 # # or B) we can disable this feature (via fallback=>0)
30 # # and define only the needed overloadings
31 # # (so all other overloadings will result in fatal errors).
32 # # See perldoc overload.
33 # # I decided for A, but uncommenting the following lines can catch some misuses.
34 # #'!' => sub{0},
35 # #'.' => sub{$_[2] ? $_[1] . $_[0]->to_string : $_[0]->to_string . $_[1]},
36 # #fallback => 0,
37 # ;
38 # # TODO: '<' => 'precedes' (or better '<=>' => ...)
39 # # 'eq' => sub {log_warn 'You should use ==' && return $_[0]==$_[1]} # similarly for 'ne'
40  
41 47 Readonly my $_SWITCHES_REGEX => qr/^(ordered|add_self|(preceding|following|first|last)_only)$/x;
42 47 my $CHECK_FOR_CYCLES = 1;
43  
44 47 our $LOG_NEW = 0;
45 47 our $LOG_EDITS = 0;
46 # tip: you can use Util::Eval doc='$Treex::Core::Node::LOG_EDITS=1;' in your scenario
47 # Note that most attributes are not set by set_attr. See TODO below.
48  
49 47 has _zone => (
50 is => 'rw',
51 writer => '_set_zone',
52 reader => '_get_zone',
53 weak_ref => 1,
54 );
55  
56 47 has id => (
57 is => 'rw',
58 trigger => \&_index_my_id,
59 );
60  
61 261 ▶sub BUILD {
Subroutine BUILD: called CC=4 SCAR=17.9
Pod undocumented
62 261 my ( $self, $arg_ref ) = @_;
63  
64 261 ▶ 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 261 261
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
26100
defined $arg_ref and defined $arg_ref->{"_called_from_core_"}
!ll&&!rl&&r
00261
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
65 and not $Treex::Core::Config::running_in_tred
66 )
67 {
68 0 log_fatal 'Because of node indexing, no nodes can be created outside of documents. '
69 . 'You have to use $zone->create_tree(...) or $node->create_child() '
70 . 'instead of Treex::Core::Node...->new().';
71 }
72 261 return;
73 }
74  
75 0 ▶sub to_string {
Subroutine to_string: not called CC=2 SCAR=17.9
Pod documented
76 0 my ($self) = @_;
77 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
78 }
79  
80 # Since we have overloaded stringification, we must overload == as well,
81 # so you can use "if ($nodeA == $nodeB){...}".
82 0 ▶sub equals {
Subroutine equals: not called CC=2 SCAR=17.9
Pod documented
83 0 my ($self, $node) = @_;
84 #return ref($node) && $node->id eq $self->id;
85 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
86 }
87  
88 0 ▶sub _not_equals {
Subroutine _not_equals: not called CC=1 SCAR=6.9
89 0 my ($self, $node) = @_;
90 0 return !$self->equals($node);
91 }
92  
93 261 ▶sub _index_my_id {
Subroutine _index_my_id: called CC=2 SCAR=7.0
94 261 my $self = shift;
95 # It is possible that get_document is undefined at the moment (for example
96 # if we are reading one large document and splitting it to many small documents).
97 # Then there is nothing to index (and everything will be probably indexed
98 # later when the document is created).
99 261 my $document = $self->get_document();
100 261 ▶ if (defined($document))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $document) 261 0 261
101 {
102 261 $document->index_node_by_id( $self->id, $self );
103 }
104 261 return;
105 }
106  
107 0 ▶sub _caller_signature {
Subroutine _caller_signature: not called CC=2 SCAR=17.9
108 0 my $level = 1;
109 0 my ($package, $filename, $line) = caller;
110 0 ▶ while ($package =~ /^Treex::Core/){
Branch: 1 branch
BranchTrueFalseTotal
if ($package =~ /^Treex::Core/) 0 0 0
111 0 ($package, $filename, $line) = caller $level++;
112 }
113 0 $package =~ s/^Treex::Block:://;
114 0 return "$package#$line";
115 }
116  
117 # ---- access to attributes ----
118  
119 # unlike attr (implemented in Treex::PML::Instance::get_data)
120 # get_attr implements only "plain" and "nested hash" attribute names,
121 # i.e. no XPath-like expressions (a/aux.rf[3]) are allowed.
122 # This results in much faster code.
123 1426 ▶sub get_attr {
Subroutine get_attr: called CC=5 SCAR=16.2
Pod documented
124 1426 my ( $self, $attr_name ) = @_;
125 1426 ▶ log_fatal('Incorrect number of arguments') if @_ != 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 1426 1426
126 1426 my $val = $self;
127 1426 for my $step ( split /\//, $attr_name ) {
128 1656 ▶ if ( !defined $val ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 206 1450 1656
129 206 ▶ log_fatal "Attribute '$attr_name' contains strange symbols."
Branch: 1 branch
BranchTrueFalseTotal
if $attr_name =~ m[[^-\w/.]] 0 206 206
130 . " For XPath like constructs (e.g. 'a/aux.rf[3]') use the 'attr' method."
131 if $attr_name =~ /[^-\w\/.]/;
132 }
133 1656 $val = $val->{$step};
134 }
135 1426 return $val;
136 }
137  
138 47 ▶use Treex::PML::Factory;
Subroutine BEGIN: called CC=1 SCAR=0.0
139  
140 138 ▶sub set_attr {
Subroutine set_attr: called CC=23 SCAR=36.0
Pod documented
141 138 my ( $self, $attr_name, $attr_value ) = @_;
142 138 ▶ log_fatal('Incorrect number of arguments') if @_ != 3;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 3 0 138 138
143 138 ▶ if ( $attr_name eq 'id' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($attr_name eq 'id') { } 0 138 138
elsif (ref $attr_value eq 'ARRAY') { } 9 129 138
144 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
145 0 log_fatal 'Setting undefined or empty ID is not allowed';
146 }
147 0 $self->get_document->index_node_by_id( $attr_value, $self );
148 }
149 elsif ( ref($attr_value) eq 'ARRAY' ) {
150 9 $attr_value = Treex::PML::List->new( @{$attr_value} );
151 }
152  
153 138 ▶ if ($attr_name =~ /\.rf$/){
Branch: 2 branches
BranchTrueFalseTotal
if ($attr_name =~ /\.rf$/) { } 12 126 138
elsif ($attr_name eq 'alignment') { } 8 118 126
154 12 my $document = $self->get_document();
155  
156 # Delete previous back references
157 12 my $old_value = $self->get_attr($attr_name);
158 12 ▶ if ($old_value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_value) 5 7 12
159 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
160 2 $document->remove_backref( $attr_name, $self->id, $old_value );
161 }
162 else {
163 3 $document->remove_backref( $attr_name, $self->id, [$old_value] );
164 }
165 }
166  
167 # Set new back references
168 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
169 12 $document->index_backref( $attr_name, $self->id, $ids );
170 }
171 elsif ($attr_name eq 'alignment'){
172 8 my $document = $self->get_document();
173 8 ▶ if ($self->{alignment}){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"alignment"}) 4 4 8
174 4 my @old_ids = map { $_->{'counterpart.rf'} } @{$self->{alignment}};
175 4 $document->remove_backref( 'alignment', $self->id, \@old_ids );
176 }
177 8 ▶ if ($attr_value && @$attr_value){
Branch: 1 branch
BranchTrueFalseTotal
if ($attr_value and @$attr_value) 8 0 8
Condition33%1/3
$attr_value and @$attr_value
!ll&&!rl&&r
008
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
178 8 my @new_ids = map { $_->{'counterpart.rf'} } @$attr_value;
179 8 $document->index_backref( $attr_name, $self->id, \@new_ids );
180 }
181 }
182  
183 # TODO: most attributes are set by Moose setters,
184 # e.g. $anode->set_form("Hi") does not call set_attr.
185 # We would need to redefine all the setter to fill wild->{edited_by}.
186 138 ▶ if ($LOG_EDITS){
Branch: 1 branch
BranchTrueFalseTotal
if ($Treex::Core::Node::LOG_EDITS) 0 138 138
187 0 my $signature = $self->wild->{edited_by};
188 0 ▶ if ($signature) {$signature .= "\n";}
Branch: 1 branch
BranchTrueFalseTotal
if ($signature) { } 0 0 0
189 0 else {$signature = '';}
190 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
191 0 $signature .= "$attr_name=$a_value ". $self->_caller_signature();
192 0 $self->wild->{edited_by} = $signature;
193 }
194  
195 #simple attributes can be accessed directly
196 138 ▶ 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" 130 8 138
Condition100%3/3
$attr_name =~ /^[\w\.]+$/ or $attr_name eq "#name"
l!l&&r!l&&!r
69618
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
197 8 ▶ log_fatal "Attribute '$attr_name' contains strange symbols."
Branch: 1 branch
BranchTrueFalseTotal
if $attr_name =~ m[[^-\w/.]] 0 8 8
198 . " No XPath like constructs (e.g. 'a/aux.rf[3]') are allowed."
199 if $attr_name =~ /[^-\w\/.]/;
200  
201 8 my $val = $self;
202 8 my @steps = split /\//, $attr_name;
203 8 while (1) {
204 16 my $step = shift @steps;
205 16 ▶ if (@steps) {
Branch: 1 branch
BranchTrueFalseTotal
if (@steps) { } 8 8 16
206 8 ▶ if ( !defined( $val->{$step} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val->{$step}) 4 4 8
207 4 $val->{$step} = Treex::PML::Factory->createStructure();
208 }
209 8 $val = $val->{$step};
210 }
211 else {
212 8 return $val->{$step} = $attr_value;
213 }
214 }
215 0 return;
216 }
217  
218 0 ▶sub get_deref_attr {
Subroutine get_deref_attr: not called CC=4 SCAR=30.0
Pod documented
219 0 my ( $self, $attr_name ) = @_;
220 0 ▶ log_fatal('Incorrect number of arguments') if @_ != 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 0 0
221 0 my $attr_value = $self->get_attr($attr_name);
222  
223 0 ▶ return if !$attr_value;
Branch: 1 branch
BranchTrueFalseTotal
unless $attr_value 0 0 0
224 0 my $document = $self->get_document();
225 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
226 if ref($attr_value) eq 'Treex::PML::List';
227 0 return $document->get_node_by_id($attr_value);
228 }
229  
230 0 ▶sub set_deref_attr {
Subroutine set_deref_attr: not called CC=3 SCAR=24.8
Pod documented
231 0 my ( $self, $attr_name, $attr_value ) = @_;
232 0 ▶ log_fatal('Incorrect number of arguments') if @_ != 3;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 3 0 0 0
233  
234 # If $attr_value is an array of nodes
235 0 ▶ if ( ref($attr_value) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attr_value eq 'ARRAY') { } 0 0 0
236 0 my @list = map { $_->id } @{$attr_value};
237 0 $attr_value = Treex::PML::List->new(@list);
238 }
239  
240 # If $attr_value is just one node
241 else {
242 0 $attr_value = $attr_value->id;
243 }
244  
245 # Set the new reference(s)
246 0 $self->set_attr( $attr_name, $attr_value );
247 0 return;
248 }
249  
250 1158 ▶sub get_bundle {
Subroutine get_bundle: called CC=2 SCAR=7.1
Pod documented
251 1158 ▶ log_fatal 'Incorrect number of arguments' if @_ != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 1158 1158
252 1158 my $self = shift;
253 1158 return $self->get_zone->get_bundle;
254 }
255  
256 # reference to embedding zone is stored only with tree root, not with nodes
257 1170 ▶sub get_zone {
Subroutine get_zone: called CC=4 SCAR=14.0
Pod documented
258 1170 ▶ log_fatal 'Incorrect number of arguments' if @_ != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 1170 1170
259 1170 my $self = shift;
260 1170 my $zone;
261 1170 ▶ if ( $self->is_root ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_root) { } 394 776 1170
262 394 $zone = $self->_get_zone;
263 }
264 else {
265 776 $zone = $self->get_root->_get_zone; ## no critic (ProtectPrivateSubs)
266 }
267  
268 1170 ▶ log_fatal "a node (" . $self->id . ") can't reveal its zone" if !$zone;
Branch: 1 branch
BranchTrueFalseTotal
unless $zone 0 1170 1170
269 1170 return $zone;
270  
271 }
272  
273 26 ▶sub remove {
Subroutine remove: called CC=11 SCAR=26.6
Pod documented
274 26 my ($self, $arg_ref) = @_;
275 26 ▶ if ( $self->is_root ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_root) 0 26 26
276 0 log_fatal 'Tree root cannot be removed using $root->remove().'
277 . ' Use $zone->remove_tree($layer) instead';
278 }
279 26 my $root = $self->get_root();
280 26 my $document = $self->get_document();
281  
282 26 my @children = $self->get_children();
283 26 ▶ if (@children){
Branch: 1 branch
BranchTrueFalseTotal
if (@children) 9 17 26
284 9 my $what_to_do = 'remove';
285 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
286 0 $what_to_do = $arg_ref->{children};
287 }
288 9 ▶ if ($what_to_do =~ /^rehang/){
Branch: 1 branch
BranchTrueFalseTotal
if ($what_to_do =~ /^rehang/) 0 9 9
289 0 foreach my $child (@children){
290 0 $child->set_parent($self->get_parent);
291 }
292 }
293 9 ▶ if ($what_to_do =~ /warn$/){
Branch: 1 branch
BranchTrueFalseTotal
if ($what_to_do =~ /warn$/) 0 9 9
294 0 log_warn $self->get_address . " is being removed by remove({children=>$what_to_do}), but it has (unexpected) children";
295 }
296 }
297  
298 # Remove the subtree from the document's indexing table
299 26 my @to_remove = ( $self, $self->get_descendants );
300 26 foreach my $node ( @to_remove) {
301 69 ▶ if ( defined $node->id ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $node->id) 69 0 69
302 69 $document->_remove_references_to_node( $node );
303 69 $document->index_node_by_id( $node->id, undef );
304 }
305 }
306  
307 # Disconnect the node from its parent (& siblings) and delete all attributes
308 # It actually does: $self->cut(); undef %$_ for ($self->descendants(), $self);
309 26 $self->destroy;
310  
311 # TODO: order normalizing can be done in a more efficient way
312 # (update just the following ords)
313 26 $root->_normalize_node_ordering();
314  
315 # By reblessing we make sure that
316 # all methods called on removed nodes will result in fatal errors.
317 26 foreach my $node (@to_remove){
318 69 bless $node, 'Treex::Core::Node::Deleted';
319 }
320 26 return;
321 }
322  
323 # Return all nodes that have a reference of the given type (e.g. 'alignment', 'a/lex.rf') to this node
324 24 ▶sub get_referencing_nodes {
Subroutine get_referencing_nodes: called CC=6 SCAR=24.4
Pod documented
325 24 my ( $self, $type, $lang, $sel ) = @_;
326 24 my $doc = $self->get_document;
327 24 my $refs = $doc->get_references_to_id( $self->id );
328 24 ▶ return if ( !$refs || !$refs->{$type} );
Branch: 1 branch
BranchTrueFalseTotal
unless $refs and $refs->{$type} 2 22 24
Condition66%2/3
$refs and $refs->{$type}
!ll&&!rl&&r
2022
MC/DC50%1/2
$refs and $refs->{$type}
$refs $refs->{$type}
Truth table66%2/3
A and B
A= $refsB= $refs->{$type}
ABresult
0X0
100
111
329 22 ▶ if ((defined $lang) && (defined $sel)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lang and defined $sel) 0 22 22
Condition33%1/3
defined $lang and defined $sel
!ll&&!rl&&r
2200
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
330 0 my @ref_filtered_by_tree;
331 0 ▶ if ($sel eq q() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sel eq '') { } 0 0 0
332 0 @ref_filtered_by_tree = grep { /(a|t)\_tree\-$lang\-.+/; }@{ $refs->{$type} };
333 }
334 else {
335 0 @ref_filtered_by_tree = grep { /(a|t)\_tree\-$lang\_$sel\-.+/; }@{ $refs->{$type} };
336 }
337 0 return map { $doc->get_node_by_id($_) } @ref_filtered_by_tree;
338 }
339 22 return map { $doc->get_node_by_id($_) } @{ $refs->{$type} };
340 }
341  
342 # Remove a reference of the given type to the given node. This will not remove a reverse reference from document
343 # index, since it is itself called when removing reverse references; use the API methods for the individual
344 # references if you want to keep reverse references up-to-date.
345 6 ▶sub remove_reference {
Subroutine remove_reference: called CC=6 SCAR=20.4
Pod undocumented
346 6 my ( $self, $type, $id ) = @_;
347  
348 6 ▶ if ( $type eq 'alignment' ) { # handle alignment links separately
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'alignment') { } 0 6 6
349  
350 0 my $links = $self->get_attr('alignment');
351  
352 0 ▶ if ($links) {
Branch: 1 branch
BranchTrueFalseTotal
if ($links) 0 0 0
353 0 $self->set_attr( 'alignment', [ grep { $_->{'counterpart.rf'} ne $id } @{$links} ] );
354 }
355 }
356 else {
357 6 my $attr = $self->get_attr($type);
358 6 ▶ log_fatal "undefined attr $type (id=$id)" if !defined $attr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attr 0 6 6
359  
360 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
361 5 $self->set_attr( $type, undef );
362 }
363 else {
364 1 $attr->delete_value($id); # TODO : will it be always a Treex::PML::List? Looks like it works.
365 }
366 }
367 6 return;
368 }
369  
370  
371 262 ▶sub fix_pml_type {
Subroutine fix_pml_type: called CC=4 SCAR=14.5
Pod documented
372 262 ▶ log_fatal 'Incorrect number of arguments' if @_ != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 262 262
373 262 my $self = shift;
374 262 ▶ if ( not $self->type() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->type) 261 1 262
375 261 my $type = $self->get_pml_type_name();
376 261 ▶ if ( not $type ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type) 0 261 261
377 0 log_warn "No PML type recognized for node $self";
378 0 return;
379 }
380 261 my $fs_file = $self->get_document()->_pmldoc;
381 261 $self->set_type_by_name( $fs_file->metaData('schema'), $type );
382 }
383 262 return;
384 }
385  
386 0 ▶sub get_pml_type_name {
Subroutine get_pml_type_name: not called CC=2 SCAR=17.9
Pod documented
387 0 ▶ log_fatal 'Incorrect number of arguments' if @_ != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 0 0
388 0 my $self = shift;
389 0 return;
390 }
391  
392 19 ▶sub get_layer {
Subroutine get_layer: called CC=3 SCAR=12.0
Pod documented
393 19 ▶ log_fatal 'Incorrect number of arguments' if @_ != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 19 19
394 19 my $self = shift;
395 19 ▶ if ( ref($self) =~ /Node::(\w)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($self) =~ /Node::(\w)$/) { } 19 0 19
396 19 return lc($1);
397 }
398 else {
399 0 log_fatal "Cannot recognize node's layer: $self";
400 }
401 }
402  
403 7 ▶sub language {
Subroutine language: called CC=2 SCAR=7.1
Pod documented
404 7 ▶ log_fatal 'Incorrect number of arguments' if @_ != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 7 7
405 7 my $self = shift;
406 7 return $self->get_zone()->language;
407 }
408  
409 0 ▶sub selector {
Subroutine selector: not called CC=2 SCAR=17.9
Pod documented
410 0 ▶ log_fatal 'Incorrect number of arguments' if @_ != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 0 0
411 0 my $self = shift;
412 0 return $self->get_zone()->selector;
413 }
414  
415 183 ▶sub create_child {
Subroutine create_child: called CC=10 SCAR=23.4
Pod documented
416 183 my $self = shift;
417  
418 #NOT VALIDATED INTENTIONALLY - passing args to to new (and it's also black magic, so I'm not touching it)
419  
420 # TODO:
421 #my $new_node = ( ref $self )->new(@_);
422 # Previous line is very strange and causes errors which are hard to debug.
423 # Magically, it works on UFAL machines, but nowhere else - I don't know why.
424 # Substituting the hash by hashref is a workaround,
425 # but the black magic is still there.
426 183 my $arg_ref;
427 183 ▶ if ( scalar @_ == 1 && ref $_[0] eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ == 1 and ref $_[0] eq 'HASH') { } 75 108 183
elsif (@_ % 2) { } 0 108 108
Condition66%2/3
scalar @_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
108075
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
428 75 $arg_ref = $_[0];
429 }
430 elsif ( @_ % 2 ) {
431 0 log_fatal "Odd number of elements for create_child";
432 }
433 else {
434 108 $arg_ref = {@_};
435 }
436  
437 # Structured attributes (e.g. morphcat/pos) must be handled separately
438 # TODO: And also attributes which don't have accessors (those are not Moose attributes).
439 # Note: mlayer_pos was not added to Treex::Core::Node::T because it goes
440 # against the "tectogrammatical ideology" and we use it as a temporary hack.
441 183 my %structured_attrs;
442 183 foreach my $attr ( keys %{$arg_ref} ) {
443 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") 62 54 116
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
444 62 $structured_attrs{$attr} = delete $arg_ref->{$attr};
445 }
446 }
447  
448 183 $arg_ref->{_called_from_core_} = 1;
449 183 my $new_node = ( ref $self )->new($arg_ref);
450 183 $new_node->set_parent($self);
451  
452 183 my $new_id = $self->generate_new_id();
453 183 $new_node->set_id($new_id);
454  
455 183 foreach my $attr ( keys %structured_attrs ) {
456 62 $new_node->set_attr( $attr, $structured_attrs{$attr} );
457 }
458  
459 # my $type = $new_node->get_pml_type_name();
460 # return $new_node if !defined $type;
461 # my $fs_file = $self->get_bundle->get_document()->_pmldoc;
462 # $self->set_type_by_name( $fs_file->metaData('schema'), $type );
463  
464 183 $new_node->fix_pml_type();
465  
466 # Remember which module (Treex block) and line number in its source code are responsible for creating this node.
467 183 ▶ if ($LOG_NEW){
Branch: 1 branch
BranchTrueFalseTotal
if ($Treex::Core::Node::LOG_NEW) 0 183 183
468 0 $new_node->wild->{created_by} = $self->_caller_signature();
469 }
470  
471 183 return $new_node;
472 }
473  
474 #************************************
475 #---- TREE NAVIGATION ------
476  
477 1008 ▶sub get_document {
Subroutine get_document: called CC=3 SCAR=11.3
Pod documented
478 1008 ▶ log_fatal 'Incorrect number of arguments' if @_ != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 1008 1008
479 1008 my $self = shift;
480 1008 my $bundle = $self->get_bundle();
481 1008 ▶ 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 1008 1008
482 1008 return $bundle->get_document();
483 }
484  
485 1144 ▶sub get_root {
Subroutine get_root: called CC=2 SCAR=7.1
Pod documented
486 1144 ▶ log_fatal 'Incorrect number of arguments' if @_ != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 1144 1144
487 1144 my $self = shift;
488 1144 return $self->root();
489 }
490  
491 1533 ▶sub is_root {
Subroutine is_root: called CC=2 SCAR=7.1
Pod documented
492 1533 ▶ log_fatal 'Incorrect number of arguments' if @_ != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 1533 1533
493 1533 my $self = shift;
494 1533 return !$self->parent;
495 }
496  
497 0 ▶sub is_leaf {
Subroutine is_leaf: not called CC=2 SCAR=17.9
Pod documented
498 0 ▶ log_fatal 'Incorrect number of arguments' if @_ != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 0 0
499 0 my $self = shift;
500 0 return not $self->firstson;
501 }
502  
503 547 ▶sub get_parent {
Subroutine get_parent: called CC=2 SCAR=7.1
Pod documented
504 547 ▶ log_fatal 'Incorrect number of arguments' if @_ != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 547 547
505 547 my $self = shift;
506 547 return $self->parent;
507 }
508  
509 189 ▶sub set_parent {
Subroutine set_parent: called CC=6 SCAR=20.1
Pod documented
510 189 my $self = shift;
511 189 my ($parent) = pos_validated_list(
512 \@_,
513 { isa => 'Treex::Core::Node' },
514 );
515  
516 # TODO check for this (but set_parent is called also from create_child)
517 #if ($self->get_document() != $parent->get_document()) {
518 # log_fatal("Cannot move a node from one document to another");
519 #}
520  
521 # We cannot detach a node by setting an undefined parent. The if statement below will die.
522 # Let's inform the user where the bad call is.
523 189 ▶ log_fatal( 'Cannot attach the node ' . $self->id . ' to an undefined parent' ) if ( !defined($parent) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parent 0 189 189
524 189 ▶ 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 189 189
Condition33%2/6
$self == $parent or $CHECK_FOR_CYCLES and $parent->is_descendant_of($self)
l!l&&r!l&&!r
00189
$CHECK_FOR_CYCLES and $parent->is_descendant_of($self)
!ll&&!rl&&r
01890
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
525 0 my $id = $self->id;
526 0 my $p_id = $parent->id;
527 0 log_fatal("Attempt to set parent of $id to the node $p_id, which would lead to a cycle.");
528 }
529  
530 # TODO: Too much FSlib (aka Treex::PML) here
531 189 $self->cut();
532 189 my $fsfile = $parent->get_document()->_pmldoc;
533 189 my @fschildren = $parent->children();
534 189 ▶ if (@fschildren) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fschildren) { } 103 86 189
535 103 Treex::PML::PasteAfter( $self, $fschildren[-1] );
536 }
537 else {
538 86 Treex::PML::Paste( $self, $parent, $fsfile->FS() );
539 }
540  
541 189 return;
542 }
543  
544 132 ▶sub _check_switches {
Subroutine _check_switches: called CC=9 SCAR=24.6
545  
546 #This method may be replaced by subtype and checked as parameter
547 132 my $self = shift;
548 132 my ($arg_ref) = pos_validated_list(
549 \@_,
550 { isa => 'Maybe[HashRef]' },
551 );
552  
553 # Check for role Ordered
554 log_fatal('This type of node does not support ordering')
555 if (
556 132 ▶ ( $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 132 132
Condition66%4/6
$arg_ref->{"ordered"} || &any(sub { $$arg_ref{$_ . '_only'}; } , "first", "last", "preceding", "following") and not $self->does("Treex::Core::Node::Ordered")
!ll&&!rl&&r
52800
$arg_ref->{"ordered"} or &any(sub { $$arg_ref{$_ . '_only'}; } , "first", "last", "preceding", "following")
l!l&&r!l&&!r
80052
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
557 &&
558 !$self->does('Treex::Core::Node::Ordered')
559 );
560  
561 # Check switches for not allowed combinations
562 log_fatal('Specified both preceding_only and following_only.')
563 132 ▶ if $arg_ref->{preceding_only} && $arg_ref->{following_only};
Branch: 1 branch
BranchTrueFalseTotal
if $arg_ref->{"preceding_only"} and $arg_ref->{"following_only"} 0 132 132
Condition33%1/3
$arg_ref->{"preceding_only"} and $arg_ref->{"following_only"}
!ll&&!rl&&r
13200
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
564 log_fatal('Specified both first_only and last_only.')
565 132 ▶ if $arg_ref->{first_only} && $arg_ref->{last_only};
Branch: 1 branch
BranchTrueFalseTotal
if $arg_ref->{"first_only"} and $arg_ref->{"last_only"} 0 132 132
Condition33%1/3
$arg_ref->{"first_only"} and $arg_ref->{"last_only"}
!ll&&!rl&&r
13200
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
566  
567 # Check for explicit "ordered" when not needed (possible typo)
568 # DZ: Turning off. This is an annoying warning when I want to explicitly say in my code that the result will be ordered and accessing the first/last item is meaningful.
569 #log_warn('Specifying (first|last|preceding|following)_only implies ordered.')
570 # if $arg_ref->{ordered}
571 # && any { $arg_ref->{ $_ . '_only' } } qw(first last preceding following);
572  
573 # Check for unknown switches
574 132 ▶ my $unknown = first { $_ !~ $_SWITCHES_REGEX } keys %{$arg_ref};
Subroutine __ANON__: called CC=1 SCAR=0.0
575 132 ▶ log_warn("Unknown switch $unknown") if defined $unknown;
Branch: 1 branch
BranchTrueFalseTotal
if defined $unknown 0 132 132
576  
577 132 return;
578 }
579  
580 # Shared processing of switches: ordered, (preceding|following|first|last)_only
581 # for subs get_children, get_descendants and get_siblings.
582 # This is quite an uneffective implementation in case of e.g. first_only
583 132 ▶sub _process_switches {
Subroutine _process_switches: called CC=10 SCAR=24.3
584 132 my $self = shift;
585 132 my ( $arg_ref, @nodes ) = pos_validated_list(
586 \@_,
587 { isa => 'Maybe[HashRef]' },
588 MX_PARAMS_VALIDATE_ALLOW_EXTRA => 1,
589 );
590  
591 # Check for unknown switches and not allowed combinations
592 132 $self->_check_switches($arg_ref);
593  
594 # Add this node if add_self
595 132 ▶ if ( $arg_ref->{add_self} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_ref->{"add_self"}) 92 40 132
596 92 push @nodes, $self;
597 }
598  
599 # Sort nodes if needed
600 132 ▶ 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) 52 80 132
Condition88%8/9
$arg_ref->{"ordered"} || &any(sub { $$arg_ref{$_ . '_only'}; } , "first", "last", "preceding", "following") and @nodes and defined $nodes[0]->ord
!ll&&!rl&&r
552552
$arg_ref->{"ordered"} || &any(sub { $$arg_ref{$_ . '_only'}; } , "first", "last", "preceding", "following") and @nodes
!ll&&!rl&&r
52377
$arg_ref->{"ordered"} or &any(sub { $$arg_ref{$_ . '_only'}; } , "first", "last", "preceding", "following")
l!l&&r!l&&!r
80052
MC/DC75%3/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 table57%4/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
601 && @nodes && defined $nodes[0]->ord
602 )
603 {
604 52 @nodes = sort { $a->ord() <=> $b->ord() } @nodes;
605 }
606  
607 # Leave preceding/following only if needed
608 132 ▶ if ( $arg_ref->{preceding_only} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($arg_ref->{'preceding_only'}) { } 0 132 132
elsif ($arg_ref->{'following_only'}) { } 0 132 132
609 0 @nodes = grep { $_->ord() <= $self->ord } @nodes;
610 }
611 elsif ( $arg_ref->{following_only} ) {
612 0 @nodes = grep { $_->ord() >= $self->ord } @nodes;
613 }
614  
615 # first_only / last_only
616 132 ▶ return $nodes[0] if $arg_ref->{first_only};
Branch: 1 branch
BranchTrueFalseTotal
if $arg_ref->{"first_only"} 0 132 132
617 132 ▶ return $nodes[-1] if $arg_ref->{last_only};
Branch: 1 branch
BranchTrueFalseTotal
if $arg_ref->{"last_only"} 0 132 132
618 132 return @nodes;
619 }
620  
621 172 ▶sub get_children {
Subroutine get_children: called CC=2 SCAR=6.9
Pod documented
622 172 my $self = shift;
623 172 my ($arg_ref) = pos_validated_list(
624 \@_,
625 { isa => 'Maybe[HashRef]', optional => 1 },
626 );
627  
628 172 my @children = $self->children();
629 172 ▶ return @children if !$arg_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $arg_ref 171 1 172
630 1 return $self->_process_switches( $arg_ref, @children );
631 }
632  
633 178 ▶sub get_descendants {
Subroutine get_descendants: called CC=5 SCAR=18.9
Pod documented
634 178 my $self = shift;
635 178 my ($arg_ref) = pos_validated_list(
636 \@_,
637 { isa => 'Maybe[HashRef]', optional => 1 },
638 );
639  
640 178 my @descendants;
641 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
642 0 my $except_node = delete $arg_ref->{except};
643 0 ▶ return () if $self == $except_node;
Branch: 1 branch
BranchTrueFalseTotal
if $self == $except_node 0 0 0
644 @descendants = map {
645 0 $_->get_descendants( { except => $except_node, add_self => 1 } )
646 } $self->get_children();
647 }
648 else {
649 178 @descendants = $self->descendants();
650 }
651 178 ▶ return @descendants if !$arg_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $arg_ref 72 106 178
652 106 return $self->_process_switches( $arg_ref, @descendants );
653 }
654  
655 16 ▶sub get_siblings {
Subroutine get_siblings: called CC=3 SCAR=11.1
Pod documented
656 16 my $self = shift;
657 16 my ($arg_ref) = pos_validated_list(
658 \@_,
659 { isa => 'Maybe[HashRef]', optional => 1 },
660 );
661 16 my $parent = $self->get_parent();
662 16 ▶ return () if !$parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 4 12 16
663 12 my @siblings = grep { $_ ne $self } $parent->get_children();
664 12 ▶ return @siblings if !$arg_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $arg_ref 12 0 12
665 0 return $self->_process_switches( $arg_ref, @siblings );
666 }
667  
668 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
669 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
670  
671 215 ▶sub is_descendant_of {
Subroutine is_descendant_of: called CC=3 SCAR=11.0
Pod documented
672 215 my $self = shift;
673 215 my ($another_node) = pos_validated_list(
674 \@_,
675 { isa => 'Treex::Core::Node' },
676 );
677  
678 215 my $parent = $self->get_parent();
679 215 ▶ while ($parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent) 212 211 423
680 212 ▶ return 1 if $parent == $another_node;
Branch: 1 branch
BranchTrueFalseTotal
if $parent == $another_node 4 208 212
681 208 $parent = $parent->get_parent();
682 }
683 211 return 0;
684 }
685 0 ▶sub dominates {
Subroutine dominates: not called CC=1 SCAR=6.9
Pod undocumented
686 0 my $self = shift;
687 0 my $another_node = shift;
688 0 return $another_node->is_descendant_of($self);
689 }
690  
691 #************************************
692 #---- OTHER ------
693  
694 12 ▶sub get_depth {
Subroutine get_depth: called CC=3 SCAR=11.0
Pod documented
695 12 ▶ log_fatal 'Incorrect number of arguments' if @_ != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 12 12
696 12 my $self = shift;
697 12 my $depth = 0;
698 12 ▶ while ( $self = $self->get_parent() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self = $self->get_parent) 12 12 24
699 12 $depth++;
700 }
701 12 return $depth;
702 }
703  
704 # This is called from $node->remove()
705 # so it must be defined in this class,
706 # but it is overriden in Treex::Core::Node::Ordered.
707 16 ▶sub _normalize_node_ordering {
Subroutine _normalize_node_ordering: called CC=1 SCAR=0.0
708 }
709  
710 0 ▶sub get_address {
Subroutine get_address: not called CC=3 SCAR=24.8
Pod documented
711 0 ▶ log_fatal 'Incorrect number of arguments' if @_ != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 0 0
712 0 my $self = shift;
713 0 my $id = $self->id;
714 0 my $bundle = $self->get_bundle();
715 0 my $doc = $bundle->get_document();
716 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
717 0 my $position = $bundle->get_position() + 1;
718  
719 #my $filename = Cwd::abs_path($file);
720 0 return "$file##$position.$id";
721 }
722  
723 # The MISC attributes from CoNLL-U files are stored as wild attributes.
724 # These methods should be in a Universal Dependencies related role but we don't have one.
725 # get_misc() returns a list of MISC attributes (possibly empty list)
726 sub get_misc
727 0 ▶{
Subroutine get_misc: not called CC=3 SCAR=24.8
Pod undocumented
728 0 my $self = shift;
729 0 my @misc;
730 0 my $wild = $self->wild();
731 0 ▶ if (exists($wild->{misc}) && defined($wild->{misc}))
Branch: 1 branch
BranchTrueFalseTotal
if (exists $wild->{"misc"} and defined $wild->{"misc"}) 0 0 0
Condition0%0/3
exists $wild->{"misc"} and defined $wild->{"misc"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $wild->{"misc"} and defined $wild->{"misc"}
exists $wild->{"misc"} defined $wild->{"misc"}
Truth table0%0/3
A and B
A= exists $wild->{"misc"}B= defined $wild->{"misc"}
ABresult
0X0
100
111
732 {
733 0 @misc = split(/\|/, $wild->{misc});
734 }
735 0 return @misc;
736 }
737  
738 # set_misc() takes a list of MISC attributes (possibly empty list)
739 sub set_misc
740 0 ▶{
Subroutine set_misc: not called CC=2 SCAR=17.9
Pod undocumented
741 0 my $self = shift;
742 0 my @misc = @_;
743 0 my $wild = $self->wild();
744 0 ▶ if (scalar(@misc) > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @misc > 0) { } 0 0 0
745 {
746 0 $wild->{misc} = join('|', @misc);
747 }
748 else
749 {
750 0 delete($wild->{misc});
751 }
752 }
753  
754 # set_misc_attr() takes an attribute name and value; assumes that MISC elements are attr=value pairs; replaces first or pushes at the end
755 sub set_misc_attr
756 0 ▶{
Subroutine set_misc_attr: not called CC=8 SCAR=42.8
Pod undocumented
757 0 my $self = shift;
758 0 my $attr = shift;
759 0 my $value = shift;
760 0 ▶ if (defined($attr) && defined($value))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $attr and defined $value) 0 0 0
Condition0%0/3
defined $attr and defined $value
!ll&&!rl&&r
000
MC/DC0%0/2
defined $attr and defined $value
defined $attr defined $value
Truth table0%0/3
A and B
A= defined $attrB= defined $value
ABresult
0X0
100
111
761 {
762 0 my @misc = $self->get_misc();
763 0 my $found = 0;
764 0 ▶ for(my $i = 0; $i <= $#misc; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#misc) 0 0 0
765 {
766 0 ▶ if ($misc[$i] =~ m/^(.+?)=(.+)$/ && $1 eq $attr)
Branch: 1 branch
BranchTrueFalseTotal
if ($misc[$i] =~ /^(.+?)=(.+)$/ and $1 eq $attr) 0 0 0
Condition0%0/3
$misc[$i] =~ /^(.+?)=(.+)$/ and $1 eq $attr
!ll&&!rl&&r
000
MC/DC0%0/2
$misc[$i] =~ /^(.+?)=(.+)$/ and $1 eq $attr
$misc[$i] =~ /^(.+?)=(.+)$/ $1 eq $attr
Truth table0%0/3
A and B
A= $misc[$i] =~ /^(.+?)=(.+)$/B= $1 eq $attr
ABresult
0X0
100
111
767 {
768 0 ▶ if ($found)
Branch: 1 branch
BranchTrueFalseTotal
if ($found) { } 0 0 0
769 {
770 0 splice(@misc, $i--, 1);
771 }
772 else
773 {
774 0 $misc[$i] = "$attr=$value";
775 0 $found = 1;
776 }
777 }
778 }
779 0 ▶ if (!$found)
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 0 0 0
780 {
781 0 push(@misc, "$attr=$value");
782 }
783 0 $self->set_misc(@misc);
784 }
785 }
786  
787 # Empty DESTROY method is a hack to get rid of the "Deep recursion warning"
788 # in Treex::PML::Node::DESTROY and MooseX::NonMoose::Meta::Role::Class::_check_superclass_destructor.
789 # Without this hack, you get the warning after creating a node with 99 or more children.
790 # Deep recursion on subroutine "Class::MOP::Method::execute" at .../5.12.2/MooseX/NonMoose/Meta/Role/Class.pm line 183.
791 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=0.0
792 }
793  
794 #*************************************
795 #---- DEPRECATED & QUESTIONABLE ------
796  
797 183 ▶sub generate_new_id { #TODO move to Core::Document?
Subroutine generate_new_id: called CC=4 SCAR=14.2
Pod documented
798 183 ▶ log_fatal 'Incorrect number of arguments' if @_ != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 183 183
799 183 my $self = shift;
800 183 my $doc = $self->get_document;
801  
802 183 my $latest_node_number = $doc->_latest_node_number;
803  
804 183 my $new_id;
805  
806 #$self->get_root->id =~ /(.+)root/;
807 #my $id_base = $1 || "";
808 my $id_base;
809 183 ▶ if ( $self->get_root->id =~ /(.+)root/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_root->id =~ /(.+)root/) { } 183 0 183
810 183 $id_base = $1;
811 }
812 else {
813 0 $id_base = q();
814 }
815  
816 183 while (1) {
817 183 $latest_node_number++;
818 183 $new_id = "${id_base}n$latest_node_number";
819 183 ▶ last if !$doc->id_is_indexed($new_id);
Branch: 1 branch
BranchTrueFalseTotal
unless $doc->id_is_indexed($new_id) 183 0 183
820  
821 }
822  
823 183 $doc->_set_latest_node_number($latest_node_number);
824  
825 183 return $new_id;
826 }
827  
828 0 ▶sub add_to_listattr {
Subroutine add_to_listattr: not called CC=4 SCAR=30.0
Pod documented
829 0 my $self = shift;
830 0 my ( $attr_name, $attr_value ) = pos_validated_list(
831 \@_,
832 { isa => 'Str' },
833 { isa => 'Any' },
834 );
835  
836 0 my $list = $self->attr($attr_name);
837 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
838 if !defined $list || ref($list) ne 'Treex::PML::List';
839 0 my @new_list = @{$list};
840 0 ▶ if ( ref($attr_value) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attr_value eq 'ARRAY') { } 0 0 0
841 0 push @new_list, @{$attr_value};
842 }
843 else {
844 0 push @new_list, $attr_value;
845 }
846 0 return $self->set_attr( $attr_name, Treex::PML::List->new(@new_list) );
847 }
848  
849 # Get more attributes at once
850 0 ▶sub get_attrs {
Subroutine get_attrs: not called CC=3 SCAR=24.8
Pod documented
851 0 my $self = shift;
852 0 my @attr_names = pos_validated_list(
853 \@_,
854 { isa => 'Any' }, #at least one parameter
855 MX_PARAMS_VALIDATE_ALLOW_EXTRA => 1,
856 );
857  
858 0 my @attr_values;
859 0 ▶ if ( ref $attr_names[-1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attr_names[-1]) { } 0 0 0
860 0 my $arg_ref = pop @attr_names;
861 0 my $change_undefs_to = $arg_ref->{undefs};
862 @attr_values = map {
863 0 ▶ defined $self->get_attr($_) ? $self->get_attr($_) : $change_undefs_to
Branch: 1 branch
BranchTrueFalseTotal
defined $self->get_attr($_) ? : 0 0 0
864 } @attr_names;
865 }
866 else {
867 0 @attr_values = map { $self->get_attr($_) } @attr_names;
868 }
869  
870 0 return @attr_values;
871 }
872  
873 # Return all attributes of the given node (sub)type that contain references
874 134 ▶sub _get_reference_attrs {
Subroutine _get_reference_attrs: called CC=1 SCAR=0.0
875 134 my ($self) = @_;
876 134 return ();
877 }
878  
879 # Return IDs of all nodes to which there are reference links from this node (must be overridden in
880 # the respective node types)
881 372 ▶sub _get_referenced_ids {
Subroutine _get_referenced_ids: called CC=5 SCAR=16.3
882 372 my ($self) = @_;
883 372 my $ret = {};
884  
885 # handle alignment separately
886 372 my $links_rf = $self->get_attr('alignment');
887 0 ▶ $ret->{alignment} = [ map { $_->{'counterpart.rf'} } @{$links_rf} ] if ($links_rf);
Branch: 1 branch
BranchTrueFalseTotal
if $links_rf 0 372 372
888  
889 # all other references
890 372 foreach my $ref_attr ( $self->_get_reference_attrs() ) {
891 868 ▶ my $val = $self->get_attr($ref_attr) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $val = $self->get_attr($ref_attr) or next 5 863 868
892 5 ▶ if ( !ref $val ) { # single-valued
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $val) { } 3 2 5
893 3 $ret->{$ref_attr} = [$val];
894 }
895 else {
896 2 $ret->{$ref_attr} = $val;
897 }
898 }
899 372 return $ret;
900 }
901  
902  
903 # ---------------------
904  
905 # changing the functionality of Treex::PML::Node's following() so that it traverses all
906 # nodes in all trees in all zones (needed for search in TrEd)
907  
908 298 ▶sub following {
Subroutine following: called CC=7 SCAR=19.5
Pod documented
909 298 my ( $self ) = @_;
910  
911 298 my $pml_following = Treex::PML::Node::following(@_);
912  
913 298 ▶ if ( $pml_following ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pml_following) { } 173 125 298
914 173 return $pml_following;
915 }
916  
917 else {
918 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
919  
920 my @all_trees = map {
921 125 ▶ ref($_) ne 'Treex::PML::Struct'
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ne 'Treex::PML::Struct' ? : 410 0 410
922 ? $_->get_all_trees
923 : ()
924 } $bundle->get_all_zones;
925  
926 125 ▶ if ( ref($self) eq 'Treex::Core::Bundle' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self eq 'Treex::Core::Bundle') { } 40 85 125
927 40 return $all_trees[0];
928 }
929  
930 else {
931 85 my $my_root = $self->get_root;
932 85 foreach my $index ( 0..$#all_trees ) {
933 443 ▶ if ( $all_trees[$index] eq $my_root ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($all_trees[$index] eq $my_root) 85 358 443
934 85 return $all_trees[$index+1];
935 }
936 }
937 0 log_fatal "Node belongs to no tree: this should never happen";
938 }
939 }
940 }
941  
942 # This is copied from Treex::PML::Node.
943 # Using Treex::PML::Node::following is faster than recursion
944 # and it does not cause "deep recursion" warnings.
945 204 ▶sub descendants {
Subroutine descendants: called CC=2 SCAR=6.9
Pod documented
946 204 my $self = $_[0];
947 204 my @kin = ();
948 204 my $desc = $self->Treex::PML::Node::following($self);
949 204 ▶ while ($desc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($desc) 515 204 719
950 515 push @kin, $desc;
951 515 $desc = $desc->Treex::PML::Node::following($self);
952 }
953 204 return @kin;
954 }
955  
956 # TODO: How to do this in an elegant way?
957 # Unless we find a better way, we must disable two perlcritics
958 package Treex::Core::Node::Removed; ## no critic (ProhibitMultiplePackages)
959 47 $Treex::Core::Node::Removed::VERSION = '2.20210102';
960 47 ▶use Treex::Core::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
961  
962 3 ▶sub AUTOLOAD { ## no critic (ProhibitAutoloading)
Subroutine AUTOLOAD: called CC=2 SCAR=8.1
963 3 our $AUTOLOAD;
964 3 ▶ if ( $AUTOLOAD !~ /DESTROY$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $Treex::Core::Node::Removed::AUTOLOAD =~ /DESTROY$/) 0 3 3
965 0 log_fatal("You cannot call any methods on removed nodes, but have called $AUTOLOAD");
966 }
967 }
968  
969 package Treex::Core::Node; ## no critic (ProhibitMultiplePackages)
970  
971 47 __PACKAGE__->meta->make_immutable;
972  
973 47 1;
974  
975 __END__
« Treex/Core/DocumentReader.pm ↑ Summary Treex/Block/Read/BaseReader.pm »