问题
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:
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.
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?
回答1:
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.
来源:https://stackoverflow.com/questions/16833316/creating-a-sparql-parameterized-query-using-append-3-predicate