Rework Stemma / StemmaUtil so that utility functions are all in the latter. Fixes #14
[scpubgit/stemmatology.git] / analysis / lib / Text / Tradition / HasStemma.pm
1 package Text::Tradition::HasStemma;
2
3 use strict;
4 use warnings;
5 use Moose::Role;
6 use Date::Parse;
7 use Text::Tradition::Stemma;
8
9 =head1 NAME
10
11 Text::Tradition::HasStemma - add-on to associate stemma hypotheses to
12 Text::Tradition objects
13
14 =head1 DESCRIPTION
15
16 It is often the case that, for a given text tradition, the order of copying
17 of the witnesses can or should be reconstructed (or at least the attempt
18 should be made.) This class is a role that can be applied to
19 Text::Tradition objects to record stemma hypotheses.  See the documentation
20 for L<Text::Tradition::Stemma> for more information.
21
22 =head1 METHODS
23
24 =head2 stemmata
25
26 Return a list of all stemmata associated with the tradition.
27
28 =head2 stemma_count
29
30 Return the number of stemma hypotheses defined for this tradition.
31
32 =head2 stemma( $idx )
33
34 Return the L<Text::Tradition::Stemma> object identified by the given index.
35
36 =head2 clear_stemmata
37
38 Delete all stemma hypotheses associated with this tradition.
39
40 =head2 has_stemweb_jobid
41
42 Returns true if there is currently a Stemweb job ID, indicating that a
43 stemma tree calculation from the Stemweb service is in process.
44
45 =head2 stemweb_jobid
46
47 Return the currently-running job ID (if any) for calculation of Stemweb 
48 trees.
49
50 =head2 set_stemweb_jobid( $jobid )
51
52 Record a job ID for a Stemweb calculation.
53
54 =cut
55
56 has 'stemmata' => (
57         traits => ['Array'],
58         isa => 'ArrayRef[Text::Tradition::Stemma]',
59         handles => {
60                 stemmata => 'elements',
61                 _add_stemma => 'push',
62                 stemma => 'get',
63                 stemma_count => 'count',
64                 clear_stemmata => 'clear',
65         },
66         default => sub { [] },
67         );
68   
69 has 'stemweb_jobid' => (
70         is => 'ro',
71         isa => 'Str',
72         writer => 'set_stemweb_jobid',
73         predicate => 'has_stemweb_jobid',
74         clearer => '_clear_stemweb_jobid',
75         );
76         
77 before 'set_stemweb_jobid' => sub {
78         my( $self ) = shift;
79         if( $self->has_stemweb_jobid ) {
80                 $self->throw( "Tradition already has a Stemweb jobid: "
81                         . $self->stemweb_jobid );
82         }
83 };
84
85 =head2 add_stemma( $dotfile )
86
87 Initializes a Text::Tradition::Stemma object from the given dotfile,
88 and associates it with the tradition.
89
90 =begin testing
91
92 use Text::Tradition;
93
94 my $t = Text::Tradition->new( 
95     'name'  => 'simple test', 
96     'input' => 'Tabular',
97     'file'  => 't/data/simple.txt',
98     );
99 is( $t->stemma_count, 0, "No stemmas added yet" );
100 my $s;
101 ok( $s = $t->add_stemma( dotfile => 't/data/simple.dot' ), "Added a simple stemma" );
102 is( ref( $s ), 'Text::Tradition::Stemma', "Got a stemma object returned" );
103 is( $t->stemma_count, 1, "Tradition claims to have a stemma" );
104 is( $t->stemma(0), $s, "Tradition hands back the right stemma" );
105
106 =end testing
107
108 =cut
109
110 sub add_stemma {
111         my $self = shift;
112         my %opts = @_;
113         my $stemma_fh;
114         if( $opts{'dotfile'} ) {
115                 open $stemma_fh, '<', $opts{'dotfile'}
116                         or warn "Could not open file " . $opts{'dotfile'};
117         } elsif( $opts{'dot'} ) {
118                 my $str = $opts{'dot'};
119                 open $stemma_fh, '<', \$str;
120         }
121         # Assume utf-8
122         binmode $stemma_fh, ':utf8';
123         my $stemma = Text::Tradition::Stemma->new( 
124                 'dot' => $stemma_fh );
125         $self->_add_stemma( $stemma ) if $stemma;
126         return $stemma;
127 }
128
129 =head2 record_stemweb_result( $format, $data )
130
131 Records the result returned by a Stemweb calculation, and clears any
132 existing job ID. Returns any new stemmata that were created.
133
134 =begin testing
135
136 use Text::Tradition;
137 use JSON qw/ from_json /;
138
139 my $t = Text::Tradition->new( 
140     'name'  => 'Stemweb test', 
141     'input' => 'Self',
142     'file'  => 't/data/besoin.xml',
143     'stemweb_jobid' => '4',
144     );
145
146 is( $t->stemma_count, 0, "No stemmas added yet" );
147
148 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"}' );
149 my $newst = $t->record_stemweb_result( $answer );
150 is( scalar @$newst, 1, "New stemma was returned from record_stemweb_result" );
151 is( $newst->[0], $t->stemma(0), "Answer has the right object" );
152 ok( !$t->has_stemweb_jobid, "Job ID was removed from tradition" );
153 is( $t->stemma_count, 1, "Tradition has new stemma" );
154 ok( $t->stemma(0)->is_undirected, "New stemma is undirected as it should be" );
155 is( $t->stemma(0)->identifier, "RHM 1382777054_0", "Stemma has correct identifier" );
156 is( $t->stemma(0)->from_jobid, 4, "New stemma has correct associated job ID" );
157
158
159 =end testing
160
161 =cut
162
163 sub record_stemweb_result {
164         my( $self, $answer ) = @_;
165         my $jobid = $self->stemweb_jobid;
166         my $stemmata = [];
167         if( $answer->{format} eq 'dot' ) {
168                 $self->add_stemma( dot => $answer->{result} );
169         } elsif( $answer->{format} eq 'newick' ) {
170                 $stemmata = Text::Tradition::Stemma->new_from_newick( $answer->{result} );
171                 my $title = sprintf( "%s %d", $answer->{algorithm}, 
172                         str2time( $answer->{start_time} ) );
173                 my $i = 0;
174                 foreach my $stemma ( @$stemmata ) {
175                         my $ititle = $title . "_$i"; $i++;
176                         $stemma->set_identifier( $ititle );
177                         $stemma->_set_from_jobid( $jobid );
178                         $self->_add_stemma( $stemma );
179                 }
180         } else {
181                 $self->throw( "Cannot parse tree results with format " . $answer->{format} );
182         }
183         $self->_clear_stemweb_jobid();
184         return $stemmata;
185 }
186
187 1;
188
189 =head1 LICENSE
190
191 This package is free software and is provided "as is" without express
192 or implied warranty.  You can redistribute it and/or modify it under
193 the same terms as Perl itself.
194
195 =head1 AUTHOR
196
197 Tara L Andrews E<lt>aurum@cpan.orgE<gt>