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',
18 predicate => 'has_rank',
21 # This contains an array of reading objects; the array is a pool,
22 # shared by the reading objects inside the pool. When a reading is
23 # added to the pool, all the same_as attributes should be updated.
26 isa => 'ArrayRef[Text::Tradition::Collation::Reading]',
29 # Deal with the non-arg option for Graph::Easy's constructor.
30 around BUILDARGS => sub {
35 if( @_ == 1 && ref( $_[0] ) ne 'HASH' ) {
36 return $class->$orig( 'name' => $_[0] );
38 return $class->$orig( @_ );
42 # Take constructor args as well as a Position argument.
43 around position => sub {
46 return $self->$orig() unless @_;
49 unless( @_ == 1 && ref( $_[0] ) eq 'Text::Tradition::Collation::Position' ) {
50 # We have constructor arguments; pass them to Position.
51 @args = ( Text::Tradition::Collation::Position->new( @_ ) );
53 $self->$orig( @args );
56 # Initialize the identity pool.
58 my( $self, $args ) = @_;
59 $self->same_as( [ $self ] );
63 # Wrapper function around 'label' attribute.
66 $self->set_attribute( 'label', $_[0] );
72 my( $self, $sigil, $backup ) = @_;
73 my @wits = $self->witnesses;
74 return 1 if grep { $_ eq $sigil } @wits;
76 return 1 if grep { $_ eq $backup } @wits;
83 my @paths = grep { $_->get_attribute( 'class' ) eq 'path' } $self->outgoing;
84 push( @paths, grep { $_->get_attribute( 'class' ) eq 'path' } $self->incoming );
86 foreach my $p ( @paths ) {
87 if( $p->has_hidden_witnesses ) {
88 foreach ( @{$p->hidden_witnesses} ) {
99 my( $self, $merged_node ) = @_;
100 # Adopt the identity pool of the other node.
101 my @now_identical = grep { $_ ne $merged_node } @{$merged_node->same_as};
102 my $new_pool = _merge_array_pool( \@now_identical, $self->same_as )
105 # TODO Adopt the relationship attributes and segment memberships of the other node.
108 ## Dealing with transposed readings. These methods are only really
109 ## applicable if we have a linear collation graph.
112 my( $self, $other_node ) = @_;
113 my $enlarged_pool = _merge_array_pool( $self->same_as,
114 $other_node->same_as );
116 # ...and set this node to point to the enlarged pool.
117 $self->same_as( $enlarged_pool );
120 sub identical_readings {
122 my @same = grep { $_ ne $self } @{$self->same_as};
126 sub _merge_array_pool {
127 my( $pool, $main_pool ) = @_;
129 foreach ( @$main_pool ) {
130 # Note which nodes are already in the main pool so that we
132 $poolhash{$_->name} = 1;
136 # Add the remaining nodes to the main pool...
137 push( @$main_pool, $_ ) unless $poolhash{$_->name};
144 my $pool = $self->same_as;
145 return $pool->[0]->name ne $self->name;
150 return $self->same_as->[0];
155 return undef unless $self->has_position;
156 return $self->position->is_colocated( @_ );
159 # Returns all readings that adjoin this one on any path.
160 sub neighbor_readings {
161 my( $self, $direction ) = @_;
162 $direction = 'both' unless $direction;
163 my @paths = grep { $_->isa( 'Text::Tradition::Collation::Path' ) } $self->edges;
165 foreach my $p ( @paths ) {
166 if( $p->to eq $self ) {
167 next if $direction eq 'forward';
168 $connected{$p->from->name} = $p->from;
169 } else { # $p->from eq $self
170 next if $direction =~ /^back/;
171 $connected{$p->to->name} = $p->to;
174 return values( %connected );
177 # Returns all readings related to the one we've got.
178 sub related_readings {
179 my( $self, $colocated ) = @_;
181 foreach my $e ( $self->edges ) {
182 next unless $e->isa( 'Text::Tradition::Collation::Relationship' );
183 next if $colocated && $e->type eq 'repetition';
184 push( @related, $e->from eq $self ? $e->to : $e->from );
189 ## Keep track of which readings are unchanged across witnesses.
192 return $self->get_attribute( 'class' ) eq 'common';
197 $self->set_attribute( 'class', 'common' );
202 $self->set_attribute( 'class', 'variant' );
206 __PACKAGE__->meta->make_immutable;
210 ######################################################
211 ## copied from Graph::Easy::Parser docs
212 ######################################################
213 # when overriding nodes, we also need ::Anon
215 package Text::Tradition::Collation::Reading::Anon;
217 use MooseX::NonMoose;
218 extends 'Text::Tradition::Collation::Reading';
219 extends 'Graph::Easy::Node::Anon';
221 __PACKAGE__->meta->make_immutable;
224 # use base qw/Text::Tradition::Collation::Reading/;
225 # use base qw/Graph::Easy::Node::Anon/;
227 ######################################################
230 package Text::Tradition::Collation::Reading::Empty;
232 use MooseX::NonMoose;
233 extends 'Graph::Easy::Node::Empty';
235 __PACKAGE__->meta->make_immutable;
238 # use base qw/Text::Tradition::Collation::Reading/;
240 ######################################################