| 1 | package Treex::Core::Node::Ordered; | ||||||||||||||||||||||||||||
| 2 | 38 | $Treex::Core::Node::Ordered::VERSION = '2.20151210'; | |||||||||||||||||||||||||||
| 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 | sub precedes { | ||||||||||||||||||||||||||||
| 19 | 2 | ▶ | log_fatal 'Incorrect number of arguments' if @_ != 2; | ||||||||||||||||||||||||||
|
Subroutine
precedes: called CC=2 SCAR=7.1
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | 2 | my ( $self, $another_node ) = @_; | |||||||||||||||||||||||||||
| 21 | 2 | return $self->ord() < $another_node->ord(); | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | sub follows { | ||||||||||||||||||||||||||||
| 24 | 0 | ▶ | log_fatal 'Incorrect number of arguments' if @_ != 2; | ||||||||||||||||||||||||||
|
Subroutine
follows: not called CC=2 SCAR=17.9
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | sub get_next_node { | ||||||||||||||||||||||||||||
| 35 | 0 | ▶ | log_fatal 'Incorrect number of arguments' if @_ != 1; | ||||||||||||||||||||||||||
|
Subroutine
get_next_node: not called CC=7 SCAR=40.3
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | next if defined $next_ord && $ord > $next_ord; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $next_ord and $ord > $next_ord
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
| |||||||||||||||||||||||||||||
| 46 | 0 | ( $next_node, $next_ord ) = ( $node, $ord ); | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | 0 | return $next_node; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | sub get_prev_node { | ||||||||||||||||||||||||||||
| 52 | 0 | ▶ | log_fatal 'Incorrect number of arguments' if @_ != 1; | ||||||||||||||||||||||||||
|
Subroutine
get_prev_node: not called CC=7 SCAR=40.3
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | next if defined $prev_ord && $ord < $prev_ord; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $prev_ord and $ord < $prev_ord
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
| |||||||||||||||||||||||||||||
| 63 | 0 | ( $prev_node, $prev_ord ) = ( $node, $ord ); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | 0 | return $prev_node; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | sub get_nodes_between { | ||||||||||||||||||||||||||||
| 69 | 4 | ▶ | log_fatal 'Incorrect number of arguments' if @_ != 2; | ||||||||||||||||||||||||||
|
Subroutine
get_nodes_between: called CC=4 SCAR=13.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 4 | my ($self, $other) = @_; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | # want $self preceding $other | ||||||||||||||||||||||||||||
| 73 | 4 | ▶ | if ( $self->ord > $other->ord ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | } @all_nodes; | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 4 | return @nodes_between; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | sub _normalize_node_ordering { | ||||||||||||||||||||||||||||
| 88 | 10 | ▶ | log_fatal 'Incorrect number of arguments' if @_ != 1; | ||||||||||||||||||||||||||
|
Subroutine
_normalize_node_ordering: called CC=4 SCAR=14.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 91 | 10 | my $new_ord = 0; | |||||||||||||||||||||||||||
| 92 | 10 | foreach my $node ( $self->get_descendants( { ordered => 1, add_self => 1 } ) ) { | |||||||||||||||||||||||||||
| 93 | 48 | $node->_set_ord($new_ord); | |||||||||||||||||||||||||||
| 94 | 48 | $new_ord++ | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | 10 | return; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | sub _check_shifting_method_args { | ||||||||||||||||||||||||||||
| 100 | 22 | ▶ | my ( $self, $reference_node, $arg_ref ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_check_shifting_method_args: called CC=10 SCAR=26.7
| |||||||||||||||||||||||||||||
| 101 | 22 | my @c = caller 1; | |||||||||||||||||||||||||||
| 102 | 22 | my $stack = "$c[3] called from $c[1], line $c[2]"; | |||||||||||||||||||||||||||
| 103 | 22 | ▶ | log_fatal( 'Incorrect number of arguments for ' . $stack ) if @_ < 2 || @_ > 3; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@_ < 2 or @_ > 3
MC/DC0%0/2
@_ < 2 or @_ > 3
@_ < 2
@_ > 3
Truth table33%1/3
A or B
A= @_ < 2B= @_ > 3
| |||||||||||||||||||||||||||||
| 104 | 22 | ▶ | log_fatal( 'Undefined reference node for ' . $stack ) if !$reference_node; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 22 | ▶ | log_fatal( 'Reference node must be from the same tree. In ' . $stack ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | if $reference_node->get_root() != $self->get_root(); | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | log_fatal '$reference_node is a descendant of $self.' | ||||||||||||||||||||||||||||
| 109 | . ' Maybe you have forgotten {without_children=>1}. ' . "\n" . $stack | ||||||||||||||||||||||||||||
| 110 | 22 | ▶ | if !$arg_ref->{without_children} && $reference_node->is_descendant_of($self); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $arg_ref->{"without_children"} and $reference_node->is_descendant_of($self)
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)
| |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 22 | ▶ | return if !defined $arg_ref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 22 | ▶ | log_fatal( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 'Second argument for shifting methods can be only options hash reference. In ' . $stack | ||||||||||||||||||||||||||||
| 116 | ) if ref $arg_ref ne 'HASH'; | ||||||||||||||||||||||||||||
| 117 | 22 | ▶ | my $unknown = first { $_ ne 'without_children' } keys %{$arg_ref}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||||
| 118 | 22 | ▶ | log_warn("Unknown switch '$unknown' for $stack") if defined $unknown; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 22 | return; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | sub shift_after_node { | ||||||||||||||||||||||||||||
| 123 | 16 | ▶ | my ( $self, $reference_node, $arg_ref ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
shift_after_node: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||
| 124 | 16 | ▶ | return if $self == $reference_node; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 16 | _check_shifting_method_args(@_); | |||||||||||||||||||||||||||
| 126 | 16 | ▶ | return $self->_shift_to_node( $reference_node, 1, $arg_ref->{without_children} ) if $arg_ref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 16 | return $self->_shift_to_node( $reference_node, 1, 0 ); | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | sub shift_before_node { | ||||||||||||||||||||||||||||
| 131 | 6 | ▶ | my ( $self, $reference_node, $arg_ref ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
shift_before_node: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||
| 132 | 6 | ▶ | return if $self == $reference_node; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 6 | _check_shifting_method_args(@_); | |||||||||||||||||||||||||||
| 134 | 6 | ▶ | return $self->_shift_to_node( $reference_node, 0, $arg_ref->{without_children} ) if $arg_ref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 6 | return $self->_shift_to_node( $reference_node, 0, 0 ); | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | sub shift_after_subtree { | ||||||||||||||||||||||||||||
| 139 | 0 | ▶ | my ( $self, $reference_node, $arg_ref ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
shift_after_subtree: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 140 | 0 | _check_shifting_method_args(@_); | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 0 | my $last_node; | |||||||||||||||||||||||||||
| 143 | 0 | ▶ | if ( $arg_ref->{without_children} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 0 | ($last_node) = reverse grep { $_ != $self } $reference_node->get_descendants( { ordered => 1, add_self => 1 } ); | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | else { | ||||||||||||||||||||||||||||
| 147 | 0 | $last_node = $reference_node->get_descendants( { except => $self, last_only => 1, add_self => 1 } ); | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | 0 | ▶ | return if !defined $last_node; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 0 | ▶ | return $self->_shift_to_node( $last_node, 1, $arg_ref->{without_children} ) if $arg_ref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 0 | return $self->_shift_to_node( $last_node, 1, 0 ); | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | sub shift_before_subtree { | ||||||||||||||||||||||||||||
| 155 | 0 | ▶ | my ( $self, $reference_node, $arg_ref ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
shift_before_subtree: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 156 | 0 | _check_shifting_method_args(@_); | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 0 | my $first_node; | |||||||||||||||||||||||||||
| 159 | 0 | ▶ | if ( $arg_ref->{without_children} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 0 | ($first_node) = grep { $_ != $self } $reference_node->get_descendants( { ordered => 1, add_self => 1 } ); | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | else { | ||||||||||||||||||||||||||||
| 163 | 0 | $first_node = $reference_node->get_descendants( { except => $self, first_only => 1, add_self => 1 } ); | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | 0 | ▶ | return if !defined $first_node; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | 0 | ▶ | return $self->_shift_to_node( $first_node, 0, $arg_ref->{without_children} ) if $arg_ref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 0 | return $self->_shift_to_node( $first_node, 0, 0 ); | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | # This method does the real work for all shift_* methods. | ||||||||||||||||||||||||||||
| 171 | # However, due to unfriendly name and arguments it's not public. | ||||||||||||||||||||||||||||
| 172 | sub _shift_to_node { | ||||||||||||||||||||||||||||
| 173 | 22 | ▶ | my ( $self, $reference_node, $after, $without_children ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_shift_to_node: called CC=12 SCAR=24.9
| |||||||||||||||||||||||||||||
| 174 | 22 | my @all_nodes = $self->get_root()->get_descendants(); | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | # Make sure that ord of all nodes is defined | ||||||||||||||||||||||||||||
| 177 | #my $maximal_ord = @all_nodes; -this does not work, since there may be gaps in ordering | ||||||||||||||||||||||||||||
| 178 | 22 | my $maximal_ord = 10000; | |||||||||||||||||||||||||||
| 179 | 22 | foreach my $d (@all_nodes) { | |||||||||||||||||||||||||||
| 180 | 146 | ▶ | if ( !defined $d->ord() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | 22 | $d->_set_ord( $maximal_ord++ ); | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | # Which nodes are to be moved? | ||||||||||||||||||||||||||||
| 186 | # $self only (the {without_children=>1} switch) | ||||||||||||||||||||||||||||
| 187 | # or $self and all its descendants (the default)? | ||||||||||||||||||||||||||||
| 188 | 22 | my @nodes_to_move; | |||||||||||||||||||||||||||
| 189 | 22 | ▶ | if ($without_children) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | 0 | @nodes_to_move = ($self); | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | else { | ||||||||||||||||||||||||||||
| 193 | 22 | @nodes_to_move = $self->get_descendants( { ordered => 1, add_self => 1 } ); | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | # Let's make a hash, so we can easily recognize which nodes are to be moved. | ||||||||||||||||||||||||||||
| 197 | 22 | my %is_moving = map { $_ => 1 } @nodes_to_move; | |||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | # Recompute ord of all nodes. | ||||||||||||||||||||||||||||
| 200 | # The technical root has ord=0 and the first node will have ord=1. | ||||||||||||||||||||||||||||
| 201 | 22 | my $counter = 1; | |||||||||||||||||||||||||||
| 202 | 22 | my $nodes_moved = 0; | |||||||||||||||||||||||||||
| 203 | 22 | @all_nodes = sort { $a->ord() <=> $b->ord() } @all_nodes; | |||||||||||||||||||||||||||
| 204 | 22 | foreach my $node (@all_nodes) { | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | # We skip nodes that are being moved. | ||||||||||||||||||||||||||||
| 207 | # Their ord is recomuted elsewhere (look 8 lines down). | ||||||||||||||||||||||||||||
| 208 | 146 | ▶ | next if $is_moving{$node}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | # If moving "after" a reference node | ||||||||||||||||||||||||||||
| 211 | # then ord of the $node can be recomputed now | ||||||||||||||||||||||||||||
| 212 | # even if it is actually the $reference_node. | ||||||||||||||||||||||||||||
| 213 | 124 | ▶ | if ($after) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 214 | 88 | $node->_set_ord( $counter++ ); | |||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | # Now we insert (i.e. recompute ord of) all nodes which are being moved. | ||||||||||||||||||||||||||||
| 218 | # The nodes are inserted in the original order. | ||||||||||||||||||||||||||||
| 219 | 124 | ▶ | if ( $node == $reference_node ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 220 | 16 | foreach my $moving_node (@nodes_to_move) { | |||||||||||||||||||||||||||
| 221 | 16 | $moving_node->_set_ord( $counter++ ); | |||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||
| 223 | 16 | $nodes_moved = 1; | |||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | # If moving "before" a node, then now it is the right moment | ||||||||||||||||||||||||||||
| 227 | # for recomputing ord of the $node | ||||||||||||||||||||||||||||
| 228 | 124 | ▶ | if ( !$after ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | 36 | $node->_set_ord( $counter++ ); | |||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | # If $is_moving{$reference_node}, e.g. when there is just one node in the tree, | ||||||||||||||||||||||||||||
| 234 | # we need to do the reordering now (otherwise the ord would be still 10000). | ||||||||||||||||||||||||||||
| 235 | 22 | ▶ | if ( !$nodes_moved ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 236 | 6 | foreach my $moving_node (@nodes_to_move) { | |||||||||||||||||||||||||||
| 237 | 6 | $moving_node->_set_ord( $counter++ ); | |||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | 22 | return; | |||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | ###!!! DZ: reverting to 7183, 2011-11-03. | ||||||||||||||||||||||||||||
| 244 | # This function by Martin is faster than my own below but it does not always return correct results. | ||||||||||||||||||||||||||||
| 245 | # The key problem could be that it relies on ord values forming a contiguous sequence which might not always be true. | ||||||||||||||||||||||||||||
| 246 | # Go to devel/hamledt/nonprojectivity and test that make yields the same numbers for the old and the new method. | ||||||||||||||||||||||||||||
| 247 | sub is_nonprojective_does_not_work { | ||||||||||||||||||||||||||||
| 248 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
is_nonprojective_does_not_work: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | # Root and its children are always projective | ||||||||||||||||||||||||||||
| 251 | 0 | my $parent = $self->get_parent(); | |||||||||||||||||||||||||||
| 252 | 0 | ▶ | return 0 if !$parent || $parent->is_root(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $parent or $parent->is_root
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
| |||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | # Edges between neighbouring nodes are always projective. | ||||||||||||||||||||||||||||
| 255 | # Check it now to make it a bit faster. | ||||||||||||||||||||||||||||
| 256 | 0 | my ( $ordA, $ordB ) = ( $self->ord, $parent->ord ); | |||||||||||||||||||||||||||
| 257 | 0 | ▶ | if ( $ordA > $ordB ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 258 | 0 | ( $ordA, $ordB ) = ( $ordB, $ordA ); | |||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||
| 260 | 0 | my $distance = $ordB - $ordA; | |||||||||||||||||||||||||||
| 261 | 0 | ▶ | return 0 if $distance == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | # Get all the descendants of $parent that are in the span of the edge. | ||||||||||||||||||||||||||||
| 264 | 0 | ▶ | my @span = grep { $_->ord > $ordA && $_->ord < $ordB } $parent->get_descendants(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | # For projective edges @span must include all the nodes between $parent and $self. | ||||||||||||||||||||||||||||
| 267 | 0 | return @span != $distance - 1; | |||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 271 | # Tells whether the node is attached to its parent nonprojectively, i.e. there | ||||||||||||||||||||||||||||
| 272 | # is at least one node between this node and its parent that is not dominated | ||||||||||||||||||||||||||||
| 273 | # by the parent. | ||||||||||||||||||||||||||||
| 274 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 275 | sub is_nonprojective | ||||||||||||||||||||||||||||
| 276 | { | ||||||||||||||||||||||||||||
| 277 | 0 | ▶ | log_fatal('Incorrect number of arguments') if(scalar(@_)!=1); | ||||||||||||||||||||||||||
|
Subroutine
is_nonprojective: not called CC=7 SCAR=40.3
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 278 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 279 | 0 | my $parent = $self->parent(); | |||||||||||||||||||||||||||
| 280 | # A node that does not have a parent cannot be nonprojective. | ||||||||||||||||||||||||||||
| 281 | 0 | ▶ | return 0 if(!$parent); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | # Get a hash of all descendants of the parent. | ||||||||||||||||||||||||||||
| 283 | 0 | my @pdesc = $parent->get_descendants({add_self=>1}); | |||||||||||||||||||||||||||
| 284 | 0 | my %pdesc; map {$pdesc{$_}++} (@pdesc); | |||||||||||||||||||||||||||
| 285 | # Figure out whether the node is to the left or to the right from its parent. | ||||||||||||||||||||||||||||
| 286 | 0 | my $nord = $self->ord(); | |||||||||||||||||||||||||||
| 287 | 0 | my $pord = $parent->ord(); | |||||||||||||||||||||||||||
| 288 | 0 | my ($x, $y); | |||||||||||||||||||||||||||
| 289 | 0 | ▶ | if($pord>$nord) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 290 | { | ||||||||||||||||||||||||||||
| 291 | 0 | $x = $self; | |||||||||||||||||||||||||||
| 292 | 0 | $y = $parent; | |||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||
| 294 | else | ||||||||||||||||||||||||||||
| 295 | { | ||||||||||||||||||||||||||||
| 296 | 0 | $x = $parent; | |||||||||||||||||||||||||||
| 297 | 0 | $y = $self; | |||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||
| 299 | # Get the ordered list of all nodes between $x and $y. | ||||||||||||||||||||||||||||
| 300 | 0 | my $xord = $x->ord(); | |||||||||||||||||||||||||||
| 301 | 0 | my $yord = $y->ord(); | |||||||||||||||||||||||||||
| 302 | 0 | ▶ | my @between = grep {$_->ord()>$xord && $_->ord()<$yord} ($parent->root()->get_descendants({ordered=>1})); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 303 | # This node is nonprojective if @between contains anything that is not in %pdesc. | ||||||||||||||||||||||||||||
| 304 | 0 | foreach my $b (@between) | |||||||||||||||||||||||||||
| 305 | { | ||||||||||||||||||||||||||||
| 306 | 0 | ▶ | if(!$pdesc{$b}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 307 | { | ||||||||||||||||||||||||||||
| 308 | 0 | return 1; | |||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||
| 311 | 0 | return 0; | |||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | 38 | 1; | |||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | __END__ | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | =encoding utf-8 | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | =head1 NAME | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | Treex::Core::Node::Ordered | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | =head1 VERSION | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | version 2.20151210 | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | Moose role for nodes which can/should be ordered by the attribute C<ord> | ||||||||||||||||||||||||||||
| 331 | (usually following the word order). | ||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | =over | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | =item ord | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | The ordering attribute, ordinal number of a node. | ||||||||||||||||||||||||||||
| 340 | The ordering should be without gaps, so | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | print join ' ', map {$_->ord} $root->get_descendants({ordered=>1}); | ||||||||||||||||||||||||||||
| 343 | # should print | ||||||||||||||||||||||||||||
| 344 | # 1 2 3 ... number_of_descendants | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | =back | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | =head1 METHODS | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | =head2 Access to nodes ordering | ||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | =over | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | =item my $boolean = $node->precedes($another_node); | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | Does this node precede C<$another_node>? | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | =item my $following_node = $node->get_next_node(); | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | Return the closest following node (according to the ordering attribute) | ||||||||||||||||||||||||||||
| 361 | or C<undef> if C<$node> is the last one in the tree. | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | =item my $preceding_node = $node->get_prev_node(); | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | Return the closest preceding node (according to the ordering attribute) | ||||||||||||||||||||||||||||
| 366 | or C<undef> if C<$node> is the first one in the tree. | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | =item my @nodes_in_between = $node->get_nodes_between($other_node); | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | Return nodes that lie between C<$node> and C<$other_node>, ordered, exclusive. | ||||||||||||||||||||||||||||
| 371 | (The returned array may be empty.) | ||||||||||||||||||||||||||||
| 372 | C<$node> and C<$other_node> can be given in any order, | ||||||||||||||||||||||||||||
| 373 | i.e. the result of C<$node->get_nodes_between($other_node)> | ||||||||||||||||||||||||||||
| 374 | is the same as the result of C<$other_node->get_nodes_between($node)>. | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | =back | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | =head2 Reordering nodes | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | Next four methods for changing the order of nodes | ||||||||||||||||||||||||||||
| 381 | (the word order defined by the attribute C<ord>) | ||||||||||||||||||||||||||||
| 382 | have an optional argument C<$arg_ref> for specifying switches. | ||||||||||||||||||||||||||||
| 383 | So far there is only one switch - C<without_children> | ||||||||||||||||||||||||||||
| 384 | which is by default set to 0. | ||||||||||||||||||||||||||||
| 385 | It means that the default behavior is to shift the node | ||||||||||||||||||||||||||||
| 386 | with all its descendants. | ||||||||||||||||||||||||||||
| 387 | Only if you want to leave the position of the descendants unchanged | ||||||||||||||||||||||||||||
| 388 | and shift just the node, use e.g. | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | $node->shift_after_node($reference_node, {without_children=>1}); | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | Shifting involves only changing the ordering attribute (C<ord>) of nodes. | ||||||||||||||||||||||||||||
| 393 | There is no rehanging (changing parents). The node which is | ||||||||||||||||||||||||||||
| 394 | going to be shifted must be already added to the tree | ||||||||||||||||||||||||||||
| 395 | and the reference node must be in the same tree. | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | For languages with left-to-right script: C<after> means "to the right of" | ||||||||||||||||||||||||||||
| 398 | and C<before> means "to the left of". | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | =over | ||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | =item $node->shift_after_node($reference_node); | ||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | Shifts (changes the C<ord> of) the node just behind the reference node. | ||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | =item $node->shift_after_subtree($reference_node); | ||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | Shifts (changes the C<ord> of) the node behind the subtree of the reference node. | ||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | =item $node->shift_before_node($reference_node); | ||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | Shifts (changes the C<ord> of) the node just in front of the reference node. | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | =item $node->shift_before_subtree($reference_node); | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | Shifts (changes the C<ord> of) the node in front of the subtree of the reference node. | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | =back | ||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | =head2 Nonprojectivity | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | =over | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | =item my $nonproj = $node->is_nonprojective(); | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | Return 1 if the node is attached to its parent nonprojectively, i.e. there is | ||||||||||||||||||||||||||||
| 427 | at least one node between this node and its parent that is not descendant of | ||||||||||||||||||||||||||||
| 428 | the parent. Return 0 otherwise. | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | =back | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | Martin Popel <popel@ufal.mff.cuni.cz> | ||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | Copyright © 2011 by Institute of Formal and Applied Linguistics, Charles University in Prague | ||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||
| 441 | This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||