| 1 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 2 | 3 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | package RDF::Trine::Exporter::GraphViz; | ||
| 4 | { | ||
| 5 | $RDF::Trine::Exporter::GraphViz::VERSION = '0.141'; | ||
| 6 | } | ||
| 7 | #ABSTRACT: Serialize RDF graphs as dot graph diagrams | ||
| 8 | |||
| 9 | 3 | ▶ | use RDF::Trine; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 0 | ▶ | use GraphViz qw(2.04); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 11 | use Scalar::Util qw(reftype blessed); | ||
| 12 | use Carp; | ||
| 13 | use RDF::NS; | ||
| 14 | |||
| 15 | our $NS = RDF::NS->new('any'); | ||
| 16 | |||
| 17 | # TODO: create RDF::Trine::Exporter as base class | ||
| 18 | use base qw(RDF::Trine::Serializer); | ||
| 19 | |||
| 20 | our %FORMATS = ( | ||
| 21 | dot => 'text/plain', | ||
| 22 | ps => 'application/postscript', | ||
| 23 | hpgl => 'application/vnd.hp-hpgl', | ||
| 24 | pcl => 'application/vnd.hp-pcl', | ||
| 25 | mif => 'application/vnd.mif', | ||
| 26 | gif => 'image/gif', | ||
| 27 | jpeg => 'image/jpeg', | ||
| 28 | png => 'image/png', | ||
| 29 | wbmp => 'image/vnd.wap.wbmp', | ||
| 30 | cmapx => 'text/html', | ||
| 31 | imap => 'application/x-httpd-imap', | ||
| 32 | 'map' => 'application/x-httpd-imap', | ||
| 33 | vrml => 'model/vrml', | ||
| 34 | fig => 'image/x-xfig', | ||
| 35 | svg => 'image/svg+xml', | ||
| 36 | svgz => 'image/svg+xml', | ||
| 37 | ); | ||
| 38 | |||
| 39 | sub new { | ||
| 40 | my ($class, %args) = @_; | ||
| 41 | |||
| 42 | my $self = bless \%args, $class; | ||
| 43 | |||
| 44 | $self->{as} ||= 'dot'; | ||
| 45 | croak 'Unknown format ' . $self->{as} | ||
| 46 | unless $FORMATS{ $self->{as} }; | ||
| 47 | |||
| 48 | $self->{mime} ||= $FORMATS{ $self->{as} }; | ||
| 49 | |||
| 50 | $self->{style} ||= { rankdir => 'BT', concentrate => 1 }; | ||
| 51 | $self->{node} ||= { shape => 'plaintext', color => 'gray' }; | ||
| 52 | $self->{resource} ||= { shape => 'box', style => 'rounded', | ||
| 53 | fontcolor => 'blue' }; | ||
| 54 | $self->{literal} ||= { shape => 'box' }; | ||
| 55 | $self->{blank} ||= { label => '', shape => 'point', | ||
| 56 | fillcolor => 'white', color => 'gray', width => '0.3' }; | ||
| 57 | $self->{variable} ||= { fontcolor => 'darkslategray' }; | ||
| 58 | $self->{prevar} ||= '?'; | ||
| 59 | $self->{alias} ||= { }; | ||
| 60 | |||
| 61 | if ( $self->{url} and (reftype($self->{url})||'') ne 'CODE' ) { | ||
| 62 | $self->{url} = sub { shift->uri }; | ||
| 63 | } | ||
| 64 | |||
| 65 | return $self; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub media_types { | ||
| 69 | my $self = shift; | ||
| 70 | return ($self->{mime}); | ||
| 71 | } | ||
| 72 | |||
| 73 | sub to_string { | ||
| 74 | my ($self, $rdf, %options) = @_; | ||
| 75 | |||
| 76 | my $g = $self->as_graphviz($rdf, %options); | ||
| 77 | |||
| 78 | my $format = ($options{as} || $self->{as}); | ||
| 79 | die "Unknown serialization format $format" unless $FORMATS{$format}; | ||
| 80 | |||
| 81 | my $method = "as_$format"; | ||
| 82 | $method = 'as_canon' if $method eq 'as_dot'; | ||
| 83 | $method = 'as_imap' if $method eq 'as_map'; | ||
| 84 | |||
| 85 | my $data; | ||
| 86 | eval { | ||
| 87 | # TODO: Catch error message sent to STDOUT by dot if this fails. | ||
| 88 | $g->$method( \$data ); | ||
| 89 | }; | ||
| 90 | |||
| 91 | return $data; | ||
| 92 | } | ||
| 93 | |||
| 94 | sub to_file { | ||
| 95 | my ($self, $file, $rdf, %options) = @_; | ||
| 96 | |||
| 97 | croak "missing file parameter" unless defined $file; | ||
| 98 | |||
| 99 | if (!ref $file) { | ||
| 100 | $options{as} = $1 if $file =~ /\.([^.]+)$/ and $FORMATS{$1}; | ||
| 101 | open (my $fh, '>', $file); | ||
| 102 | $file = $fh; | ||
| 103 | } | ||
| 104 | |||
| 105 | print {$file} $self->to_string( $rdf, %options ); | ||
| 106 | } | ||
| 107 | |||
| 108 | sub serialize_model_to_string { | ||
| 109 | shift->to_string(@_); | ||
| 110 | } | ||
| 111 | |||
| 112 | sub serialize_model_to_file { | ||
| 113 | shift->to_file(@_); | ||
| 114 | } | ||
| 115 | |||
| 116 | sub serialize_iterator_to_string { | ||
| 117 | shift->to_string(@_); | ||
| 118 | } | ||
| 119 | |||
| 120 | sub as_graphviz { | ||
| 121 | my ($self, $rdf, %options) = @_; | ||
| 122 | return unless blessed $rdf; | ||
| 123 | $rdf = $rdf->as_stream if $rdf->isa('RDF::Trine::Model'); | ||
| 124 | return $self->iterator_as_graphviz( $rdf, %options ); | ||
| 125 | } | ||
| 126 | |||
| 127 | sub iterator_as_graphviz { | ||
| 128 | my ($self, $iter, %options) = @_; | ||
| 129 | |||
| 130 | # We could make use of named graphs in a later version... | ||
| 131 | $options{title} ||= $self->{title}; | ||
| 132 | |||
| 133 | $options{namespaces} ||= $self->{namespaces} || $NS; | ||
| 134 | $options{root} ||= $self->{root}; | ||
| 135 | $options{prevar} ||= $self->{prevar}; | ||
| 136 | |||
| 137 | my $get_alias = $options{alias} || $self->{alias} || { }; | ||
| 138 | if (((reftype $get_alias) || '') eq 'HASH') { | ||
| 139 | my $hash = $get_alias; | ||
| 140 | $get_alias = sub { $hash->{shift} }; | ||
| 141 | } | ||
| 142 | |||
| 143 | # Basic options. Should be more configurable. | ||
| 144 | my %gopt = %{$self->{style}}; | ||
| 145 | $gopt{node} ||= $self->{node}; | ||
| 146 | |||
| 147 | my %root_style = ( color => 'red' ); | ||
| 148 | |||
| 149 | $gopt{name} = $options{title} if defined $options{title}; | ||
| 150 | |||
| 151 | my $get_edge = $options{edge} || $self->{edge}; | ||
| 152 | |||
| 153 | my $g = GraphViz->new( %gopt ); | ||
| 154 | my %nsprefix = reverse %{$options{namespaces}}; | ||
| 155 | |||
| 156 | my $get_label = sub { | ||
| 157 | my $resource = shift; | ||
| 158 | my $label = do { $_ = $resource->uri; $get_alias->( $_ ); }; | ||
| 159 | if (!defined $label) { | ||
| 160 | my ($local, $qname) = eval { $resource->qname }; | ||
| 161 | my $prefix = $local ? $nsprefix{$local} : ""; | ||
| 162 | $label = $prefix ? "$prefix:$qname" : $resource->as_string; | ||
| 163 | } | ||
| 164 | return $label; | ||
| 165 | }; | ||
| 166 | |||
| 167 | my %seen; | ||
| 168 | while (my $t = $iter->next) { | ||
| 169 | my @nodes; | ||
| 170 | my $edge_options = { }; | ||
| 171 | if ($get_edge) { | ||
| 172 | $_ = $t->predicate->uri; | ||
| 173 | $edge_options = $get_edge->( $t->predicate ); | ||
| 174 | } | ||
| 175 | next unless defined $edge_options; | ||
| 176 | |||
| 177 | foreach my $pos (qw(subject object)) { | ||
| 178 | my $n = $t->$pos(); | ||
| 179 | my $label; | ||
| 180 | if ($n->is_literal) { | ||
| 181 | $label = $n->literal_value; | ||
| 182 | } elsif( $n->is_resource ) { | ||
| 183 | $label = $get_label->($n); | ||
| 184 | } elsif( $n->is_blank ) { | ||
| 185 | $label = $n->as_string; | ||
| 186 | } elsif( $n->is_variable ) { | ||
| 187 | $label = $options{prevar}.$n->name; | ||
| 188 | } | ||
| 189 | # TODO: what about nodes with equal label? | ||
| 190 | push(@nodes, $label); | ||
| 191 | next if ($seen{ $label }++); | ||
| 192 | if ( $n->is_literal ) { | ||
| 193 | # TODO: add language / datatype | ||
| 194 | $g->add_node( $label, %{$self->{literal}} ); | ||
| 195 | } elsif ( $n->is_resource ) { | ||
| 196 | my %layout = %{$self->{resource}}; | ||
| 197 | $layout{URL} = $self->{url}->( $n ) if $self->{url}; | ||
| 198 | if ( ($options{'root'} || '') eq $n->uri ) { | ||
| 199 | $layout{$_} = $root_style{$_} for keys %root_style; | ||
| 200 | } | ||
| 201 | $g->add_node( $label, %layout ); | ||
| 202 | } elsif ( $n->is_blank ) { | ||
| 203 | $g->add_node( $label, %{$self->{blank}} ); | ||
| 204 | } elsif ( $n->is_variable ) { | ||
| 205 | $g->add_node( $label, %{$self->{variable}} ); | ||
| 206 | } | ||
| 207 | } | ||
| 208 | |||
| 209 | $edge_options->{label} //= $get_label->( $t->predicate ); | ||
| 210 | $g->add_edge( @nodes, %$edge_options ); | ||
| 211 | } | ||
| 212 | |||
| 213 | return $g; | ||
| 214 | } | ||
| 215 | |||
| 216 | 1; | ||
| 217 | |||
| 218 | |||
| 219 | __END__ | ||