- Documentation
- Reference manual
- Packages
- SWI-Prolog Semantic Web Library 3.0
- Introduction
- Scalability
- Two RDF APIs
- Plugin modules for rdf_db
- library(semweb/turtle): Turtle: Terse RDF Triple Language
- library(semweb/rdf_ntriples): Process files in the RDF N-Triples format
- library(semweb/rdfa): Extract RDF from an HTML or XML DOM
- library(semweb/rdfs): RDFS related queries
- Managing RDF input files
- library(semweb/sparql_client): SPARQL client library
- library(semweb/rdf_compare): Compare RDF graphs
- library(semweb/rdf_portray): Portray RDF resources
- Related packages
- Version 3 release notes
- SWI-Prolog Semantic Web Library 3.0
10 library(semweb/sparql_client): SPARQL client library
This module provides a SPARQL client. For example:
?- sparql_query('select * where { ?x rdfs:label "Amsterdam" }', Row, [ host('dbpedia.org'), path('/sparql/')]). Row = row('http://www.ontologyportal.org/WordNet#WN30-108949737') ; false.
Or, querying a local server using an ASK
query:
?- sparql_query('ask { owl:Class rdfs:label "Class" }', Row, [ host('localhost'), port(3020), path('/sparql/')]). Row = true.
HTTPS servers are supported using the scheme(https)
option:
?- sparql_query('select * where { ?x rdfs:label "Amsterdam"@nl }', Row, [ scheme(https), host('query.wikidata.org'), path('/sparql') ]).
- [nondet]sparql_query(+Query, -Result, +Options)
- Execute a SPARQL query on an HTTP SPARQL endpoint. Query is
an atom that denotes the query. Result is unified to a term
rdf(S,P,O)
forCONSTRUCT
andDESCRIBE
queries,row(...)
forSELECT
queries andtrue
orfalse
forASK
queries. Options areVariables that are unbound in SPARQL (e.g., due to SPARQL optional clauses), are bound in Prolog to the atom
'$null$'
.- endpoint(+URL)
- May be used as alternative to Scheme, Host, Port and Path to specify the endpoint in a single option.
- host(+Host)
- port(+Port)
- path(+Path)
- scheme(+Scheme)
- The above four options set the location of the server.
- search(+ListOfParams)
- Provide additional query parameters, such as the graph.
- variable_names(-ListOfNames)
- Unifies ListOfNames with a list of atoms that describe the
names of the variables in a
SELECT
query.
Remaining options are passed to http_open/3. The defaults for Host, Port and Path can be set using sparql_set_server/1. The initial default for port is 80 and path is‘/sparql/`.
For example, the ClioPatria server understands the parameter
entailment
. The code below queries for all triples using _rdfs_entailment.?- sparql_query('select * where { ?s ?p ?o }', Row, [ search([entailment=rdfs]) ]).
Another useful option is the
request_header
which, for example, may be used to trick force a server to reply using a particular document format:?- sparql_query( 'select * where { ?s ?p ?o }', Row, [ host('integbio.jp'), path('/rdf/sparql'), request_header('Accept' = 'application/sparql-results+xml') ]).
- sparql_set_server(+OptionOrList)
- Set sparql server default options. Provided defaults are: host, port and
repository. For example:
sparql_set_server([ host(localhost), port(8080) path(world) ])
The default for port is 80 and path is
/sparql/
. - sparql_read_xml_result(+Input, -Result)
- Specs from http://www.w3.org/TR/rdf-sparql-XMLres/.
The returned
Result term is of the format:
- select(VarNames, Rows)
- Where VarNames is a term
v(Name, ...)
and Rows is a list ofrow(....)
containing the column values in the same order as the variable names. - ask(Bool)
- Where Bool is either
true
orfalse
- [det]sparql_read_json_result(+Input, -Result)
- The returned Result term is of the format:
- select(VarNames, Rows)
- Where VarNames is a term
v(Name, ...)
and Rows is a list ofrow(....)
containing the column values in the same order as the variable names. - ask(Bool)
- Where Bool is either
true
orfalse