1 package Text::Tradition::Collation;
3 use Encode qw( decode_utf8 );
7 use IPC::Run qw( run binary );
9 use Text::Tradition::Collation::Reading;
10 use Text::Tradition::Collation::RelationshipStore;
11 use Text::Tradition::Error;
13 use XML::LibXML::XPathContext;
19 default => sub { Graph->new() },
27 isa => 'Text::Tradition::Collation::RelationshipStore',
29 relationships => 'relationships',
30 related_readings => 'related_readings',
31 get_relationship => 'get_relationship',
32 del_relationship => 'del_relationship',
34 writer => '_set_relations',
39 isa => 'Text::Tradition',
40 writer => '_set_tradition',
45 isa => 'HashRef[Text::Tradition::Collation::Reading]',
49 _add_reading => 'set',
50 del_reading => 'delete',
51 has_reading => 'exists',
54 default => sub { {} },
57 has 'wit_list_separator' => (
66 default => 'base text',
89 isa => 'Text::Tradition::Collation::Reading',
90 writer => '_set_start',
96 isa => 'Text::Tradition::Collation::Reading',
101 has 'cached_svg' => (
104 predicate => 'has_cached_svg',
105 clearer => 'wipe_svg',
108 has 'cached_table' => (
111 predicate => 'has_cached_table',
112 clearer => 'wipe_table',
115 has '_graphcalc_done' => (
123 Text::Tradition::Collation - a software model for a text collation
128 my $t = Text::Tradition->new(
129 'name' => 'this is a text',
131 'file' => '/path/to/tei_parallel_seg_file.xml' );
133 my $c = $t->collation;
134 my @readings = $c->readings;
135 my @paths = $c->paths;
136 my @relationships = $c->relationships;
138 my $svg_variant_graph = $t->collation->as_svg();
142 Text::Tradition is a library for representation and analysis of collated
143 texts, particularly medieval ones. The Collation is the central feature of
144 a Tradition, where the text, its sequence of readings, and its relationships
145 between readings are actually kept.
151 The constructor. Takes a hash or hashref of the following arguments:
155 =item * tradition - The Text::Tradition object to which the collation
158 =item * linear - Whether the collation should be linear; that is, whether
159 transposed readings should be treated as two linked readings rather than one,
160 and therefore whether the collation graph is acyclic. Defaults to true.
162 =item * baselabel - The default label for the path taken by a base text
163 (if any). Defaults to 'base text'.
165 =item * wit_list_separator - The string to join a list of witnesses for
166 purposes of making labels in display graphs. Defaults to ', '.
168 =item * ac_label - The extra label to tack onto a witness sigil when
169 representing another layer of path for the given witness - that is, when
170 a text has more than one possible reading due to scribal corrections or
171 the like. Defaults to ' (a.c.)'.
173 =item * wordsep - The string used to separate words in the original text.
184 =head2 wit_list_separator
192 Simple accessors for collation attributes.
196 The meta-reading at the start of every witness path.
200 The meta-reading at the end of every witness path.
204 Returns all Reading objects in the graph.
206 =head2 reading( $id )
208 Returns the Reading object corresponding to the given ID.
210 =head2 add_reading( $reading_args )
212 Adds a new reading object to the collation.
213 See L<Text::Tradition::Collation::Reading> for the available arguments.
215 =head2 del_reading( $object_or_id )
217 Removes the given reading from the collation, implicitly removing its
218 paths and relationships.
220 =head2 merge_readings( $main, $second, $concatenate, $with_str )
222 Merges the $second reading into the $main one. If $concatenate is true, then
223 the merged node will carry the text of both readings, concatenated with either
224 $with_str (if specified) or a sensible default (the empty string if the
225 appropriate 'join_*' flag is set on either reading, or else $self->wordsep.)
227 The first two arguments may be either readings or reading IDs.
229 =head2 has_reading( $id )
231 Predicate to see whether a given reading ID is in the graph.
233 =head2 reading_witnesses( $object_or_id )
235 Returns a list of sigils whose witnesses contain the reading.
239 Returns all reading paths within the document - that is, all edges in the
240 collation graph. Each path is an arrayref of [ $source, $target ] reading IDs.
242 =head2 add_path( $source, $target, $sigil )
244 Links the given readings in the collation in sequence, under the given witness
245 sigil. The readings may be specified by object or ID.
247 =head2 del_path( $source, $target, $sigil )
249 Links the given readings in the collation in sequence, under the given witness
250 sigil. The readings may be specified by object or ID.
252 =head2 has_path( $source, $target );
254 Returns true if the two readings are linked in sequence in any witness.
255 The readings may be specified by object or ID.
259 Returns all Relationship objects in the collation.
261 =head2 add_relationship( $reading, $other_reading, $options )
263 Adds a new relationship of the type given in $options between the two readings,
264 which may be specified by object or ID. Returns a value of ( $status, @vectors)
265 where $status is true on success, and @vectors is a list of relationship edges
266 that were ultimately added.
267 See L<Text::Tradition::Collation::Relationship> for the available options.
273 $self->_set_relations( Text::Tradition::Collation::RelationshipStore->new( 'collation' => $self ) );
274 $self->_set_start( $self->add_reading( { 'collation' => $self, 'is_start' => 1 } ) );
275 $self->_set_end( $self->add_reading( { 'collation' => $self, 'is_end' => 1 } ) );
278 ### Reading construct/destruct functions
281 my( $self, $reading ) = @_;
282 unless( ref( $reading ) eq 'Text::Tradition::Collation::Reading' ) {
283 my %args = %$reading;
284 $reading = Text::Tradition::Collation::Reading->new(
285 'collation' => $self,
288 # First check to see if a reading with this ID exists.
289 if( $self->reading( $reading->id ) ) {
290 throw( "Collation already has a reading with id " . $reading->id );
292 $self->_graphcalc_done(0);
293 $self->_add_reading( $reading->id => $reading );
294 # Once the reading has been added, put it in both graphs.
295 $self->sequence->add_vertex( $reading->id );
296 $self->relations->add_reading( $reading->id );
300 around del_reading => sub {
305 if( ref( $arg ) eq 'Text::Tradition::Collation::Reading' ) {
308 # Remove the reading from the graphs.
309 $self->_graphcalc_done(0);
310 $self->_clear_cache; # Explicitly clear caches to GC the reading
311 $self->sequence->delete_vertex( $arg );
312 $self->relations->delete_reading( $arg );
315 $self->$orig( $arg );
322 my $cxfile = 't/data/Collatex-16.xml';
323 my $t = Text::Tradition->new(
325 'input' => 'CollateX',
328 my $c = $t->collation;
330 my $rno = scalar $c->readings;
331 # Split n21 for testing purposes
332 my $new_r = $c->add_reading( { 'id' => 'n21p0', 'text' => 'un', 'join_next' => 1 } );
333 my $old_r = $c->reading( 'n21' );
334 $old_r->alter_text( 'to' );
335 $c->del_path( 'n20', 'n21', 'A' );
336 $c->add_path( 'n20', 'n21p0', 'A' );
337 $c->add_path( 'n21p0', 'n21', 'A' );
339 ok( $c->reading( 'n21p0' ), "New reading exists" );
340 is( scalar $c->readings, $rno, "Reading add offset by flatten_ranks" );
342 # Combine n3 and n4 ( with his )
343 $c->merge_readings( 'n3', 'n4', 1 );
344 ok( !$c->reading('n4'), "Reading n4 is gone" );
345 is( $c->reading('n3')->text, 'with his', "Reading n3 has both words" );
347 # Collapse n9 and n10 ( rood / root )
348 $c->merge_readings( 'n9', 'n10' );
349 ok( !$c->reading('n10'), "Reading n10 is gone" );
350 is( $c->reading('n9')->text, 'rood', "Reading n9 has an unchanged word" );
352 # Combine n21 and n21p0
353 my $remaining = $c->reading('n21');
354 $remaining ||= $c->reading('n22'); # one of these should still exist
355 $c->merge_readings( 'n21p0', $remaining, 1 );
356 ok( !$c->reading('n21'), "Reading $remaining is gone" );
357 is( $c->reading('n21p0')->text, 'unto', "Reading n21p0 merged correctly" );
366 # We only need the IDs for adding paths to the graph, not the reading
367 # objects themselves.
368 my( $kept, $deleted, $combine, $combine_char ) = $self->_stringify_args( @_ );
369 $self->_graphcalc_done(0);
371 # The kept reading should inherit the paths and the relationships
372 # of the deleted reading.
373 foreach my $path ( $self->sequence->edges_at( $deleted ) ) {
374 my @vector = ( $kept );
375 push( @vector, $path->[1] ) if $path->[0] eq $deleted;
376 unshift( @vector, $path->[0] ) if $path->[1] eq $deleted;
377 next if $vector[0] eq $vector[1]; # Don't add a self loop
378 my %wits = %{$self->sequence->get_edge_attributes( @$path )};
379 $self->sequence->add_edge( @vector );
380 my $fwits = $self->sequence->get_edge_attributes( @vector );
381 @wits{keys %$fwits} = values %$fwits;
382 $self->sequence->set_edge_attributes( @vector, \%wits );
384 $self->relations->merge_readings( $kept, $deleted, $combine_char );
386 # Do the deletion deed.
388 my $kept_obj = $self->reading( $kept );
389 my $del_obj = $self->reading( $deleted );
390 my $joinstr = $combine_char;
391 unless( defined $joinstr ) {
392 $joinstr = '' if $kept_obj->join_next || $del_obj->join_prior;
393 $joinstr = $self->wordsep unless defined $joinstr;
395 $kept_obj->alter_text( join( $joinstr, $kept_obj->text, $del_obj->text ) );
397 $self->del_reading( $deleted );
401 # Helper function for manipulating the graph.
402 sub _stringify_args {
403 my( $self, $first, $second, @args ) = @_;
405 if ref( $first ) eq 'Text::Tradition::Collation::Reading';
406 $second = $second->id
407 if ref( $second ) eq 'Text::Tradition::Collation::Reading';
408 return( $first, $second, @args );
411 # Helper function for manipulating the graph.
412 sub _objectify_args {
413 my( $self, $first, $second, $arg ) = @_;
414 $first = $self->reading( $first )
415 unless ref( $first ) eq 'Text::Tradition::Collation::Reading';
416 $second = $self->reading( $second )
417 unless ref( $second ) eq 'Text::Tradition::Collation::Reading';
418 return( $first, $second, $arg );
425 # We only need the IDs for adding paths to the graph, not the reading
426 # objects themselves.
427 my( $source, $target, $wit ) = $self->_stringify_args( @_ );
429 $self->_graphcalc_done(0);
430 # Connect the readings
431 $self->sequence->add_edge( $source, $target );
432 # Note the witness in question
433 $self->sequence->set_edge_attribute( $source, $target, $wit, 1 );
439 if( ref( $_[0] ) eq 'ARRAY' ) {
446 # We only need the IDs for adding paths to the graph, not the reading
447 # objects themselves.
448 my( $source, $target, $wit ) = $self->_stringify_args( @args );
450 $self->_graphcalc_done(0);
451 if( $self->sequence->has_edge_attribute( $source, $target, $wit ) ) {
452 $self->sequence->delete_edge_attribute( $source, $target, $wit );
454 unless( keys %{$self->sequence->get_edge_attributes( $source, $target )} ) {
455 $self->sequence->delete_edge( $source, $target );
460 # Extra graph-alike utility
463 my( $source, $target, $wit ) = $self->_stringify_args( @_ );
464 return undef unless $self->sequence->has_edge( $source, $target );
465 return $self->sequence->has_edge_attribute( $source, $target, $wit );
468 =head2 clear_witness( @sigil_list )
470 Clear the given witnesses out of the collation entirely, removing references
471 to them in paths, and removing readings that belong only to them. Should only
472 be called via $tradition->del_witness.
477 my( $self, @sigils ) = @_;
479 $self->_graphcalc_done(0);
480 # Clear the witness(es) out of the paths
481 foreach my $e ( $self->paths ) {
482 foreach my $sig ( @sigils ) {
483 $self->del_path( $e, $sig );
487 # Clear out the newly unused readings
488 foreach my $r ( $self->readings ) {
489 unless( $self->reading_witnesses( $r ) ) {
490 $self->del_reading( $r );
495 sub add_relationship {
497 my( $source, $target, $opts ) = $self->_stringify_args( @_ );
498 my( @vectors ) = $self->relations->add_relationship( $source,
499 $self->reading( $source ), $target, $self->reading( $target ), $opts );
500 $self->_graphcalc_done(0);
504 around qw/ get_relationship del_relationship / => sub {
508 if( @args == 1 && ref( $args[0] ) eq 'ARRAY' ) {
511 my( $source, $target ) = $self->_stringify_args( @args );
512 $self->$orig( $source, $target );
515 =head2 reading_witnesses( $reading )
517 Return a list of sigils corresponding to the witnesses in which the reading appears.
521 sub reading_witnesses {
522 my( $self, $reading ) = @_;
523 # We need only check either the incoming or the outgoing edges; I have
524 # arbitrarily chosen "incoming". Thus, special-case the start node.
525 if( $reading eq $self->start ) {
526 return map { $_->sigil } $self->tradition->witnesses;
529 foreach my $e ( $self->sequence->edges_to( $reading ) ) {
530 my $wits = $self->sequence->get_edge_attributes( @$e );
531 @all_witnesses{ keys %$wits } = 1;
533 my $acstr = $self->ac_label;
534 foreach my $acwit ( grep { $_ =~ s/^(.*)\Q$acstr\E$/$1/ } keys %all_witnesses ) {
535 delete $all_witnesses{$acwit.$acstr} if exists $all_witnesses{$acwit};
537 return keys %all_witnesses;
540 =head1 OUTPUT METHODS
542 =head2 as_svg( \%options )
544 Returns an SVG string that represents the graph, via as_dot and graphviz.
545 See as_dot for a list of options. Must have GraphViz (dot) installed to run.
550 my( $self, $opts ) = @_;
551 throw( "Need GraphViz installed to output SVG" )
552 unless File::Which::which( 'dot' );
553 my $want_subgraph = exists $opts->{'from'} || exists $opts->{'to'};
554 $self->calculate_ranks() unless( $self->_graphcalc_done || $opts->{'nocalc'} );
555 if( !$self->has_cached_svg || $opts->{'recalc'} || $want_subgraph ) {
556 my @cmd = qw/dot -Tsvg/;
558 my $dotfile = File::Temp->new();
560 # $dotfile->unlink_on_destroy(0);
561 binmode $dotfile, ':utf8';
562 print $dotfile $self->as_dot( $opts );
563 push( @cmd, $dotfile->filename );
564 run( \@cmd, ">", binary(), \$svg );
565 $svg = decode_utf8( $svg );
566 $self->cached_svg( $svg ) unless $want_subgraph;
569 return $self->cached_svg;
574 =head2 as_dot( \%options )
576 Returns a string that is the collation graph expressed in dot
577 (i.e. GraphViz) format. Options include:
592 my( $self, $opts ) = @_;
593 my $startrank = $opts->{'from'} if $opts;
594 my $endrank = $opts->{'to'} if $opts;
595 my $color_common = $opts->{'color_common'} if $opts;
596 my $STRAIGHTENHACK = !$startrank && !$endrank && $self->end->rank
597 && $self->end->rank > 100;
598 $STRAIGHTENHACK = 1 if $opts->{'straight'}; # even for subgraphs or small graphs
600 # Check the arguments
602 return if $endrank && $startrank > $endrank;
603 return if $startrank > $self->end->rank;
605 if( defined $endrank ) {
606 return if $endrank < 0;
607 $endrank = undef if $endrank == $self->end->rank;
610 my $graph_name = $self->tradition->name;
611 $graph_name =~ s/[^\w\s]//g;
612 $graph_name = join( '_', split( /\s+/, $graph_name ) );
620 'fillcolor' => 'white',
625 'arrowhead' => 'open',
626 'color' => '#000000',
627 'fontcolor' => '#000000',
630 my $dot = sprintf( "digraph %s {\n", $graph_name );
631 $dot .= "\tgraph " . _dot_attr_string( \%graph_attrs ) . ";\n";
632 $dot .= "\tnode " . _dot_attr_string( \%node_attrs ) . ";\n";
634 # Output substitute start/end readings if necessary
636 $dot .= "\t\"#SUBSTART#\" [ label=\"...\" ];\n";
639 $dot .= "\t\"#SUBEND#\" [ label=\"...\" ];\n";
641 if( $STRAIGHTENHACK ) {
643 my $startlabel = $startrank ? 'SUBSTART' : 'START';
644 $dot .= "\tsubgraph { rank=same \"#$startlabel#\" \"#SILENT#\" }\n";
645 $dot .= "\t\"#SILENT#\" [ shape=diamond,color=white,penwidth=0,label=\"\" ];"
647 my %used; # Keep track of the readings that actually appear in the graph
648 # Sort the readings by rank if we have ranks; this speeds layout.
649 my @all_readings = $self->end->has_rank
650 ? sort { $a->rank <=> $b->rank } $self->readings
652 # TODO Refrain from outputting lacuna nodes - just grey out the edges.
653 foreach my $reading ( @all_readings ) {
654 # Only output readings within our rank range.
655 next if $startrank && $reading->rank < $startrank;
656 next if $endrank && $reading->rank > $endrank;
657 $used{$reading->id} = 1;
658 # Need not output nodes without separate labels
659 next if $reading->id eq $reading->text;
661 my $label = $reading->text;
662 $label .= '-' if $reading->join_next;
663 $label = "-$label" if $reading->join_prior;
664 $label =~ s/\"/\\\"/g;
665 $rattrs->{'label'} = $label;
666 $rattrs->{'fillcolor'} = '#b3f36d' if $reading->is_common && $color_common;
667 $dot .= sprintf( "\t\"%s\" %s;\n", $reading->id, _dot_attr_string( $rattrs ) );
670 # Add the real edges. Need to weight one edge per rank jump, in a
672 # my $weighted = $self->_add_edge_weights;
673 my @edges = $self->paths;
674 my( %substart, %subend );
675 foreach my $edge ( @edges ) {
676 # Do we need to output this edge?
677 if( $used{$edge->[0]} && $used{$edge->[1]} ) {
678 my $label = $self->_path_display_label( $self->path_witnesses( $edge ) );
679 my $variables = { %edge_attrs, 'label' => $label };
681 # Account for the rank gap if necessary
682 my $rank0 = $self->reading( $edge->[0] )->rank
683 if $self->reading( $edge->[0] )->has_rank;
684 my $rank1 = $self->reading( $edge->[1] )->rank
685 if $self->reading( $edge->[1] )->has_rank;
686 if( defined $rank0 && defined $rank1 && $rank1 - $rank0 > 1 ) {
687 $variables->{'minlen'} = $rank1 - $rank0;
690 # Add the calculated edge weights
691 # if( exists $weighted->{$edge->[0]}
692 # && $weighted->{$edge->[0]} eq $edge->[1] ) {
693 # # $variables->{'color'} = 'red';
694 # $variables->{'weight'} = 3.0;
697 # EXPERIMENTAL: make edge width reflect no. of witnesses
698 my $extrawidth = scalar( $self->path_witnesses( $edge ) ) * 0.2;
699 $variables->{'penwidth'} = $extrawidth + 0.8; # gives 1 for a single wit
701 my $varopts = _dot_attr_string( $variables );
702 $dot .= sprintf( "\t\"%s\" -> \"%s\" %s;\n",
703 $edge->[0], $edge->[1], $varopts );
704 } elsif( $used{$edge->[0]} ) {
705 $subend{$edge->[0]} = 1;
706 } elsif( $used{$edge->[1]} ) {
707 $substart{$edge->[1]} = 1;
710 # Add substitute start and end edges if necessary
711 foreach my $node ( keys %substart ) {
712 my $witstr = $self->_path_display_label ( $self->reading_witnesses( $self->reading( $node ) ) );
713 my $variables = { %edge_attrs, 'label' => $witstr };
714 my $varopts = _dot_attr_string( $variables );
715 $dot .= "\t\"#SUBSTART#\" -> \"$node\" $varopts;";
717 foreach my $node ( keys %subend ) {
718 my $witstr = $self->_path_display_label ( $self->reading_witnesses( $self->reading( $node ) ) );
719 my $variables = { %edge_attrs, 'label' => $witstr };
720 my $varopts = _dot_attr_string( $variables );
721 $dot .= "\t\"$node\" -> \"#SUBEND#\" $varopts;";
724 if( $STRAIGHTENHACK ) {
725 my $endlabel = $endrank ? 'SUBEND' : 'END';
726 $dot .= "\t\"#$endlabel#\" -> \"#SILENT#\" [ color=white,penwidth=0 ];\n";
733 sub _dot_attr_string {
736 foreach my $k ( sort keys %$hash ) {
738 push( @attrs, $k.'="'.$v.'"' );
740 return( '[ ' . join( ', ', @attrs ) . ' ]' );
743 sub _add_edge_weights {
745 # Walk the graph from START to END, choosing the successor node with
746 # the largest number of witness paths each time.
748 my $curr = $self->start->id;
749 my $ranked = $self->end->has_rank;
750 while( $curr ne $self->end->id ) {
751 my $rank = $ranked ? $self->reading( $curr )->rank : 0;
752 my @succ = sort { $self->path_witnesses( $curr, $a )
753 <=> $self->path_witnesses( $curr, $b ) }
754 $self->sequence->successors( $curr );
755 my $next = pop @succ;
756 my $nextrank = $ranked ? $self->reading( $next )->rank : 0;
757 # Try to avoid lacunae in the weighted path.
759 ( $self->reading( $next )->is_lacuna ||
760 $nextrank - $rank > 1 ) ){
763 $weighted->{$curr} = $next;
769 =head2 path_witnesses( $edge )
771 Returns the list of sigils whose witnesses are associated with the given edge.
772 The edge can be passed as either an array or an arrayref of ( $source, $target ).
777 my( $self, @edge ) = @_;
778 # If edge is an arrayref, cope.
779 if( @edge == 1 && ref( $edge[0] ) eq 'ARRAY' ) {
783 my @wits = keys %{$self->sequence->get_edge_attributes( @edge )};
787 # Helper function. Make a display label for the given witnesses, showing a.c.
788 # witnesses only where the main witness is not also in the list.
789 sub _path_display_label {
792 map { $wits{$_} = 1 } @_;
794 # If an a.c. wit is listed, remove it if the main wit is also listed.
795 # Otherwise keep it for explicit listing.
796 my $aclabel = $self->ac_label;
798 foreach my $w ( sort keys %wits ) {
799 if( $w =~ /^(.*)\Q$aclabel\E$/ ) {
800 if( exists $wits{$1} ) {
803 push( @disp_ac, $w );
808 # See if we are in a majority situation.
809 my $maj = scalar( $self->tradition->witnesses ) * 0.6;
810 if( scalar keys %wits > $maj ) {
811 unshift( @disp_ac, 'majority' );
812 return join( ', ', @disp_ac );
814 return join( ', ', sort keys %wits );
818 =head2 readings_at_rank( $rank )
820 Returns a list of readings at a given rank, taken from the alignment table.
824 sub readings_at_rank {
825 my( $self, $rank ) = @_;
826 my $table = $self->alignment_table;
827 # Table rank is real rank - 1.
828 my @elements = map { $_->{'tokens'}->[$rank-1] } @{$table->{'alignment'}};
830 foreach my $e ( @elements ) {
831 next unless ref( $e ) eq 'HASH';
832 next unless exists $e->{'t'};
833 $readings{$e->{'t'}->id} = $e->{'t'};
835 return values %readings;
840 Returns a GraphML representation of the collation. The GraphML will contain
841 two graphs. The first expresses the attributes of the readings and the witness
842 paths that link them; the second expresses the relationships that link the
843 readings. This is the native transfer format for a tradition.
852 my $datafile = 't/data/florilegium_tei_ps.xml';
853 my $tradition = Text::Tradition->new( 'input' => 'TEI',
858 ok( $tradition, "Got a tradition object" );
859 is( scalar $tradition->witnesses, 13, "Found all witnesses" );
860 ok( $tradition->collation, "Tradition has a collation" );
862 my $c = $tradition->collation;
863 is( scalar $c->readings, $READINGS, "Collation has all readings" );
864 is( scalar $c->paths, $PATHS, "Collation has all paths" );
865 is( scalar $c->relationships, 0, "Collation has all relationships" );
867 # Add a few relationships
868 $c->add_relationship( 'w123', 'w125', { 'type' => 'collated' } );
869 $c->add_relationship( 'w193', 'w196', { 'type' => 'collated' } );
870 $c->add_relationship( 'w257', 'w262', { 'type' => 'transposition' } );
872 # Now write it to GraphML and parse it again.
874 my $graphml = $c->as_graphml;
875 my $st = Text::Tradition->new( 'input' => 'Self', 'string' => $graphml );
876 is( scalar $st->collation->readings, $READINGS, "Reparsed collation has all readings" );
877 is( scalar $st->collation->paths, $PATHS, "Reparsed collation has all paths" );
878 is( scalar $st->collation->relationships, 3, "Reparsed collation has new relationships" );
885 my( $self, $options ) = @_;
886 $self->calculate_ranks unless $self->_graphcalc_done;
888 my $start = $options->{'from'}
889 ? $self->reading( $options->{'from'} ) : $self->start;
890 my $end = $options->{'to'}
891 ? $self->reading( $options->{'to'} ) : $self->end;
892 if( $start->has_rank && $end->has_rank && $end->rank < $start->rank ) {
893 throw( 'Start node must be before end node' );
895 # The readings need to be ranked for this to work.
896 $start = $self->start unless $start->has_rank;
897 $end = $self->end unless $end->has_rank;
901 my $graphml_ns = 'http://graphml.graphdrawing.org/xmlns';
902 my $xsi_ns = 'http://www.w3.org/2001/XMLSchema-instance';
903 my $graphml_schema = 'http://graphml.graphdrawing.org/xmlns ' .
904 'http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd';
906 # Create the document and root node
907 my $graphml = XML::LibXML->createDocument( "1.0", "UTF-8" );
908 my $root = $graphml->createElementNS( $graphml_ns, 'graphml' );
909 $graphml->setDocumentElement( $root );
910 $root->setNamespace( $xsi_ns, 'xsi', 0 );
911 $root->setAttributeNS( $xsi_ns, 'schemaLocation', $graphml_schema );
913 # List of attribute types to save on our objects and their corresponding
919 'RelationshipType' => 'string',
920 'RelationshipScope' => 'string',
923 # List of attribute names *not* to save on our objects.
924 # We will also not save any attribute beginning with _.
926 map { $skipsave{$_} = 1 } qw/ cached_svg /;
928 # Add the data keys for the graph. Include an extra key 'version' for the
929 # GraphML output version.
932 my %graph_attributes = ( 'version' => 'string' );
933 # Graph attributes include those of Tradition and those of Collation.
935 my $tmeta = $self->tradition->meta;
936 my $cmeta = $self->meta;
937 map { $gattr_from{$_->name} = 'Tradition' } $tmeta->get_all_attributes;
938 map { $gattr_from{$_->name} = 'Collation' } $cmeta->get_all_attributes;
939 foreach my $attr ( ( $tmeta->get_all_attributes, $cmeta->get_all_attributes ) ) {
940 next if $attr->name =~ /^_/;
941 next if $skipsave{$attr->name};
942 next unless $save_types{$attr->type_constraint->name};
943 $graph_attributes{$attr->name} = $save_types{$attr->type_constraint->name};
946 foreach my $datum ( sort keys %graph_attributes ) {
947 $graph_data_keys{$datum} = 'dg'.$gdi++;
948 my $key = $root->addNewChild( $graphml_ns, 'key' );
949 $key->setAttribute( 'attr.name', $datum );
950 $key->setAttribute( 'attr.type', $graph_attributes{$datum} );
951 $key->setAttribute( 'for', 'graph' );
952 $key->setAttribute( 'id', $graph_data_keys{$datum} );
955 # Add the data keys for reading nodes
956 my %reading_attributes;
957 my $rmeta = Text::Tradition::Collation::Reading->meta;
958 foreach my $attr( $rmeta->get_all_attributes ) {
959 next if $attr->name =~ /^_/;
960 next if $skipsave{$attr->name};
961 next unless $save_types{$attr->type_constraint->name};
962 $reading_attributes{$attr->name} = $save_types{$attr->type_constraint->name};
966 foreach my $datum ( sort keys %reading_attributes ) {
967 $node_data_keys{$datum} = 'dn'.$ndi++;
968 my $key = $root->addNewChild( $graphml_ns, 'key' );
969 $key->setAttribute( 'attr.name', $datum );
970 $key->setAttribute( 'attr.type', $reading_attributes{$datum} );
971 $key->setAttribute( 'for', 'node' );
972 $key->setAttribute( 'id', $node_data_keys{$datum} );
975 # Add the data keys for edges, that is, paths and relationships. Path
976 # data does not come from a Moose class so is here manually.
979 my %edge_attributes = (
980 witness => 'string', # ID/label for a path
981 extra => 'boolean', # Path key
983 my @path_attributes = keys %edge_attributes; # track our manual additions
984 my $pmeta = Text::Tradition::Collation::Relationship->meta;
985 foreach my $attr( $pmeta->get_all_attributes ) {
986 next if $attr->name =~ /^_/;
987 next if $skipsave{$attr->name};
988 next unless $save_types{$attr->type_constraint->name};
989 $edge_attributes{$attr->name} = $save_types{$attr->type_constraint->name};
991 foreach my $datum ( sort keys %edge_attributes ) {
992 $edge_data_keys{$datum} = 'de'.$edi++;
993 my $key = $root->addNewChild( $graphml_ns, 'key' );
994 $key->setAttribute( 'attr.name', $datum );
995 $key->setAttribute( 'attr.type', $edge_attributes{$datum} );
996 $key->setAttribute( 'for', 'edge' );
997 $key->setAttribute( 'id', $edge_data_keys{$datum} );
1000 # Add the collation graph itself
1001 my $sgraph = $root->addNewChild( $graphml_ns, 'graph' );
1002 $sgraph->setAttribute( 'edgedefault', 'directed' );
1003 $sgraph->setAttribute( 'id', $self->tradition->name );
1004 $sgraph->setAttribute( 'parse.edgeids', 'canonical' );
1005 $sgraph->setAttribute( 'parse.edges', scalar($self->paths) );
1006 $sgraph->setAttribute( 'parse.nodeids', 'canonical' );
1007 $sgraph->setAttribute( 'parse.nodes', scalar($self->readings) );
1008 $sgraph->setAttribute( 'parse.order', 'nodesfirst' );
1010 # Collation attribute data
1011 foreach my $datum ( keys %graph_attributes ) {
1013 if( $datum eq 'version' ) {
1015 } elsif( $gattr_from{$datum} eq 'Tradition' ) {
1016 $value = $self->tradition->$datum;
1018 $value = $self->$datum;
1020 _add_graphml_data( $sgraph, $graph_data_keys{$datum}, $value );
1025 # Add our readings to the graph
1026 foreach my $n ( sort { $a->id cmp $b->id } $self->readings ) {
1027 next if $n->has_rank && $n ne $self->start && $n ne $self->end &&
1028 ( $n->rank < $start->rank || $n->rank > $end->rank );
1029 $use_readings{$n->id} = 1;
1030 # Add to the main graph
1031 my $node_el = $sgraph->addNewChild( $graphml_ns, 'node' );
1032 my $node_xmlid = 'n' . $node_ctr++;
1033 $node_hash{ $n->id } = $node_xmlid;
1034 $node_el->setAttribute( 'id', $node_xmlid );
1035 foreach my $d ( keys %reading_attributes ) {
1037 _add_graphml_data( $node_el, $node_data_keys{$d}, $nval )
1042 # Add the path edges to the sequence graph
1044 foreach my $e ( sort { $a->[0] cmp $b->[0] } $self->sequence->edges() ) {
1045 # We add an edge in the graphml for every witness in $e.
1046 next unless( $use_readings{$e->[0]} || $use_readings{$e->[1]} );
1047 my @edge_wits = sort $self->path_witnesses( $e );
1048 $e->[0] = $self->start unless $use_readings{$e->[0]};
1049 $e->[1] = $self->end unless $use_readings{$e->[1]};
1050 foreach my $wit ( @edge_wits ) {
1051 my( $id, $from, $to ) = ( 'e'.$edge_ctr++,
1052 $node_hash{ $e->[0] },
1053 $node_hash{ $e->[1] } );
1054 my $edge_el = $sgraph->addNewChild( $graphml_ns, 'edge' );
1055 $edge_el->setAttribute( 'source', $from );
1056 $edge_el->setAttribute( 'target', $to );
1057 $edge_el->setAttribute( 'id', $id );
1059 # It's a witness path, so add the witness
1061 my $key = $edge_data_keys{'witness'};
1062 # Is this an ante-corr witness?
1063 my $aclabel = $self->ac_label;
1064 if( $wit =~ /^(.*)\Q$aclabel\E$/ ) {
1065 # Keep the base witness
1067 # ...and record that this is an 'extra' reading path
1068 _add_graphml_data( $edge_el, $edge_data_keys{'extra'}, $aclabel );
1070 _add_graphml_data( $edge_el, $edge_data_keys{'witness'}, $base );
1074 # Add the relationship graph to the XML
1075 map { delete $edge_data_keys{$_} } @path_attributes;
1076 $self->relations->_as_graphml( $graphml_ns, $root, \%node_hash,
1077 $node_data_keys{'id'}, \%edge_data_keys );
1079 # Save and return the thing
1080 my $result = decode_utf8( $graphml->toString(1) );
1084 sub _add_graphml_data {
1085 my( $el, $key, $value ) = @_;
1086 return unless defined $value;
1087 my $data_el = $el->addNewChild( $el->namespaceURI, 'data' );
1088 $data_el->setAttribute( 'key', $key );
1089 $data_el->appendText( $value );
1094 Returns a CSV alignment table representation of the collation graph, one
1095 row per witness (or witness uncorrected.)
1101 my $table = $self->alignment_table;
1102 my $csv = Text::CSV->new( { binary => 1, quote_null => 0 } );
1104 # Make the header row
1105 $csv->combine( map { $_->{'witness'} } @{$table->{'alignment'}} );
1106 push( @result, decode_utf8( $csv->string ) );
1107 # Make the rest of the rows
1108 foreach my $idx ( 0 .. $table->{'length'} - 1 ) {
1109 my @rowobjs = map { $_->{'tokens'}->[$idx] } @{$table->{'alignment'}};
1110 my @row = map { $_ ? $_->{'t'}->text : $_ } @rowobjs;
1111 $csv->combine( @row );
1112 push( @result, decode_utf8( $csv->string ) );
1114 return join( "\n", @result );
1117 =head2 alignment_table( $use_refs, $include_witnesses )
1119 Return a reference to an alignment table, in a slightly enhanced CollateX
1120 format which looks like this:
1122 $table = { alignment => [ { witness => "SIGIL",
1123 tokens => [ { t => "TEXT" }, ... ] },
1124 { witness => "SIG2",
1125 tokens => [ { t => "TEXT" }, ... ] },
1127 length => TEXTLEN };
1129 If $use_refs is set to 1, the reading object is returned in the table
1130 instead of READINGTEXT; if not, the text of the reading is returned.
1132 If $include_witnesses is set to a hashref, only the witnesses whose sigil
1133 keys have a true hash value will be included.
1137 sub alignment_table {
1139 $self->calculate_ranks() unless $self->_graphcalc_done;
1140 return $self->cached_table if $self->has_cached_table;
1142 # Make sure we can do this
1143 throw( "Need a linear graph in order to make an alignment table" )
1144 unless $self->linear;
1145 $self->calculate_ranks unless $self->end->has_rank;
1147 my $table = { 'alignment' => [], 'length' => $self->end->rank - 1 };
1148 my @all_pos = ( 1 .. $self->end->rank - 1 );
1149 foreach my $wit ( sort { $a->sigil cmp $b->sigil } $self->tradition->witnesses ) {
1150 # print STDERR "Making witness row(s) for " . $wit->sigil . "\n";
1151 my @wit_path = $self->reading_sequence( $self->start, $self->end, $wit->sigil );
1152 my @row = _make_witness_row( \@wit_path, \@all_pos );
1153 push( @{$table->{'alignment'}},
1154 { 'witness' => $wit->sigil, 'tokens' => \@row } );
1155 if( $wit->is_layered ) {
1156 my @wit_ac_path = $self->reading_sequence( $self->start, $self->end,
1157 $wit->sigil.$self->ac_label );
1158 my @ac_row = _make_witness_row( \@wit_ac_path, \@all_pos );
1159 push( @{$table->{'alignment'}},
1160 { 'witness' => $wit->sigil.$self->ac_label, 'tokens' => \@ac_row } );
1163 $self->cached_table( $table );
1167 sub _make_witness_row {
1168 my( $path, $positions ) = @_;
1170 map { $char_hash{$_} = undef } @$positions;
1172 foreach my $rdg ( @$path ) {
1173 my $rtext = $rdg->text;
1174 $rtext = '#LACUNA#' if $rdg->is_lacuna;
1175 print STDERR "rank " . $rdg->rank . "\n" if $debug;
1176 # print STDERR "No rank for " . $rdg->id . "\n" unless defined $rdg->rank;
1177 $char_hash{$rdg->rank} = { 't' => $rdg };
1179 my @row = map { $char_hash{$_} } @$positions;
1180 # Fill in lacuna markers for undef spots in the row
1181 my $last_el = shift @row;
1182 my @filled_row = ( $last_el );
1183 foreach my $el ( @row ) {
1184 # If we are using node reference, make the lacuna node appear many times
1185 # in the table. If not, use the lacuna tag.
1186 if( $last_el && $last_el->{'t'}->is_lacuna && !defined $el ) {
1189 push( @filled_row, $el );
1195 =head1 NAVIGATION METHODS
1197 =head2 reading_sequence( $first, $last, $sigil, $backup )
1199 Returns the ordered list of readings, starting with $first and ending
1200 with $last, for the witness given in $sigil. If a $backup sigil is
1201 specified (e.g. when walking a layered witness), it will be used wherever
1202 no $sigil path exists. If there is a base text reading, that will be
1203 used wherever no path exists for $sigil or $backup.
1207 # TODO Think about returning some lazy-eval iterator.
1208 # TODO Get rid of backup; we should know from what witness is whether we need it.
1210 sub reading_sequence {
1211 my( $self, $start, $end, $witness ) = @_;
1213 $witness = $self->baselabel unless $witness;
1214 my @readings = ( $start );
1217 while( $n && $n->id ne $end->id ) {
1218 if( exists( $seen{$n->id} ) ) {
1219 throw( "Detected loop for $witness at " . $n->id );
1223 my $next = $self->next_reading( $n, $witness );
1225 throw( "Did not find any path for $witness from reading " . $n->id );
1227 push( @readings, $next );
1230 # Check that the last reading is our end reading.
1231 my $last = $readings[$#readings];
1232 throw( "Last reading found from " . $start->text .
1233 " for witness $witness is not the end!" ) # TODO do we get this far?
1234 unless $last->id eq $end->id;
1239 =head2 next_reading( $reading, $sigil );
1241 Returns the reading that follows the given reading along the given witness
1247 # Return the successor via the corresponding path.
1249 my $answer = $self->_find_linked_reading( 'next', @_ );
1250 return undef unless $answer;
1251 return $self->reading( $answer );
1254 =head2 prior_reading( $reading, $sigil )
1256 Returns the reading that precedes the given reading along the given witness
1262 # Return the predecessor via the corresponding path.
1264 my $answer = $self->_find_linked_reading( 'prior', @_ );
1265 return $self->reading( $answer );
1268 sub _find_linked_reading {
1269 my( $self, $direction, $node, $path ) = @_;
1271 # Get a backup if we are dealing with a layered witness
1273 my $aclabel = $self->ac_label;
1274 if( $path && $path =~ /^(.*)\Q$aclabel\E$/ ) {
1278 my @linked_paths = $direction eq 'next'
1279 ? $self->sequence->edges_from( $node )
1280 : $self->sequence->edges_to( $node );
1281 return undef unless scalar( @linked_paths );
1283 # We have to find the linked path that contains all of the
1284 # witnesses supplied in $path.
1285 my( @path_wits, @alt_path_wits );
1286 @path_wits = sort( $self->_witnesses_of_label( $path ) ) if $path;
1287 @alt_path_wits = sort( $self->_witnesses_of_label( $alt_path ) ) if $alt_path;
1290 foreach my $le ( @linked_paths ) {
1291 if( $self->sequence->has_edge_attribute( @$le, $self->baselabel ) ) {
1294 my @le_wits = sort $self->path_witnesses( $le );
1295 if( _is_within( \@path_wits, \@le_wits ) ) {
1296 # This is the right path.
1297 return $direction eq 'next' ? $le->[1] : $le->[0];
1298 } elsif( _is_within( \@alt_path_wits, \@le_wits ) ) {
1302 # Got this far? Return the alternate path if it exists.
1303 return $direction eq 'next' ? $alt_le->[1] : $alt_le->[0]
1306 # Got this far? Return the base path if it exists.
1307 return $direction eq 'next' ? $base_le->[1] : $base_le->[0]
1310 # Got this far? We have no appropriate path.
1311 warn "Could not find $direction node from " . $node->id
1312 . " along path $path";
1318 my( $set1, $set2 ) = @_;
1319 my $ret = @$set1; # will be 0, i.e. false, if set1 is empty
1320 foreach my $el ( @$set1 ) {
1321 $ret = 0 unless grep { /^\Q$el\E$/ } @$set2;
1326 # Return the string that joins together a list of witnesses for
1327 # display on a single path.
1328 sub _witnesses_of_label {
1329 my( $self, $label ) = @_;
1330 my $regex = $self->wit_list_separator;
1331 my @answer = split( /\Q$regex\E/, $label );
1335 =head2 common_readings
1337 Returns the list of common readings in the graph (i.e. those readings that are
1338 shared by all non-lacunose witnesses.)
1342 sub common_readings {
1344 my @common = grep { $_->is_common } $self->readings;
1348 =head2 path_text( $sigil, [, $start, $end ] )
1350 Returns the text of a witness (plus its backup, if we are using a layer)
1351 as stored in the collation. The text is returned as a string, where the
1352 individual readings are joined with spaces and the meta-readings (e.g.
1353 lacunae) are omitted. Optional specification of $start and $end allows
1354 the generation of a subset of the witness text.
1359 my( $self, $wit, $start, $end ) = @_;
1360 $start = $self->start unless $start;
1361 $end = $self->end unless $end;
1362 my @path = grep { !$_->is_meta } $self->reading_sequence( $start, $end, $wit );
1365 foreach my $r ( @path ) {
1366 if( $r->join_prior || !$last || $last->join_next ) {
1367 $pathtext .= $r->text;
1369 $pathtext .= ' ' . $r->text;
1376 =head1 INITIALIZATION METHODS
1378 These are mostly for use by parsers.
1380 =head2 make_witness_path( $witness )
1382 Link the array of readings contained in $witness->path (and in
1383 $witness->uncorrected_path if it exists) into collation paths.
1384 Clear out the arrays when finished.
1386 =head2 make_witness_paths
1388 Call make_witness_path for all witnesses in the tradition.
1392 # For use when a collation is constructed from a base text and an apparatus.
1393 # We have the sequences of readings and just need to add path edges.
1394 # When we are done, clear out the witness path attributes, as they are no
1396 # TODO Find a way to replace the witness path attributes with encapsulated functions?
1398 sub make_witness_paths {
1400 foreach my $wit ( $self->tradition->witnesses ) {
1401 # print STDERR "Making path for " . $wit->sigil . "\n";
1402 $self->make_witness_path( $wit );
1406 sub make_witness_path {
1407 my( $self, $wit ) = @_;
1408 my @chain = @{$wit->path};
1409 my $sig = $wit->sigil;
1410 # Add start and end if necessary
1411 unshift( @chain, $self->start ) unless $chain[0] eq $self->start;
1412 push( @chain, $self->end ) unless $chain[-1] eq $self->end;
1413 foreach my $idx ( 0 .. $#chain-1 ) {
1414 $self->add_path( $chain[$idx], $chain[$idx+1], $sig );
1416 if( $wit->is_layered ) {
1417 @chain = @{$wit->uncorrected_path};
1418 unshift( @chain, $self->start ) unless $chain[0] eq $self->start;
1419 push( @chain, $self->end ) unless $chain[-1] eq $self->end;
1420 foreach my $idx( 0 .. $#chain-1 ) {
1421 my $source = $chain[$idx];
1422 my $target = $chain[$idx+1];
1423 $self->add_path( $source, $target, $sig.$self->ac_label )
1424 unless $self->has_path( $source, $target, $sig );
1428 $wit->clear_uncorrected_path;
1431 =head2 equivalence_graph( \%readingmap, $startrank, $endrank )
1433 Returns an equivalence graph of the collation, in which all readings
1434 related via a 'colocated' relationship are transformed into a single
1435 vertex. Can be used to determine the validity of a new relationship. The
1436 mapping between equivalence vertices and reading IDs will be stored in the
1437 hash whose reference is passed as readingmap. For a subset of the graph,
1438 pass in a start and/or an ending rank (this only works if L<calculate_ranks>
1439 has been called at least once.)
1443 sub equivalence_graph {
1444 my( $self, $map, $start, $end ) = @_;
1445 $start = undef unless $self->end->has_rank;
1446 $end = undef unless $self->end->has_rank;
1448 my $eqgraph = Graph->new();
1451 foreach my $r ( $self->readings ) {
1452 unless( $r eq $self->start || $r eq $self->end ) {
1453 next if $start && $r->rank < $start;
1454 next if $end && $r->rank > $end;
1456 next if exists $map->{$r->id};
1457 my @rels = $r->related_readings( 'colocated' );
1459 # Make an equivalence vertex
1460 my $rn = 'equivalence_' . $rel_ctr++;
1461 $eqgraph->add_vertex( $rn );
1462 # Note which readings belong to this vertex.
1465 $map->{$_->id} = $rn;
1468 # Add a new node to mirror the old node.
1469 $map->{$r->id} = $r->id;
1470 $eqgraph->add_vertex( $r->id );
1475 foreach my $p ( $self->paths ) {
1476 my $efrom = exists $map->{$p->[0]} ? $map->{$p->[0]}
1477 : $map->{$self->start->id};
1478 my $eto = exists $map->{$p->[1]} ? $map->{$p->[1]}
1479 : $map->{$self->end->id};
1480 $eqgraph->add_edge( $efrom, $eto );
1485 =head2 calculate_ranks
1487 Calculate the reading ranks (that is, their aligned positions relative
1488 to each other) for the graph. This can only be called on linear collations.
1492 use Text::Tradition;
1494 my $cxfile = 't/data/Collatex-16.xml';
1495 my $t = Text::Tradition->new(
1497 'input' => 'CollateX',
1500 my $c = $t->collation;
1503 my $table = $c->alignment_table;
1504 ok( $c->has_cached_table, "Alignment table was cached" );
1505 is( $c->alignment_table, $table, "Cached table returned upon second call" );
1506 $c->calculate_ranks;
1507 is( $c->alignment_table, $table, "Cached table retained with no rank change" );
1508 $c->add_relationship( 'n24', 'n23', { 'type' => 'spelling' } );
1509 isnt( $c->alignment_table, $table, "Alignment table changed after relationship add" );
1515 sub calculate_ranks {
1517 # Save the existing ranks, in case we need to invalidate the cached SVG.
1519 map { $existing_ranks{$_} = $_->rank } $self->readings;
1520 # Walk a version of the graph where every node linked by a relationship
1521 # edge is fundamentally the same node, and do a topological ranking on
1522 # the nodes in this graph.
1524 my $topo_graph = $self->equivalence_graph( \%rel_containers );
1526 # Now do the rankings, starting with the start node.
1527 my $topo_start = $rel_containers{$self->start->id};
1528 my $node_ranks = { $topo_start => 0 };
1529 my @curr_origin = ( $topo_start );
1530 # A little iterative function.
1531 while( @curr_origin ) {
1532 @curr_origin = _assign_rank( $topo_graph, $node_ranks, @curr_origin );
1534 # Transfer our rankings from the topological graph to the real one.
1535 foreach my $r ( $self->readings ) {
1536 if( defined $node_ranks->{$rel_containers{$r->id}} ) {
1537 $r->rank( $node_ranks->{$rel_containers{$r->id}} );
1539 # Die. Find the last rank we calculated.
1540 my @all_defined = sort { ( $node_ranks->{$rel_containers{$a->id}}||-1 )
1541 <=> ( $node_ranks->{$rel_containers{$b->id}}||-1 ) }
1543 my $last = pop @all_defined;
1544 throw( "Ranks not calculated after $last - do you have a cycle in the graph?" );
1547 # Do we need to invalidate the cached data?
1548 if( $self->has_cached_svg || $self->has_cached_table ) {
1549 foreach my $r ( $self->readings ) {
1550 next if defined( $existing_ranks{$r} )
1551 && $existing_ranks{$r} == $r->rank;
1552 # Something has changed, so clear the cache
1553 $self->_clear_cache;
1554 # ...and recalculate the common readings.
1555 $self->calculate_common_readings();
1559 # The graph calculation information is now up to date.
1560 $self->_graphcalc_done(1);
1564 my( $graph, $node_ranks, @current_nodes ) = @_;
1565 # Look at each of the children of @current_nodes. If all the child's
1566 # parents have a rank, assign it the highest rank + 1 and add it to
1567 # @next_nodes. Otherwise skip it; we will return when the highest-ranked
1568 # parent gets a rank.
1570 foreach my $c ( @current_nodes ) {
1571 warn "Current reading $c has no rank!"
1572 unless exists $node_ranks->{$c};
1573 # print STDERR "Looking at child of node $c, rank "
1574 # . $node_ranks->{$c} . "\n";
1575 foreach my $child ( $graph->successors( $c ) ) {
1576 next if exists $node_ranks->{$child};
1577 my $highest_rank = -1;
1579 foreach my $parent ( $graph->predecessors( $child ) ) {
1580 if( exists $node_ranks->{$parent} ) {
1581 $highest_rank = $node_ranks->{$parent}
1582 if $highest_rank <= $node_ranks->{$parent};
1589 my $c_rank = $highest_rank + 1;
1590 # print STDERR "Assigning rank $c_rank to node $child \n";
1591 $node_ranks->{$child} = $c_rank;
1592 push( @next_nodes, $child );
1600 $self->wipe_svg if $self->has_cached_svg;
1601 $self->wipe_table if $self->has_cached_table;
1605 =head2 flatten_ranks
1607 A convenience method for parsing collation data. Searches the graph for readings
1608 with the same text at the same rank, and merges any that are found.
1614 my %unique_rank_rdg;
1616 foreach my $rdg ( $self->readings ) {
1617 next unless $rdg->has_rank;
1618 my $key = $rdg->rank . "||" . $rdg->text;
1619 if( exists $unique_rank_rdg{$key} ) {
1621 # print STDERR "Combining readings at same rank: $key\n";
1623 $self->merge_readings( $unique_rank_rdg{$key}, $rdg );
1624 # TODO see if this now makes a common point.
1626 $unique_rank_rdg{$key} = $rdg;
1629 # If we merged readings, the ranks are still fine but the alignment
1630 # table is wrong. Wipe it.
1631 $self->wipe_table() if $changed;
1635 =head2 calculate_common_readings
1637 Goes through the graph identifying the readings that appear in every witness
1638 (apart from those with lacunae at that spot.) Marks them as common and returns
1643 use Text::Tradition;
1645 my $cxfile = 't/data/Collatex-16.xml';
1646 my $t = Text::Tradition->new(
1648 'input' => 'CollateX',
1651 my $c = $t->collation;
1653 my @common = $c->calculate_common_readings();
1654 is( scalar @common, 8, "Found correct number of common readings" );
1655 my @marked = sort $c->common_readings();
1656 is( scalar @common, 8, "All common readings got marked as such" );
1657 my @expected = qw/ n1 n11 n16 n19 n20 n5 n6 n7 /;
1658 is_deeply( \@marked, \@expected, "Found correct list of common readings" );
1664 sub calculate_common_readings {
1667 map { $_->is_common( 0 ) } $self->readings;
1668 # Implicitly calls calculate_ranks
1669 my $table = $self->alignment_table;
1670 foreach my $idx ( 0 .. $table->{'length'} - 1 ) {
1671 my @row = map { $_->{'tokens'}->[$idx]
1672 ? $_->{'tokens'}->[$idx]->{'t'} : '' }
1673 @{$table->{'alignment'}};
1675 foreach my $r ( @row ) {
1677 $hash{$r->id} = $r unless $r->is_meta;
1679 $hash{'UNDEF'} = $r;
1682 if( keys %hash == 1 && !exists $hash{'UNDEF'} ) {
1683 my( $r ) = values %hash;
1685 push( @common, $r );
1691 =head2 text_from_paths
1693 Calculate the text array for all witnesses from the path, for later consistency
1694 checking. Only to be used if there is no non-graph-based way to know the
1699 sub text_from_paths {
1701 foreach my $wit ( $self->tradition->witnesses ) {
1702 my @readings = $self->reading_sequence( $self->start, $self->end, $wit->sigil );
1704 foreach my $r ( @readings ) {
1705 next if $r->is_meta;
1706 push( @text, $r->text );
1708 $wit->text( \@text );
1709 if( $wit->is_layered ) {
1710 my @ucrdgs = $self->reading_sequence( $self->start, $self->end,
1711 $wit->sigil.$self->ac_label );
1713 foreach my $r ( @ucrdgs ) {
1714 next if $r->is_meta;
1715 push( @uctext, $r->text );
1717 $wit->layertext( \@uctext );
1722 =head1 UTILITY FUNCTIONS
1724 =head2 common_predecessor( $reading_a, $reading_b )
1726 Find the last reading that occurs in sequence before both the given readings.
1728 =head2 common_successor( $reading_a, $reading_b )
1730 Find the first reading that occurs in sequence after both the given readings.
1734 use Text::Tradition;
1736 my $cxfile = 't/data/Collatex-16.xml';
1737 my $t = Text::Tradition->new(
1739 'input' => 'CollateX',
1742 my $c = $t->collation;
1744 is( $c->common_predecessor( 'n24', 'n23' )->id,
1745 'n20', "Found correct common predecessor" );
1746 is( $c->common_successor( 'n24', 'n23' )->id,
1747 '#END#', "Found correct common successor" );
1749 is( $c->common_predecessor( 'n19', 'n17' )->id,
1750 'n16', "Found correct common predecessor for readings on same path" );
1751 is( $c->common_successor( 'n21', 'n10' )->id,
1752 '#END#', "Found correct common successor for readings on same path" );
1758 ## Return the closest reading that is a predecessor of both the given readings.
1759 sub common_predecessor {
1761 my( $r1, $r2 ) = $self->_objectify_args( @_ );
1762 return $self->_common_in_path( $r1, $r2, 'predecessors' );
1765 sub common_successor {
1767 my( $r1, $r2 ) = $self->_objectify_args( @_ );
1768 return $self->_common_in_path( $r1, $r2, 'successors' );
1771 sub _common_in_path {
1772 my( $self, $r1, $r2, $dir ) = @_;
1773 my $iter = $r1->rank > $r2->rank ? $r1->rank : $r2->rank;
1774 $iter = $self->end->rank - $iter if $dir eq 'successors';
1776 my @last_checked = ( $r1, $r2 );
1778 while( !@candidates ) {
1780 foreach my $lc ( @last_checked ) {
1781 foreach my $p ( $lc->$dir ) {
1782 if( $all_seen{$p->id} ) {
1783 push( @candidates, $p );
1785 $all_seen{$p->id} = 1;
1786 push( @new_lc, $p );
1790 @last_checked = @new_lc;
1792 my @answer = sort { $a->rank <=> $b->rank } @candidates;
1793 return $dir eq 'predecessors' ? pop( @answer ) : shift ( @answer );
1797 Text::Tradition::Error->throw(
1798 'ident' => 'Collation error',
1804 __PACKAGE__->meta->make_immutable;
1808 This package is free software and is provided "as is" without express
1809 or implied warranty. You can redistribute it and/or modify it under
1810 the same terms as Perl itself.
1814 Tara L Andrews E<lt>aurum@cpan.orgE<gt>