flatten identical nodes by rank; allow alignment table with objects
[scpubgit/stemmatology.git] / lib / Text / Tradition / Collation.pm
CommitLineData
dd3b58b0 1package Text::Tradition::Collation;
d047cd52 2
910a0a6d 3use Encode qw( decode_utf8 );
4use File::Temp;
d047cd52 5use Graph::Easy;
8e1394aa 6use IPC::Run qw( run binary );
910a0a6d 7use Text::CSV_XS;
3265b0ce 8use Text::Tradition::Collation::Path;
b15511bf 9use Text::Tradition::Collation::Reading;
10use Text::Tradition::Collation::Relationship;
11use Text::Tradition::Collation::Segment;
df6d9812 12use XML::LibXML;
dd3b58b0 13use Moose;
14
15has 'graph' => (
d047cd52 16 is => 'ro',
17 isa => 'Graph::Easy',
18 handles => {
910a0a6d 19 add_reading => 'add_node',
eca16057 20 add_lacuna => 'add_node',
910a0a6d 21 del_reading => 'del_node',
22 del_segment => 'del_node',
23 add_path => 'add_edge',
24 del_path => 'del_edge',
25 reading => 'node',
26 path => 'edge',
27 readings => 'nodes',
28 segments => 'nodes',
29 paths => 'edges',
30 relationships => 'edges',
d047cd52 31 },
32 default => sub { Graph::Easy->new( undirected => 0 ) },
33 );
910a0a6d 34
dd3b58b0 35
f6066bac 36has 'tradition' => ( # TODO should this not be ro?
8e1394aa 37 is => 'rw',
d047cd52 38 isa => 'Text::Tradition',
39 );
dd3b58b0 40
8e1394aa 41has 'svg' => (
42 is => 'ro',
43 isa => 'Str',
44 writer => '_save_svg',
45 predicate => 'has_svg',
46 );
47
8e1394aa 48has 'graphml' => (
49 is => 'ro',
df6d9812 50 isa => 'Str',
8e1394aa 51 writer => '_save_graphml',
52 predicate => 'has_graphml',
53 );
54
910a0a6d 55has 'csv' => (
56 is => 'ro',
57 isa => 'Str',
58 writer => '_save_csv',
59 predicate => 'has_csv',
60 );
61
3a1f2523 62# Keeps track of the lemmas within the collation. At most one lemma
63# per position in the graph.
64has 'lemmata' => (
65 is => 'ro',
66 isa => 'HashRef[Maybe[Str]]',
67 default => sub { {} },
68 );
69
4a8828f0 70has 'wit_list_separator' => (
7854e12e 71 is => 'rw',
72 isa => 'Str',
73 default => ', ',
74 );
75
76has 'baselabel' => (
77 is => 'rw',
78 isa => 'Str',
79 default => 'base text',
80 );
4a8828f0 81
1f563ac3 82has 'collapsed' => (
15d2d3df 83 is => 'rw',
84 isa => 'Bool',
85 );
86
87has 'linear' => (
88 is => 'rw',
89 isa => 'Bool',
90 default => 1,
91 );
1f563ac3 92
ef9d481f 93has 'ac_label' => (
94 is => 'rw',
95 isa => 'Str',
96 default => ' (a.c.)',
97 );
98
1f563ac3 99
dd3b58b0 100# The collation can be created two ways:
101# 1. Collate a set of witnesses (with CollateX I guess) and process
102# the results as in 2.
103# 2. Read a pre-prepared collation in one of a variety of formats,
104# and make the graph from that.
105
106# The graph itself will (for now) be immutable, and the positions
107# within the graph will also be immutable. We need to calculate those
108# positions upon graph construction. The equivalences between graph
109# nodes will be mutable, entirely determined by the user (or possibly
110# by some semantic pre-processing provided by the user.) So the
111# constructor should just make an empty equivalences object. The
112# constructor will also need to make the witness objects, if we didn't
113# come through option 1.
114
d047cd52 115sub BUILD {
116 my( $self, $args ) = @_;
8e1394aa 117 $self->graph->use_class('node', 'Text::Tradition::Collation::Reading');
3265b0ce 118 $self->graph->use_class('edge', 'Text::Tradition::Collation::Path');
d047cd52 119
4a8828f0 120 # Pass through any graph-specific options.
121 my $shape = exists( $args->{'shape'} ) ? $args->{'shape'} : 'ellipse';
122 $self->graph->set_attribute( 'node', 'shape', $shape );
d047cd52 123}
784877d9 124
eca16057 125around add_lacuna => sub {
126 my $orig = shift;
127 my $self = shift;
128 my $id = shift @_;
129 my $l = $self->$orig( '#LACUNA_' . $id . '#' );
130 $l->is_lacuna( 1 );
131 return $l;
132};
133
7854e12e 134# Wrapper around add_path
135
136around add_path => sub {
137 my $orig = shift;
138 my $self = shift;
139
140 # Make sure there are three arguments
141 unless( @_ == 3 ) {
910a0a6d 142 warn "Call add_path with args source, target, witness";
143 return;
7854e12e 144 }
145 # Make sure the proposed path does not yet exist
b15511bf 146 # NOTE 'reading' will currently return readings and segments
7854e12e 147 my( $source, $target, $wit ) = @_;
148 $source = $self->reading( $source )
910a0a6d 149 unless ref( $source ) eq 'Text::Tradition::Collation::Reading';
7854e12e 150 $target = $self->reading( $target )
910a0a6d 151 unless ref( $target ) eq 'Text::Tradition::Collation::Reading';
7854e12e 152 foreach my $path ( $source->edges_to( $target ) ) {
910a0a6d 153 if( $path->label eq $wit && $path->class eq 'edge.path' ) {
154 return;
155 }
7854e12e 156 }
157 # Do the deed
158 $self->$orig( @_ );
159};
160
3265b0ce 161# Wrapper around paths
162around paths => sub {
163 my $orig = shift;
164 my $self = shift;
165
b15511bf 166 my @result = grep { $_->sub_class eq 'path' } $self->$orig( @_ );
df6d9812 167 return @result;
168};
169
170around relationships => sub {
171 my $orig = shift;
172 my $self = shift;
b15511bf 173 my @result = grep { $_->sub_class eq 'relationship' } $self->$orig( @_ );
174 return @result;
175};
176
177around readings => sub {
178 my $orig = shift;
179 my $self = shift;
180 my @result = grep { $_->sub_class ne 'segment' } $self->$orig( @_ );
181 return @result;
182};
183
184around segments => sub {
185 my $orig = shift;
186 my $self = shift;
187 my @result = grep { $_->sub_class eq 'segment' } $self->$orig( @_ );
3265b0ce 188 return @result;
189};
190
7854e12e 191# Wrapper around merge_nodes
784877d9 192
193sub merge_readings {
194 my $self = shift;
195 my $first_node = shift;
196 my $second_node = shift;
197 $first_node->merge_from( $second_node );
198 unshift( @_, $first_node, $second_node );
199 return $self->graph->merge_nodes( @_ );
200}
201
15d2d3df 202# Extra graph-alike utility
203sub has_path {
204 my( $self, $source, $target, $label ) = @_;
205 my @paths = $source->edges_to( $target );
206 my @relevant = grep { $_->label eq $label } @paths;
b15511bf 207 return scalar @relevant;
208}
209
210## Dealing with groups of readings, i.e. segments.
211
212sub add_segment {
213 my( $self, @items ) = @_;
214 my $segment = Text::Tradition::Collation::Segment->new( 'members' => \@items );
215 return $segment;
15d2d3df 216}
217
3265b0ce 218## Dealing with relationships between readings. This is a different
4cdd82f1 219## sort of graph edge. Return a success/failure value and a list of
220## node pairs that have been linked.
3265b0ce 221
222sub add_relationship {
b15511bf 223 my( $self, $source, $target, $options ) = @_;
ef9d481f 224
225 # Make sure there is not another relationship between these two
b15511bf 226 # readings or segments already
ef9d481f 227 $source = $self->reading( $source )
910a0a6d 228 unless ref( $source ) && $source->isa( 'Graph::Easy::Node' );
ef9d481f 229 $target = $self->reading( $target )
910a0a6d 230 unless ref( $target ) && $target->isa( 'Graph::Easy::Node' );
4cdd82f1 231 foreach my $rel ( $source->edges_to( $target ), $target->edges_to( $source ) ) {
910a0a6d 232 if( $rel->class eq 'edge.relationship' ) {
233 return ( undef, "Relationship already exists between these readings" );
234 }
4cdd82f1 235 }
910a0a6d 236 if( $options->{'equal_rank'} && !relationship_valid( $source, $target ) ) {
237 return ( undef, 'Relationship creates witness loop' );
ef9d481f 238 }
239
910a0a6d 240 # TODO Think about positional hilarity if relationships are added after positions
241 # are assigned.
242
4cdd82f1 243 my @joined = ( [ $source->name, $target->name ] ); # Keep track of the nodes we join.
244
245 $options->{'this_relation'} = [ $source, $target ];
f6066bac 246 my $rel;
247 eval { $rel = Text::Tradition::Collation::Relationship->new( %$options ) };
248 if( $@ ) {
249 return ( undef, $@ );
250 }
3265b0ce 251 $self->graph->add_edge( $source, $target, $rel );
910a0a6d 252
253 # TODO Handle global relationship setting
254
4cdd82f1 255 return( 1, @joined );
3265b0ce 256}
257
910a0a6d 258sub relationship_valid {
259 my( $source, $target ) = @_;
260 # Check that linking the source and target in a relationship won't lead
261 # to a path loop for any witness.
262 my @proposed_related = ( $source, $target );
263 push( @proposed_related, $source->related_readings );
264 push( @proposed_related, $target->related_readings );
265 my %pr_ids;
266 map { $pr_ids{ $_->name } = 1 } @proposed_related;
267 # The lists of 'in' and 'out' should not have any element that appears
268 # in 'proposed_related'.
269 foreach my $pr ( @proposed_related ) {
270 foreach my $e ( $pr->incoming ) {
271 if( exists $pr_ids{ $e->from->name } ) {
272 return 0;
273 }
274 }
275 foreach my $e ( $pr->outgoing ) {
276 if( exists $pr_ids{ $e->to->name } ) {
277 return 0;
278 }
279 }
280 }
281 return 1;
282}
283
8e1394aa 284=head2 Output method(s)
285
286=over
287
288=item B<as_svg>
289
290print $graph->as_svg( $recalculate );
291
292Returns an SVG string that represents the graph. Uses GraphViz to do
4a8828f0 293this, because Graph::Easy doesn\'t cope well with long graphs. Unless
8e1394aa 294$recalculate is passed (and is a true value), the method will return a
295cached copy of the SVG after the first call to the method.
296
297=cut
298
299sub as_svg {
300 my( $self, $recalc ) = @_;
301 return $self->svg if $self->has_svg;
302
3265b0ce 303 $self->collapse_graph_paths();
8e1394aa 304
305 my @cmd = qw/dot -Tsvg/;
306 my( $svg, $err );
910a0a6d 307 my $dotfile = File::Temp->new();
d9e873d0 308 ## TODO REMOVE
eca16057 309 # $dotfile->unlink_on_destroy(0);
910a0a6d 310 binmode $dotfile, ':utf8';
311 print $dotfile $self->as_dot();
312 push( @cmd, $dotfile->filename );
313 run( \@cmd, ">", binary(), \$svg );
314 $svg = decode_utf8( $svg );
df6d9812 315 $self->_save_svg( $svg );
3265b0ce 316 $self->expand_graph_paths();
8e1394aa 317 return $svg;
318}
319
df6d9812 320=item B<as_dot>
321
322print $graph->as_dot( $view, $recalculate );
323
324Returns a string that is the collation graph expressed in dot
325(i.e. GraphViz) format. The 'view' argument determines what kind of
326graph is produced.
327 * 'path': a graph of witness paths through the collation (DEFAULT)
328 * 'relationship': a graph of how collation readings relate to
329 each other
330
331=cut
332
333sub as_dot {
334 my( $self, $view ) = @_;
335 $view = 'path' unless $view;
336 # TODO consider making some of these things configurable
337 my $dot = sprintf( "digraph %s {\n", $self->tradition->name );
338 $dot .= "\tedge [ arrowhead=open ];\n";
339 $dot .= "\tgraph [ rankdir=LR ];\n";
340 $dot .= sprintf( "\tnode [ fontsize=%d, fillcolor=%s, style=%s, shape=%s ];\n",
910a0a6d 341 11, "white", "filled", $self->graph->get_attribute( 'node', 'shape' ) );
df6d9812 342
343 foreach my $reading ( $self->readings ) {
910a0a6d 344 # Need not output nodes without separate labels
345 next if $reading->name eq $reading->label;
346 # TODO output readings or segments, but not both
347 next if $reading->class eq 'node.segment';
348 $dot .= sprintf( "\t\"%s\" [ label=\"%s\" ];\n", $reading->name, $reading->label );
df6d9812 349 }
350
351 my @edges = $view eq 'relationship' ? $self->relationships : $self->paths;
352 foreach my $edge ( @edges ) {
910a0a6d 353 my %variables = ( 'color' => '#000000',
354 'fontcolor' => '#000000',
355 'label' => $edge->label,
356 );
357 my $varopts = join( ', ', map { $_.'="'.$variables{$_}.'"' } sort keys %variables );
358 $dot .= sprintf( "\t\"%s\" -> \"%s\" [ %s ];\n",
359 $edge->from->name, $edge->to->name, $varopts );
df6d9812 360 }
df6d9812 361 $dot .= "}\n";
362 return $dot;
363}
364
8e1394aa 365=item B<as_graphml>
366
367print $graph->as_graphml( $recalculate )
368
369Returns a GraphML representation of the collation graph, with
370transposition information and position information. Unless
371$recalculate is passed (and is a true value), the method will return a
372cached copy of the SVG after the first call to the method.
373
374=cut
375
376sub as_graphml {
377 my( $self, $recalc ) = @_;
378 return $self->graphml if $self->has_graphml;
379
380 # Some namespaces
381 my $graphml_ns = 'http://graphml.graphdrawing.org/xmlns';
382 my $xsi_ns = 'http://www.w3.org/2001/XMLSchema-instance';
383 my $graphml_schema = 'http://graphml.graphdrawing.org/xmlns ' .
910a0a6d 384 'http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd';
8e1394aa 385
386 # Create the document and root node
387 my $graphml = XML::LibXML->createDocument( "1.0", "UTF-8" );
388 my $root = $graphml->createElementNS( $graphml_ns, 'graphml' );
389 $graphml->setDocumentElement( $root );
390 $root->setNamespace( $xsi_ns, 'xsi', 0 );
391 $root->setAttributeNS( $xsi_ns, 'schemaLocation', $graphml_schema );
392
f6066bac 393 # TODO Add some global graph data
394
8e1394aa 395 # Add the data keys for nodes
ef9d481f 396 my %node_data_keys;
397 my $ndi = 0;
d9e873d0 398 foreach my $datum ( qw/ name reading identical rank class / ) {
910a0a6d 399 $node_data_keys{$datum} = 'dn'.$ndi++;
400 my $key = $root->addNewChild( $graphml_ns, 'key' );
401 $key->setAttribute( 'attr.name', $datum );
402 $key->setAttribute( 'attr.type', 'string' );
403 $key->setAttribute( 'for', 'node' );
404 $key->setAttribute( 'id', $node_data_keys{$datum} );
8e1394aa 405 }
406
df6d9812 407 # Add the data keys for edges, i.e. witnesses
ef9d481f 408 my $edi = 0;
409 my %edge_data_keys;
b15511bf 410 foreach my $edge_key( qw/ witness_main witness_ante_corr relationship class / ) {
910a0a6d 411 $edge_data_keys{$edge_key} = 'de'.$edi++;
412 my $key = $root->addNewChild( $graphml_ns, 'key' );
413 $key->setAttribute( 'attr.name', $edge_key );
414 $key->setAttribute( 'attr.type', 'string' );
415 $key->setAttribute( 'for', 'edge' );
416 $key->setAttribute( 'id', $edge_data_keys{$edge_key} );
8e1394aa 417 }
ef9d481f 418
8e1394aa 419 # Add the graph, its nodes, and its edges
420 my $graph = $root->addNewChild( $graphml_ns, 'graph' );
421 $graph->setAttribute( 'edgedefault', 'directed' );
422 $graph->setAttribute( 'id', 'g0' ); # TODO make this meaningful
423 $graph->setAttribute( 'parse.edgeids', 'canonical' );
df6d9812 424 $graph->setAttribute( 'parse.edges', scalar($self->paths) );
8e1394aa 425 $graph->setAttribute( 'parse.nodeids', 'canonical' );
df6d9812 426 $graph->setAttribute( 'parse.nodes', scalar($self->readings) );
8e1394aa 427 $graph->setAttribute( 'parse.order', 'nodesfirst' );
428
429 my $node_ctr = 0;
430 my %node_hash;
b15511bf 431 # Add our readings to the graph
b5054ca9 432 foreach my $n ( sort { $a->name cmp $b->name } $self->readings ) {
910a0a6d 433 my $node_el = $graph->addNewChild( $graphml_ns, 'node' );
434 my $node_xmlid = 'n' . $node_ctr++;
435 $node_hash{ $n->name } = $node_xmlid;
436 $node_el->setAttribute( 'id', $node_xmlid );
437 _add_graphml_data( $node_el, $node_data_keys{'name'}, $n->name );
438 _add_graphml_data( $node_el, $node_data_keys{'reading'}, $n->label );
d9e873d0 439 _add_graphml_data( $node_el, $node_data_keys{'rank'}, $n->rank )
440 if $n->has_rank;
910a0a6d 441 _add_graphml_data( $node_el, $node_data_keys{'class'}, $n->sub_class );
442 _add_graphml_data( $node_el, $node_data_keys{'identical'}, $n->primary->name )
443 if $n->has_primary;
8e1394aa 444 }
445
b15511bf 446 # Add any segments we have
447 foreach my $n ( sort { $a->name cmp $b->name } $self->segments ) {
910a0a6d 448 my $node_el = $graph->addNewChild( $graphml_ns, 'node' );
449 my $node_xmlid = 'n' . $node_ctr++;
450 $node_hash{ $n->name } = $node_xmlid;
451 $node_el->setAttribute( 'id', $node_xmlid );
452 _add_graphml_data( $node_el, $node_data_keys{'class'}, $n->sub_class );
453 _add_graphml_data( $node_el, $node_data_keys{'name'}, $n->name );
b15511bf 454 }
455
456 # Add the path, relationship, and segment edges
df6d9812 457 my $edge_ctr = 0;
ef9d481f 458 foreach my $e ( sort { $a->from->name cmp $b->from->name } $self->graph->edges() ) {
910a0a6d 459 my( $name, $from, $to ) = ( 'e'.$edge_ctr++,
460 $node_hash{ $e->from->name() },
461 $node_hash{ $e->to->name() } );
462 my $edge_el = $graph->addNewChild( $graphml_ns, 'edge' );
463 $edge_el->setAttribute( 'source', $from );
464 $edge_el->setAttribute( 'target', $to );
465 $edge_el->setAttribute( 'id', $name );
466 # Add the edge class
467 _add_graphml_data( $edge_el, $edge_data_keys{'class'}, $e->sub_class );
468 if( $e->sub_class eq 'path' ) {
469 # It's a witness path, so add the witness
470 my $base = $e->label;
471 my $key = $edge_data_keys{'witness_main'};
472 # TODO kind of hacky
473 if( $e->label =~ /^(.*?)\s+(\(a\.c\.\))$/ ) {
474 $base = $1;
475 $key = $edge_data_keys{'witness_ante_corr'};
476 }
477 _add_graphml_data( $edge_el, $key, $base );
478 } elsif( $e->sub_class eq 'relationship' ) {
479 # It's a relationship
480 _add_graphml_data( $edge_el, $edge_data_keys{'relationship'}, $e->label );
481 } # else a segment, nothing to record but source, target, class
8e1394aa 482 }
483
484 # Return the thing
df6d9812 485 $self->_save_graphml( $graphml->toString(1) );
486 return $graphml->toString(1);
487}
488
b15511bf 489sub _add_graphml_data {
490 my( $el, $key, $value ) = @_;
491 my $data_el = $el->addNewChild( $el->namespaceURI, 'data' );
492 return unless defined $value;
493 $data_el->setAttribute( 'key', $key );
494 $data_el->appendText( $value );
8e1394aa 495}
496
910a0a6d 497=item B<as_csv>
498
499print $graph->as_csv( $recalculate )
500
501Returns a CSV alignment table representation of the collation graph, one
502row per witness (or witness uncorrected.) Unless $recalculate is passed
503(and is a true value), the method will return a cached copy of the CSV
504after the first call to the method.
505
506=cut
507
508sub as_csv {
509 my( $self, $recalc ) = @_;
510 return $self->csv if $self->has_csv;
511 my $table = $self->make_alignment_table;
512 my $csv = Text::CSV_XS->new( { binary => 1, quote_null => 0 } );
513 my @result;
514 foreach my $row ( @$table ) {
515 $csv->combine( @$row );
516 push( @result, decode_utf8( $csv->string ) );
517 }
518 $self->_save_csv( join( "\n", @result ) );
519 return $self->csv;
520}
521
0e476982 522# Make an alignment table - $noderefs controls whether the objects
523# in the table are the nodes or simply their readings.
9f3ba6f7 524
910a0a6d 525sub make_alignment_table {
0e476982 526 my( $self, $noderefs ) = @_;
910a0a6d 527 unless( $self->linear ) {
528 warn "Need a linear graph in order to make an alignment table";
529 return;
530 }
531 my $table;
532 my @all_pos = sort { $a <=> $b } $self->possible_positions;
533 foreach my $wit ( $self->tradition->witnesses ) {
eca16057 534 # print STDERR "Making witness row(s) for " . $wit->sigil . "\n";
0e476982 535 my @row = _make_witness_row( $wit->path, \@all_pos, $noderefs );
910a0a6d 536 unshift( @row, $wit->sigil );
537 push( @$table, \@row );
538 if( $wit->has_ante_corr ) {
0e476982 539 my @ac_row = _make_witness_row( $wit->uncorrected_path, \@all_pos, $noderefs );
910a0a6d 540 unshift( @ac_row, $wit->sigil . $self->ac_label );
541 push( @$table, \@ac_row );
542 }
543 }
0e476982 544
910a0a6d 545 # Return a table where the witnesses read in columns rather than rows.
546 my $turned = _turn_table( $table );
547 return $turned;
548}
549
550sub _make_witness_row {
0e476982 551 my( $path, $positions, $noderefs ) = @_;
910a0a6d 552 my %char_hash;
553 map { $char_hash{$_} = undef } @$positions;
554 foreach my $rdg ( @$path ) {
eca16057 555 my $rtext = $rdg->text;
556 $rtext = '#LACUNA#' if $rdg->is_lacuna;
0e476982 557 $char_hash{$rdg->rank} = $noderefs ? $rdg : $rtext;
910a0a6d 558 }
559 my @row = map { $char_hash{$_} } @$positions;
eca16057 560 # Fill in lacuna markers for undef spots in the row
561 my $last_el = shift @row;
562 my @filled_row = ( $last_el );
563 foreach my $el ( @row ) {
0e476982 564 # If we are using node reference, make the lacuna node appear many times
565 # in the table. If not, use the lacuna tag.
566 if( $last_el && _el_is_lacuna( $last_el ) && !defined $el ) {
567 $el = $noderefs ? $last_el : '#LACUNA#';
eca16057 568 }
569 push( @filled_row, $el );
570 $last_el = $el;
571 }
572 return @filled_row;
910a0a6d 573}
574
0e476982 575# Tiny utility function to say if a table element is a lacuna
576sub _el_is_lacuna {
577 my $el = shift;
578 return 1 if $el eq '#LACUNA#';
579 return 1 if ref( $el ) eq 'Text::Tradition::Collation::Reading'
580 && $el->is_lacuna;
581 return 0;
582}
583
910a0a6d 584# Helper to turn the witnesses along columns rather than rows. Assumes
585# equal-sized rows.
586sub _turn_table {
587 my( $table ) = @_;
588 my $result = [];
589 return $result unless scalar @$table;
590 my $nrows = scalar @{$table->[0]};
591 foreach my $idx ( 0 .. $nrows - 1 ) {
592 foreach my $wit ( 0 .. $#{$table} ) {
593 $result->[$idx]->[$wit] = $table->[$wit]->[$idx];
594 }
595 }
596 return $result;
597}
598
599
3265b0ce 600sub collapse_graph_paths {
1f563ac3 601 my $self = shift;
3265b0ce 602 # Our collation graph has an path per witness. This is great for
1f563ac3 603 # calculation purposes, but terrible for display. Thus we want to
3265b0ce 604 # display only one path between any two nodes.
1f563ac3 605
606 return if $self->collapsed;
607
3265b0ce 608 print STDERR "Collapsing witness paths in graph...\n";
1f563ac3 609
610 # Don't list out every witness if we have more than half to list.
910a0a6d 611 my $majority = int( scalar( $self->tradition->witnesses ) / 2 ) + 1;
a0093bf2 612 # But don't compress if there are only a few witnesses.
613 $majority = 4 if $majority < 4;
b15511bf 614 foreach my $node ( $self->readings ) {
910a0a6d 615 my $newlabels = {};
616 # We will visit each node, so we only look ahead.
617 foreach my $edge ( $node->outgoing() ) {
618 next unless $edge->class eq 'edge.path';
619 add_hash_entry( $newlabels, $edge->to->name, $edge->name );
620 $self->del_path( $edge );
621 }
622
623 foreach my $newdest ( keys %$newlabels ) {
624 my $label;
625 my @compressed_wits = @{$newlabels->{$newdest}};
626 if( @compressed_wits < $majority ) {
627 $label = join( ', ', sort( @{$newlabels->{$newdest}} ) );
628 } else {
629 ## TODO FIX THIS HACK
630 my @aclabels;
631 foreach my $wit ( @compressed_wits ) {
632 push( @aclabels, $wit ) if( $wit =~ /^(.*?)(\s*\(?a\.\s*c\.\)?)$/ );
633 }
634 $label = join( ', ', 'majority', sort( @aclabels ) );
635 }
636
637 my $newpath = $self->add_path( $node, $self->reading( $newdest ), $label );
638 $newpath->hidden_witnesses( \@compressed_wits );
639 }
1f563ac3 640 }
641
642 $self->collapsed( 1 );
643}
644
3265b0ce 645sub expand_graph_paths {
1f563ac3 646 my $self = shift;
3265b0ce 647 # Our collation graph has only one path between any two nodes.
1f563ac3 648 # This is great for display, but not so great for analysis.
3265b0ce 649 # Expand this so that each witness has its own path between any
1f563ac3 650 # two reading nodes.
651 return unless $self->collapsed;
652
3265b0ce 653 print STDERR "Expanding witness paths in graph...\n";
3265b0ce 654 foreach my $path( $self->paths ) {
910a0a6d 655 my $from = $path->from;
656 my $to = $path->to;
657 warn sprintf( "No hidden witnesses on %s -> %s ?", $from->name, $to->name )
658 unless $path->has_hidden_witnesses;
659 my @wits = @{$path->hidden_witnesses};
660 $self->del_path( $path );
661 foreach ( @wits ) {
662 $self->add_path( $from, $to, $_ );
663 }
1f563ac3 664 }
665 $self->collapsed( 0 );
666}
667
8e1394aa 668=back
669
de51424a 670=head2 Navigation methods
671
672=over
673
8e1394aa 674=item B<start>
675
676my $beginning = $collation->start();
677
678Returns the beginning of the collation, a meta-reading with label '#START#'.
679
680=cut
681
682sub start {
4a8828f0 683 # Return the beginning reading of the graph.
8e1394aa 684 my $self = shift;
685 my( $new_start ) = @_;
686 if( $new_start ) {
910a0a6d 687 $self->del_reading( '#START#' );
688 $self->graph->rename_node( $new_start, '#START#' );
689 }
690 # Make sure the start node has a start position.
d9e873d0 691 unless( $self->reading( '#START#' )->has_rank ) {
692 $self->reading( '#START#' )->rank( '0' );
8e1394aa 693 }
694 return $self->reading('#START#');
695}
696
910a0a6d 697=item B<end>
698
699my $end = $collation->end();
700
701Returns the end of the collation, a meta-reading with label '#END#'.
702
703=cut
704
705sub end {
706 my $self = shift;
707 my( $new_end ) = @_;
708 if( $new_end ) {
709 $self->del_reading( '#END#' );
710 $self->graph->rename_node( $new_end, '#END#' );
711 }
712 return $self->reading('#END#');
713}
714
e2902068 715=item B<reading_sequence>
716
717my @readings = $graph->reading_sequence( $first, $last, $path[, $alt_path] );
718
719Returns the ordered list of readings, starting with $first and ending
720with $last, along the given witness path. If no path is specified,
721assume that the path is that of the base text (if any.)
722
723=cut
724
910a0a6d 725# TODO Think about returning some lazy-eval iterator.
726
e2902068 727sub reading_sequence {
728 my( $self, $start, $end, $witness, $backup ) = @_;
729
930ff666 730 $witness = $self->baselabel unless $witness;
e2902068 731 my @readings = ( $start );
732 my %seen;
733 my $n = $start;
930ff666 734 while( $n && $n ne $end ) {
910a0a6d 735 if( exists( $seen{$n->name()} ) ) {
736 warn "Detected loop at " . $n->name();
737 last;
738 }
739 $seen{$n->name()} = 1;
740
741 my $next = $self->next_reading( $n, $witness, $backup );
742 warn "Did not find any path for $witness from reading " . $n->name
743 unless $next;
744 push( @readings, $next );
745 $n = $next;
e2902068 746 }
747 # Check that the last reading is our end reading.
748 my $last = $readings[$#readings];
749 warn "Last reading found from " . $start->label() .
910a0a6d 750 " for witness $witness is not the end!"
751 unless $last eq $end;
e2902068 752
753 return @readings;
754}
755
4a8828f0 756=item B<next_reading>
8e1394aa 757
4a8828f0 758my $next_reading = $graph->next_reading( $reading, $witpath );
8e1394aa 759
4a8828f0 760Returns the reading that follows the given reading along the given witness
930ff666 761path.
8e1394aa 762
763=cut
764
4a8828f0 765sub next_reading {
e2902068 766 # Return the successor via the corresponding path.
8e1394aa 767 my $self = shift;
4a8828f0 768 return $self->_find_linked_reading( 'next', @_ );
8e1394aa 769}
770
4a8828f0 771=item B<prior_reading>
8e1394aa 772
4a8828f0 773my $prior_reading = $graph->prior_reading( $reading, $witpath );
8e1394aa 774
4a8828f0 775Returns the reading that precedes the given reading along the given witness
930ff666 776path.
8e1394aa 777
778=cut
779
4a8828f0 780sub prior_reading {
e2902068 781 # Return the predecessor via the corresponding path.
8e1394aa 782 my $self = shift;
4a8828f0 783 return $self->_find_linked_reading( 'prior', @_ );
8e1394aa 784}
785
4a8828f0 786sub _find_linked_reading {
e2902068 787 my( $self, $direction, $node, $path, $alt_path ) = @_;
788 my @linked_paths = $direction eq 'next'
910a0a6d 789 ? $node->outgoing() : $node->incoming();
e2902068 790 return undef unless scalar( @linked_paths );
8e1394aa 791
e2902068 792 # We have to find the linked path that contains all of the
793 # witnesses supplied in $path.
794 my( @path_wits, @alt_path_wits );
795 @path_wits = $self->witnesses_of_label( $path ) if $path;
796 @alt_path_wits = $self->witnesses_of_label( $alt_path ) if $alt_path;
797 my $base_le;
798 my $alt_le;
799 foreach my $le ( @linked_paths ) {
910a0a6d 800 if( $le->name eq $self->baselabel ) {
801 $base_le = $le;
802 } else {
803 my @le_wits = $self->witnesses_of_label( $le->name );
804 if( _is_within( \@path_wits, \@le_wits ) ) {
805 # This is the right path.
806 return $direction eq 'next' ? $le->to() : $le->from();
807 } elsif( _is_within( \@alt_path_wits, \@le_wits ) ) {
808 $alt_le = $le;
809 }
810 }
8e1394aa 811 }
e2902068 812 # Got this far? Return the alternate path if it exists.
813 return $direction eq 'next' ? $alt_le->to() : $alt_le->from()
910a0a6d 814 if $alt_le;
e2902068 815
816 # Got this far? Return the base path if it exists.
817 return $direction eq 'next' ? $base_le->to() : $base_le->from()
910a0a6d 818 if $base_le;
e2902068 819
820 # Got this far? We have no appropriate path.
8e1394aa 821 warn "Could not find $direction node from " . $node->label
910a0a6d 822 . " along path $path";
8e1394aa 823 return undef;
824}
825
4a8828f0 826# Some set logic.
827sub _is_within {
828 my( $set1, $set2 ) = @_;
7854e12e 829 my $ret = @$set1; # will be 0, i.e. false, if set1 is empty
4a8828f0 830 foreach my $el ( @$set1 ) {
910a0a6d 831 $ret = 0 unless grep { /^\Q$el\E$/ } @$set2;
4a8828f0 832 }
833 return $ret;
834}
835
de51424a 836
837## INITIALIZATION METHODS - for use by parsers
4a8828f0 838# Walk the paths for each witness in the graph, and return the nodes
e2902068 839# that the graph has in common. If $using_base is true, some
840# different logic is needed.
7e450e44 841# NOTE This does not create paths; it merely finds common readings.
4a8828f0 842
843sub walk_witness_paths {
7e450e44 844 my( $self ) = @_;
4a8828f0 845 # For each witness, walk the path through the graph.
846 # Then we need to find the common nodes.
847 # TODO This method is going to fall down if we have a very gappy
848 # text in the collation.
849 my $paths = {};
3a1f2523 850 my @common_readings;
910a0a6d 851 foreach my $wit ( $self->tradition->witnesses ) {
852 my $curr_reading = $self->start;
7e450e44 853 my @wit_path = $self->reading_sequence( $self->start, $self->end,
910a0a6d 854 $wit->sigil );
855 $wit->path( \@wit_path );
856
857 # Detect the common readings.
858 @common_readings = _find_common( \@common_readings, \@wit_path );
4a8828f0 859 }
860
861 # Mark all the nodes as either common or not.
3a1f2523 862 foreach my $cn ( @common_readings ) {
910a0a6d 863 print STDERR "Setting " . $cn->name . " / " . $cn->label
864 . " as common node\n";
865 $cn->make_common;
4a8828f0 866 }
867 foreach my $n ( $self->readings() ) {
910a0a6d 868 $n->make_variant unless $n->is_common;
4a8828f0 869 }
3a1f2523 870 # Return an array of the common nodes in order.
871 return @common_readings;
4a8828f0 872}
873
930ff666 874sub _find_common {
875 my( $common_readings, $new_path ) = @_;
876 my @cr;
877 if( @$common_readings ) {
910a0a6d 878 foreach my $n ( @$new_path ) {
879 push( @cr, $n ) if grep { $_ eq $n } @$common_readings;
880 }
930ff666 881 } else {
910a0a6d 882 push( @cr, @$new_path );
930ff666 883 }
884 return @cr;
885}
886
887sub _remove_common {
888 my( $common_readings, $divergence ) = @_;
889 my @cr;
890 my %diverged;
891 map { $diverged{$_->name} = 1 } @$divergence;
892 foreach( @$common_readings ) {
910a0a6d 893 push( @cr, $_ ) unless $diverged{$_->name};
930ff666 894 }
895 return @cr;
896}
897
898
7e450e44 899# For use when a collation is constructed from a base text and an apparatus.
900# We have the sequences of readings and just need to add path edges.
e2902068 901
6a222840 902sub make_witness_paths {
903 my( $self ) = @_;
910a0a6d 904 foreach my $wit ( $self->tradition->witnesses ) {
905 print STDERR "Making path for " . $wit->sigil . "\n";
906 $self->make_witness_path( $wit );
7854e12e 907 }
7854e12e 908}
909
6a222840 910sub make_witness_path {
7854e12e 911 my( $self, $wit ) = @_;
912 my @chain = @{$wit->path};
15d2d3df 913 my $sig = $wit->sigil;
7854e12e 914 foreach my $idx ( 0 .. $#chain-1 ) {
910a0a6d 915 $self->add_path( $chain[$idx], $chain[$idx+1], $sig );
7854e12e 916 }
d9e873d0 917 if( $wit->has_ante_corr ) {
918 @chain = @{$wit->uncorrected_path};
919 foreach my $idx( 0 .. $#chain-1 ) {
920 my $source = $chain[$idx];
921 my $target = $chain[$idx+1];
922 $self->add_path( $source, $target, $sig.$self->ac_label )
923 unless $self->has_path( $source, $target, $sig );
924 }
15d2d3df 925 }
e2902068 926}
927
910a0a6d 928sub calculate_ranks {
929 my $self = shift;
930 # Walk a version of the graph where every node linked by a relationship
931 # edge is fundamentally the same node, and do a topological ranking on
932 # the nodes in this graph.
933 my $topo_graph = Graph::Easy->new();
934 my %rel_containers;
935 my $rel_ctr = 0;
936 # Add the nodes
937 foreach my $r ( $self->readings ) {
938 next if exists $rel_containers{$r->name};
939 my @rels = $r->related_readings( 'colocated' );
940 if( @rels ) {
941 # Make a relationship container.
942 push( @rels, $r );
943 my $rn = $topo_graph->add_node( 'rel_container_' . $rel_ctr++ );
944 foreach( @rels ) {
945 $rel_containers{$_->name} = $rn;
946 }
947 } else {
948 # Add a new node to mirror the old node.
949 $rel_containers{$r->name} = $topo_graph->add_node( $r->name );
950 }
4a8828f0 951 }
3a1f2523 952
910a0a6d 953 # Add the edges. Need only one edge between any pair of nodes.
954 foreach my $r ( $self->readings ) {
955 foreach my $n ( $r->neighbor_readings( 'forward' ) ) {
956 $topo_graph->add_edge_once( $rel_containers{$r->name},
957 $rel_containers{$n->name} );
958 }
959 }
960
961 # Now do the rankings, starting with the start node.
962 my $topo_start = $rel_containers{$self->start->name};
963 my $node_ranks = { $topo_start->name => 0 };
964 my @curr_origin = ( $topo_start );
965 # A little iterative function.
966 while( @curr_origin ) {
967 @curr_origin = _assign_rank( $node_ranks, @curr_origin );
968 }
969 # Transfer our rankings from the topological graph to the real one.
970 foreach my $r ( $self->readings ) {
971 $r->rank( $node_ranks->{$rel_containers{$r->name}->name} );
de51424a 972 }
8e1394aa 973}
3a1f2523 974
910a0a6d 975sub _assign_rank {
976 my( $node_ranks, @current_nodes ) = @_;
977 # Look at each of the children of @current_nodes. If all the child's
978 # parents have a rank, assign it the highest rank + 1 and add it to
979 # @next_nodes. Otherwise skip it.
980 my @next_nodes;
981 foreach my $c ( @current_nodes ) {
982 warn "Current reading " . $c->name . "has no rank!"
983 unless exists $node_ranks->{$c->name};
984 # print STDERR "Looking at child of node " . $c->name . ", rank "
985 # . $node_ranks->{$c->name} . "\n";
986 my @children = map { $_->to } $c->outgoing;
987 foreach my $child ( @children ) {
988 next if exists $node_ranks->{$child->name};
989 my $highest_rank = -1;
990 my $skip = 0;
991 my @parents = map { $_->from } $child->incoming;
992 foreach my $parent ( @parents ) {
993 if( exists $node_ranks->{$parent->name} ) {
994 $highest_rank = $node_ranks->{$parent->name}
995 if $highest_rank <= $node_ranks->{$parent->name};
996 } else {
997 $skip = 1;
998 last;
999 }
1000 }
1001 next if $skip;
1002 # print STDERR "Assigning rank " . ( $highest_rank + 1 ) . " to node " . $child->name . "\n";
1003 $node_ranks->{$child->name} = $highest_rank + 1;
1004 push( @next_nodes, $child );
1005 }
1006 }
1007 return @next_nodes;
4cdd82f1 1008}
910a0a6d 1009
0e476982 1010# Another method to make up for rough collation methods. If the same reading
1011# appears multiple times at the same rank, collapse the nodes.
1012sub flatten_ranks {
1013 my $self = shift;
1014 my %unique_rank_rdg;
1015 foreach my $rdg ( $self->readings ) {
1016 next unless $rdg->has_rank;
1017 my $key = $rdg->rank . "||" . $rdg->text;
1018 if( exists $unique_rank_rdg{$key} ) {
1019 # Combine!
1020 print STDERR "Combining readings at same rank: $key\n";
1021 $self->merge_readings( $unique_rank_rdg{$key}, $rdg );
1022 } else {
1023 $unique_rank_rdg{$key} = $rdg;
1024 }
1025 }
1026}
1027
1028
4cdd82f1 1029sub possible_positions {
3a1f2523 1030 my $self = shift;
910a0a6d 1031 my %all_pos;
1032 map { $all_pos{ $_->rank } = 1 } $self->readings;
1033 return keys %all_pos;
3a1f2523 1034}
1035
4cdd82f1 1036# TODO think about indexing this.
3a1f2523 1037sub readings_at_position {
4cdd82f1 1038 my( $self, $position, $strict ) = @_;
4cdd82f1 1039 my @answer;
1040 foreach my $r ( $self->readings ) {
910a0a6d 1041 push( @answer, $r ) if $r->is_at_position( $position, $strict );
4cdd82f1 1042 }
3a1f2523 1043 return @answer;
1044}
1045
1046## Lemmatizer functions
1047
1048sub init_lemmata {
1049 my $self = shift;
4cdd82f1 1050
1051 foreach my $position ( $self->possible_positions ) {
910a0a6d 1052 $self->lemmata->{$position} = undef;
3a1f2523 1053 }
1054
1055 foreach my $cr ( $self->common_readings ) {
910a0a6d 1056 $self->lemmata->{$cr->position->maxref} = $cr->name;
3a1f2523 1057 }
1058}
7e450e44 1059
1060sub common_readings {
1061 my $self = shift;
1062 my @common = grep { $_->is_common } $self->readings();
d9e873d0 1063 return sort { $a->rank <=> $b->rank } @common;
7e450e44 1064}
3a1f2523 1065
1066=item B<lemma_readings>
1067
1068my @state = $graph->lemma_readings( @readings_delemmatized );
1069
1070Takes a list of readings that have just been delemmatized, and returns
1071a set of tuples of the form ['reading', 'state'] that indicates what
1072changes need to be made to the graph.
1073
1074=over
1075
1076=item *
1077
1078A state of 1 means 'lemmatize this reading'
1079
1080=item *
1081
1082A state of 0 means 'delemmatize this reading'
1083
1084=item *
1085
1086A state of undef means 'an ellipsis belongs in the text here because
1087no decision has been made / an earlier decision was backed out'
1088
1089=back
1090
1091=cut
1092
1093sub lemma_readings {
1094 my( $self, @toggled_off_nodes ) = @_;
1095
1096 # First get the positions of those nodes which have been
1097 # toggled off.
1098 my $positions_off = {};
4cdd82f1 1099 map { $positions_off->{ $_->position->reference } = $_->name }
1100 @toggled_off_nodes;
de51424a 1101
3a1f2523 1102 # Now for each position, we have to see if a node is on, and we
4cdd82f1 1103 # have to see if a node has been turned off. The lemmata hash
1104 # should contain fixed positions, range positions whose node was
1105 # just turned off, and range positions whose node is on.
3a1f2523 1106 my @answer;
4cdd82f1 1107 my %fixed_positions;
1108 # TODO One of these is probably redundant.
1109 map { $fixed_positions{$_} = 0 } keys %{$self->lemmata};
1110 map { $fixed_positions{$_} = 0 } keys %{$positions_off};
1111 map { $fixed_positions{$_} = 1 } $self->possible_positions;
1112 foreach my $pos ( sort { Text::Tradition::Collation::Position::str_cmp( $a, $b ) } keys %fixed_positions ) {
910a0a6d 1113 # Find the state of this position. If there is an active node,
1114 # its name will be the state; otherwise the state will be 0
1115 # (nothing at this position) or undef (ellipsis at this position)
1116 my $active = undef;
1117 $active = $self->lemmata->{$pos} if exists $self->lemmata->{$pos};
1118
1119 # Is there a formerly active node that was toggled off?
1120 if( exists( $positions_off->{$pos} ) ) {
1121 my $off_node = $positions_off->{$pos};
1122 if( $active && $active ne $off_node) {
1123 push( @answer, [ $off_node, 0 ], [ $active, 1 ] );
1124 } else {
1125 unless( $fixed_positions{$pos} ) {
1126 $active = 0;
1127 delete $self->lemmata->{$pos};
1128 }
1129 push( @answer, [ $off_node, $active ] );
1130 }
1131
1132 # No formerly active node, so we just see if there is a currently
1133 # active one.
1134 } elsif( $active ) {
1135 # Push the active node, whatever it is.
1136 push( @answer, [ $active, 1 ] );
1137 } else {
1138 # Push the state that is there. Arbitrarily use the first node
1139 # at that position.
1140 my @pos_nodes = $self->readings_at_position( $pos );
1141 push( @answer, [ $pos_nodes[0]->name, $self->lemmata->{$pos} ] );
1142 delete $self->lemmata->{$pos} unless $fixed_positions{$pos};
1143 }
3a1f2523 1144 }
4cdd82f1 1145
3a1f2523 1146 return @answer;
1147}
1148
de51424a 1149=item B<toggle_reading>
1150
1151my @readings_delemmatized = $graph->toggle_reading( $reading_name );
1152
1153Takes a reading node name, and either lemmatizes or de-lemmatizes
1154it. Returns a list of all readings that are de-lemmatized as a result
1155of the toggle.
1156
1157=cut
1158
1159sub toggle_reading {
1160 my( $self, $rname ) = @_;
1161
1162 return unless $rname;
1163 my $reading = $self->reading( $rname );
1164 if( !$reading || $reading->is_common() ) {
910a0a6d 1165 # Do nothing, it's a common node.
1166 return;
de51424a 1167 }
1168
1169 my $pos = $reading->position;
4cdd82f1 1170 my $fixed = $reading->position->fixed;
1171 my $old_state = $self->lemmata->{$pos->reference};
1172
de51424a 1173 my @readings_off;
1174 if( $old_state && $old_state eq $rname ) {
910a0a6d 1175 # Turn off the node. We turn on no others by default.
1176 push( @readings_off, $reading );
de51424a 1177 } else {
910a0a6d 1178 # Turn on the node.
1179 $self->lemmata->{$pos->reference} = $rname;
1180 # Any other 'on' readings in the same position should be off
1181 # if we have a fixed position.
1182 push( @readings_off, $self->same_position_as( $reading, 1 ) )
1183 if $pos->fixed;
1184 # Any node that is an identical transposed one should be off.
1185 push( @readings_off, $reading->identical_readings );
de51424a 1186 }
1187 @readings_off = unique_list( @readings_off );
910a0a6d 1188
de51424a 1189 # Turn off the readings that need to be turned off.
1190 my @readings_delemmatized;
1191 foreach my $n ( @readings_off ) {
910a0a6d 1192 my $npos = $n->position;
1193 my $state = undef;
1194 $state = $self->lemmata->{$npos->reference}
1195 if defined $self->lemmata->{$npos->reference};
1196 if( $state && $state eq $n->name ) {
1197 # this reading is still on, so turn it off
1198 push( @readings_delemmatized, $n );
1199 my $new_state = undef;
1200 if( $npos->fixed && $n eq $reading ) {
1201 # This is the reading that was clicked, so if there are no
1202 # other readings there and this is a fixed position, turn off
1203 # the position. In all other cases, restore the ellipsis.
1204 my @other_n = $self->same_position_as( $n ); # TODO do we need strict?
1205 $new_state = 0 unless @other_n;
1206 }
1207 $self->lemmata->{$npos->reference} = $new_state;
1208 } elsif( $old_state && $old_state eq $n->name ) {
1209 # another reading has already been turned on here
1210 push( @readings_delemmatized, $n );
1211 } # else some other reading was on anyway, so pass.
de51424a 1212 }
1213 return @readings_delemmatized;
1214}
1215
1216sub same_position_as {
4cdd82f1 1217 my( $self, $reading, $strict ) = @_;
de51424a 1218 my $pos = $reading->position;
4cdd82f1 1219 my %onpath = ( $reading->name => 1 );
1220 # TODO This might not always be sufficient. We really want to
1221 # exclude all readings on this one's path between its two
1222 # common points.
1223 map { $onpath{$_->name} = 1 } $reading->neighbor_readings;
1224 my @same = grep { !$onpath{$_->name} }
1225 $self->readings_at_position( $reading->position, $strict );
de51424a 1226 return @same;
1227}
3a1f2523 1228
4a8828f0 1229# Return the string that joins together a list of witnesses for
1230# display on a single path.
1231sub path_label {
1232 my $self = shift;
1233 return join( $self->wit_list_separator, @_ );
1234}
1235
1236sub witnesses_of_label {
de51424a 1237 my( $self, $label ) = @_;
4a8828f0 1238 my $regex = $self->wit_list_separator;
de51424a 1239 my @answer = split( /\Q$regex\E/, $label );
1240 return @answer;
4a8828f0 1241}
8e1394aa 1242
de51424a 1243sub unique_list {
1244 my( @list ) = @_;
1245 my %h;
1246 map { $h{$_->name} = $_ } @list;
1247 return values( %h );
1248}
1249
1f563ac3 1250sub add_hash_entry {
1251 my( $hash, $key, $entry ) = @_;
1252 if( exists $hash->{$key} ) {
910a0a6d 1253 push( @{$hash->{$key}}, $entry );
1f563ac3 1254 } else {
910a0a6d 1255 $hash->{$key} = [ $entry ];
1f563ac3 1256 }
1257}
1258
dd3b58b0 1259no Moose;
1260__PACKAGE__->meta->make_immutable;