Rework Stemma / StemmaUtil so that utility functions are all in the latter. Fixes #14
[scpubgit/stemmatology.git] / analysis / lib / Text / Tradition / HasStemma.pm
index 70bab13..18ecc67 100644 (file)
@@ -3,6 +3,7 @@ package Text::Tradition::HasStemma;
 use strict;
 use warnings;
 use Moose::Role;
+use Date::Parse;
 use Text::Tradition::Stemma;
 
 =head1 NAME
@@ -36,6 +37,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' => (
@@ -51,6 +66,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 )
 
@@ -96,6 +126,64 @@ 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. Returns any new stemmata that were created.
+
+=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"}' );
+my $newst = $t->record_stemweb_result( $answer );
+is( scalar @$newst, 1, "New stemma was returned from record_stemweb_result" );
+is( $newst->[0], $t->stemma(0), "Answer has the right object" );
+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" );
+is( $t->stemma(0)->from_jobid, 4, "New stemma has correct associated job ID" );
+
+
+=end testing
+
+=cut
+
+sub record_stemweb_result {
+       my( $self, $answer ) = @_;
+       my $jobid = $self->stemweb_jobid;
+       my $stemmata = [];
+       if( $answer->{format} eq 'dot' ) {
+               $self->add_stemma( dot => $answer->{result} );
+       } elsif( $answer->{format} eq 'newick' ) {
+               $stemmata = Text::Tradition::Stemma->new_from_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 );
+                       $stemma->_set_from_jobid( $jobid );
+                       $self->_add_stemma( $stemma );
+               }
+       } else {
+               $self->throw( "Cannot parse tree results with format " . $answer->{format} );
+       }
+       $self->_clear_stemweb_jobid();
+       return $stemmata;
+}
+
 1;
 
 =head1 LICENSE