File: Encoder.pm

package info (click to toggle)
librdf-aref-perl 0.20-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 444 kB
  • ctags: 52
  • sloc: perl: 606; makefile: 11
file content (313 lines) | stat: -rw-r--r-- 7,650 bytes parent folder | download
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package RDF::aREF::Encoder;
use strict;
use warnings;
use v5.10;

our $VERSION = '0.20';

use RDF::NS;
use Scalar::Util qw(blessed reftype);

sub new {
    my ($class, %options) = @_;

    if (!defined $options{ns}) {
        $options{ns} = RDF::NS->new;
    } elsif (!$options{ns}) {
        $options{ns} = bless {
            rdf =>  'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
            rdfs => 'http://www.w3.org/2000/01/rdf-schema#',
            owl =>  'http://www.w3.org/2002/07/owl#',
            xsd =>  'http://www.w3.org/2001/XMLSchema#',
        }, 'RDF::NS';
    } elsif ( !blessed $options{ns} or !$options{ns}->isa('RDF::NS') ) {
        $options{ns} = RDF::NS->new($options{ns});
    }

    $options{sn} = $options{ns}->REVERSE;

    bless \%options, $class;
}

sub qname {
    my ($self, $uri) = @_;
    return unless $self->{sn};
    my @qname = $self->{sn}->qname($uri);
    return @qname ? join('_',@qname) : undef;
}

sub uri {
    my ($self, $uri) = @_;

    if ( my $qname = $self->qname($uri) ) {
        return $qname;
    } else {
        return "<$uri>";
    }
}

sub predicate {
    my ($self, $predicate) = @_;

    return 'a' if $predicate eq 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type';

    if ( my $qname = $self->qname($predicate) ) {
        return $qname;
    } else {
        return $predicate;
    }
}

sub object {
    my ($self, $object) = @_;

    if (reftype $object eq 'HASH') {
        if ($object->{type} eq 'literal') {
            $self->literal( $object->{value}, $object->{lang}, $object->{datatype} )
        } elsif ($object->{type} eq 'bnode') {
            $object->{value}
        } else {
            $self->uri($object->{value})
        }
    } elsif (reftype $object eq 'ARRAY') {
        if (@$object != 2 ) {
            $self->literal(@$object)
        } elsif ($object->[0] eq 'URI') {
            $self->uri("".$object->[1])
        } elsif ($object->[0] eq 'BLANK') {
            $self->bnode($object->[1])
        } else {
            return
        }
    } else {
        return
    }
}

sub literal {
    my ($self, $value, $language, $datatype) = @_;
    if ($language) {
        $value.'@'.$language
    } elsif ($datatype) {
        $value.'^'.$self->uri($datatype)
    } else {
        $value.'@'
    }
}

sub bnode {
    '_:'.$_[1]
}

sub add_objects {
    my ($self, $map, $predicate, $objects) = @_;
    $predicate = $self->predicate($predicate);

    return unless @$objects;
    my @objects = map { $self->object($_) } @$objects;

    if (ref $map->{$predicate}) {
        push @{$map->{$predicate}}, @objects;
    } elsif ($map->{$predicate}) {
        unshift @objects, $map->{$predicate};
        $map->{$predicate} = \@objects;
    } else {
        $map->{$predicate} = @objects > 1 ? \@objects : $objects[0];
    }
}

