1 package Text::Tradition::Collation::Reading;
5 use Text::Tradition::Collation::Position;
7 extends 'Graph::Easy::Node';
11 isa => 'Text::Tradition::Collation::Position',
12 predicate => 'has_position',
15 # This contains an array of reading objects; the array is a pool,
16 # shared by the reading objects inside the pool. When a reading is
17 # added to the pool, all the same_as attributes should be updated.
20 isa => 'ArrayRef[Text::Tradition::Collation::Reading]',
23 # Deal with the non-arg option for Graph::Easy's constructor.
24 around BUILDARGS => sub {
29 if( @_ == 1 && ref( $_[0] ) ne 'HASH' ) {
30 return $class->$orig( 'name' => $_[0] );
32 return $class->$orig( @_ );
36 # Take constructor args as well as a Position argument.
37 around position => sub {
40 return $self->$orig() unless @_;
43 unless( @_ == 1 && ref( $_[0] ) eq 'Text::Tradition::Collation::Position' ) {
44 # We have constructor arguments; pass them to Position.
45 @args = ( Text::Tradition::Collation::Position->new( @_ ) );
47 $self->$orig( @args );
50 # Initialize the identity pool.
52 my( $self, $args ) = @_;
53 $self->same_as( [ $self ] );
57 # Wrapper function around 'label' attribute.
60 $self->set_attribute( 'label', $_[0] );
66 my( $self, $merged_node ) = @_;
67 # Adopt the identity pool of the other node.
68 my @now_identical = grep { $_ ne $merged_node } @{$merged_node->same_as};
69 my $new_pool = _merge_array_pool( \@now_identical, $self->same_as )
72 # TODO Adopt the relationship attributes of the other node.
75 ## Dealing with transposed readings. These methods are only really
76 ## applicable if we have a linear collation graph.
79 my( $self, $other_node ) = @_;
80 my $enlarged_pool = _merge_array_pool( $self->same_as,
81 $other_node->same_as );
83 # ...and set this node to point to the enlarged pool.
84 $self->same_as( $enlarged_pool );
87 sub identical_readings {
89 my @same = grep { $_ ne $self } @{$self->same_as};
93 sub _merge_array_pool {
94 my( $pool, $main_pool ) = @_;
96 foreach ( @$main_pool ) {
97 # Note which nodes are already in the main pool so that we
99 $poolhash{$_->name} = 1;
103 # Add the remaining nodes to the main pool...
104 push( @$main_pool, $_ ) unless $poolhash{$_->name};
111 my $pool = $self->same_as;
112 return $pool->[0]->name ne $self->name;
117 return $self->same_as->[0];
122 return undef unless $self->has_position;
123 return $self->position->is_colocated( @_ );
126 # Returns all readings that adjoin this one on any path.
127 sub neighbor_readings {
128 my( $self, $direction ) = @_;
129 $direction = 'both' unless $direction;
130 my @paths = grep { $_->isa( 'Text::Tradition::Collation::Path' ) } $self->edges;
132 foreach my $p ( @paths ) {
133 if( $p->to eq $self ) {
134 next if $direction eq 'forward';
135 $connected{$p->from->name} = $p->from;
136 } else { # $p->from eq $self
137 next if $direction =~ /^back/;
138 $connected{$p->to->name} = $p->to;
141 return values( %connected );
144 sub adjust_neighbor_position {
146 return unless $self->position->fixed;
148 # TODO This is a naive and repetitive implementation and
150 foreach my $neighbor ( $self->neighbor_readings('forward') ) {
151 next unless !$neighbor->is_common &&
152 $neighbor->position->common == $self->position->common;
153 if( $neighbor->position->fixed &&
154 $neighbor->position->min == $self->position->min ) {
155 warn sprintf( "Readings %s and %s are at the same position!",
156 $neighbor->name, $self->name );
158 next if $neighbor->position->fixed || $neighbor->position->matched;
159 $neighbor->position->min( $self->position->min + 1 );
160 # Recurse if necessary.
161 $neighbor->adjust_neighbor_position()
162 unless $neighbor->position->fixed;
164 foreach my $neighbor ( $self->neighbor_readings('back') ) {
165 next unless !$neighbor->is_common &&
166 $neighbor->position->common == $self->position->common;
167 if( $neighbor->position->fixed &&
168 $neighbor->position->min == $self->position->min ) {
169 warn sprintf( "Readings %s and %s are at the same position!",
170 $neighbor->name, $self->name );
172 next if $neighbor->position->fixed || $neighbor->position->matched;
173 $neighbor->position->max( $self->position->max - 1 );
174 # Recurse if necessary.
175 $neighbor->adjust_neighbor_position()
176 unless $neighbor->position->fixed;
182 my( $self, $other ) = @_;
183 # Adjust the position of both these nodes to be as restrictive as possible.
184 unless( $self->position->is_colocated( $other->position ) ) {
185 warn "Cannot match positions of non-colocated readings";
188 my $sp = $self->position;
189 my $op = $other->position;
190 my $newmin = $sp->min > $op->min ? $sp->min : $op->min;
191 my $newmax = $sp->max < $op->max ? $sp->max : $op->max;
192 my $newpos = Text::Tradition::Collation::Position->new(
193 'common' => $sp->common,
198 # We are setting the positions to be the same object. I don't
199 # think that actually matters. We may eventually want unique
200 # objects for each position.
201 $self->position( $newpos );
202 $other->position( $newpos );
203 $self->adjust_neighbor_position();
204 $other->adjust_neighbor_position();
207 ## Keep track of which readings are unchanged across witnesses.
211 return $self->get_attribute( 'class' ) eq 'common';
216 $self->set_attribute( 'class', 'common' );
221 $self->set_attribute( 'class', 'variant' );
225 __PACKAGE__->meta->make_immutable;
229 ######################################################
230 ## copied from Graph::Easy::Parser docs
231 ######################################################
232 # when overriding nodes, we also need ::Anon
234 package Text::Tradition::Collation::Reading::Anon;
236 use MooseX::NonMoose;
237 extends 'Text::Tradition::Collation::Reading';
238 extends 'Graph::Easy::Node::Anon';
240 __PACKAGE__->meta->make_immutable;
243 # use base qw/Text::Tradition::Collation::Reading/;
244 # use base qw/Graph::Easy::Node::Anon/;
246 ######################################################
249 package Text::Tradition::Collation::Reading::Empty;
251 use MooseX::NonMoose;
252 extends 'Graph::Easy::Node::Empty';
254 __PACKAGE__->meta->make_immutable;
257 # use base qw/Text::Tradition::Collation::Reading/;
259 ######################################################