allow multiple stemmas on a tradition
[scpubgit/stemmatology.git] / lib / Text / Tradition.pm
index f2ae1f6..8801e30 100644 (file)
@@ -3,6 +3,7 @@ package Text::Tradition;
 use Module::Load;
 use Moose;
 use Text::Tradition::Collation;
+use Text::Tradition::Stemma;
 use Text::Tradition::Witness;
 
 use vars qw( $VERSION );
@@ -32,6 +33,18 @@ has 'name' => (
     isa => 'Str',
     default => 'Tradition',
     );
+    
+has 'stemmata' => (
+       traits => ['Array'],
+       isa => 'ArrayRef[Text::Tradition::Stemma]',
+       handles => {
+               all_stemmata => 'elements',
+               _add_stemma => 'push',
+               stemma => 'get',
+               stemma_count => 'count',
+               clear_stemmata => 'clear',
+       },
+       );
   
 # Create the witness before trying to add it
 around 'add_witness' => sub {
@@ -120,6 +133,8 @@ following:
 
 =item * CTE - a TEI XML format produced by Classical Text Editor
 
+=item * JSON - an alignment table in JSON format, as produced by CollateX and other tools
+
 =item * KUL - a specific CSV format for variants, not documented here
 
 =item * TEI - a TEI parallel segmentation format file
@@ -246,7 +261,7 @@ sub BUILD {
         $self->_save_collation( $collation );
 
         # Call the appropriate parser on the given data
-        my @format_standalone = qw/ Self CollateX CTE TEI Tabular /;
+        my @format_standalone = qw/ Self CollateText CollateX CTE JSON TEI Tabular /;
         my @format_basetext = qw/ KUL /;
         my $use_base;
         my $format = $init_args->{'input'};
@@ -276,6 +291,51 @@ sub BUILD {
     }
 }
 
+=head2 add_stemma( $dotfile )
+
+Initializes a Text::Tradition::Stemma object from the given dotfile,
+and associates it with the tradition.
+
+=begin testing
+
+use Text::Tradition;
+
+my $t = Text::Tradition->new( 
+    'name'  => 'simple test', 
+    'input' => 'Tabular',
+    'file'  => 't/data/simple.txt',
+    );
+
+my $s;
+ok( $s = $t->add_stemma( dotfile => 't/data/simple.dot' ), "Added a simple stemma" );
+is( ref( $s ), 'Text::Tradition::Stemma', "Got a stemma object returned" );
+is( $t->stemma_count, 1, "Tradition claims to have a stemma" );
+is( $t->stemma(0), $s, "Tradition hands back the right stemma" );
+
+=end testing
+
+=cut
+
+sub add_stemma {
+       my $self = shift;
+       my %opts = @_;
+       my $stemma_fh;
+       if( $opts{'dotfile'} ) {
+               open $stemma_fh, '<', $opts{'dotfile'}
+                       or warn "Could not open file " . $opts{'dotfile'};
+       } elsif( $opts{'dot'} ) {
+               my $str = $opts{'dot'};
+               open $stemma_fh, '<', \$str;
+       }
+       # Assume utf-8
+       binmode $stemma_fh, ':utf8';
+       my $stemma = Text::Tradition::Stemma->new( 
+               'collation' => $self->collation,
+               'dot' => $stemma_fh );
+       $self->_add_stemma( $stemma ) if $stemma;
+       return $stemma;
+}
+
 no Moose;
 __PACKAGE__->meta->make_immutable;