| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package RDF::aREF::Query; |
|
2
|
7
|
|
|
7
|
|
42
|
use strict; |
|
|
7
|
|
|
|
|
10
|
|
|
|
7
|
|
|
|
|
170
|
|
|
3
|
7
|
|
|
7
|
|
29
|
use warnings; |
|
|
7
|
|
|
|
|
25
|
|
|
|
7
|
|
|
|
|
138
|
|
|
4
|
7
|
|
|
7
|
|
54
|
use v5.10; |
|
|
7
|
|
|
|
|
21
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.27'; |
|
7
|
|
|
|
|
|
|
|
|
8
|
7
|
|
|
7
|
|
2696
|
use RDF::aREF::Decoder qw(qName languageTag); |
|
|
7
|
|
|
|
|
20
|
|
|
|
7
|
|
|
|
|
463
|
|
|
9
|
7
|
|
|
7
|
|
45
|
use Carp qw(croak); |
|
|
7
|
|
|
|
|
12
|
|
|
|
7
|
|
|
|
|
236
|
|
|
10
|
7
|
|
|
7
|
|
32
|
use RDF::NS; |
|
|
7
|
|
|
|
|
12
|
|
|
|
7
|
|
|
|
|
5922
|
|
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new { |
|
13
|
26
|
|
|
26
|
0
|
79
|
my ($class, %options) = @_; |
|
14
|
|
|
|
|
|
|
|
|
15
|
26
|
|
33
|
|
|
78
|
my $expression = $options{query} // croak "query required"; |
|
16
|
26
|
|
33
|
|
|
163
|
my $ns = $options{ns} // RDF::NS->new; |
|
17
|
26
|
|
33
|
|
|
804815
|
my $decoder = $options{decoder} // RDF::aREF::Decoder->new( ns => $ns ); |
|
18
|
|
|
|
|
|
|
|
|
19
|
26
|
|
|
|
|
157
|
my $self = bless { |
|
20
|
|
|
|
|
|
|
items => [], |
|
21
|
|
|
|
|
|
|
decoder => $decoder |
|
22
|
|
|
|
|
|
|
}, $class; |
|
23
|
|
|
|
|
|
|
|
|
24
|
26
|
|
|
|
|
155
|
my @items = split /\s*\|\s*/, $expression; |
|
25
|
26
|
100
|
|
|
|
108
|
foreach my $expr ( @items ? @items : '' ) { |
|
26
|
32
|
|
|
|
|
66
|
my $type = 'any'; |
|
27
|
32
|
|
|
|
|
56
|
my ($language, $datatype); |
|
28
|
|
|
|
|
|
|
|
|
29
|
32
|
100
|
|
|
|
179
|
if ($expr =~ /^(.*)\.$/) { |
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
30
|
6
|
|
|
|
|
12
|
$type = 'resource'; |
|
31
|
6
|
|
|
|
|
21
|
$expr = $1; |
|
32
|
|
|
|
|
|
|
} elsif ( $expr =~ /^([^@]*)@([^@]*)$/ ) { |
|
33
|
10
|
|
|
|
|
53
|
($expr, $language) = ($1, $2); |
|
34
|
10
|
50
|
66
|
|
|
56
|
if ( $language eq '' or $language =~ languageTag ) { |
|
35
|
10
|
|
|
|
|
22
|
$type = 'literal'; |
|
36
|
|
|
|
|
|
|
} else { |
|
37
|
0
|
|
|
|
|
0
|
croak 'invalid languageTag in aREF query'; |
|
38
|
|
|
|
|
|
|
} |
|
39
|
|
|
|
|
|
|
} elsif ( $expr =~ /^([^^]*)\^([^^]*)$/ ) { # TODO: support explicit IRI |
|
40
|
3
|
|
|
|
|
20
|
($expr, $datatype) = ($1, $2); |
|
41
|
3
|
50
|
|
|
|
17
|
if ( $datatype =~ qName ) { |
|
42
|
3
|
|
|
|
|
7
|
$type = 'literal'; |
|
43
|
3
|
|
|
|
|
15
|
$datatype = $decoder->prefixed_name( split '_', $datatype ); |
|
44
|
3
|
100
|
|
|
|
10
|
$datatype = undef if $datatype eq $decoder->prefixed_name('xsd','string'); |
|
45
|
|
|
|
|
|
|
} else { |
|
46
|
0
|
|
|
|
|
0
|
croak 'invalid datatype qName in aREF query'; |
|
47
|
|
|
|
|
|
|
} |
|
48
|
|
|
|
|
|
|
} |
|
49
|
|
|
|
|
|
|
|
|
50
|
32
|
|
|
|
|
82
|
my @path = split /\./, $expr; |
|
51
|
32
|
|
|
|
|
70
|
foreach (@path) { |
|
52
|
37
|
50
|
66
|
|
|
204
|
croak "invalid aref path expression: $_" if $_ !~ qName and $_ ne 'a'; |
|
53
|
|
|
|
|
|
|
} |
|
54
|
|
|
|
|
|
|
|
|
55
|
32
|
|
|
|
|
44
|
push @{$self->{items}}, { |
|
|
32
|
|
|
|
|
200
|
|
|
56
|
|
|
|
|
|
|
path => \@path, |
|
57
|
|
|
|
|
|
|
type => $type, |
|
58
|
|
|
|
|
|
|
language => $language, |
|
59
|
|
|
|
|
|
|
datatype => $datatype, |
|
60
|
|
|
|
|
|
|
}; |
|
61
|
|
|
|
|
|
|
} |
|
62
|
|
|
|
|
|
|
|
|
63
|
26
|
|
|
|
|
151
|
$self; |
|
64
|
|
|
|
|
|
|
} |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub query { |
|
67
|
0
|
|
|
0
|
1
|
0
|
my ($self) = @_; |
|
68
|
|
|
|
|
|
|
join '|', map { |
|
69
|
0
|
|
|
|
|
0
|
my $q = join '.', @{$_->{path}}; |
|
|
0
|
|
|
|
|
0
|
|
|
70
|
0
|
0
|
|
|
|
0
|
if ($_->{type} eq 'literal') { |
|
|
|
0
|
|
|
|
|
|
|
71
|
0
|
0
|
|
|
|
0
|
if ($_->{datatype}) { |
|
72
|
0
|
|
|
|
|
0
|
$q .= '^' . $_->{datatype}; |
|
73
|
|
|
|
|
|
|
} else { |
|
74
|
0
|
|
0
|
|
|
0
|
$q .= '@' . ($_->{language} // ''); |
|
75
|
|
|
|
|
|
|
} |
|
76
|
|
|
|
|
|
|
} elsif ($_->{type} eq 'resource') { |
|
77
|
0
|
|
|
|
|
0
|
$q .= '.'; |
|
78
|
|
|
|
|
|
|
} |
|
79
|
0
|
|
|
|
|
0
|
$q; |
|
80
|
0
|
|
|
|
|
0
|
} @{$self->{items}} |
|
|
0
|
|
|
|
|
0
|
|
|
81
|
|
|
|
|
|
|
} |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub apply { |
|
84
|
26
|
|
|
26
|
0
|
81
|
my ($self, $rdf, $subject) = @_; |
|
85
|
26
|
|
|
|
|
33
|
map { $self->_apply_item($_, $rdf, $subject) } @{$self->{items}}; |
|
|
32
|
|
|
|
|
88
|
|
|
|
26
|
|
|
|
|
76
|
|
|
86
|
|
|
|
|
|
|
} |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub _apply_item { |
|
89
|
32
|
|
|
32
|
|
80
|
my ($self, $item, $rdf, $subject) = @_; |
|
90
|
|
|
|
|
|
|
|
|
91
|
32
|
|
|
|
|
51
|
my $decoder = $self->{decoder}; |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
# TODO: Support RDF::Trine::Model |
|
94
|
|
|
|
|
|
|
# TODO: try abbreviated *and* full URI? |
|
95
|
32
|
|
|
|
|
64
|
my @current = $rdf; |
|
96
|
32
|
100
|
|
|
|
107
|
if ($subject) { |
|
97
|
25
|
100
|
|
|
|
65
|
if ($rdf->{_id}) { |
|
98
|
2
|
50
|
|
|
|
6
|
return if $rdf->{_id} ne $subject; |
|
99
|
|
|
|
|
|
|
} else { |
|
100
|
23
|
|
|
|
|
63
|
@current = ($rdf->{$subject}); |
|
101
|
|
|
|
|
|
|
} |
|
102
|
|
|
|
|
|
|
} |
|
103
|
|
|
|
|
|
|
|
|
104
|
32
|
|
|
|
|
47
|
my @path = @{$item->{path}}; |
|
|
32
|
|
|
|
|
86
|
|
|
105
|
32
|
100
|
100
|
|
|
94
|
if (!@path and $item->{type} ne 'resource') { |
|
106
|
1
|
50
|
|
|
|
3
|
if ($item->{type} eq 'any') { |
|
107
|
1
|
50
|
|
|
|
12
|
return ($subject ? $subject : $rdf->{_id}); |
|
108
|
|
|
|
|
|
|
} |
|
109
|
|
|
|
|
|
|
} |
|
110
|
|
|
|
|
|
|
|
|
111
|
31
|
|
|
|
|
95
|
while (my $field = shift @path) { |
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
# get objects in aREF |
|
114
|
88
|
|
|
|
|
174
|
@current = grep { defined } |
|
115
|
55
|
100
|
66
|
|
|
250
|
map { (ref $_ and ref $_ eq 'ARRAY') ? @$_ : $_ } |
|
116
|
37
|
|
|
|
|
69
|
map { $_->{$field} } @current; |
|
|
55
|
|
|
|
|
160
|
|
|
117
|
37
|
100
|
|
|
|
94
|
return if !@current; |
|
118
|
|
|
|
|
|
|
|
|
119
|
35
|
100
|
100
|
|
|
191
|
if (@path or $item->{type} eq 'resource') { |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
# get resources |
|
122
|
38
|
|
|
|
|
66
|
@current = grep { defined } |
|
123
|
11
|
|
|
|
|
26
|
map { $decoder->resource($_) } @current; |
|
|
38
|
|
|
|
|
86
|
|
|
124
|
|
|
|
|
|
|
|
|
125
|
11
|
100
|
|
|
|
28
|
if (@path) { |
|
126
|
|
|
|
|
|
|
# TODO: only if RDF given as predicate map! |
|
127
|
7
|
|
|
|
|
10
|
@current = grep { defined } map { $rdf->{$_} } @current; |
|
|
25
|
|
|
|
|
51
|
|
|
|
25
|
|
|
|
|
108
|
|
|
128
|
|
|
|
|
|
|
} |
|
129
|
|
|
|
|
|
|
} |
|
130
|
|
|
|
|
|
|
} |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
# last path element |
|
133
|
29
|
|
|
|
|
59
|
@current = grep { defined } map { $decoder->object($_) } @current; |
|
|
59
|
|
|
|
|
114
|
|
|
|
59
|
|
|
|
|
157
|
|
|
134
|
|
|
|
|
|
|
|
|
135
|
29
|
100
|
|
|
|
86
|
if ($item->{type} eq 'literal') { |
|
136
|
13
|
|
|
|
|
25
|
@current = grep { @$_ > 1 } @current; |
|
|
23
|
|
|
|
|
44
|
|
|
137
|
|
|
|
|
|
|
|
|
138
|
13
|
100
|
|
|
|
40
|
if ($item->{language}) { # TODO: use language tag substring |
|
|
|
100
|
|
|
|
|
|
|
139
|
5
|
100
|
|
|
|
9
|
@current = grep { $_->[1] and $_->[1] eq $item->{language} } @current; |
|
|
11
|
|
|
|
|
37
|
|
|
140
|
|
|
|
|
|
|
} elsif ($item->{datatype}) { # TODO: support qName and explicit IRI |
|
141
|
2
|
50
|
|
|
|
3
|
@current = grep { $_->[2] and $_->[2] eq $item->{datatype} } @current; |
|
|
2
|
|
|
|
|
11
|
|
|
142
|
|
|
|
|
|
|
} |
|
143
|
|
|
|
|
|
|
} |
|
144
|
|
|
|
|
|
|
|
|
145
|
29
|
|
|
|
|
96
|
map { $_->[0] } @current; # IRI or string value |
|
|
50
|
|
|
|
|
263
|
|
|
146
|
|
|
|
|
|
|
} |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
1; |
|
149
|
|
|
|
|
|
|
__END__ |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=head1 NAME |
|
152
|
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
RDF::aREF::Query - aREF query expression |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
my $rdf = { |
|
158
|
|
|
|
|
|
|
'http://example.org/book' => { |
|
159
|
|
|
|
|
|
|
dct_creator => [ |
|
160
|
|
|
|
|
|
|
'http://example.org/alice', |
|
161
|
|
|
|
|
|
|
'http://example.org/bob' |
|
162
|
|
|
|
|
|
|
] |
|
163
|
|
|
|
|
|
|
}, |
|
164
|
|
|
|
|
|
|
'http://example.org/alice' => { |
|
165
|
|
|
|
|
|
|
foaf_name => "Alice" |
|
166
|
|
|
|
|
|
|
}, |
|
167
|
|
|
|
|
|
|
'http://example.org/bob' => { |
|
168
|
|
|
|
|
|
|
foaf_name => "Bob" |
|
169
|
|
|
|
|
|
|
} |
|
170
|
|
|
|
|
|
|
}; |
|
171
|
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
my $getnames = RDF::aREF::Query->new( |
|
173
|
|
|
|
|
|
|
query => 'dct_creator.foaf_name' |
|
174
|
|
|
|
|
|
|
); |
|
175
|
|
|
|
|
|
|
my @names = $getnames->apply( $rdf, 'http://example.org/boo' ); |
|
176
|
|
|
|
|
|
|
$getnames->query; # 'dct_creator.foaf_name' |
|
177
|
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
use RDF::aREF qw(aref_query_map); |
|
179
|
|
|
|
|
|
|
my $record = aref_query_map( $rdf, $publication, { |
|
180
|
|
|
|
|
|
|
'dct_creator@' => 'creator', |
|
181
|
|
|
|
|
|
|
'dct_creator.foaf_name' => 'creator', |
|
182
|
|
|
|
|
|
|
}); |
|
183
|
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
185
|
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
Implements L<aREF query|http://gbv.github.io/aREF/aREF.html#aref-query>, a |
|
187
|
|
|
|
|
|
|
query language to access strings and nodes from agiven RDF graph. |
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
See also functions C<aref_query> and C<aref_query_map> in L<RDF::aREF> for |
|
190
|
|
|
|
|
|
|
convenient application. |
|
191
|
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=head1 CONFIGURATION |
|
193
|
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
The constructor expects the following options: |
|
195
|
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=over |
|
197
|
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=item query |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
L<aREF query|http://gbv.github.io/aREF/aREF.html#aref-query> expression |
|
201
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=item decoder |
|
203
|
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
Instance of L<RDF::aREF::Decoder> to map qNames to URIs. A new instance is |
|
205
|
|
|
|
|
|
|
created unless given. |
|
206
|
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=item ns |
|
208
|
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
Optional namespace map (L<RDF::NS>), passed to the constructor of |
|
210
|
|
|
|
|
|
|
L<RDF::aREF::Decoder> if no decoder is given. |
|
211
|
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
=back |
|
213
|
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
=head1 METHODS |
|
215
|
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=head1 apply( $graph [, $origin ] ) |
|
217
|
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
Perform the query on a given RDF graph. The graph can be given as aREF |
|
219
|
|
|
|
|
|
|
structure (subject map or predicate map) or as instance of |
|
220
|
|
|
|
|
|
|
L<RDF::Trine::Model>. An origin subject node must be provided unless the RDF |
|
221
|
|
|
|
|
|
|
graph is provided as L<predicate |
|
222
|
|
|
|
|
|
|
map|http://gbv.github.io/aREF/aREF.html#predicate-maps>. |
|
223
|
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=head1 query |
|
225
|
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
Returns the aREF query expression |
|
227
|
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
229
|
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
Use SPARQL for more complex queries, e.g. with L<RDF::Trine::Store::SPARQL>. |
|
231
|
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=cut |