prolog - 使用 append/3 谓词创建 SPARQL 参数化查询

标签 prolog swi-prolog sparql

与我之前的帖子相关:How to parameterize a SPARQL query in SWI Prolog?

为了练习,我试图实现一个仅使用 append/3 谓词构建和执行 SPARQL 查询的谓词(不同于我在旧帖子中提出的解决方案),但它没有'工作得很好。

这是我的谓词:

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/')] ).

因为我发现将 " 字符直接插入到字符串中存在一些问题。(也就是说,将 " 放入 "" ;也许我可以将 " 字符到 "" 中,通过某种方式转义表示字符串的开头和结尾。我不知道。)

我正在尝试通过以下方式构建我的查询: 在 Prolog 中,字符串是 ASCII 字符的列表,因此我创建了一个 string\list Q1 来表示字符串:'select COUNT(*) where {?place a dbpedia-owl:Place ; rdfs:label " ,这是我查询的第一部分。然后,我将 Place 变量的值附加到它,该变量将是表示地点的字符串(例如 "Roma" ),从而创建新字符串 Q2。然后我将 End 字符串附加到 Q2,创建最终查询 Query,其中 end 是我查询的最后一部分:%End = @en }}' 最后,我通过 sparql_query/3 内置谓词执行 SPARQL 查询,将我的最终查询 Query 和需要其他两个参数(如前一篇文章的良好工作示例)。

问题是它似乎不起作用。在 Prolog shell 中,我执行以下命令:

  1. 加载所需的 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.
    
  2. 我执行我的谓词:

    ?- 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 列表翻译成字符,它的值是:

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

那是我原来的查询(所以看起来查询字符串会以正确的方式构建)但似乎问题出在其他 sparql_query/3 参数中,这很奇怪,因为它是从之前的帖子解决方案复制而来,效果很好。为什么?我错过了什么?

最佳答案

你得到错误是因为 sparql_query/3 的查询(第一个)参数是一个原子。因此,构建查询的最简单方法是

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

请注意,这仅在 Place des 不包含双引号或 SPARQL 字符串语法定义的其他特殊字符时有效。

关于prolog - 使用 append/3 谓词创建 SPARQL 参数化查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16833316/

相关文章:

list - 关于如何在序言中对列表中的元素求和的一些麻烦

variables - 保存变量 Prolog

Prolog 在限定时间内找到所有解决方案

visual-studio-code - 如何在 VSCode 中添加 SWI-Prolog 终端?

sparql - 如何使用 SPARQL 代码获取 RDF 文件或本体中所有谓词的列表?

prolog - 在 SICStus Prolog 中使用 CLMUL

prolog - Prolog 中长度/2 的逻辑推理数(swi-pl)

c# - SWI-prolog 到 C# 断言不工作

SPARQL 涵盖属性的所有子属性

python - 在 python 中设置 rdf 三重存储的好解决方案?