| 1 | # Copyright 2015, 2016, 2017 Kevin Ryde | ||
| 2 | # | ||
| 3 | # This file is part of Graph-Graph6. | ||
| 4 | # | ||
| 5 | # Graph-Graph6 is free software; you can redistribute it and/or modify it under | ||
| 6 | # the terms of the GNU General Public License as published by the Free | ||
| 7 | # Software Foundation; either version 3, or (at your option) any later | ||
| 8 | # version. | ||
| 9 | # | ||
| 10 | # Graph-Graph6 is distributed in the hope that it will be useful, but WITHOUT | ||
| 11 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| 12 | # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
| 13 | # more details. | ||
| 14 | # | ||
| 15 | # You should have received a copy of the GNU General Public License along | ||
| 16 | # with Graph-Graph6. If not, see <http://www.gnu.org/licenses/>. | ||
| 17 | |||
| 18 | package GraphViz2::Parse::Graph6; | ||
| 19 | use 5.006; | ||
| 20 | use strict; | ||
| 21 | use warnings; | ||
| 22 | use Moo; | ||
| 23 | use Graph::Graph6; | ||
| 24 | |||
| 25 | our $VERSION = 8; | ||
| 26 | |||
| 27 | |||
| 28 | has graph => (default => sub { | ||
| 29 | require GraphViz2; | ||
| 30 | return GraphViz2->new; # directed=>0 is default | ||
| 31 | }, | ||
| 32 | is => 'rw', | ||
| 33 | # isa => 'GraphViz2', # class requirement not enforced | ||
| 34 | required => 0, | ||
| 35 | ); | ||
| 36 | |||
| 37 | # this not documented yet ... | ||
| 38 | sub default_vertex_name_func { | ||
| 39 | return $_[0]; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub create { | ||
| 43 | my ($self, %params) = @_; | ||
| 44 | |||
| 45 | # this parameter not documented yet ... | ||
| 46 | my $vertex_name_func = $params{'vertex_name_func'} | ||
| 47 | || \&default_vertex_name_func; | ||
| 48 | |||
| 49 | my $graph = $self->graph; | ||
| 50 | my $ret = Graph::Graph6::read_graph | ||
| 51 | (filename => $params{'file_name'}, | ||
| 52 | fh => $params{'fh'}, | ||
| 53 | str => $params{'str'}, | ||
| 54 | |||
| 55 | # Setting directedness is undocumented circa GraphViz2 2.46. Would | ||
| 56 | # quite like to set it, reckoned as information from the input applied | ||
| 57 | # to the graph, but would want something like a default_global() to | ||
| 58 | # control its global attributes like "directed". | ||
| 59 | # | ||
| 60 | # format_func => sub { | ||
| 61 | # my ($format) = @_; | ||
| 62 | # $graph->global->{'directed'} | ||
| 63 | # = ($format eq 'digraph6' ? 'digraph' : 'graph'); | ||
| 64 | # }, | ||
| 65 | |||
| 66 | num_vertices_func => sub { | ||
| 67 | my ($n) = @_; | ||
| 68 | foreach my $i (0 .. $n-1) { | ||
| 69 | $graph->add_node(name => $vertex_name_func->($i)); | ||
| 70 | } | ||
| 71 | }, | ||
| 72 | edge_func => sub { | ||
| 73 | my ($from, $to) = @_; | ||
| 74 | $graph->add_edge(from => $vertex_name_func->($from), | ||
| 75 | to => $vertex_name_func->($to)); | ||
| 76 | }, | ||
| 77 | error_func => sub { | ||
| 78 | $graph->log(error => join('',@_)); | ||
| 79 | }); | ||
| 80 | |||
| 81 | # Normally log('error') is a die, but if it returns for any reason then | ||
| 82 | # $ret is undef and return undef here. Not documenting this as such yet, | ||
| 83 | # but this is probably reasonable. | ||
| 84 | # | ||
| 85 | return ($ret ? $self : undef); | ||
| 86 | } | ||
| 87 | |||
| 88 | 1; | ||
| 89 | __END__ |