使用 append/3 谓词创建 SPARQL 参数化查询 [英] Creating a SPARQL parameterized query using append/3 predicate

查看:60
本文介绍了使用 append/3 谓词创建 SPARQL 参数化查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与我之前的帖子相关:如何在 SWI Prolog 中参数化 SPARQL 查询?

为了练习,我试图实现一个仅使用 append/3 谓词来构建和执行 SPARQL 查询的谓词(与我旧帖子中提出的解决方案不同),但它没有效果不佳.

这是我的谓词:

buildQuery(Place, Query, Row) :-% Q1 = 'select COUNT(*) where {?place a dbpedia-owl:Place ;rdfs:标签Q1 = [39, 115, 101, 108, 101, 99, 116, 32, 67, 79, 85, 78, 84, 40, 42, 41, 32, 119, 104, 101, 11,12, 32, 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, 4, 1, 3, 3100, 102, 115, 58,108, 97, 98, 101, 108, 32, 34],追加(Q1,地方,Q2),%End = @en }}'结束 = [34, 64, 105, 116, 32, 125, 39],追加(Q2,结束,查询),sparql_query(Query, Row, [host('dbpedia.org'), path('/sparql/')]).

因为我发现将 " 字符直接插入字符串时遇到了一些问题.(也就是说,将 " 放入 ""; 也许我可以将一个 " 字符放入表示字符串开头和结尾的 "" 以某种方式对其进行转义.我不知道.)>

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

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

  1. 加载所需的 SPARQL 库:

    ?- use_module(library(semweb/sparql_client)).% library(uri) 编译成 uri 0.02 秒,290,256 字节% library(readutil) 编译成 read_util 0.00 秒,17,464 字节% library(socket) 编译成套接字 0.00 秒,11,936 字节% library(option) 编译成 swi_option 0.00 秒,14,288 字节% library(base64) 编译成 base64 0.01 秒,17,912 字节% library(debug) 编译成 prolog_debug 0.00 秒,21,864 字节% library(http/http_open) 编译成 http_open 0.03 秒,438,368 字节% library(sgml) 编译成 sgml 0.01 秒,39,480 字节% library(quintus) 编译成 quintus 0.00 秒,23,896 字节% rewrite 编译为 rewrite 0.00 秒,35,336 字节% library(record) 编译成记录 0.00 秒,31,368 字节% rdf_parser 编译成 rdf_parser 0.01 秒,132,840 字节% library(gensym) 编译成 gensym 0.00 秒,4,792 字节% rdf_triple 编译成 rdf_triple 0.00 秒,39,672 字节% library(rdf) 编译成 rdf 0.02 秒,244,240 字节% library(semweb/sparql_client) 编译成 sparql_client 0.06 秒,707,080 字节真的.

  2. 我执行我的谓词:

    ?- buildQuery("Roma", Query, Row), write(Query).错误: uri:uri_query_components/2: 类型错误: `atomic' 预期, 发现 `[39,115,101,108,101,99,116,32,67,79,85,78,84,40,42,41,32,119,104,101,1301,19132,101,101,191316,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]"^ 例外:(12) 忽略(http_open:parts_search([protocol(http), host('dbpedia.org'), port(80), path('/sparql/'), search([...])], _G1079)) ?蠕变

如您所见,它出错了.奇怪的是我的 Query 值(我使用 write/1 打印)似乎没问题.其实如果我把ASCII列表翻译成字符,它的值是:

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

这是我的原始查询(因此查询字符串似乎将以正确的方式构建)但问题似乎出在其他 sparql_query/3 参数中,这很奇怪,因为它是从上一篇文章的解决方案复制而来,效果很好.为什么?我错过了什么?

解决方案

您会收到错误消息,因为 sparql_query/3 的 Query(第一个)参数是一个原子.因此,构建查询的最简单方法是

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

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

Related to my previous post: How to parameterize a SPARQL query in SWI Prolog?

In order to exercise, I was trying to implement a predicate that builds and executes a SPARQL query using only the append/3 predicate (differently from the solution proposed in my old post) but it doesn't work well.

This is my predicate:

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

Because I have found some problem inserting the " character directly into a string. (That is, putting " into ""; maybe I can put a " character into the "" that represents the beginning and the end of a string by escaping it in some way. I don't know.)

I am trying to build my query in the following way: In Prolog a string is a list of ASCII characters, so I have created a string\list Q1 that represents the string: 'select COUNT(*) where {?place a dbpedia-owl:Place ; rdfs:label ", which is the first part of my query. Then I append to it the value of the Place variable that will be a string that represents a place (for example, "Roma"), creating the new string Q2. Then I append the End string to Q2 creating the final query Query, where end is the last part of my query: %End = @en }}' Finally, I execute the SPARQL query by sparql_query/3 built in predicate, passing to it my final query, Query, and the other two parameters needed (as in the previous post's good working example).

The problem is that it seems not work. In the Prolog shell I execute the following command:

  1. To load the needed SPARQL library:

    ?- 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. I execute my predicate:

    ?- 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
    

As you can see, it goes into an error. The strange thing is that my Query value (I printed it using write/1) seems to be ok. In fact, if I translate the ASCII list into characters, its value is:

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

That is my original query (so it seems that the query string will build in the right way) but seems that the problem is in the others sparql_query/3 parameter and this is strange because it was copied from the previous post solution that works great. Why? What am I missing?

解决方案

You get the error because the Query (first) argument of sparql_query/3 is an atom. So, the simplest way to build your query is

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

Note that this only works as long as Place des not contain a double quote or other special character as defined by the SPARQL string syntax.

这篇关于使用 append/3 谓词创建 SPARQL 参数化查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