Skip to content

Commit e170228

Browse files
committed
Add a description method.
This returns a translated string description of the graph. The string that is returned is suitable for use as the "alt" text for the image returned by one of the image methods.
1 parent 8c0898d commit e170228

1 file changed

Lines changed: 53 additions & 1 deletion

File tree

macros/math/SimpleGraph.pl

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ sub labels {
593593

594594
sub labelsString {
595595
my $self = shift;
596-
return join(', ', @{ $self->{labels} });
596+
return join($main::PG->maketext(', '), @{ $self->{labels} });
597597
}
598598

599599
sub vertexLabel {
@@ -785,6 +785,43 @@ sub isIsomorphic {
785785
return 0;
786786
}
787787

788+
sub description {
789+
my ($self, %options) = @_;
790+
791+
my $description = $main::PG->maketext('A graph with vertices [_1].', $self->labelsString);
792+
793+
my $comma = $main::PG->maketext(', ');
794+
795+
my @edgeText;
796+
for my $i (0 .. $self->lastVertexIndex) {
797+
for my $j ($i + 1 .. $self->lastVertexIndex) {
798+
next unless $self->hasEdge($i, $j);
799+
push(
800+
@edgeText,
801+
$options{includeWeights}
802+
? $main::PG->maketext(
803+
'[_1] and [_2] with weight [_3]', $self->vertexLabel($i),
804+
$self->vertexLabel($j), $self->edgeWeight($i, $j)
805+
)
806+
: $main::PG->maketext('[_1] and [_2]', $self->vertexLabel($i), $self->vertexLabel($j))
807+
);
808+
}
809+
}
810+
if (@edgeText == 1) {
811+
$description .= $main::PG->maketext(" There is an edge between [_1].", $edgeText[0]);
812+
} elsif (@edgeText == 2) {
813+
$description .= $main::PG->maketext(" There are edges between [_1] and [_2].", $edgeText[0], $edgeText[1]);
814+
} elsif (@edgeText) {
815+
$description .= $main::PG->maketext(
816+
' There are edges between [_1][_2]and [_3].',
817+
join($comma, @edgeText[ 0 .. $#edgeText - 1 ]),
818+
$comma, $edgeText[-1]
819+
);
820+
}
821+
822+
return $description;
823+
}
824+
788825
sub image {
789826
my ($self, %options) = @_;
790827

@@ -2381,6 +2418,21 @@ =head2 isIsomorphic
23812418
all possible permutations of the other graph, and so should not be used for
23822419
graphs with a large number of vertices (probably no more than 8).
23832420
2421+
=head2 description
2422+
2423+
$graph->description(%options);
2424+
2425+
Returns a textual description of the graph. The string that is returned is
2426+
translated and is suitable to be used as the C<alt> text for the graph image
2427+
returned by one of the following image methods. Note that the description just
2428+
lists the vertices and edges, and does not describe the layout for the
2429+
specialized layout image methods.
2430+
2431+
At this point the only option that can be set via the C<%options> argument is
2432+
C<includeWeights>. If C<includeWeights> is set to 1, then the edge weights will
2433+
be included in the description. If this is 0, then edge weights will not be
2434+
included. Default is 0.
2435+
23842436
=head2 image
23852437
23862438
$graph->image(%options);

0 commit comments

Comments
 (0)