sub add_triple {
    my $self = shift;
    my $aref = shift;

    my ($subject, $predicate, $object) = @_; # TODO: support callback format

    if (@_ == 1) { # RDF::Trine::Statement
        $subject   = $_[0]->subject->is_blank ? $_[0]->subject->sse 
                                              : $_[0]->subject->uri_value;
        $predicate = $_[0]->predicate->uri_value;
        $object    = $_[0]->object;
    }

    # TODO: support predicate maps
    # TODO: create new bnode identifiers, if requested
    my $map = ($aref->{ $subject } //= { }); # TODO: $self->subject($s);
    $self->add_objects( $map, $predicate, [$object] );
}

sub add_iterator {
    my ($self, $aref, $iterator) = @_;    
    while (my $statement = $iterator->next) {
        $self->add_triple($aref, $statement);
    }
}

sub add_hashref {
    my ($self, $aref, $hashref) = @_;

    while (my ($s,$ps) = each %$hashref) {
        foreach my $p (keys %$ps) {
            $self->add_objects(
                $aref->{ $s } //= { }, # TODO $self->subject($s)
                $p,
                $hashref->{$s}->{$p}
            )
        }
    }

}

1;
__END__

=head1 NAME

RDF::aREF::Encoder - encode RDF to another RDF Encoding Form

=head1 SYNOPSIS

    use RDF::aREF::Encoder;
    my $encoder = RDF::aREF::Encoder->new;
    
    # encode parts of aREF

    my $qname  = $encoder->qname('http://schema.org/Review'); # 'schema_Review'

    my $predicate = $encoder->predicate(
        'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'
    ); # 'a'

    my $object = $encoder->object({
        type  => 'literal',
        value => 'hello, world!',
        lang  => 'en'
    }); # 'hello, world!@en'

    # method also accepts RDF::Trine::Node instances
    my $object = $encoder->object( RDF::Trine::Resource->new($iri) );

    # encode RDF graphs (see also function 'encode_aref' in RDF::aREF)
    use RDF::Trine::Parser;
    my $aref = { };
    RDF::Trine::Parser->parse_file ( $base_uri, $fh, sub {
        $encoder->add_triple( $aref, $_[0] );
    } );

=head1 DESCRIPTION

This module provides methods to encode RDF data in another RDF Encoding Form
(aREF). As aREF was designed to facilitate creation of RDF data, it may be
easier to create aREF "by hand" instead of using this module!

=head1 OPTIONS

=head2 ns

A default namespace map, given as version string of module L<RDF::NS> for
stable qNames or as instance of L<RDF::NS>. The most recent installed version
of L<RDF::NS> is used by default. The value C<0> can be used to only use
required namespace mappings (rdf, rdfs, owl and xsd).

=head1 METHODS

Note that no syntax checking is applied, e.g. whether a given URI is a valid
URI or whether a given language is a valid language tag!

=head2 qname( $uri )

Abbreviate an URI as qName or return C<undef>. For instance
C<http://purl.org/dc/terms/title> is abbreviated to "C<dct_title>".

=head2 uri( $uri )

Abbreviate an URI or as qName or enclose it in angular brackets.

=head2 predicate( $uri )

Return an predicate URI as qNamem, as "C<a>", or as given URI.

=head2 literal( $value, $language_tag, $datatype_uri )

Encode a literal RDF node by either appending "C<@>" and an optional
language tag, or "C<^>" and an datatype URI.

=head2 bnode( $identifier )

Encode a blank node by prepending "C<_:>" to its identifier.

=head2 object( $object )

Encode an RDF object given either as hash reference as defined in
L<RDF/JSON|http://www.w3.org/TR/rdf-json/> format (see also method
C<as_hashref> of L<RDF::Trine::Model>), or in array reference as
internally used by L<RDF::Trine>.

A hash reference is expected to have the following fields:

=over

=item type

one of C<uri>, C<literal> or C<bnode> (required)

=item value

the URI of the object, its lexical value or a blank node label depending on
whether the object is a uri, literal or bnode

=item lang

the language of a literal value (optional but if supplied it must not be empty)

=item datatype

the datatype URI of the literal value (optional)

=back

An array reference is expected to consists of

=over

=item 

three elements (value, language tag, and datatype uri) for literal nodes,

=item 

two elements "C<URI>" and the URI for URI nodes,

=item

two elements "C<BLANK>" and the blank node identifier for blank nodes.

=back

=head2 add_triple( $aref, $statement )

Add a L<RDF::Trine::Stament> to an aREF subject map.

I<experimental>

=head2 add_iterator( $aref, $iterator )

Add a L<RDF::Trine::Iterator> to an aREF subject map.

I<experimental>

=head2 add_objects( $predicate_map, $predicate, $objects )

I<experimental>

=head2 add_hashref( $aref, $rdf )

Add RDF given in L<RDF/JSON|http://www.w3.org/TR/rdf-json/> format (as returned
by method C<as_hashref> in L<RDF::Trine::Model>).

I<experimental>

=head1 SEE ALSO

L<RDF::aREF::Decoder>, L<RDF::Trine::Node>

=cut