3

我很难将以下 SPARQL 查询参数化。

首先我加载请求库:

?- use_module(library(semweb/sparql_client)).
%   library(uri) compiled into uri 0.02 sec, 290,256 bytes
%   library(readutil) compiled into read_util 0.00 sec, 17,464 bytes
%   library(socket) compiled into socket 0.00 sec, 11,936 bytes
%   library(option) compiled into swi_option 0.00 sec, 14,288 bytes
%   library(base64) compiled into base64 0.00 sec, 17,912 bytes
%   library(debug) compiled into prolog_debug 0.00 sec, 21,864 bytes
%  library(http/http_open) compiled into http_open 0.03 sec, 438,368 bytes
%   library(sgml) compiled into sgml 0.00 sec, 39,480 bytes
%     library(quintus) compiled into quintus 0.00 sec, 23,896 bytes
%    rewrite compiled into rewrite 0.01 sec, 35,336 bytes
%    library(record) compiled into record 0.00 sec, 31,368 bytes
%   rdf_parser compiled into rdf_parser 0.01 sec, 132,840 bytes
%    library(gensym) compiled into gensym 0.00 sec, 4,792 bytes
%   rdf_triple compiled into rdf_triple 0.00 sec, 39,672 bytes
%  library(rdf) compiled into rdf 0.01 sec, 244,240 bytes
% library(semweb/sparql_client) compiled into sparql_client 0.04 sec, 707,080 bytes
true.

如您所见,我有这个查询,它似乎运行良好:

?- sparql_query('select COUNT(*) where {?place a dbpedia-owl:Place ; rdfs:label "Pescara"@it.}', Row, [ host('dbpedia.org'), path('/sparql/')]).
Row = row(literal(type('http://www.w3.org/2001/XMLSchema#integer', '1'))).

我的问题是我希望这个查询是参数化的。在前面的示例中,我有一个Pescara值是固定的,应该是一个变量。我有类似的东西:

?- Place = 'Roma'

并在查询中:

 ?- sparql_query('select COUNT(*) where {?place a dbpedia-owl:Place ; rdfs:label $Place@it.}', Row, [ host('dbpedia.org'), path('/sparql/')]).

这似乎不起作用。

4

1 回答 1

5

您可以使用atom_concat/3atomic_list_concat/3

:- val('select COUNT(*) where {?place a dbpedia-owl:Place ; rdfs:label $Place@it.}').

12 ?- Z='rdfs:label $', val(X), atom_concat(A,B,X), atom_concat(Z,C,B), 
      atom_concat('Place',D,C), Place='"Roma"', 
      atomic_list_concat([A,Z,Place,D],R).
.....
Place = '"Roma"',
R = 'select COUNT(*) where {?place a dbpedia-owl:Place ; rdfs:label $"Roma"@it.}' ;
false.

接着

?- sparql_query($R, Row, [ host('dbpedia.org'), path('/sparql/')]).

或者,简单地说,

makeQuery(Place, Query, Row) :-   %% e.g. Place = '"Rome"'
    atomic_list_concat( [ 'select COUNT(*) where {?place a dbpedia-owl:Place ;',
      ' rdfs:label $', Place, '@it.}'], Query),
    sparql_query(Query, Row, [ host('dbpedia.org'), path('/sparql/')] ).
于 2013-05-29T19:31:59.617 回答