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',
26 # This contains an array of reading objects; the array is a pool,
27 # shared by the reading objects inside the pool. When a reading is
28 # added to the pool, all the same_as attributes should be updated.
31 isa => 'ArrayRef[Text::Tradition::Collation::Reading]',
34 # Deal with the non-arg option for Graph::Easy's constructor.
35 around BUILDARGS => sub {
40 if( @_ == 1 && ref( $_[0] ) ne 'HASH' ) {
41 return $class->$orig( 'name' => $_[0] );
43 return $class->$orig( @_ );
47 # Take constructor args as well as a Position argument.
48 around position => sub {
51 return $self->$orig() unless @_;
54 unless( @_ == 1 && ref( $_[0] ) eq 'Text::Tradition::Collation::Position' ) {
55 # We have constructor arguments; pass them to Position.
56 @args = ( Text::Tradition::Collation::Position->new( @_ ) );
58 $self->$orig( @args );
61 # A lacuna node is also a meta node.
62 before is_lacuna => sub {
63 my( $self, $arg ) = @_;
69 # Initialize the identity pool.
71 my( $self, $args ) = @_;
72 $self->same_as( [ $self ] );
76 # Wrapper function around 'label' attribute.
80 $self->set_attribute( 'label', $_[0] );
82 $self->del_attribute( 'label' );
89 my( $self, $sigil, $backup ) = @_;
90 my @wits = $self->witnesses;
91 return 1 if grep { $_ eq $sigil } @wits;
93 return 1 if grep { $_ eq $backup } @wits;
100 my @paths = grep { $_->get_attribute( 'class' ) eq 'path' } $self->outgoing;
101 push( @paths, grep { $_->get_attribute( 'class' ) eq 'path' } $self->incoming );
103 foreach my $p ( @paths ) {
104 if( $p->has_hidden_witnesses ) {
105 foreach ( @{$p->hidden_witnesses} ) {
109 $wits{$p->label} = 1;
116 my( $self, $merged_node ) = @_;
117 # Adopt the identity pool of the other node.
118 my @now_identical = grep { $_ ne $merged_node } @{$merged_node->same_as};
119 my $new_pool = _merge_array_pool( \@now_identical, $self->same_as )
122 # TODO Adopt the relationship attributes and segment memberships of the other node.
125 ## Dealing with transposed readings. These methods are only really
126 ## applicable if we have a linear collation graph.
129 my( $self, $other_node ) = @_;
130 my $enlarged_pool = _merge_array_pool( $self->same_as,
131 $other_node->same_as );
133 # ...and set this node to point to the enlarged pool.
134 $self->same_as( $enlarged_pool );
137 sub identical_readings {
139 my @same = grep { $_ ne $self } @{$self->same_as};
143 sub _merge_array_pool {
144 my( $pool, $main_pool ) = @_;
146 foreach ( @$main_pool ) {
147 # Note which nodes are already in the main pool so that we
149 $poolhash{$_->name} = 1;
153 # Add the remaining nodes to the main pool...
154 push( @$main_pool, $_ ) unless $poolhash{$_->name};
161 my $pool = $self->same_as;
162 return $pool->[0]->name ne $self->name;
167 return $self->same_as->[0];
172 return undef unless $self->has_position;
173 return $self->position->is_colocated( @_ );
176 # Looks from the outside like an accessor for a Boolean, but really
177 # sets the node's class. Should apply to start, end, and lacunae.
182 if( defined $arg && $arg ) {
183 $self->set_attribute( 'class', 'meta' );
184 } elsif ( defined $arg ) {
185 $self->del_attribute( 'class' );
187 return $self->sub_class eq 'meta';
190 # Returns all readings that adjoin this one on any path.
191 sub neighbor_readings {
192 my( $self, $direction ) = @_;
193 $direction = 'both' unless $direction;
194 my @paths = grep { $_->isa( 'Text::Tradition::Collation::Path' ) } $self->edges;
196 foreach my $p ( @paths ) {
197 if( $p->to eq $self ) {
198 next if $direction eq 'forward';
199 $connected{$p->from->name} = $p->from;
200 } else { # $p->from eq $self
201 next if $direction =~ /^back/;
202 $connected{$p->to->name} = $p->to;
205 return values( %connected );
208 # Returns all readings related to the one we've got.
209 sub related_readings {
210 my( $self, $colocated, $queried ) = @_;
211 $queried = { $self->name => 1 } unless $queried;
213 # Get the nodes directly related to this one
214 foreach my $e ( $self->edges ) {
215 next unless $e->isa( 'Text::Tradition::Collation::Relationship' );
216 next if $colocated && $e->type eq 'repetition';
217 my $n = $e->from eq $self ? $e->to : $e->from;
218 next if $queried->{$n->name};
219 push( @related, $n );
221 # Now query those nodes for their relations, recursively
222 map { $queried->{$_->name} = 1 } @related;
224 foreach ( @related ) {
225 push( @also_related, $_->related_readings( $colocated, $queried ) );
227 push( @related, @also_related );
231 ## Keep track of which readings are unchanged across witnesses.
234 return $self->get_attribute( 'class' ) eq 'common';
237 ## TODO Rationalize make_common, is_meta, etc.
240 $self->set_attribute( 'class', 'common' );
245 $self->set_attribute( 'class', 'variant' );
249 __PACKAGE__->meta->make_immutable;
253 ######################################################
254 ## copied from Graph::Easy::Parser docs
255 ######################################################
256 # when overriding nodes, we also need ::Anon
258 package Text::Tradition::Collation::Reading::Anon;
260 use MooseX::NonMoose;
261 extends 'Text::Tradition::Collation::Reading';
262 extends 'Graph::Easy::Node::Anon';
264 __PACKAGE__->meta->make_immutable;
267 # use base qw/Text::Tradition::Collation::Reading/;
268 # use base qw/Graph::Easy::Node::Anon/;
270 ######################################################
273 package Text::Tradition::Collation::Reading::Empty;
275 use MooseX::NonMoose;
276 extends 'Graph::Easy::Node::Empty';
278 __PACKAGE__->meta->make_immutable;
281 # use base qw/Text::Tradition::Collation::Reading/;
283 ######################################################