make the first couple of tests pass
[scpubgit/stemmatology.git] / lib / Text / Tradition.pm
1 package Text::Tradition;
2
3 use Moose;
4 use Text::Tradition::Collation;
5 use Text::Tradition::Witness;
6
7 has 'collation' => (
8     is => 'ro',
9     isa => 'Text::Tradition::Collation',
10     writer => '_save_collation',
11     );
12
13 has 'witnesses' => (
14     traits => ['Array'],
15     is => 'rw',
16     isa => 'ArrayRef[Text::Tradition::Witness]',
17     handles => {
18         all_options    => 'elements',
19         add_option     => 'push',
20         map_options    => 'map',
21         option_count   => 'count',
22         sorted_options => 'sort',
23     },
24     );
25
26 sub BUILD {
27     my( $self, $init_args ) = @_;
28     print STDERR "Calling tradition build\n";
29
30     $DB::single = 1;
31     if( exists $init_args->{'witnesses'} ) {
32         # We got passed an uncollated list of witnesses.  Make a
33         # witness object for each witness, and then send them to the
34         # collator.
35         my $autosigil = 0;
36         foreach my $wit ( %{$init_args->{'witnesses'}} ) {
37             # Each item in the list is either a string or an arrayref.
38             # If it's a string, it is a filename; if it's an arrayref,
39             # it is a tuple of 'sigil, file'.  Handle either case.
40             my $args;
41             if( ref( $wit ) eq 'ARRAY' ) {
42                 $args = { 'sigil' => $wit->[0],
43                           'file' => $wit->[1] };
44             } else {
45                 $args = { 'sigil' => chr( $autosigil+65 ),
46                           'file' => $wit };
47                 $autosigil++;
48             }
49             $self->witnesses->push( Text::Tradition::Witness->new( $args ) );
50             # TODO Now how to collate these?
51         }
52     } else {
53         # Else we got passed args intended for the collator.
54         $init_args->{'tradition'} = $self;
55         $self->_save_collation( Text::Tradition::Collation->new( %$init_args ) );
56         $self->witnesses( $self->collation->create_witnesses() );
57     }
58 }
59
60 # The user will usually be instantiating a Tradition object, and
61 # examining its collation.  The information about the tradition can
62 # come via several routes:
63 # - graphML from CollateX or elsewhere, standalone
64 # - TEI parallel segmentation
65 # - Leuven-style spreadsheet of variants, converted to CSV, plus base text
66 # - apparatus pulled from CTE, plus base text
67 # From this we should be able to get basic witness information.
68
69 # Alternatively the user can just give us the uncollated texts.  Then
70 # instead of passing a collation, s/he is passing a set of witnesses
71 # from which we will generate a collation.  Those witnesses can be in
72 # plaintext or in TEI with certain constraints adopted.
73
74 # So the constructor for a tradition needs to take one of these infosets,
75 # and construct the collation and the witness objects.
76
77 no Moose;
78 __PACKAGE__->meta->make_immutable;