| 1 | # RDF::Trine::Exporter::RDFPatch | ||||||||||||||||||||||||||||
| 2 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | =head1 NAME | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | RDF::Trine::Exporter::RDFPatch - RDF-Patch Export | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | =head1 VERSION | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | This document describes RDF::Trine::Exporter::RDFPatch version 1.019 | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | use RDF::Trine::Exporter::RDFPatch; | ||||||||||||||||||||||||||||
| 15 | my $serializer = RDF::Trine::Exporter::RDFPatch->new(); | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | The RDF::Trine::Exporter::RDFPatch class provides an API for serializing RDF | ||||||||||||||||||||||||||||
| 20 | graphs to the RDF-Patch syntax. | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =head1 METHODS | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =over 4 | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =cut | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | package RDF::Trine::Exporter::RDFPatch; | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 68 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 68 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 68 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 68 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 68 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 68 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 68 | ▶ | use List::Util qw(min); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 68 | ▶ | use RDF::Trine::Node; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 68 | ▶ | use RDF::Trine::Statement; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 68 | ▶ | use RDF::Trine::Error qw(:try); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | ###################################################################### | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 68 | our ($VERSION); | |||||||||||||||||||||||||||
| 46 | 68 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | 68 | $VERSION = '1.019'; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | ###################################################################### | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =item C<< new ( sink => $sink ) >> | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | Returns a new RDF-Patch exporter object. | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =cut | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 3 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.8
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | 3 | my $class = shift; | |||||||||||||||||||||||||||
| 60 | 3 | my $ns = {}; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 3 | my %args = @_; | |||||||||||||||||||||||||||
| 63 | 3 | ▶ | if (exists $args{ namespaces }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 2 | $ns = $args{ namespaces }; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 3 | my $sink = $args{sink}; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 3 | my %rev; | |||||||||||||||||||||||||||
| 70 | 3 | ▶ | while (my ($ns, $uri) = each(%{ $ns })) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 4 | ▶ | if (blessed($uri)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | $uri = $uri->uri_value; | |||||||||||||||||||||||||||
| 73 | 0 | ▶ | if (blessed($uri)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 0 | $uri = $uri->uri_value; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | 4 | $rev{ $uri } = $ns; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 3 | my $self = bless( { | |||||||||||||||||||||||||||
| 81 | first => 1, | ||||||||||||||||||||||||||||
| 82 | ns => \%rev, | ||||||||||||||||||||||||||||
| 83 | last => [], | ||||||||||||||||||||||||||||
| 84 | sink => $sink, | ||||||||||||||||||||||||||||
| 85 | }, $class ); | ||||||||||||||||||||||||||||
| 86 | 3 | return $self; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 101 | ▶ | sub _sink { | ||||||||||||||||||||||||||
|
Subroutine
_sink: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 90 | 101 | my $self = shift; | |||||||||||||||||||||||||||
| 91 | 101 | return $self->{sink}; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =item C<< comment ( $c ) >> | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | Serializes a comment with the given string. | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =cut | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 2 | ▶ | sub comment { | ||||||||||||||||||||||||||
|
Subroutine
comment: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 101 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 102 | 2 | my $c = shift; | |||||||||||||||||||||||||||
| 103 | 2 | $c =~ s/\n/\n# /g; | |||||||||||||||||||||||||||
| 104 | 2 | $self->_sink->emit("# $c\n"); | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =item C<< emit_operation ( $op, @operands ) >> | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | Serializes an operation identified by the character C<< $op >>, followed by C<< @operands >> | ||||||||||||||||||||||||||||
| 110 | (separated by a single space) and a trailing DOT and newline. | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =cut | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 12 | ▶ | sub emit_operation { | ||||||||||||||||||||||||||
|
Subroutine
emit_operation: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 115 | 12 | my $self = shift; | |||||||||||||||||||||||||||
| 116 | 12 | my $op = shift; | |||||||||||||||||||||||||||
| 117 | 12 | my @args = @_; | |||||||||||||||||||||||||||
| 118 | 12 | $self->_sink->emit($op); | |||||||||||||||||||||||||||
| 119 | 12 | foreach my $arg (@args) { | |||||||||||||||||||||||||||
| 120 | 36 | $self->_sink->emit(' '); | |||||||||||||||||||||||||||
| 121 | 36 | $self->_sink->emit($arg); | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | 12 | $self->_sink->emit(" .\n"); | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =item C<< add ( $st ) >> | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | Serializes an add/insert operation for the given statement object. | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =cut | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 11 | ▶ | sub add { | ||||||||||||||||||||||||||
|
Subroutine
add: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 133 | 11 | my $self = shift; | |||||||||||||||||||||||||||
| 134 | 11 | my $st = shift; | |||||||||||||||||||||||||||
| 135 | 11 | ▶ | if ($self->{first}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 3 | my $header = $self->_header(); | |||||||||||||||||||||||||||
| 137 | 3 | $self->_sink->emit($header); | |||||||||||||||||||||||||||
| 138 | 3 | $self->{first} = 0; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | 11 | my @list = $self->terms_as_string_list( $st->nodes ); | |||||||||||||||||||||||||||
| 141 | 11 | $self->emit_operation( 'A', @list ); | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =item C<< delete ( $st ) >> | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | Serializes a delete operation for the given statement object. | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =cut | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 1 | ▶ | sub delete { | ||||||||||||||||||||||||||
|
Subroutine
delete: called CC=2 SCAR=8.1
Pod
documented
| |||||||||||||||||||||||||||||
| 151 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 152 | 1 | my $st = shift; | |||||||||||||||||||||||||||
| 153 | 1 | ▶ | if ($self->{first}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 0 | my $header = $self->_header(); | |||||||||||||||||||||||||||
| 155 | 0 | $self->_sink->emit($header); | |||||||||||||||||||||||||||
| 156 | 0 | $self->{first} = 0; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | 1 | my @list = $self->terms_as_string_list( $st->nodes ); | |||||||||||||||||||||||||||
| 159 | 1 | $self->emit_operation( 'D', @list ); | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 3 | ▶ | sub _header { | ||||||||||||||||||||||||||
|
Subroutine
_header: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 163 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 164 | 3 | my %ns = reverse(%{ $self->{ns} }); | |||||||||||||||||||||||||||
| 165 | 3 | my @nskeys = sort keys %ns; | |||||||||||||||||||||||||||
| 166 | 3 | my $header = ''; | |||||||||||||||||||||||||||
| 167 | 3 | ▶ | if (@nskeys) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 2 | foreach my $ns (sort @nskeys) { | |||||||||||||||||||||||||||
| 169 | 4 | my $uri = $ns{ $ns }; | |||||||||||||||||||||||||||
| 170 | 4 | $header .= "\@prefix $ns: <$uri> .\n"; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | 2 | $header .= "\n"; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | 3 | return $header; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =item C<< statement_as_string ( $st ) >> | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | Returns a string with the supplied RDF::Trine::Statement object serialized as an RDF-Patch string. | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =cut | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 0 | ▶ | sub statement_as_string { | ||||||||||||||||||||||||||
|
Subroutine
statement_as_string: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 184 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 185 | 0 | my $st = shift; | |||||||||||||||||||||||||||
| 186 | 0 | my @nodes = $st->nodes; | |||||||||||||||||||||||||||
| 187 | 0 | my @str_nodes = $self->terms_as_string_list( @nodes ); | |||||||||||||||||||||||||||
| 188 | 0 | return join(' ', @str_nodes); | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =item C<< terms_as_string_list ( @terms ) >> | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | Returns a list with each supplied term serialized as RDF-Patch strings. | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | =cut | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 12 | ▶ | sub terms_as_string_list { | ||||||||||||||||||||||||||
|
Subroutine
terms_as_string_list: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 198 | 12 | my $self = shift; | |||||||||||||||||||||||||||
| 199 | 12 | my @nodes = @_; | |||||||||||||||||||||||||||
| 200 | 12 | my @str_nodes = map { $self->node_as_concise_string($_) } @nodes; | |||||||||||||||||||||||||||
| 201 | 12 | if (1) { | |||||||||||||||||||||||||||
| 202 | 12 | foreach my $i (0 .. min(scalar(@nodes), scalar(@{$self->{'last'}}))) { | |||||||||||||||||||||||||||
| 203 | 39 | ▶ | if (defined($self->{'last'}[$i]) and $nodes[$i]->equal( $self->{'last'}[$i])) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $self->{"last"}[$i] and $nodes[$i]->equal($self->{"last"}[$i])
MC/DC100%2/2
defined $self->{"last"}[$i] and $nodes[$i]->equal($self->{"last"}[$i])
defined $self->{"last"}[$i]
$nodes[$i]->equal($self->{"last"}[$i])
Truth table100%3/3
A and B
A= defined $self->{"last"}[$i]B= $nodes[$i]->equal($self->{"last"}[$i])
| |||||||||||||||||||||||||||||
| 204 | 6 | $str_nodes[$i] = 'R'; | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | 12 | @{ $self->{'last'} } = @nodes; | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | 12 | return @str_nodes; | |||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | =item C<< node_as_concise_string >> | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | Returns a string representation using RDF-Patch syntax shortcuts (e.g. PrefixNames). | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | =cut | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 36 | ▶ | sub node_as_concise_string { | ||||||||||||||||||||||||||
|
Subroutine
node_as_concise_string: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 219 | 36 | my $self = shift; | |||||||||||||||||||||||||||
| 220 | 36 | my $obj = shift; | |||||||||||||||||||||||||||
| 221 | 36 | ▶ | if ($obj->isa('RDF::Trine::Node::Resource')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | 24 | my $value; | |||||||||||||||||||||||||||
| 223 | 24 | ▶ | try { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 224 | 24 | my ($ns,$local) = $obj->qname; | |||||||||||||||||||||||||||
| 225 | 19 | ▶ | if (blessed($self) and exists $self->{ns}{$ns}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($self) and exists $self->{"ns"}{$ns}
MC/DC50%1/2
builtin::blessed($self) and exists $self->{"ns"}{$ns}
builtin::blessed($self)
exists $self->{"ns"}{$ns}
Truth table66%2/3
A and B
A= builtin::blessed($self)B= exists $self->{"ns"}{$ns}
| |||||||||||||||||||||||||||||
| 226 | 13 | $value = join(':', $self->{ns}{$ns}, $local); | |||||||||||||||||||||||||||
| 227 | 13 | $self->{used_ns}{ $self->{ns}{$ns} }++; | |||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | 24 | ▶ | } catch RDF::Trine::Error with {} otherwise {}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 230 | 24 | ▶ | if ($value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 231 | 13 | return $value; | |||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | 23 | return $obj->as_ntriples; | |||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | 68 | 1; | |||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | __END__ | ||||||||||||||||||||||||||||