1 package Text::Tradition::Collation::Reading;
4 use Moose::Util::TypeConstraints;
7 extends 'Graph::Easy::Node';
11 => where { $_ =~ /^\d+\,\d+$/ },
12 message { 'Position must be of the form x,y' };
17 predicate => 'has_position',
20 # This contains an array of reading objects; the array is a pool,
21 # shared by the reading objects inside the pool. When a reading is
22 # added to the pool, all the same_as attributes should be updated.
25 isa => 'ArrayRef[Text::Tradition::Collation::Reading]',
28 # # This is a hash mapping of 'relationship => reading'.
29 # # TODO we should validate the relationships sometime.
30 has 'relationships' => (
32 isa => 'HashRef[Text::Tradition::Collation::Reading]',
33 default => sub { {} },
36 # Deal with the non-arg option for Graph::Easy's constructor.
37 around BUILDARGS => sub {
42 if( @_ == 1 && ref( $_[0] ) ne 'HASH' ) {
43 return $class->$orig( 'name' => $_[0] );
45 return $class->$orig( @_ );
49 # Initialize the identity pool.
51 my( $self, $args ) = @_;
52 $self->same_as( [ $self ] );
56 # Wrapper function around 'label' attribute.
59 $self->set_attribute( 'label', $_[0] );
61 return $self->get_attribute( 'label' );
65 my( $self, $merged_node ) = @_;
66 # Adopt the identity pool of the other node.
67 my @now_identical = grep { $_ ne $merged_node } @{$merged_node->same_as};
68 my $new_pool = _merge_array_pool( \@now_identical, $self->same_as )
71 # Adopt the relationship attributes of the other node.
72 my $now_rel = $merged_node->relationships;
73 foreach my $key ( %$now_rel ) {
74 if( $self->has_relationship( $key ) ) {
75 my $related = $self->get_relationship( $key );
76 if( $now_rel->{$key} ne $related ) {
77 warn( sprintf( "Merged reading %s has relationship %s to reading %s instead of %s; skipping",
78 $merged_node->name, $key,
81 } # else no action needed
83 $self->set_relationship( $key, $now_rel->{$key} );
89 my( $self, $other_node ) = @_;
90 my $enlarged_pool = _merge_array_pool( $self->same_as,
91 $other_node->same_as );
93 # ...and set this node to point to the enlarged pool.
94 $self->same_as( $enlarged_pool );
97 sub identical_readings {
99 my @same = grep { $_ ne $self } @{$self->same_as};
103 sub _merge_array_pool {
104 my( $pool, $main_pool ) = @_;
106 foreach ( @$main_pool ) {
107 # Note which nodes are already in the main pool so that we
109 $poolhash{$_->name} = 1;
113 # Add the remaining nodes to the main pool...
114 push( @$main_pool, $_ ) unless $poolhash{$_->name};
121 my $pool = $self->same_as;
122 return $pool->[0]->name eq $self->name;
127 return $self->same_as->[0];
130 # Much easier to do this with a hash than with an array of Relationship objects,
131 # which would be the proper OO method.
133 sub has_relationship {
134 my( $self, $rel ) = @_;
135 return exists( $self->relationships->{ $rel } );
138 sub get_relationship {
139 my( $self, $rel ) = @_;
140 if( $self->has_relationship( $rel ) ) {
141 return $self->relationships->{ $rel };
146 sub set_relationship {
147 my( $self, $rel, $value ) = @_;
148 $self->relationships->{ $rel } = $value;
153 return $self->get_attribute( 'class' ) eq 'common';
158 $self->set_attribute( 'class', 'common' );
163 $self->set_attribute( 'class', 'variant' );
167 __PACKAGE__->meta->make_immutable;
171 ######################################################
172 ## copied from Graph::Easy::Parser docs
173 ######################################################
174 # when overriding nodes, we also need ::Anon
176 package Text::Tradition::Collation::Reading::Anon;
178 use MooseX::NonMoose;
179 extends 'Text::Tradition::Collation::Reading';
180 extends 'Graph::Easy::Node::Anon';
182 __PACKAGE__->meta->make_immutable;
185 # use base qw/Text::Tradition::Collation::Reading/;
186 # use base qw/Graph::Easy::Node::Anon/;
188 ######################################################
191 package Text::Tradition::Collation::Reading::Empty;
193 use MooseX::NonMoose;
194 extends 'Graph::Easy::Node::Empty';
196 __PACKAGE__->meta->make_immutable;
199 # use base qw/Text::Tradition::Collation::Reading/;
201 ######################################################