Chris@0: /* $Id$ Chris@0: Chris@0: Part of SWI-Prolog Chris@0: Chris@0: Author: Jan Wielemaker Chris@0: E-mail: wielemak@science.uva.nl Chris@0: WWW: http://www.swi-prolog.org Chris@0: Copyright (C): 2004-2006, University of Amsterdam Chris@0: Chris@0: This program is free software; you can redistribute it and/or Chris@0: modify it under the terms of the GNU General Public License Chris@0: as published by the Free Software Foundation; either version 2 Chris@0: of the License, or (at your option) any later version. Chris@0: Chris@0: This program is distributed in the hope that it will be useful, Chris@0: but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: GNU General Public License for more details. Chris@0: Chris@0: You should have received a copy of the GNU Lesser General Public Chris@0: License along with this library; if not, write to the Free Software Chris@0: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Chris@0: Chris@0: As a special exception, if you link this library with other files, Chris@0: compiled with a Free Software compiler, to produce an executable, this Chris@0: library does not by itself cause the resulting executable to be covered Chris@0: by the GNU General Public License. This exception does not however Chris@0: invalidate any other reasons why the executable file might be covered by Chris@0: the GNU General Public License. Chris@0: */ Chris@0: Chris@0: :- module(sparql_client, Chris@0: [ sparql_query/3, % +Query, -Row, +Options Chris@0: sparql_set_server/1 % +Options Chris@0: ]). Chris@0: :- use_module(library('http/http_open')). Chris@0: :- use_module(sparql_xml_result). Chris@0: :- use_module(library(lists)). Chris@0: :- use_module(library(rdf)). Chris@0: Chris@0: Chris@0: %% sparql_query(+Query, -Result, +Options) is nondet. Chris@0: % Chris@0: % Execute a SPARQL query on an HTTP SPARQL endpoint. Query is an Chris@0: % atom that denotes the query. Result is unified to a term Chris@0: % rdf(S,P,O) for =construct= queries and row(...) for =select= Chris@0: % queries. Chris@0: Chris@0: sparql_query(Query, Row, Options) :- Chris@0: sparql_param(host(Host), Options), Chris@0: sparql_param(port(Port), Options), Chris@0: sparql_param(path(Path), Options), Chris@0: http_open([ protocol(http), Chris@0: host(Host), Chris@0: port(Port), Chris@0: path(Path), Chris@0: search([ query = Query Chris@0: ]) Chris@0: ], In, Chris@0: [ header(content_type, ContentType) Chris@0: ]), Chris@0: plain_content_type(ContentType, CleanType), Chris@0: read_reply(CleanType, In, Row). Chris@0: Chris@0: read_reply('application/rdf+xml', In, Row) :- !, Chris@0: call_cleanup(load_rdf(stream(In), RDF), close(In)), Chris@0: member(Row, RDF). Chris@0: read_reply('application/sparql-result+xml', In, Row) :- !, Chris@0: call_cleanup(sparql_read_xml_result(stream(In), Result), Chris@0: close(In)), Chris@0: xml_result(Result, Row). Chris@0: read_reply(Type, In, _) :- Chris@0: close(In), Chris@0: throw(error(domain_error(sparql_result_document, Type), _)). Chris@0: Chris@0: plain_content_type(Type, Plain) :- Chris@0: sub_atom(Type, B, _, _, (;)), !, Chris@0: sub_string(Type, 0, B, _, Main), Chris@0: normalize_space(atom(Plain), Main). Chris@0: plain_content_type(Type, Type). Chris@0: Chris@0: xml_result(ask(Bool), Result) :- !, Chris@0: Result = Bool. Chris@0: xml_result(select(_VarNames, Rows), Result) :- Chris@0: member(Result, Rows). Chris@0: Chris@0: Chris@0: Chris@0: Chris@0: Chris@0: /******************************* Chris@0: * SETTINGS * Chris@0: *******************************/ Chris@0: Chris@0: :- dynamic Chris@0: sparql_setting/1. Chris@0: Chris@0: sparql_param(Param, Options) :- Chris@0: memberchk(Param, Options), !. Chris@0: sparql_param(Param, _Options) :- Chris@0: sparql_setting(Param), !. Chris@0: sparql_param(Param, _Options) :- Chris@0: functor(Param, Name, _), Chris@0: throw(error(existence_error(option, Name), _)). Chris@0: Chris@0: %% sparql_set_server(+OptionOrList) Chris@0: % Chris@0: % Set sparql server default options. Provided defaults are: Chris@0: % host, port and repository. For example: Chris@0: % Chris@0: %% set_sparql_default([ host(localhost), Chris@0: %% port(8080) Chris@0: %% repository(world) Chris@0: % ]) Chris@0: Chris@0: sparql_set_server([]) :- !. Chris@0: sparql_set_server([H|T]) :- !, Chris@0: sparql_set_server(H), Chris@0: sparql_set_server(T). Chris@0: sparql_set_server(Term) :- Chris@0: functor(Term, Name, Arity), Chris@0: functor(Unbound, Name, Arity), Chris@0: retractall(sparql_setting(Unbound)), Chris@0: assert(sparql_setting(Term)).