gpt4 book ai didi

prolog - 如何在 SWI Prolog 中参数化 SPARQL 查询?

转载 作者:行者123 更新时间:2023-12-04 00:42:59 25 4
gpt4 key购买 nike

我很难使以下 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/')]).

这似乎行不通。

最佳答案

您可以使用 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/')] ).

关于prolog - 如何在 SWI Prolog 中参数化 SPARQL 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16820606/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com