change calling of enum for RT ticket 90694
[scpubgit/stemmatology.git] / base / lib / Text / Tradition / Collation / Relationship.pm
CommitLineData
22222af9 1package Text::Tradition::Collation::Relationship;
2
3use Moose;
4use Moose::Util::TypeConstraints;
5
d8ef0a2b 6enum 'RelationshipScope' => [ qw( local document global ) ];
22222af9 7
8no Moose::Util::TypeConstraints;
9
027d819c 10=head1 NAME
11
12Text::Tradition::Collation::Relationship - represents a syntactic or semantic
13relationship between two readings
14
15=head1 DESCRIPTION
16
17Text::Tradition is a library for representation and analysis of collated
18texts, particularly medieval ones. A relationship connects two readings
19within a collation, usually when they appear in the same place in different
20texts.
21
22=head1 CONSTRUCTOR
23
24=head2 new
25
26Creates a new relationship. Usually called via $collation->add_relationship.
27Options include:
28
22222af9 29=over 4
30
0fa07e25 31=item * type - Can be one of spelling, orthographic, grammatical, lexical,
32collated, repetition, transposition. All but the last two are only valid
33relationships between readings that occur at the same point in the text.
34The 'collated' relationship should only be used by parsers to align readings
35in the graph when the input information would otherwise be lost, e.g. from
36an alignment table.
22222af9 37
0fa07e25 38=item * displayform - (Optional) The reading that should be displayed if the
39related nodes are treated as one.
732e9c4e 40
0fa07e25 41=item * scope - (Optional) A meta-attribute. Can be one of 'local',
42'document', or 'global'. Denotes whether the relationship between the two
43readings holds always, independent of context, either within this tradition
44or across all traditions.
4633f9e4 45
0fa07e25 46=item * annotation - (Optional) A freeform note to attach to the relationship.
4633f9e4 47
94654e27 48=item * alters_meaning - Indicate whether, in context, the related words cause
49the text to have different meanings. Possible values are 0 (no), 1 (slightly),
50and >1 (yes).
51
0fa07e25 52=item * non_correctable - (Optional) True if the reading would not have been
53corrected independently.
22222af9 54
0fa07e25 55=item * non_independent - (Optional) True if the variant is unlikely to have
56occurred independently in unrelated witnesses.
22222af9 57
22222af9 58=back
59
027d819c 60=head1 ACCESSORS
61
62=head2 type
63
64=head2 displayform
65
66=head2 scope
67
4633f9e4 68=head2 annotation
69
027d819c 70=head2 non_correctable
71
72=head2 non_independent
73
74See the option descriptions above.
75
22222af9 76=cut
77
78has 'type' => (
79 is => 'ro',
24efa55d 80 isa => 'Str',
22222af9 81 required => 1,
82 );
83
84has 'reading_a' => (
85 is => 'ro',
86 isa => 'Str',
87 required => 1,
88 );
89
90has 'reading_b' => (
91 is => 'ro',
92 isa => 'Str',
93 required => 1,
94 );
95
732e9c4e 96has 'displayform' => (
97 is => 'ro',
98 isa => 'Str',
99 predicate => 'has_displayform',
100 );
101
22222af9 102has 'scope' => (
103 is => 'ro',
104 isa => 'RelationshipScope',
105 default => 'local',
106 );
4633f9e4 107
108has 'annotation' => (
109 is => 'ro',
110 isa => 'Str',
31aaf446 111 predicate => 'has_annotation',
4633f9e4 112 );
94654e27 113
114has 'alters_meaning' => (
115 is => 'rw',
116 isa => 'Int',
117 default => 0,
118 );
22222af9 119
120has 'non_correctable' => (
121 is => 'ro',
122 isa => 'Bool',
123 );
124
125has 'non_independent' => (
126 is => 'ro',
127 isa => 'Bool',
128 );
732e9c4e 129
94654e27 130around 'alters_meaning' => sub {
131 my $orig = shift;
132 my $self = shift;
133 if( @_ ) {
134 if( $_[0] eq 'no' ) {
135 return $self->$orig( 0 );
136 } elsif( $_[0] eq 'slightly' ) {
137 return $self->$orig( 1 );
138 } elsif( $_[0] eq 'yes' ) {
139 return $self->$orig( 2 );
140 }
141 }
142 return $self->$orig( @_ );
143};
144
22222af9 145# A read-only meta-Boolean attribute.
027d819c 146
147=head2 colocated
148
149Returns true if the relationship type is one that requires that its readings
150occupy the same place in the collation.
151
152=cut
153
22222af9 154sub colocated {
155 my $self = shift;
156 return $self->type !~ /^(repetition|transposition)$/;
157}
158
027d819c 159=head2 nonlocal
160
161Returns true if the relationship scope is anything other than 'local'.
162
163=cut
164
22222af9 165sub nonlocal {
166 my $self = shift;
167 return $self->scope ne 'local';
168}
169
24efa55d 170=head2 is_equivalent( $otherrel )
171
172Returns true if the type and scope of $otherrel match ours.
173
174=cut
175
176sub is_equivalent {
177 my( $self, $other, $check_ann ) = @_;
178 my $oksofar = $self->type eq $other->type && $self->scope eq $other->scope;
179 if( $check_ann ) {
180 return $oksofar && $self->annotation eq $other->annotation;
181 } else {
182 return $oksofar;
183 }
184}
185
22222af9 186no Moose;
187__PACKAGE__->meta->make_immutable;
188
1891;