与我之前的帖子相关:如何在 SWI Prolog 中参数化 SPARQL 查询? https://stackoverflow.com/questions/16820606/how-to-parameterize-a-sparql-query-in-swi-prolog/16822345?noredirect=1#comment24256405_16822345
为了练习,我尝试实现一个谓词,该谓词仅使用append/3谓词(与我旧帖子中提出的解决方案不同),但效果不佳。
这是我的谓词:
buildQuery(Place, Query, Row) :-
% Q1 = 'select COUNT(*) where {?place a dbpedia-owl:Place ; rdfs:label "
Q1 = [39, 115, 101, 108, 101, 99, 116, 32, 67, 79, 85, 78, 84, 40, 42, 41, 32, 119, 104, 101, 114, 101, 32, 123, 63, 112, 108, 97, 99,
101, 32, 97, 32, 100, 98, 112, 101, 100, 105, 97, 45, 111, 119, 108, 58, 80, 108, 97, 99, 101, 32, 59, 32, 114, 100, 102, 115, 58,
108, 97, 98, 101, 108, 32, 34],
append(Q1, Place, Q2),
%End = @en }}'
End = [34, 64, 105, 116, 32, 125, 39],
append(Q2, End, Query),
sparql_query(Query, Row, [ host('dbpedia.org'), path('/sparql/')] ).
因为我发现插入时出现一些问题"字符直接转为字符串。 (也就是说,把" into ""
;也许我可以放一个"字符进入""
通过以某种方式转义字符串来表示字符串的开头和结尾。我不知道。)
我正在尝试通过以下方式构建我的查询:
在 Prolog 中,字符串是 ASCII 字符的列表,所以我创建了一个字符串\列表 Q1代表字符串:'select COUNT(*) where {?place a dbpedia-owl:Place ; rdfs:label "
,这是我的查询的第一部分。然后我将其值附加到Place变量,该变量将是表示地点的字符串(例如,"Roma"
),创建新字符串Q2。然后我附加End字符串到 Q2 创建最终查询Query, where end是我查询的最后一部分:%结束 = @en }}'最后,我执行 SPARQL 查询sparql_query/3内置谓词,将我的最终查询传递给它,Query,以及所需的其他两个参数(如上一篇文章的良好工作示例中所示)。
问题是它似乎不起作用。在 Prolog shell 中,我执行以下命令:
-
要加载所需的 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.01 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.01 sec, 39,480 bytes
% library(quintus) compiled into quintus 0.00 sec, 23,896 bytes
% rewrite compiled into rewrite 0.00 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.02 sec, 244,240 bytes
% library(semweb/sparql_client) compiled into sparql_client 0.06 sec, 707,080 bytes
true.
-
我执行我的谓词:
?- buildQuery("Roma", Query, Row), write(Query).
ERROR: uri:uri_query_components/2: Type error: `atomic' expected, found `[39,115,101,108,101,99,116,32,67,79,85,78,84,40,42,41,32,119,104,101,114,101,32,123,63,112,108,97,99,101,32,97,32,100,98,112,101,100,105,97,45,111,119,108,58,80,108,97,99,101,32,59,32,114,100,102,115,58,108,97,98,101,108,32,34,82,111,109,97,34,64,105,116,32,125,39]'
^ Exception: (12) ignore(http_open:parts_search([protocol(http), host('dbpedia.org'), port(80), path('/sparql/'), search([...])], _G1079)) ? creep
正如您所看到的,它出现了错误。奇怪的是我的查询值(我使用 write/1 打印它)似乎没问题。事实上,如果我将 ASCII 列表翻译成字符,它的值为:
'选择 COUNT(*) where {?放置 dbpedia-owl:Place ; rdfs:标签“罗马”@it }'
这是我原来的查询(所以看起来查询字符串将以正确的方式构建),但问题似乎出在其他查询上sparql_query/3参数,这很奇怪,因为它是从之前的帖子解决方案复制而来的,效果很好。为什么?我缺少什么?