d2aa2617d787fca0938760827f2a735a83ec81b0
[scpubgit/stemmatology.git] / stemmaweb / lib / stemmaweb / Controller / Stexaminer.pm
1 package stemmaweb::Controller::Stexaminer;
2 use Moose;
3 use namespace::autoclean;
4 use Encode qw/ decode_utf8 /;
5 use File::Temp;
6 use JSON;
7 use Text::Tradition::Analysis qw/ run_analysis wit_stringify /;
8 use Text::Tradition::Collation;
9 use Text::Tradition::Stemma;
10
11 BEGIN { extends 'Catalyst::Controller' }
12
13
14 =head1 NAME
15
16 stemmaweb::Controller::Stexaminer - Simple controller for stemma display
17
18 =head1 DESCRIPTION
19
20 The stemma analysis tool with the pretty colored table.
21
22 =head1 METHODS
23
24 =head2 index
25
26  GET stexaminer/$textid
27  
28 Renders the application for the text identified by $textid.
29
30 =cut
31
32 sub index :Path :Args(1) {
33     my( $self, $c, $textid ) = @_;
34     my $m = $c->model('Directory');
35         my $tradition = $m->tradition( $textid );
36         if( $tradition->stemma_count ) {
37                 my $stemma = $tradition->stemma(0);
38                 $c->stash->{svg} = $stemma->as_svg( { size => [ 600, 350 ] } );
39                 $c->stash->{graphdot} = $stemma->editable({ linesep => ' ' });
40                 $c->stash->{text_title} = $tradition->name;
41                 $c->stash->{template} = 'stexaminer.tt'; 
42                 # TODO Run the analysis as AJAX from the loaded page.
43                 my $t = run_analysis( $tradition, 'exclude_type1' => 1 );
44                 # Stringify the reading groups
45                 foreach my $loc ( @{$t->{'variants'}} ) {
46                         my $mst = wit_stringify( $loc->{'missing'} );
47                         $loc->{'missing'} = $mst;
48                         foreach my $rhash ( @{$loc->{'readings'}} ) {
49                                 my $gst = wit_stringify( $rhash->{'group'} );
50                                 $rhash->{'group'} = $gst;
51                         }
52                 }
53                 # Values for TT rendering
54                 $c->stash->{variants} = $t->{'variants'};
55                 $c->stash->{total} = $t->{'variant_count'};
56                 $c->stash->{genealogical} = $t->{'genealogical_count'};
57                 $c->stash->{conflict} = $t->{'conflict_count'};         
58                 # Also make a JSON stash of the data for the statistics tables
59                 $c->stash->{reading_statistics} = to_json( $t->{'variants'} );
60         } else {
61                 $c->stash->{error} = 'Tradition ' . $tradition->name 
62                         . 'has no stemma for analysis.';
63         }
64 }
65
66 =head2 graphsvg
67
68   POST stexaminer/graphsvg
69         dot: <stemmagraph dot string> 
70         layerwits: [ <a.c. witnesses ] 
71   
72 Returns an SVG string of the given graph, extended to include the given 
73 layered witnesses.
74
75 =cut
76
77 sub graphsvg :Local {
78         my( $self, $c ) = @_;
79         my $dot = $c->request->param('dot');
80         my @layerwits = $c->request->param('layerwits[]');
81         open my $stemma_fh, '<', \$dot;
82         binmode( $stemma_fh, ':encoding(UTF-8)' );
83         my $emptycoll = Text::Tradition::Collation->new();
84         my $tempstemma = Text::Tradition::Stemma->new( 
85                 collation => $emptycoll, 'dot' => $stemma_fh );
86         my $svgopts = { size => [ 600, 350 ] };
87         if( @layerwits ) {
88                 $svgopts->{'layerwits'} = \@layerwits;
89         }
90         $c->stash->{'result'} = $tempstemma->as_svg( $svgopts );
91         $c->forward('View::SVG');
92 }
93
94 =head2 end
95
96 Attempt to render a view, if needed.
97
98 =cut
99
100 sub end : ActionClass('RenderView') {}
101
102 =head1 AUTHOR
103
104 Tara L Andrews
105
106 =head1 LICENSE
107
108 This library is free software. You can redistribute it and/or modify
109 it under the same terms as Perl itself.
110
111 =cut
112
113 __PACKAGE__->meta->make_immutable;
114
115 1;