Query/Federate.pm untested

filter: Statementstmt 0.0% 0 / 190 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 246 CC 11 SCAR 25.4
CC
11
Cov
0%
algebra_fixup
30.0
_join_services_for_bgp_triples
30.0
_services_for_bgp_triples
12.0
CRAP 12.7
1 # RDF::Query::Federate
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Query::Federate - A subclass of RDF::Query for efficient federated query execution.
7  
8 =head1 VERSION
9  
10 This document describes RDF::Query::Federate version 2.919.
11  
12 =head1 STATUS
13  
14 This module's API and functionality should be considered deprecated.
15 If you need functionality that this module provides,
16 please L<get in touch|http://www.perlrdf.org/>.
17  
18 =head1 SYNOPSIS
19  
20 my $service = RDF::Query::ServiceDescription->new( $url );
21 my $query = new RDF::Query::Federate ( $sparql );
22 $query->add_service( $service );
23 my $stream = $query->execute();
24  
25 =head1 DESCRIPTION
26  
27 ...
28  
29 =cut
30  
31 package RDF::Query::Federate;
32  
33 use strict;
34 use warnings;
35 no warnings 'redefine';
36 use base qw(RDF::Query);
37  
38 use Data::Dumper;
39 use Log::Log4perl;
40 use Scalar::Util qw(blessed);
41  
42 use RDF::Query;
43 use RDF::Query::Federate::Plan;
44 use RDF::Query::ServiceDescription;
45 use RDF::Trine::Iterator qw(sgrep smap swatch);
46  
47 ######################################################################
48  
49 our ($VERSION);
50 BEGIN {
51 $VERSION = '2.919';
52 }
53  
54  
55 ######################################################################
56  
57 =head1 METHODS
58  
59 =over 4
60  
61 =cut
62  
63  
64 =item C<< new ( $query, \%options ) >>
65  
66 =item C<< new ( $query, $base_uri, $languri, $lang ) >>
67  
68 Returns a new RDF::Query::Federate object for the specified C<$query>.
69 The query language defaults to SPARQLP, but may be set specifically by
70 specifying either C<$languri> or C<$lang>, whose acceptable values are:
71  
72 $lang: 'rdql', 'sparql11', or 'sparql'
73  
74 $languri: 'http://www.w3.org/TR/rdf-sparql-query/', or 'http://jena.hpl.hp.com/2003/07/query/RDQL'
75  
76 =cut
77  
78 sub new {
79 my $class = shift;
80 my $query = shift;
81 my $base_uri = shift;
82 my $languri = shift;
83 my $lang = shift || 'sparql11';
84 return $class->SUPER::new( $query, $base_uri, $languri, $lang, @_ );
85 }
86  
87  
88 =item C<< add_service ( $service_description ) >>
89  
90 Adds the service described by C<< $service_description >> to the query's list
91 of data sources.
92  
93 =cut
94  
95 sub add_service {
96 my $self = shift;
97 my $service = shift;
98 push(@{ $self->{ services } }, $service);
99 $self->add_computed_statement_generator( $service->computed_statement_generator );
100
101 # and clear out the per-execution query cache, because adding a service might affect the cached values
102 $self->{_query_cache} = {};
103 return;
104 }
105  
106 =item C<< services >>
107  
108 =cut
109  
110 sub services {
111 my $self = shift;
112 return @{ $self->{ services } || [] };
113 }
114  
115  
116  
117 =item C<< algebra_fixup ( $algebra, $bridge, $base_uri, $ns ) >>
118  
119 Called in the fixup method of ::Algebra classes, returns either an optimized
120 ::Algebra object ready for execution, or undef (in which case it will be
121 prepared for execution by the ::Algebra::* class itself.
122  
123 =cut
124  
125 sub algebra_fixup {
126 my $self = shift;
127 my $pattern = shift;
128 my $bridge = shift;
129 my $base_uri = shift;
130 my $ns = shift;
131 my $l = Log::Log4perl->get_logger("rdf.query.federate");
132
133 $l->trace("RDF::Query::Federate::algebra_fixup called");
134 if ($self->{force_no_optimization}) {
135 $l->debug("force_no_optimization flag is set, so not performing federation optimization");
136 return;
137 }
138
139 # optimize BGPs when we're using service descriptions for pattern matching
140 # (instead of local model-based matching). figure out which triples in the
141 # bgp can be sent to which endpoints, and construct appropriate SERVICE
142 # algebra objects to use instead of the BGP.
143 if ($pattern->isa('RDF::Query::Algebra::BasicGraphPattern')) {
144 if ($self->{ service_predicates }) {
145 my @patterns = $self->_services_for_bgp_triples( $pattern, $bridge, $base_uri, $ns );
146 my $simple_ggp = RDF::Query::Algebra::GroupGraphPattern->new( @patterns );
147 my @optimized = $self->_join_services_for_bgp_triples( $pattern, $bridge, $base_uri, $ns, \@patterns );
148
149 if ($l->is_debug) {
150 foreach my $i (0 .. $#optimized) {
151 $l->debug("OPTIMIZED $i:\n" . $optimized[$i]->as_sparql({}, '') . "\n---------------\n");
152 }
153 }
154
155 my @plan = (@optimized, $simple_ggp);
156 while (scalar(@plan) > 1) {
157 my $rhs = pop(@plan);
158 my $lhs = pop(@plan);
159 my $union = RDF::Query::Algebra::Union->new( $lhs, $rhs );
160 push(@plan, $union);
161 }
162
163 my $fixed = do {
164 # turn off optimizations, so we don't end up right back here, trying to optimize this replacement GGP.
165 local($self->{force_no_optimization}) = 1;
166 my $ggp = $plan[0];
167 $ggp->fixup( $self, $bridge, $base_uri, $ns );
168 };
169
170 my $bgp = "\n" . $pattern->as_sparql({}, '');
171 $bgp =~ s/\n/\n\t/g;
172 $l->info("replacing BGP =====> {$bgp\n}\n\ WITH =====>\n" . $fixed->as_sparql({}, ''));
173 return $fixed;
174 }
175 }
176 return $bridge->fixup( $pattern, $self, $base_uri, $ns );
177 }
178  
179  
180 sub _join_services_for_bgp_triples {
181 my $self = shift;
182 my $pattern = shift;
183 my $bridge = shift;
184 my $base_uri = shift;
185 my $ns = shift;
186 my $simplep = shift;
187 my $l = Log::Log4perl->get_logger("rdf.query.federate");
188
189 my @join_patterns; # array of tuples, each containing a service pattern and a list of applicable triples
190 foreach my $sd (@{ $self->{services} }) {
191 my $patterns = $sd->patterns;
192 foreach my $bgp (@$patterns) {
193 push( @join_patterns, [ $bgp, $sd, [], [] ] );
194 }
195 }
196
197 my @triples = $pattern->triples;
198 foreach my $i (0 .. $#triples) {
199 my $triple = $triples[ $i ];
200 my $simple = $simplep->[ $i ];
201
202 $l->debug("looking at triple: " . $triple->as_sparql({}, ''));
203
204 my @services;
205 my $pred = $triple->predicate;
206 if ($pred->isa('RDF::Trine::Node::Variable')) {
207 throw RDF::Query::Error::ExecutionError -text => "Cannot use triples with variable predicates with federation endpoints";
208 } else {
209 foreach my $service_pat (@join_patterns) {
210 my ($bgp, $sd) = @$service_pat;
211 if ($bgp->subsumes( $triple )) {
212 $l->debug("triple {" . $triple->as_sparql({}, "\t") . "} is subsumed by bgp: {" . $bgp->as_sparql({}, "\t") . "}\n");
213 push( @{ $service_pat->[2] }, $triple );
214 } else {
215 $l->debug("triple {" . $triple->as_sparql({}, "\t") . "} IS NOT subsumed by bgp: {" . $bgp->as_sparql({}, "\t") . "}\n");
216 push( @{ $service_pat->[3] }, $simple );
217 }
218 }
219
220 }
221 }
222
223 my @patterns;
224 foreach my $service_pat (@join_patterns) {
225 my (undef, $sd, $join_triples, $simple_triples) = @$service_pat;
226 $l->debug("=====> SERVICE <" . $sd->url . ">\n");
227 $l->debug(Dumper({ $sd->url => [$join_triples] }));
228 my @triples = @$join_triples;
229
230 next unless (scalar(@triples) > 1);
231 my $serviceurl = RDF::Query::Node::Resource->new( $sd->url );
232 my $bgp = RDF::Query::Algebra::BasicGraphPattern->new( @triples );
233 unless ($bgp->connected) {
234 $l->debug("BGP isn't connected. Ignoring.");
235 next;
236 }
237 my $ggp = RDF::Query::Algebra::GroupGraphPattern->new( $bgp );
238 my $service = RDF::Query::Algebra::Service->new( $serviceurl, $ggp );
239 $l->debug("Triples can be grouped together: \n" . $service->as_sparql( {}, '' ));
240
241 my $fullggp = RDF::Query::Algebra::GroupGraphPattern->new( $service, @$simple_triples );
242 push(@patterns, $fullggp);
243 }
244 return @patterns;
245 }
246  
247 sub _services_for_bgp_triples {
248 my $self = shift;
249 my $pattern = shift;
250 my $bridge = shift;
251 my $base_uri = shift;
252 my $ns = shift;
253
254 my $preds = $self->{ service_predicates };
255
256 my %service_triples; # arrays of triples, keyed by service urls
257 my @service_triples; # array of tuples each containing a triple and a list of applicable services
258 foreach my $triple ($pattern->triples) {
259 my @services;
260 my $pred = $triple->predicate;
261 if ($pred->isa('RDF::Trine::Node::Variable')) {
262 throw RDF::Query::Error::ExecutionError -text => "Cannot use triples with variable predicates with federation endpoints";
263 } else {
264 my $purl = $pred->uri_value;
265 my $services = $preds->{ $purl } || [];
266 if (scalar(@$services) == 0) {
267 throw RDF::Query::Error::ExecutionError -text => "Triple is not described as a capability of any federation endpoint: " . $triple->as_sparql;
268 } else {
269 foreach my $sd (@$services) {
270 push( @{ $service_triples{ $sd->url } }, $triple );
271 push( @services, $sd->url );
272 }
273 }
274 }
275 push( @service_triples, [ $triple, \@services ] );
276 }
277
278 my @patterns;
279 my %triples_for_single_service;
280 foreach my $data (@service_triples) {
281 my ($triple, $services) = @$data;
282 my @services = @$services;
283 my @spatterns;
284 # if (scalar(@services) == 1) {
285 # push( @{ $triples_for_single_service{ $services[0] } }, $triple );
286 # } else {
287 foreach my $surl (@services) {
288 my $serviceurl = RDF::Query::Node::Resource->new( $surl );
289 my $ggp = RDF::Query::Algebra::GroupGraphPattern->new( $triple );
290 my $service = RDF::Query::Algebra::Service->new( $serviceurl, $ggp );
291 push(@spatterns, $service);
292 }
293 while (@spatterns > 1) {
294 my @patterns = splice( @spatterns, 0, 2, () );
295 my $union = RDF::Query::Algebra::Union->new( map { RDF::Query::Algebra::GroupGraphPattern->new($_) } @patterns );
296 push(@spatterns, $union );
297 }
298 my $ggp = RDF::Query::Algebra::GroupGraphPattern->new( @spatterns );
299 push(@patterns, $ggp);
300 # }
301 }
302 # foreach my $surl (keys %triples_for_single_service) {
303 # my $triples = $triples_for_single_service{ $surl };
304 # warn "triples for only $surl: " . Dumper($triples) if ($debug);
305 # my $serviceurl = RDF::Query::Node::Resource->new( $surl );
306 # my $ggp = RDF::Query::Algebra::GroupGraphPattern->new( @$triples );
307 # my $service = RDF::Query::Algebra::Service->new( $serviceurl, $ggp );
308 # unshift(@patterns, $service);
309 # }
310 return @patterns;
311 }
312  
313 =begin private
314  
315 =item C<< plan_class >>
316  
317 Returns the class name for Plan generation. This method should be overloaded by
318 RDF::Query subclasses if the implementation also provides a subclass of
319 RDF::Query::Plan.
320  
321 =end private
322  
323 =cut
324  
325 sub plan_class {
326 return 'RDF::Query::Federate::Plan';
327 }
328  
329  
330 1;
331  
332 __END__