finish and test record_stemweb_result for Tradition (#12)
[scpubgit/stemmatology.git] / analysis / lib / Text / Tradition / HasStemma.pm
index c797fab..68e04f5 100644 (file)
@@ -3,15 +3,22 @@ package Text::Tradition::HasStemma;
 use strict;
 use warnings;
 use Moose::Role;
+use Date::Parse;
 use Text::Tradition::Stemma;
+use Text::Tradition::StemmaUtil qw/ parse_newick /;
 
 =head1 NAME
 
-Text::Tradition::HasStemma - add-on to associate stemma hypotheses to Text::Tradition objects
+Text::Tradition::HasStemma - add-on to associate stemma hypotheses to
+Text::Tradition objects
 
 =head1 DESCRIPTION
 
-It is often the case that, for a given text tradition, the order of copying of the witnesses can or should be reconstructed (or at least the attempt should be made.) This class is a role that can be applied to Text::Tradition objects to record stemma hypotheses.  See the documentation for L<Text::Tradition::Stemma> for more information.
+It is often the case that, for a given text tradition, the order of copying
+of the witnesses can or should be reconstructed (or at least the attempt
+should be made.) This class is a role that can be applied to
+Text::Tradition objects to record stemma hypotheses.  See the documentation
+for L<Text::Tradition::Stemma> for more information.
 
 =head1 METHODS
 
@@ -31,6 +38,20 @@ Return the L<Text::Tradition::Stemma> object identified by the given index.
 
 Delete all stemma hypotheses associated with this tradition.
 
+=head2 has_stemweb_jobid
+
+Returns true if there is currently a Stemweb job ID, indicating that a
+stemma tree calculation from the Stemweb service is in process.
+
+=head2 stemweb_jobid
+
+Return the currently-running job ID (if any) for calculation of Stemweb 
+trees.
+
+=head2 set_stemweb_jobid( $jobid )
+
+Record a job ID for a Stemweb calculation.
+
 =cut
 
 has 'stemmata' => (
@@ -46,6 +67,21 @@ has 'stemmata' => (
        default => sub { [] },
        );
   
+has 'stemweb_jobid' => (
+       is => 'ro',
+       isa => 'Str',
+       writer => 'set_stemweb_jobid',
+       predicate => 'has_stemweb_jobid',
+       clearer => '_clear_stemweb_jobid',
+       );
+       
+before 'set_stemweb_jobid' => sub {
+       my( $self ) = shift;
+       if( $self->has_stemweb_jobid ) {
+               $self->throw( "Tradition already has a Stemweb jobid: "
+                       . $self->stemweb_jobid );
+       }
+};
 
 =head2 add_stemma( $dotfile )
 
@@ -61,7 +97,6 @@ my $t = Text::Tradition->new(
     'input' => 'Tabular',
     'file'  => 't/data/simple.txt',
     );
-$t->enable_stemmata;
 is( $t->stemma_count, 0, "No stemmas added yet" );
 my $s;
 ok( $s = $t->add_stemma( dotfile => 't/data/simple.dot' ), "Added a simple stemma" );
@@ -92,6 +127,57 @@ sub add_stemma {
        return $stemma;
 }
 
+=head2 record_stemweb_result( $format, $data )
+
+Records the result returned by a Stemweb calculation, and clears any
+existing job ID.
+
+=begin testing
+
+use Text::Tradition;
+use JSON qw/ from_json /;
+
+my $t = Text::Tradition->new( 
+    'name'  => 'Stemweb test', 
+    'input' => 'Self',
+    'file'  => 't/data/besoin.xml',
+    'stemweb_jobid' => '4',
+    );
+
+is( $t->stemma_count, 0, "No stemmas added yet" );
+
+my $answer = from_json( '{"status": 0, "job_id": "4", "algorithm": "RHM", "format": "newick", "start_time": "2013-10-26 10:44:14.050263", "result": "((((((((((((F,U),V),S),T1),T2),A),J),B),L),D),M),C);\n", "end_time": "2013-10-26 10:45:55.398944"}' );
+$t->record_stemweb_result( $answer );
+ok( !$t->has_stemweb_jobid, "Job ID was removed from tradition" );
+is( $t->stemma_count, 1, "Tradition has new stemma" );
+ok( $t->stemma(0)->is_undirected, "New stemma is undirected as it should be" );
+is( $t->stemma(0)->identifier, "RHM 1382777054_0", "Stemma has correct identifier" );
+
+
+=end testing
+
+=cut
+
+sub record_stemweb_result {
+       my( $self, $answer ) = @_;
+       if( $answer->{format} eq 'dot' ) {
+               $self->add_stemma( dot => $answer->{result} );
+       } elsif( $answer->{format} eq 'newick' ) {
+               my $stemmata = parse_newick( $answer->{result} );
+               my $title = sprintf( "%s %d", $answer->{algorithm}, 
+                       str2time( $answer->{start_time} ) );
+               my $i = 0;
+               foreach my $stemma ( @$stemmata ) {
+                       my $ititle = $title . "_$i"; $i++;
+                       $stemma->set_identifier( $ititle );
+                       $self->_add_stemma( $stemma );
+               }
+       } else {
+               $self->throw( "Cannot parse tree results with format " . $answer->{format} );
+       }
+       $self->_clear_stemweb_jobid();
+}
+
 1;
 
 =head1 LICENSE