Treex/Core/Node/Ordered.pm

filter: Statementstmt 43.2% 93 / 215 Branchbran 25.8% 30 / 116 Conditioncond 15.1% 5 / 33 MC/DCmcdc 10.0% 2 / 20 Subroutinesub 57.1% 12 / 21 Podpod 75.0% 9 / 12 totaltotal 36.2% 151 / 417 CC 81 SCAR 46.3
CC
81
Cov
35%
get_gap
420.0
get_next_node
56.0
get_prev_node
56.0
CRAP 102.5
« Treex/Block/Util/Find.pm ↑ Summary Treex/Core/TredView/TreeLayout.pm »
1 package Treex::Core::Node::Ordered;
2 38 $Treex::Core::Node::Ordered::VERSION = '2.20210102';
3 38 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # with Moose >= 2.00, this must be present also in roles
6 38 ▶use MooseX::SemiAffordanceAccessor;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 38 ▶use Treex::Core::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 38 ▶use List::Util qw(first); # TODO: this wouldn't be needed if there was Treex::Core::Common for roles
Subroutine BEGIN: called CC=1 SCAR=0.0
9 38 ▶use Treex::Core::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 38 has ord => (
12 is => 'ro',
13 isa => 'Treex::Type::NonNegativeInt',
14 writer => '_set_ord',
15 reader => 'ord',
16 );
17  
18 2 ▶sub precedes {
Subroutine precedes: called CC=2 SCAR=7.1
Pod documented
19 2 ▶ log_fatal 'Incorrect number of arguments' if @_ != 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 2 2
20 2 my ( $self, $another_node ) = @_;
21 2 return $self->ord() < $another_node->ord();
22 }
23 0 ▶sub follows {
Subroutine follows: not called CC=2 SCAR=17.9
Pod undocumented
24 0 ▶ log_fatal 'Incorrect number of arguments' if @_ != 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 0 0
25 0 my ( $self, $another_node ) = @_;
26 0 return $self->ord() > $another_node->ord();
27 }
28  
29 # Methods get_next_node and get_prev_node are implemented
30 # so they can handle deprecated fractional ords.
31 # When no "fract-ords" will be used in the whole TectoMT nor Treex
32 # this could be reimplemented a bit more effectively.
33 # TODO
34 0 ▶sub get_next_node {
Subroutine get_next_node: not called CC=7 SCAR=40.3
Pod documented
35 0 ▶ log_fatal 'Incorrect number of arguments' if @_ != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 0 0
36 0 my $self = shift;
37 0 my $my_ord = $self->ord();
38 0 ▶ log_fatal('Undefined ordering value') if !defined $my_ord;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $my_ord 0 0 0
39  
40 # Find closest higher ord
41 0 my ( $next_node, $next_ord ) = ( undef, undef );
42 0 foreach my $node ( $self->get_root()->get_descendants() ) {
43 0 my $ord = $node->ord();
44 0 ▶ next if $ord <= $my_ord;
Branch: 1 branch
BranchTrueFalseTotal
if $ord <= $my_ord 0 0 0
45 0 ▶ next if defined $next_ord && $ord > $next_ord;
Branch: 1 branch
BranchTrueFalseTotal
if defined $next_ord and $ord > $next_ord 0 0 0
Condition0%0/3
defined $next_ord and $ord > $next_ord
!ll&&!rl&&r
000
MC/DC0%0/2
defined $next_ord and $ord > $next_ord
defined $next_ord $ord > $next_ord
Truth table0%0/3
A and B
A= defined $next_ordB= $ord > $next_ord
ABresult
0X0
100
111
46 0 ( $next_node, $next_ord ) = ( $node, $ord );
47 }
48 0 return $next_node;
49 }
50  
51 0 ▶sub get_prev_node {
Subroutine get_prev_node: not called CC=7 SCAR=40.3
Pod documented
52 0 ▶ log_fatal 'Incorrect number of arguments' if @_ != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 0 0
53 0 my $self = shift;
54 0 my $my_ord = $self->ord();
55 0 ▶ log_fatal('Undefined ordering value') if !defined $my_ord;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $my_ord 0 0 0
56  
57 # Find closest lower ord
58 0 my ( $prev_node, $prev_ord ) = ( undef, undef );
59 0 foreach my $node ( $self->get_root()->get_descendants() ) {
60 0 my $ord = $node->ord();
61 0 ▶ next if $ord >= $my_ord;
Branch: 1 branch
BranchTrueFalseTotal
if $ord >= $my_ord 0 0 0
62 0 ▶ next if defined $prev_ord && $ord < $prev_ord;
Branch: 1 branch
BranchTrueFalseTotal
if defined $prev_ord and $ord < $prev_ord 0 0 0
Condition0%0/3
defined $prev_ord and $ord < $prev_ord
!ll&&!rl&&r
000
MC/DC0%0/2
defined $prev_ord and $ord < $prev_ord
defined $prev_ord $ord < $prev_ord
Truth table0%0/3
A and B
A= defined $prev_ordB= $ord < $prev_ord
ABresult
0X0
100
111
63 0 ( $prev_node, $prev_ord ) = ( $node, $ord );
64 }
65 0 return $prev_node;
66 }
67  
68 4 ▶sub get_nodes_between {
Subroutine get_nodes_between: called CC=4 SCAR=13.9
Pod documented
69 4 ▶ log_fatal 'Incorrect number of arguments' if @_ != 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 4 4
70 4 my ($self, $other) = @_;
71  
72 # want $self preceding $other
73 4 ▶ if ( $self->ord > $other->ord ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ord > $other->ord) 2 2 4
74 2 my $temp = $other;
75 2 $other = $self;
76 2 $self = $temp;
77 }
78  
79 4 my @all_nodes = $self->get_root->get_descendants({ordered => 1});
80 my @nodes_between = grep {
81 4 ▶ $_->ord > $self->ord && $_->ord < $other->ord
Condition100%3/3
$_->ord > $self->ord && $_->ord < $other->ord
!ll&&!rl&&r
8816
MC/DC100%2/2
$_->ord > $self->ord && $_->ord < $other->ord
$_->ord > $self->ord $_->ord < $other->ord
Truth table100%3/3
A && B
A= $_->ord > $self->ordB= $_->ord < $other->ord
ABresult
0X0
100
111
82 } @all_nodes;
83  
84 4 return @nodes_between;
85 }
86  
87 10 ▶sub _normalize_node_ordering {
Subroutine _normalize_node_ordering: called CC=4 SCAR=14.0
88 10 ▶ log_fatal 'Incorrect number of arguments' if @_ != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 10 10
89 10 my $self = shift;
90 10 ▶ log_fatal('Ordering normalization can be applied only on root nodes!') if $self->get_parent();
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_parent 0 10 10
91 # Do not use add_self => 1. Unshift myself as the first element instead.
92 # Otherwise normalization will not work as expected if the current ord of the root is nonzero and/or a non-root node has zero.
93 10 my @nodes = $self->get_descendants( { ordered => 1 } );
94 10 unshift(@nodes, $self);
95 10 my $new_ord = 0;
96 10 foreach my $node (@nodes) {
97 48 $node->_set_ord($new_ord);
98 48 $new_ord++
99 }
100 10 return;
101 }
102  
103 22 ▶sub _check_shifting_method_args {
Subroutine _check_shifting_method_args: called CC=10 SCAR=26.7
104 22 my ( $self, $reference_node, $arg_ref ) = @_;
105 22 my @c = caller 1;
106 22 my $stack = "$c[3] called from $c[1], line $c[2]";
107 22 ▶ log_fatal( 'Incorrect number of arguments for ' . $stack ) if @_ < 2 || @_ > 3;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 2 or @_ > 3 0 22 22
Condition33%1/3
@_ < 2 or @_ > 3
l!l&&r!l&&!r
0022
MC/DC0%0/2
@_ < 2 or @_ > 3
@_ < 2 @_ > 3
Truth table33%1/3
A or B
A= @_ < 2B= @_ > 3
ABresult
1X1
011
000
108 22 ▶ log_fatal( 'Undefined reference node for ' . $stack ) if !$reference_node;
Branch: 1 branch
BranchTrueFalseTotal
unless $reference_node 0 22 22
109 22 ▶ log_fatal( 'Reference node must be from the same tree. In ' . $stack )
Branch: 1 branch
BranchTrueFalseTotal
if $reference_node->get_root != $self->get_root 0 22 22
110 if $reference_node->get_root() != $self->get_root();
111  
112 log_fatal '$reference_node is a descendant of $self.'
113 . ' Maybe you have forgotten {without_children=>1}. ' . "\n" . $stack
114 22 ▶ if !$arg_ref->{without_children} && $reference_node->is_descendant_of($self);
Branch: 1 branch
BranchTrueFalseTotal
if not $arg_ref->{"without_children"} and $reference_node->is_descendant_of($self) 0 22 22
Condition33%1/3
not $arg_ref->{"without_children"} and $reference_node->is_descendant_of($self)
!ll&&!rl&&r
0220
MC/DC0%0/2
not $arg_ref->{"without_children"} and $reference_node->is_descendant_of($self)
not $arg_ref->{"without_children"} $reference_node->is_descendant_of($self)
Truth table33%1/3
not(A) and B
A= not $arg_ref->{"without_children"}B= $reference_node->is_descendant_of($self)
ABresult
0X0
100
111
115  
116 22 ▶ return if !defined $arg_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg_ref 0 22 22
117  
118 22 ▶ log_fatal(
Branch: 1 branch
BranchTrueFalseTotal
if ref $arg_ref ne "HASH" 0 22 22
119 'Second argument for shifting methods can be only options hash reference. In ' . $stack
120 ) if ref $arg_ref ne 'HASH';
121 0 ▶ my $unknown = first { $_ ne 'without_children' } keys %{$arg_ref};
Subroutine __ANON__: not called CC=1 SCAR=0.4
122 22 ▶ log_warn("Unknown switch '$unknown' for $stack") if defined $unknown;
Branch: 1 branch
BranchTrueFalseTotal
if defined $unknown 0 22 22
123 22 return;
124 }
125  
126 16 ▶sub shift_after_node {
Subroutine shift_after_node: called CC=3 SCAR=11.3
Pod documented
127 16 my ( $self, $reference_node, $arg_ref ) = @_;
128 16 ▶ return if $self == $reference_node;
Branch: 1 branch
BranchTrueFalseTotal
if $self == $reference_node 0 16 16
129 16 _check_shifting_method_args(@_);
130 16 ▶ return $self->_shift_to_node( $reference_node, 1, $arg_ref->{without_children} ) if $arg_ref;
Branch: 1 branch
BranchTrueFalseTotal
if $arg_ref 0 16 16
131 16 return $self->_shift_to_node( $reference_node, 1, 0 );
132 }
133  
134 6 ▶sub shift_before_node {
Subroutine shift_before_node: called CC=3 SCAR=11.3
Pod documented
135 6 my ( $self, $reference_node, $arg_ref ) = @_;
136 6 ▶ return if $self == $reference_node;
Branch: 1 branch
BranchTrueFalseTotal
if $self == $reference_node 0 6 6
137 6 _check_shifting_method_args(@_);
138 6 ▶ return $self->_shift_to_node( $reference_node, 0, $arg_ref->{without_children} ) if $arg_ref;
Branch: 1 branch
BranchTrueFalseTotal
if $arg_ref 0 6 6
139 6 return $self->_shift_to_node( $reference_node, 0, 0 );
140 }
141  
142 0 ▶sub shift_after_subtree {
Subroutine shift_after_subtree: not called CC=4 SCAR=30.0
Pod documented
143 0 my ( $self, $reference_node, $arg_ref ) = @_;
144 0 _check_shifting_method_args(@_);
145  
146 0 my $last_node;
147 0 ▶ if ( $arg_ref->{without_children} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_ref->{'without_children'}) { } 0 0 0
148 0 ($last_node) = reverse grep { $_ != $self } $reference_node->get_descendants( { ordered => 1, add_self => 1 } );
149 }
150 else {
151 0 $last_node = $reference_node->get_descendants( { except => $self, last_only => 1, add_self => 1 } );
152 }
153 0 ▶ return if !defined $last_node;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $last_node 0 0 0
154 0 ▶ return $self->_shift_to_node( $last_node, 1, $arg_ref->{without_children} ) if $arg_ref;
Branch: 1 branch
BranchTrueFalseTotal
if $arg_ref 0 0 0
155 0 return $self->_shift_to_node( $last_node, 1, 0 );
156 }
157  
158 0 ▶sub shift_before_subtree {
Subroutine shift_before_subtree: not called CC=4 SCAR=30.0
Pod documented
159 0 my ( $self, $reference_node, $arg_ref ) = @_;
160 0 _check_shifting_method_args(@_);
161  
162 0 my $first_node;
163 0 ▶ if ( $arg_ref->{without_children} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_ref->{'without_children'}) { } 0 0 0
164 0 ($first_node) = grep { $_ != $self } $reference_node->get_descendants( { ordered => 1, add_self => 1 } );
165 }
166 else {
167 0 $first_node = $reference_node->get_descendants( { except => $self, first_only => 1, add_self => 1 } );
168 }
169 0 ▶ return if !defined $first_node;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $first_node 0 0 0
170 0 ▶ return $self->_shift_to_node( $first_node, 0, $arg_ref->{without_children} ) if $arg_ref;
Branch: 1 branch
BranchTrueFalseTotal
if $arg_ref 0 0 0
171 0 return $self->_shift_to_node( $first_node, 0, 0 );
172 }
173  
174 # This method does the real work for all shift_* methods.
175 # However, due to unfriendly name and arguments it's not public.
176 22 ▶sub _shift_to_node {
Subroutine _shift_to_node: called CC=12 SCAR=24.9
177 22 my ( $self, $reference_node, $after, $without_children ) = @_;
178 22 my @all_nodes = $self->get_root()->get_descendants();
179  
180 # Make sure that ord of all nodes is defined
181 #my $maximal_ord = @all_nodes; -this does not work, since there may be gaps in ordering
182 22 my $maximal_ord = 10000;
183 22 foreach my $d (@all_nodes) {
184 146 ▶ if ( !defined $d->ord() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $d->ord) 22 124 146
185 22 $d->_set_ord( $maximal_ord++ );
186 }
187 }
188  
189 # Which nodes are to be moved?
190 # $self only (the {without_children=>1} switch)
191 # or $self and all its descendants (the default)?
192 22 my @nodes_to_move;
193 22 ▶ if ($without_children) {
Branch: 1 branch
BranchTrueFalseTotal
if ($without_children) { } 0 22 22
194 0 @nodes_to_move = ($self);
195 }
196 else {
197 22 @nodes_to_move = $self->get_descendants( { ordered => 1, add_self => 1 } );
198 }
199  
200 # Let's make a hash, so we can easily recognize which nodes are to be moved.
201 22 my %is_moving = map { $_ => 1 } @nodes_to_move;
202  
203 # Recompute ord of all nodes.
204 # The technical root has ord=0 and the first node will have ord=1.
205 22 my $counter = 1;
206 22 my $nodes_moved = 0;
207 22 @all_nodes = sort { $a->ord() <=> $b->ord() } @all_nodes;
208 22 foreach my $node (@all_nodes) {
209  
210 # We skip nodes that are being moved.
211 # Their ord is recomuted elsewhere (look 8 lines down).
212 146 ▶ next if $is_moving{$node};
Branch: 1 branch
BranchTrueFalseTotal
if $is_moving{$node} 22 124 146
213  
214 # If moving "after" a reference node
215 # then ord of the $node can be recomputed now
216 # even if it is actually the $reference_node.
217 124 ▶ if ($after) {
Branch: 1 branch
BranchTrueFalseTotal
if ($after) 88 36 124
218 88 $node->_set_ord( $counter++ );
219 }
220  
221 # Now we insert (i.e. recompute ord of) all nodes which are being moved.
222 # The nodes are inserted in the original order.
223 124 ▶ if ( $node == $reference_node ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node == $reference_node) 16 108 124
224 16 foreach my $moving_node (@nodes_to_move) {
225 16 $moving_node->_set_ord( $counter++ );
226 }
227 16 $nodes_moved = 1;
228 }
229  
230 # If moving "before" a node, then now it is the right moment
231 # for recomputing ord of the $node
232 124 ▶ if ( !$after ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($after) 36 88 124
233 36 $node->_set_ord( $counter++ );
234 }
235 }
236  
237 # If $is_moving{$reference_node}, e.g. when there is just one node in the tree,
238 # we need to do the reordering now (otherwise the ord would be still 10000).
239 22 ▶ if ( !$nodes_moved ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($nodes_moved) 6 16 22
240 6 foreach my $moving_node (@nodes_to_move) {
241 6 $moving_node->_set_ord( $counter++ );
242 }
243 }
244 22 return;
245 }
246  
247 ###!!! DZ: reverting to 7183, 2011-11-03.
248 # This function by Martin is faster than my own below but it does not always return correct results.
249 # The key problem could be that it relies on ord values forming a contiguous sequence which might not always be true.
250 # Go to devel/hamledt/nonprojectivity and test that make yields the same numbers for the old and the new method.
251 0 ▶sub is_nonprojective_does_not_work {
Subroutine is_nonprojective_does_not_work: not called CC=6 SCAR=37.4
Pod undocumented
252 0 my ($self) = @_;
253  
254 # Root and its children are always projective
255 0 my $parent = $self->get_parent();
256 0 ▶ return 0 if !$parent || $parent->is_root();
Branch: 1 branch
BranchTrueFalseTotal
if not $parent or $parent->is_root 0 0 0
Condition0%0/3
not $parent or $parent->is_root
l!l&&r!l&&!r
000
MC/DC0%0/2
not $parent or $parent->is_root
not $parent $parent->is_root
Truth table0%0/3
not(A) or B
A= not $parentB= $parent->is_root
ABresult
1X1
011
000
257  
258 # Edges between neighbouring nodes are always projective.
259 # Check it now to make it a bit faster.
260 0 my ( $ordA, $ordB ) = ( $self->ord, $parent->ord );
261 0 ▶ if ( $ordA > $ordB ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ordA > $ordB) 0 0 0
262 0 ( $ordA, $ordB ) = ( $ordB, $ordA );
263 }
264 0 my $distance = $ordB - $ordA;
265 0 ▶ return 0 if $distance == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $distance == 1 0 0 0
266  
267 # Get all the descendants of $parent that are in the span of the edge.
268 0 ▶ my @span = grep { $_->ord > $ordA && $_->ord < $ordB } $parent->get_descendants();
Condition0%0/3
$_->ord > $ordA && $_->ord < $ordB
!ll&&!rl&&r
000
MC/DC0%0/2
$_->ord > $ordA && $_->ord < $ordB
$_->ord > $ordA $_->ord < $ordB
Truth table0%0/3
A && B
A= $_->ord > $ordAB= $_->ord < $ordB
ABresult
0X0
100
111
269  
270 # For projective edges @span must include all the nodes between $parent and $self.
271 0 return @span != $distance - 1;
272 }
273  
274 #------------------------------------------------------------------------------
275 # Tells whether the node is attached to its parent nonprojectively, i.e. there
276 # is at least one node between this node and its parent that is not dominated
277 # by the parent.
278 #------------------------------------------------------------------------------
279 sub is_nonprojective
280 0 ▶{
Subroutine is_nonprojective: not called CC=7 SCAR=40.3
Pod documented
281 0 ▶ log_fatal('Incorrect number of arguments') if(scalar(@_)!=1);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ != 1 0 0 0
282 0 my $self = shift;
283 0 my $parent = $self->parent();
284 # A node that does not have a parent cannot be nonprojective.
285 0 ▶ return 0 if(!$parent);
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 0 0 0
286 # Get a hash of all descendants of the parent.
287 0 my @pdesc = $parent->get_descendants({add_self=>1});
288 0 my %pdesc; map {$pdesc{$_}++} (@pdesc);
289 # Figure out whether the node is to the left or to the right from its parent.
290 0 my $nord = $self->ord();
291 0 my $pord = $parent->ord();
292 0 my ($x, $y);
293 0 ▶ if($pord>$nord)
Branch: 1 branch
BranchTrueFalseTotal
if ($pord > $nord) { } 0 0 0
294 {
295 0 $x = $self;
296 0 $y = $parent;
297 }
298 else
299 {
300 0 $x = $parent;
301 0 $y = $self;
302 }
303 # Get the ordered list of all nodes between $x and $y.
304 0 my $xord = $x->ord();
305 0 my $yord = $y->ord();
306 0 ▶ my @between = grep {$_->ord()>$xord && $_->ord()<$yord} ($parent->root()->get_descendants({ordered=>1}));
Condition0%0/3
$_->ord > $xord && $_->ord < $yord
!ll&&!rl&&r
000
MC/DC0%0/2
$_->ord > $xord && $_->ord < $yord
$_->ord > $xord $_->ord < $yord
Truth table0%0/3
A && B
A= $_->ord > $xordB= $_->ord < $yord
ABresult
0X0
100
111
307 # This node is nonprojective if @between contains anything that is not in %pdesc.
308 0 foreach my $b (@between)
309 {
310 0 ▶ if(!$pdesc{$b})
Branch: 1 branch
BranchTrueFalseTotal
unless ($pdesc{$b}) 0 0 0
311 {
312 0 return 1;
313 }
314 }
315 0 return 0;
316 }
317  
318 #------------------------------------------------------------------------------
319 # If the node is in a gap that causes nonprojectivity of a dependency, this
320 # method returns the ordered list of all nodes in the same gap. (The gap is
321 # contiguous; there may be multiple gaps within one nonprojectivity.) If the
322 # node is member of multiple overlapping gaps (corresponding to different
323 # nonprojective edges going over the node), this method returns the smallest
324 # gap containing the node. If the node is not part of any gap, the method
325 # returns an empty list.
326 #------------------------------------------------------------------------------
327 sub get_gap
328 0 ▶{
Subroutine get_gap: not called CC=20 SCAR=60.4
Pod undocumented
329 0 ▶ log_fatal('Incorrect number of arguments') if(scalar(@_)!=1);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ != 1 0 0 0
330 0 my $self = shift;
331 # We need access to all nodes in the tree.
332 0 my $root = $self->get_root();
333 0 my @nodes = $root->get_descendants({'ordered' => 1});
334 # Normally the index of a node in @nodes will be the node's ord() - 1.
335 # But it is not guaranteed. We will thus work only with the indices of
336 # the elements of @nodes. To be able to ask a node about its index, we will
337 # temporarily store the indices as wild attributes.
338 0 ▶ for(my $i = 0; $i <= $#nodes; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#nodes) 0 0 0
339 {
340 0 $nodes[$i]->wild()->{i} = $i;
341 }
342 # Find all dependencies that go nonprojectively over this node.
343 0 my $is = $self->wild()->{i};
344 0 my @over;
345 0 foreach my $node (@nodes)
346 {
347 0 ▶ next if($node == $self);
Branch: 1 branch
BranchTrueFalseTotal
if $node == $self 0 0 0
348 0 my $parent = $node->parent();
349 # Nodes that depend directly on the root cannot be nonprojective.
350 0 ▶ next if($parent->is_root());
Branch: 1 branch
BranchTrueFalseTotal
if $parent->is_root 0 0 0
351 0 my $i = $node->wild()->{i};
352 0 my $j = $parent->wild()->{i};
353 0 ▶ if($i<$is && $j>$is || $j<$is && $i>$is)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $is and $j > $is or $j < $is and $i > $is) 0 0 0
Condition0%0/9
$i < $is and $j > $is or $j < $is and $i > $is
l!l&&r!l&&!r
000
$i < $is and $j > $is
!ll&&!rl&&r
000
$j < $is and $i > $is
!ll&&!rl&&r
000
MC/DC0%0/4
$i < $is and $j > $is or $j < $is and $i > $is
$i < $is $j > $is $j < $is $i > $is
Truth table0%0/7
A and B or C and D
A= $i < $isB= $j > $isC= $j < $isD= $i > $is
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
354 {
355 0 ▶ if(!$self->is_descendant_of($parent))
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_descendant_of($parent)) 0 0 0
356 {
357 0 ▶ my %record =
Branch: 2 branches
BranchTrueFalseTotal
$i < $j ? : 0 0 0
$j > $i ? : 0 0 0
358 (
359 'child' => $node,
360 'parent' => $parent,
361 'lord' => $i<$j ? $i : $j,
362 'rord' => $j>$i ? $j : $i
363 );
364 0 push(@over, \%record);
365 }
366 }
367 }
368 # For every nonprojectivity where this node is in the gap, get the nodes
369 # in the gap.
370 0 my @gaps;
371 0 foreach my $nprj (@over)
372 {
373 0 my @gap = ($self);
374 # Look for other gap members on the left.
375 0 ▶ for(my $i = $is-1; $i > $nprj->{lord}; $i--)
Branch: 1 branch
BranchTrueFalseTotal
if ($i > $nprj->{"lord"}) 0 0 0
376 {
377 0 ▶ if(!$nodes[$i]->is_descendant_of($nprj->{parent}))
Branch: 1 branch
BranchTrueFalseTotal
if (not $nodes[$i]->is_descendant_of($nprj->{'parent'})) { } 0 0 0
378 {
379 0 unshift(@gap, $nodes[$i]);
380 }
381 else
382 {
383 0 last;
384 }
385 }
386 # Look for other gap members on the right.
387 0 ▶ for(my $i = $is+1; $i < $nprj->{rord}; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $nprj->{"rord"}) 0 0 0
388 {
389 0 ▶ if(!$nodes[$i]->is_descendant_of($nprj->{parent}))
Branch: 1 branch
BranchTrueFalseTotal
if (not $nodes[$i]->is_descendant_of($nprj->{'parent'})) { } 0 0 0
390 {
391 0 push(@gap, $nodes[$i]);
392 }
393 else
394 {
395 0 last;
396 }
397 }
398 0 push(@gaps, \@gap);
399 }
400 # Clean up. Remove the temporary wild attributes.
401 0 foreach my $node (@nodes)
402 {
403 0 delete($node->wild()->{i});
404 }
405 0 ▶ if(scalar(@gaps)>=1)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @gaps >= 1) { } 0 0 0
406 {
407 # If there are multiple overlapping gaps, return the shortest one.
408 # If two gaps have the same shortest length, the choice is arbitrary.
409 0 @gaps = sort {scalar(@{$a}) <=> scalar(@{$b})} (@gaps);
410 0 return @{$gaps[0]};
411 }
412 else
413 {
414 0 return ();
415 }
416 }
417  
418 38 1;
419  
420 __END__
« Treex/Block/Util/Find.pm ↑ Summary Treex/Core/TredView/TreeLayout.pm »