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' };
19 # This contains an array of reading objects; the array is a pool,
20 # shared by the reading objects inside the pool. When a reading is
21 # added to the pool, all the same_as attributes should be updated.
24 isa => 'ArrayRef[Text::Tradition::Collation::Reading]',
27 # # This is a hash mapping of 'relationship => reading'.
28 # # TODO we should validate the relationships sometime.
29 has 'relationships' => (
31 isa => 'HashRef[Text::Tradition::Collation::Reading]',
32 default => sub { {} },
35 # Deal with the non-arg option for Graph::Easy's constructor.
36 around BUILDARGS => sub {
41 if( @_ == 1 && ref( $_[0] ) ne 'HASH' ) {
42 return $class->$orig( 'name' => $_[0] );
44 return $class->$orig( @_ );
48 # Initialize the identity pool.
50 my( $self, $args ) = @_;
51 $self->same_as( [ $self ] );
55 my( $self, $merged_node ) = @_;
56 # Adopt the identity pool of the other node.
57 my @now_identical = grep { $_ ne $merged_node } @{$merged_node->same_as};
58 my $new_pool = _merge_array_pool( \@now_identical, $self->same_as )
61 # Adopt the relationship attributes of the other node.
62 my $now_rel = $merged_node->relationships;
63 foreach my $key ( %$now_rel ) {
64 if( $self->has_relationship( $key ) ) {
65 my $related = $self->get_relationship( $key );
66 if( $now_rel->{$key} ne $related ) {
67 warn( sprintf( "Merged reading %s has relationship %s to reading %s instead of %s; skipping",
68 $merged_node->name, $key,
71 } # else no action needed
73 $self->set_relationship( $key, $now_rel->{$key} );
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 _merge_array_pool {
88 my( $pool, $main_pool ) = @_;
90 foreach ( @$main_pool ) {
91 # Note which nodes are already in the main pool so that we
93 $poolhash{$_->name} = 1;
97 # Add the remaining nodes to the main pool...
98 push( @$main_pool, $_ ) unless $poolhash{$_->name};
105 my $pool = $self->same_as;
106 return $pool->[0]->name eq $self->name;
111 return $self->same_as->[0];
114 # Much easier to do this with a hash than with an array of Relationship objects,
115 # which would be the proper OO method.
117 sub has_relationship {
118 my( $self, $rel ) = @_;
119 return exists( $self->relationships->{ $rel } );
122 sub get_relationship {
123 my( $self, $rel ) = @_;
124 if( $self->has_relationship( $rel ) ) {
125 return $self->relationships->{ $rel };
130 sub set_relationship {
131 my( $self, $rel, $value ) = @_;
132 $self->relationships->{ $rel } = $value;
136 __PACKAGE__->meta->make_immutable;
140 ######################################################
141 ## copied from Graph::Easy::Parser docs
142 ######################################################
143 # when overriding nodes, we also need ::Anon
145 package Text::Tradition::Collation::Reading::Anon;
147 use MooseX::NonMoose;
148 extends 'Text::Tradition::Collation::Reading';
149 extends 'Graph::Easy::Node::Anon';
151 __PACKAGE__->meta->make_immutable;
154 # use base qw/Text::Tradition::Collation::Reading/;
155 # use base qw/Graph::Easy::Node::Anon/;
157 ######################################################
160 package Text::Tradition::Collation::Reading::Empty;
162 use MooseX::NonMoose;
163 extends 'Graph::Easy::Node::Empty';
165 __PACKAGE__->meta->make_immutable;
168 # use base qw/Text::Tradition::Collation::Reading/;
170 ######################################################