File: RDF.pm

package info (click to toggle)
libcatmandu-rdf-perl 0.32-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 220 kB
  • sloc: perl: 469; sh: 4; makefile: 2
file content (164 lines) | stat: -rw-r--r-- 3,668 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package Catmandu::Exporter::RDF;

use namespace::clean;
use Catmandu::Sane;
use Moo;
use RDF::Trine::Serializer;
use RDF::Trine::Model;
use RDF::aREF;

our $VERSION = '0.32';

with 'Catmandu::RDF';
with 'Catmandu::Exporter';

# internal attributes
has decoder => (
    is => 'lazy'
);

has serializer => (
    is => 'lazy'
);

has model => (
    is => 'lazy'
);

sub _build_decoder {
    RDF::aREF::Decoder->new( 
        ns => $_[0]->ns // ($_[0]->ns eq 0 ? { } : RDF::NS->new),
        callback => $_[0]->model 
    );
}

sub _build_serializer {
    RDF::Trine::Serializer->new($_[0]->type // 'ntriples');
}

sub _build_model {
    my $self = shift;

    # Streaming output when we have type => NTriples
    if (lc($self->type // 'ntriples') eq 'ntriples') {
        sub {
            require RDF::Trine::Statement;
            eval {
                my $st = RDF::aREF::Decoder::trine_statement(@_);
                $self->fh->print($self->serializer->statement_as_string($st));
            };
            $self->decoder->error($@) if $@;
        };
    }
    else {
        RDF::Trine::Model->new;
    }
}

sub add {
    my ($self, $aref) = @_;
    $self->decoder->decode($aref, keep_bnode_map => 1);
}

sub commit {
    my ($self) = @_;

    if (ref $self->model eq 'RDF::Trine::Model') {
        $self->model->end_bulk_ops;
        $self->decoder->clean_bnodes;
        $self->serializer->serialize_model_to_file( $self->fh, $self->model );
    }
}

1;
__END__

=head1 NAME

Catmandu::Exporter::RDF - serialize RDF data

=head1 SYNOPSIS

In Perl code:
    
    use Catmandu -all;

    my $exporter = exporter('RDF',
        file => 'export.rdf',
        type => 'XML',
        fix  => 'rdf.fix'
    );

    $exporter->add( $aref ); # pass RDF data in aREF encoding

    $exporter->commit;

=head1 DESCRIPTION

This L<Catmandu::Exporter> exports RDF data in different RDF serializations.

=head1 CONFIGURATION

=over

=item file

=item fh

=item encoding

=item fix

Default configuration options of L<Catmandu::Exporter>.  The option C<fix> is
supported as derived from L<Catmandu::Fixable>. For every C<add> or for every
item in C<add_many> the given fixes will be applied first.

=item type

A serialization form can be set with option C<type> with default value
C<NTriples>. The option must refer to a subclass of L<RDF::Trine::Serializer>,
for instance C<Turtle> for RDF/Turtle with L<RDF::Trine::Serializer::Turtle>.
The first letter is transformed uppercase, so C<< format => 'turtle' >> will
work as well. In addition there are aliases C<ttl> for C<Turtle>, C<n3> for
C<Notation3>, C<xml> and C<XML> for C<RDFXML>, C<json> for C<RDFJSON>.

When the option C<type> is set to 'NTriples' the export can be streamed in all 
other cases the results are exported in bulk after C<commit()>.

=item ns

The option C<ns> can refer to an instance of or to a constructor argument of
L<RDF::NS>. Use a fixed date, such as "C<20130816>" to make sure your URI
namespace prefixes are stable.

=back

=head1 METHODS

See also L<Catmandu::Exporter>.

=head2 add( ... )

RDF data is added given in B<another RDF Encoding Form (aREF)> as 
implemented with L<RDF::aREF> and defined at L<http://github.com/gbv/aref>.

=head2 count

Returns the number of times C<add> has been called. In contrast to other
Catmandu exporters, this does not reflect the number of exporter records
because RDF data is always merged to one RDF graph.

=head2 uri( $uri )

Expand and abbreviated with L<RDF::NS>. For instance "C<dc:title>" is expanded
to "C<http://purl.org/dc/elements/1.1/title>".

=cut

=head1 SEE ALSO

Serialization is based on L<RDF::Trine::Serializer>.

=encoding utf8

=cut