make relationship mapper segments adapt to node+edge size of graph
[scpubgit/stemmatology.git] / stemmaweb / lib / stemmaweb / Controller / Relation.pm
1 package stemmaweb::Controller::Relation;
2 use Moose;
3 use namespace::autoclean;
4 use TryCatch;
5
6 BEGIN { extends 'Catalyst::Controller' }
7
8
9 =head1 NAME
10
11 stemmaweb::Controller::Relation - Controller for the relationship mapper
12
13 =head1 DESCRIPTION
14
15 The reading relationship mapper with draggable nodes.
16
17 =head1 METHODS
18
19 =head2 index
20
21  GET relation/$textid
22  
23 Renders the application for the text identified by $textid.
24
25 =cut
26
27 sub index :Path :Args(0) {
28         my( $self, $c ) = @_;
29         $c->stash->{'template'} = 'relate.tt';
30 }
31
32 =head2 help
33
34  GET relation/help
35
36 Returns the help window HTML.
37
38 =cut
39
40 sub help :Local :Args(0) {
41         my( $self, $c ) = @_;
42         $c->stash->{'template'} = 'relatehelp.tt';
43 }
44
45 =head2 definitions
46
47  GET relation/definitions
48  
49 Returns a data structure giving the valid types and scopes for a relationship.
50
51 =cut
52
53 sub definitions :Local :Args(0) {
54         my( $self, $c ) = @_;
55         my $valid_relationships = [ qw/ spelling orthographic grammatical lexical transposition / ];
56         my $valid_scopes = [ qw/ local global / ];
57         $c->stash->{'result'} = { 'types' => $valid_relationships, 'scopes' => $valid_scopes };
58         $c->forward('View::JSON');
59 }
60
61 =head2 text
62
63  GET relation/$textid/
64  
65  Runs the relationship mapper for the specified text ID.
66  
67 =cut
68
69 sub text :Chained('/') :PathPart('relation') :CaptureArgs(1) {
70         my( $self, $c, $textid ) = @_;
71         # If the tradition has more than 500 ranks or so, split it up.
72         my $tradition = $c->model('Directory')->tradition( $textid );
73         # See how big the tradition is. Edges are more important than nodes
74         # when it comes to rendering difficulty.
75         my $numnodes = scalar $tradition->collation->readings;
76         my $numedges = scalar $tradition->collation->paths;
77         my $length = $tradition->collation->end->rank;
78         # We should display no more than roughly 500 nodes, or roughly 700
79         # edges, at a time.
80         my $segments = $numnodes / 500;
81         if( $numedges / 700 > $segments ) {
82                 $segments = $numedges / 700;
83         }
84         my $segsize = sprintf( "%.0f", $length / $segments );
85         my $margin = sprintf( "%.0f", $segsize / 10 );
86         if( $segments > 1 ) {
87                 # Segment the tradition in order not to overload the browser.
88                 my @divs;
89                 my $r = 0;
90                 while( $r + $margin < $length ) {
91                         push( @divs, $r );
92                         $r += $segsize;
93                 }
94                 $c->stash->{'textsegments'} = [];
95                 $c->stash->{'segsize'} = $segsize;
96                 $c->stash->{'margin'} = $margin;
97                 foreach my $i ( 0..$#divs ) {
98                         my $seg = { 'start' => $divs[$i] };
99                         $seg->{'display'} = "Segment " . ($i+1);
100                         push( @{$c->stash->{'textsegments'}}, $seg );
101                 }
102         }
103         $DB::single = 1;
104         $c->stash->{'textid'} = $textid;
105         $c->stash->{'tradition'} = $tradition;
106 }
107
108 sub main :Chained('text') :PathPart('') :Args(0) {
109         my( $self, $c ) = @_;
110         my $startseg = $c->req->param('start');
111         my $tradition = delete $c->stash->{'tradition'};
112         my $collation = $tradition->collation;
113         my $svgopts;
114         if( $startseg ) {
115                 # Only render the subgraph from startseg to endseg or to END,
116                 # whichever is less.
117                 my $endseg = $startseg + $c->stash->{'segsize'} + $c->stash->{'margin'};
118                 $svgopts = { 'from' => $startseg };
119                 $svgopts->{'to'} = $endseg if $endseg < $collation->end->rank;
120         } elsif( exists $c->stash->{'textsegments'} ) {
121                 # This is the unqualified load of a long tradition. We implicitly start 
122                 # at zero, but go only as far as 550.
123                 my $endseg = $c->stash->{'segsize'} + $c->stash->{'margin'};
124                 $startseg = 0;
125                 $svgopts = { 'to' => $endseg };
126         }
127         my $svg_str = $collation->as_svg( $svgopts );
128         $svg_str =~ s/\n//gs;
129         $c->stash->{'startseg'} = $startseg if defined $startseg;
130         $c->stash->{'svg_string'} = $svg_str;
131         $c->stash->{'text_title'} = $tradition->name;
132         $c->stash->{'template'} = 'relate.tt';
133 }
134
135 =head2 relationships
136
137  GET relation/$textid/relationships
138
139 Returns the list of relationships defined for this text.
140
141  POST relation/$textid/relationships { request }
142  
143 Attempts to define the requested relationship within the text. Returns 200 on
144 success or 403 on error.
145
146  DELETE relation/$textid/relationships { request }
147  
148
149 =cut
150
151 sub relationships :Chained('text') :PathPart :Args(0) {
152         my( $self, $c ) = @_;
153         my $tradition = delete $c->stash->{'tradition'};
154         my $collation = $tradition->collation;
155         my $m = $c->model('Directory');
156         if( $c->request->method eq 'GET' ) {
157                 my @pairs = $collation->relationships; # returns the edges
158                 my @all_relations;
159                 foreach my $p ( @pairs ) {
160                         my $relobj = $collation->relations->get_relationship( @$p );
161                         next if $relobj->type eq 'collated'; # Don't show these
162                         my $relhash = { source => $p->[0], target => $p->[1], 
163                                   type => $relobj->type, scope => $relobj->scope };
164                         $relhash->{'note'} = $relobj->annotation if $relobj->has_annotation;
165                         push( @all_relations, $relhash );
166                 }
167                 $c->stash->{'result'} = \@all_relations;
168         } elsif( $c->request->method eq 'POST' ) {
169                 my $node = $c->request->param('source_id');
170                 my $target = $c->request->param('target_id');
171                 my $relation = $c->request->param('rel_type');
172                 my $note = $c->request->param('note');
173                 my $scope = $c->request->param('scope');
174         
175                 my $opts = { 'type' => $relation,
176                                          'scope' => $scope };
177                 $opts->{'annotation'} = $note if $note;
178                 
179                 try {
180                         my @vectors = $collation->add_relationship( $node, $target, $opts );
181                         $c->stash->{'result'} = \@vectors;
182                         $m->save( $tradition );
183                 } catch( Text::Tradition::Error $e ) {
184                         $c->response->status( '403' );
185                         $c->stash->{'result'} = { 'error' => $e->message };
186                 }
187         } elsif( $c->request->method eq 'DELETE' ) {
188                 my $node = $c->request->param('source_id');
189                 my $target = $c->request->param('target_id');
190         
191                 try {
192                         my @vectors = $collation->del_relationship( $node, $target );
193                         $m->save( $tradition );
194                         $c->stash->{'result'} = \@vectors;
195                 } catch( Text::Tradition::Error $e ) {
196                         $c->response->status( '403' );
197                         $c->stash->{'result'} = { 'error' => $e->message };
198                 }       
199         }
200         $c->forward('View::JSON');
201 }               
202                 
203
204 =head2 end
205
206 Attempt to render a view, if needed.
207
208 =cut
209
210 sub end : ActionClass('RenderView') {}
211
212 =head1 AUTHOR
213
214 Tara L Andrews
215
216 =head1 LICENSE
217
218 This library is free software. You can redistribute it and/or modify
219 it under the same terms as Perl itself.
220
221 =cut
222
223 __PACKAGE__->meta->make_immutable;
224
225 1;