split out persistence / DB functionality
[scpubgit/stemmatology.git] / base / lib / Text / Tradition.pm
CommitLineData
dd3b58b0 1package Text::Tradition;
2
65ed66b9 3use JSON qw / from_json /;
4a8828f0 4use Module::Load;
dd3b58b0 5use Moose;
951ddfe8 6use Moose::Util qw/ does_role apply_all_roles /;
8e1394aa 7use Text::Tradition::Collation;
951ddfe8 8use Text::Tradition::Error;
8e1394aa 9use Text::Tradition::Witness;
cf7e4e7b 10use Text::Tradition::User;
951ddfe8 11use TryCatch;
dd3b58b0 12
331c2dbf 13use vars qw( $VERSION );
37bf09f4 14$VERSION = "1.1";
15
16# Enable plugin(s) if available
17eval { with 'Text::Tradition::HasStemma'; };
ed5b9b70 18# Don't warn normally
19# if( $@ ) {
20# warn "Text::Tradition::Analysis not found. Disabling stemma analysis functionality";
21# };
e92d4229 22eval { with 'Text::Tradition::Language'; };
331c2dbf 23
dd3b58b0 24has 'collation' => (
8e1394aa 25 is => 'ro',
26 isa => 'Text::Tradition::Collation',
27 writer => '_save_collation',
28 );
dd3b58b0 29
3b853983 30has 'witness_hash' => (
31 traits => ['Hash'],
32 isa => 'HashRef[Text::Tradition::Witness]',
8e1394aa 33 handles => {
3b853983 34 witness => 'get',
35 add_witness => 'set',
36 del_witness => 'delete',
37 has_witness => 'exists',
38 witnesses => 'values',
8e1394aa 39 },
3b853983 40 default => sub { {} },
8e1394aa 41 );
c5104dc0 42
df6d9812 43has 'name' => (
44 is => 'rw',
45 isa => 'Str',
46 default => 'Tradition',
47 );
56cf65bd 48
3579c22b 49has '_initialized' => (
10943ab0 50 is => 'ro',
51 isa => 'Bool',
52 default => undef,
53 writer => '_init_done',
54 );
55
f54b1ba7 56has 'user' => (
57 is => 'rw',
58 isa => 'Text::Tradition::User',
59 required => 0,
60 predicate => 'has_user',
ec7ea4e6 61 clearer => 'clear_user',
d8a14401 62 weak_ref => 1
f54b1ba7 63 );
3724dfa7 64
65has 'public' => (
66 is => 'rw',
67 isa => 'Bool',
68 required => 0,
69 default => sub { 0; },
70 );
71
3b853983 72# Create the witness before trying to add it
910a0a6d 73around 'add_witness' => sub {
74 my $orig = shift;
75 my $self = shift;
331c2dbf 76 # TODO allow add of a Witness object?
fae52efd 77 my %args = @_ == 1 ? %{$_[0]} : @_;
78 $args{'tradition'} = $self;
fae52efd 79 my $new_wit = Text::Tradition::Witness->new( %args );
3b853983 80 $self->$orig( $new_wit->sigil => $new_wit );
910a0a6d 81 return $new_wit;
82};
331c2dbf 83
3b853983 84# Allow deletion of witness by object as well as by sigil
85around 'del_witness' => sub {
86 my $orig = shift;
87 my $self = shift;
88 my @key_args;
89 foreach my $arg ( @_ ) {
90 push( @key_args,
91 ref( $arg ) eq 'Text::Tradition::Witness' ? $arg->sigil : $arg );
92 }
93 return $self->$orig( @key_args );
94};
95
96# Don't allow an empty hash value
97around 'witness' => sub {
98 my( $orig, $self, $arg ) = @_;
99 return unless $self->has_witness( $arg );
100 return $self->$orig( $arg );
101};
102
331c2dbf 103=head1 NAME
104
105Text::Tradition - a software model for a set of collated texts
106
107=head1 SYNOPSIS
108
109 use Text::Tradition;
110 my $t = Text::Tradition->new(
111 'name' => 'this is a text',
112 'input' => 'TEI',
113 'file' => '/path/to/tei_parallel_seg_file.xml' );
114
115 my @text_wits = $t->witnesses();
116 my $manuscript_a = $t->witness( 'A' );
82fa4d57 117
118 $t = Text::Tradition->new();
119 $t->add_witness( 'sourcetype' => 'xmldesc',
120 'file' => '/path/to/teitranscription.xml' );
121 $t->add_witness( 'sourcetype => 'plaintext', 'sigil' => 'Q',
122 'string' => 'The quick brown fox jumped over the lazy dogs' );
123 ## TODO
124 $t->collate_texts;
331c2dbf 125
126 my $text_path_svg = $t->collation->as_svg();
127 ## See Text::Tradition::Collation for more on text collation itself
128
129=head1 DESCRIPTION
130
131Text::Tradition is a library for representation and analysis of collated
132texts, particularly medieval ones. A 'tradition' refers to the aggregation
133of surviving versions of a text, generally preserved in multiple
134manuscripts (or 'witnesses'). A Tradition object thus has one more more
135Witnesses, as well as a Collation that represents the unity of all versions
136of the text.
137
138=head1 METHODS
139
140=head2 new
141
142Creates and returns a new text tradition object. The following options are
143accepted.
144
145General options:
146
147=over 4
148
149=item B<name> - The name of the text.
150
151=back
152
153Initialization based on a collation file:
154
155=over 4
156
157=item B<input> - The input format of the collation file. Can be one of the
158following:
159
160=over 4
161
162=item * Self - a GraphML format produced by this module
163
164=item * CollateX - a GraphML format produced by CollateX
165
166=item * CTE - a TEI XML format produced by Classical Text Editor
167
a445ce40 168=item * JSON - an alignment table in JSON format, as produced by CollateX and
169other tools
331c2dbf 170
171=item * TEI - a TEI parallel segmentation format file
172
a445ce40 173=item * Tabular - a spreadsheet collation. See the documentation for
174L<Text::Tradition::Parser::Tabular> for an explanation of additional options.
331c2dbf 175
176=back
177
178=item B<file> - The name of the file that contains the data. One of 'file'
179or 'string' should be specified.
180
181=item B<string> - A text string that contains the data. One of 'file' or
182'string' should be specified.
183
331c2dbf 184=back
185
186Initialization based on a list of witnesses [NOT YET IMPLEMENTED]:
187
188=over 4
189
190=item B<witnesses> - A reference to an array of Text::Tradition::Witness
191objects that carry the text to be collated.
192
193=item B<collator> - A reference to a collation program that will accept
194Witness objects.
195
196=back
197
198=head2 B<witnesses>
199
200Return the Text::Tradition::Witness objects associated with this tradition,
201as an array.
202
044d1e45 203=head2 B<witness>( $sigil )
204
205Returns the Text::Tradition::Witness object whose sigil is $sigil, or undef
206if there is no such object within the tradition.
207
331c2dbf 208=head2 B<add_witness>( %opts )
209
210Instantiate a new witness with the given options (see documentation for
211Text::Tradition::Witness) and add it to the tradition.
212
044d1e45 213=head2 B<del_witness>( $sigil )
214
215Delete the witness with the given sigil from the tradition. Returns the
216witness object for the deleted witness.
217
331c2dbf 218=begin testing
219
220use_ok( 'Text::Tradition', "can use module" );
221
222my $t = Text::Tradition->new( 'name' => 'empty' );
223is( ref( $t ), 'Text::Tradition', "initialized an empty Tradition object" );
224is( $t->name, 'empty', "object has the right name" );
225is( scalar $t->witnesses, 0, "object has no witnesses" );
226
227my $simple = 't/data/simple.txt';
228my $s = Text::Tradition->new(
229 'name' => 'inline',
230 'input' => 'Tabular',
231 'file' => $simple,
232 );
233is( ref( $s ), 'Text::Tradition', "initialized a Tradition object" );
234is( $s->name, 'inline', "object has the right name" );
235is( scalar $s->witnesses, 3, "object has three witnesses" );
236
044d1e45 237my $wit_a = $s->witness('A');
238is( ref( $wit_a ), 'Text::Tradition::Witness', "Found a witness A" );
239if( $wit_a ) {
240 is( $wit_a->sigil, 'A', "Witness A has the right sigil" );
241}
242is( $s->witness('X'), undef, "There is no witness X" );
243ok( !exists $s->{'witnesses'}->{'X'}, "Witness key X not created" );
244
82fa4d57 245my $wit_d = $s->add_witness( 'sigil' => 'D', 'sourcetype' => 'collation' );
044d1e45 246is( ref( $wit_d ), 'Text::Tradition::Witness', "new witness created" );
247is( $wit_d->sigil, 'D', "witness has correct sigil" );
331c2dbf 248is( scalar $s->witnesses, 4, "object now has four witnesses" );
249
3b853983 250my $del = $s->del_witness( 'D' );
044d1e45 251is( $del, $wit_d, "Deleted correct witness" );
3b853983 252is( scalar $s->witnesses, 3, "object has three witnesses again" );
253
331c2dbf 254# TODO test initialization by witness list when we have it
255
256=end testing
257
258=cut
910a0a6d 259
df6d9812 260
8e1394aa 261sub BUILD {
262 my( $self, $init_args ) = @_;
fae52efd 263
264 # First, make a collation object. This will use only those arguments in
265 # init_args that apply to the collation.
266 my $collation = Text::Tradition::Collation->new( %$init_args,
267 'tradition' => $self );
268 $self->_save_collation( $collation );
c5104dc0 269
fae52efd 270 if( exists $init_args->{'input'} ) {
910a0a6d 271 # Call the appropriate parser on the given data
a731e73a 272 my @format_standalone = qw/ Self CollateText CollateX CTE JSON TEI Tabular /;
dfc37e38 273 my @format_basetext = qw/ KUL /;
274 my $use_base;
275 my $format = $init_args->{'input'};
dfc37e38 276 if( $format && !( grep { $_ eq $format } @format_standalone )
277 && !( grep { $_ eq $format } @format_basetext ) ) {
278 warn "Unrecognized input format $format; not parsing";
910a0a6d 279 return;
280 }
dfc37e38 281 if( $format && grep { $_ eq $format } @format_basetext ) {
282 $use_base = 1;
283 if( !exists $init_args->{'base'} ) {
284 warn "Cannot make a collation from $format without a base text";
285 return;
286 }
287 }
4a8828f0 288
910a0a6d 289 # Now do the parsing.
910a0a6d 290 if( $format ) {
dfc37e38 291 if( $use_base ) {
292 $format = 'BaseText'; # Use the BaseText module for parsing,
293 # but retain the original input arg.
910a0a6d 294 }
295 my $mod = "Text::Tradition::Parser::$format";
296 load( $mod );
dfc37e38 297 $mod->can('parse')->( $self, $init_args );
910a0a6d 298 }
c5104dc0 299 }
10943ab0 300 $self->_init_done( 1 );
fae52efd 301 return $self;
302}
303
304=head2 add_json_witnesses( $jsonstring, $options )
305
306Adds a set of witnesses from a JSON array specification. This is a wrapper
307to parse the JSON and call add_witness (with the specified $options) for
308each element therein.
309
310=cut
311
312sub add_json_witnesses {
313 my( $self, $jsonstr, $extraopts ) = @_;
65ed66b9 314 my $witarray = from_json( $jsonstr );
315 foreach my $witspec ( @{$witarray->{witnesses}} ) {
fae52efd 316 my $opts = $extraopts || {};
317 $opts->{'sourcetype'} = 'json';
318 $opts->{'object'} = $witspec;
319 $self->add_witness( $opts );
320 }
8e1394aa 321}
c5104dc0 322
951ddfe8 323sub throw {
324 Text::Tradition::Error->throw(
325 'ident' => 'Tradition error',
326 'message' => $_[0],
327 );
328}
329
dd3b58b0 330no Moose;
331__PACKAGE__->meta->make_immutable;
331c2dbf 332
333
334=head1 BUGS / TODO
335
336=over
337
338=item * Allow tradition to be initialized via passing to a collator.
339
340=back
341
342=head1 LICENSE
343
344This package is free software and is provided "as is" without express
345or implied warranty. You can redistribute it and/or modify it under
346the same terms as Perl itself.
347
348=head1 AUTHOR
349
350Tara L Andrews E<lt>aurum@cpan.orgE<gt>