line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
# $Id: N3.pm,v 1.18 2010-12-02 23:41:57 Martin Exp $
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
=head1 NAME
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
RDF::Simple::Serialiser::N3 - Output RDF triples in Notation3 format
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
Same as L,
|
11
|
|
|
|
|
|
|
except when you call serialise(),
|
12
|
|
|
|
|
|
|
you get back a string in Notation3 format.
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 PRIVATE METHODS
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=over
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=cut
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
package RDF::Simple::Serialiser::N3;
|
21
|
|
|
|
|
|
|
|
22
|
2
|
|
|
2
|
|
90647
|
use strict;
|
|
2
|
|
|
|
|
13
|
|
|
2
|
|
|
|
|
76
|
|
23
|
2
|
|
|
2
|
|
11
|
use warnings;
|
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
48
|
|
24
|
|
|
|
|
|
|
|
25
|
2
|
|
|
2
|
|
601
|
use Data::Dumper; # for debugging only
|
|
2
|
|
|
|
|
6704
|
|
|
2
|
|
|
|
|
100
|
|
26
|
2
|
|
|
2
|
|
1086
|
use Regexp::Common;
|
|
2
|
|
|
|
|
5411
|
|
|
2
|
|
|
|
|
8
|
|
27
|
|
|
|
|
|
|
# We need the version with the new render() method:
|
28
|
2
|
|
|
2
|
|
321222
|
use RDF::Simple::Serialiser 1.007;
|
|
2
|
|
|
|
|
71556
|
|
|
2
|
|
|
|
|
75
|
|
29
|
|
|
|
|
|
|
|
30
|
2
|
|
|
2
|
|
16
|
use base 'RDF::Simple::Serialiser';
|
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
343
|
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
our
|
33
|
|
|
|
|
|
|
$VERSION = do { my @r = (q$Revision: 1.18 $ =~ /\d+/g); sprintf "%d."."%03d" x $#r, @r };
|
34
|
|
|
|
|
|
|
|
35
|
2
|
|
|
2
|
|
15
|
use constant DEBUG => 0;
|
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
2046
|
|
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=item render
|
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
This method does all the Notation3 formatting.
|
40
|
|
|
|
|
|
|
You should not be calling this method,
|
41
|
|
|
|
|
|
|
you should be calling the serialise() method.
|
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=cut
|
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub render
|
46
|
|
|
|
|
|
|
{
|
47
|
2
|
|
|
2
|
1
|
8574
|
my $self = shift;
|
48
|
|
|
|
|
|
|
# Required arg1 = arrayref:
|
49
|
2
|
|
|
|
|
6
|
my $raObjects = shift;
|
50
|
|
|
|
|
|
|
# Required arg2 = hashref of namespaces:
|
51
|
2
|
|
|
|
|
4
|
my $rhNS = shift;
|
52
|
2
|
|
|
|
|
5
|
my $sRet = q{};
|
53
|
2
|
|
|
|
|
8
|
foreach my $sNS (keys %$rhNS)
|
54
|
|
|
|
|
|
|
{
|
55
|
4
|
100
|
66
|
|
|
26
|
if (defined $rhNS->{$sNS} && ($rhNS->{$sNS}ne q{}))
|
56
|
|
|
|
|
|
|
{
|
57
|
3
|
|
|
|
|
12
|
$sRet .= qq"\@prefix $sNS: <$rhNS->{$sNS}> .\n";
|
58
|
3
|
|
|
|
|
6
|
$self->{_iTriples_}++;
|
59
|
|
|
|
|
|
|
} # if
|
60
|
|
|
|
|
|
|
} # foreach
|
61
|
2
|
|
|
|
|
26
|
$sRet .= qq{\n};
|
62
|
2
|
|
|
|
|
4
|
my %hsClassPrinted;
|
63
|
|
|
|
|
|
|
OBJECT:
|
64
|
2
|
|
|
|
|
6
|
foreach my $object (@$raObjects)
|
65
|
|
|
|
|
|
|
{
|
66
|
3
|
|
|
|
|
6
|
DEBUG && print STDERR " DDD in render(), object is ", Dumper($object);
|
67
|
|
|
|
|
|
|
# We delete elements as we process them, so that during debugging
|
68
|
|
|
|
|
|
|
# we can see what's leftover:
|
69
|
3
|
|
50
|
|
|
21
|
my $sId = delete $object->{NodeId} || q{};
|
70
|
3
|
50
|
|
|
|
12
|
if ($sId ne q{})
|
71
|
|
|
|
|
|
|
{
|
72
|
0
|
|
|
|
|
0
|
$sId = qq{:$sId};
|
73
|
|
|
|
|
|
|
}
|
74
|
|
|
|
|
|
|
else
|
75
|
|
|
|
|
|
|
{
|
76
|
3
|
|
|
|
|
7
|
$sId = delete $object->{Uri};
|
77
|
|
|
|
|
|
|
}
|
78
|
3
|
|
|
|
|
9
|
my $sClass = delete $object->{Class};
|
79
|
3
|
50
|
|
|
|
9
|
if (! $sClass)
|
80
|
|
|
|
|
|
|
{
|
81
|
0
|
|
|
|
|
0
|
print STDERR " EEE object has no Class: ", Dumper($object);
|
82
|
0
|
|
|
|
|
0
|
next OBJECT;
|
83
|
|
|
|
|
|
|
} # if
|
84
|
3
|
50
|
|
|
|
31
|
if ($sClass !~ m/[^:]+:[^:]+/)
|
85
|
|
|
|
|
|
|
{
|
86
|
|
|
|
|
|
|
# Class is not explicitly qualified with a "prefix:", ergo now
|
87
|
|
|
|
|
|
|
# explicitly qualify in the default namespace:
|
88
|
0
|
|
|
|
|
0
|
$sClass = qq{:$sClass};
|
89
|
0
|
0
|
|
|
|
0
|
if (! $hsClassPrinted{$sClass})
|
90
|
|
|
|
|
|
|
{
|
91
|
0
|
|
|
|
|
0
|
$sRet .= qq{$sClass a owl:Class .\n\n};
|
92
|
0
|
|
|
|
|
0
|
$self->{_iTriples_}++;
|
93
|
0
|
|
|
|
|
0
|
$hsClassPrinted{$sClass}++;
|
94
|
|
|
|
|
|
|
} # if
|
95
|
|
|
|
|
|
|
} # if
|
96
|
3
|
|
|
|
|
12
|
$sRet .= qq{$sId a $sClass .\n};
|
97
|
3
|
|
|
|
|
6
|
$self->{_iTriples_}++;
|
98
|
3
|
50
|
|
|
|
10
|
if ($object->{Uri})
|
99
|
|
|
|
|
|
|
{
|
100
|
0
|
|
|
|
|
0
|
$sRet .= qq{$sId rdf:about <$object->{Uri}> .\n};
|
101
|
0
|
|
|
|
|
0
|
$self->{_iTriples_}++;
|
102
|
0
|
|
|
|
|
0
|
delete $object->{Uri};
|
103
|
|
|
|
|
|
|
} # if
|
104
|
|
|
|
|
|
|
LITERAL:
|
105
|
3
|
|
|
|
|
10
|
foreach my $sProp (keys %{$object->{literal}})
|
|
3
|
|
|
|
|
13
|
|
106
|
|
|
|
|
|
|
{
|
107
|
|
|
|
|
|
|
LITERAL_PROPERTY:
|
108
|
5
|
|
|
|
|
8
|
foreach my $sVal (@{$object->{literal}->{$sProp}})
|
|
5
|
|
|
|
|
14
|
|
109
|
|
|
|
|
|
|
{
|
110
|
5
|
100
|
|
|
|
24
|
if ($sVal !~ m/\A$RE{num}{decimal}\z/)
|
111
|
|
|
|
|
|
|
{
|
112
|
|
|
|
|
|
|
# Value is non-numeric; assume it's a string and put quotes
|
113
|
|
|
|
|
|
|
# around it:
|
114
|
4
|
|
|
|
|
789
|
$sVal = qq{"$sVal"};
|
115
|
|
|
|
|
|
|
} # if
|
116
|
5
|
|
|
|
|
144
|
$sRet .= qq{$sId $sProp $sVal .\n};
|
117
|
5
|
|
|
|
|
18
|
$self->{_iTriples_}++;
|
118
|
|
|
|
|
|
|
} # foreach LITERAL_PROPERTY
|
119
|
|
|
|
|
|
|
} # foreach LITERAL
|
120
|
3
|
|
|
|
|
12
|
delete $object->{literal};
|
121
|
|
|
|
|
|
|
NODEID:
|
122
|
3
|
|
|
|
|
5
|
foreach my $sProp (keys %{$object->{nodeid}})
|
|
3
|
|
|
|
|
12
|
|
123
|
|
|
|
|
|
|
{
|
124
|
|
|
|
|
|
|
NODEID_PROPERTY:
|
125
|
0
|
|
|
|
|
0
|
foreach my $sVal (@{$object->{nodeid}->{$sProp}})
|
|
0
|
|
|
|
|
0
|
|
126
|
|
|
|
|
|
|
{
|
127
|
0
|
|
|
|
|
0
|
$sRet .= qq{$sId $sProp :$sVal .\n};
|
128
|
0
|
|
|
|
|
0
|
$self->{_iTriples_}++;
|
129
|
|
|
|
|
|
|
} # foreach NODEID_PROPERTY
|
130
|
|
|
|
|
|
|
} # foreach NODEID
|
131
|
3
|
|
|
|
|
8
|
delete $object->{nodeid};
|
132
|
|
|
|
|
|
|
RESOURCE:
|
133
|
3
|
|
|
|
|
6
|
foreach my $sProp (keys %{$object->{resource}})
|
|
3
|
|
|
|
|
9
|
|
134
|
|
|
|
|
|
|
{
|
135
|
|
|
|
|
|
|
RESOURCE_PROPERTY:
|
136
|
2
|
|
|
|
|
3
|
foreach my $sVal (@{$object->{resource}->{$sProp}})
|
|
2
|
|
|
|
|
4
|
|
137
|
|
|
|
|
|
|
{
|
138
|
2
|
50
|
|
|
|
7
|
if ($self->_looks_like_uri($sVal))
|
139
|
|
|
|
|
|
|
{
|
140
|
0
|
|
|
|
|
0
|
$sVal = qq{<$sVal>};
|
141
|
|
|
|
|
|
|
} # if
|
142
|
2
|
|
|
|
|
282
|
$sRet .= qq{$sId $sProp $sVal .\n};
|
143
|
2
|
|
|
|
|
8
|
$self->{_iTriples_}++;
|
144
|
|
|
|
|
|
|
} # foreach RESOURCE_PROPERTY
|
145
|
|
|
|
|
|
|
} # foreach RESOURCE
|
146
|
3
|
|
|
|
|
9
|
delete $object->{resource};
|
147
|
3
|
50
|
|
|
|
13
|
print STDERR Dumper($object) if keys %$object;
|
148
|
3
|
|
|
|
|
8
|
$sRet .= qq{\n};
|
149
|
|
|
|
|
|
|
} # foreach OBJECT
|
150
|
2
|
|
|
|
|
8
|
return $sRet;
|
151
|
|
|
|
|
|
|
} # render
|
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=back
|
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=head1 PUBLIC METHODS
|
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=over
|
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=item get_triple_count
|
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
Returns the number of triples created since the last call to
|
163
|
|
|
|
|
|
|
reset_triple_count().
|
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=cut
|
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
sub get_triple_count
|
168
|
|
|
|
|
|
|
{
|
169
|
1
|
|
|
1
|
1
|
789
|
my $self = shift;
|
170
|
1
|
|
|
|
|
8
|
return $self->{_iTriples_};
|
171
|
|
|
|
|
|
|
} # get_triple_count
|
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=item reset_triple_count
|
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
Resets the internal counter of triples to zero.
|
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=cut
|
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
sub reset_triple_count
|
181
|
|
|
|
|
|
|
{
|
182
|
0
|
|
|
0
|
1
|
|
my $self = shift;
|
183
|
0
|
|
|
|
|
|
$self->{_iTriples_} = 0;
|
184
|
|
|
|
|
|
|
} # get_triple_count
|
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
1;
|
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
__END__
|