View source with formatted comments or as raw
    1/*  $Id$
    2
    3    Part of SWI-Prolog
    4
    5    Author:        Jan Wielemaker
    6    E-mail:        wielemak@science.uva.nl
    7    WWW:           http://www.swi-prolog.org
    8    Copyright (C): 2004-2006, University of Amsterdam
    9
   10    This program is free software; you can redistribute it and/or
   11    modify it under the terms of the GNU General Public License
   12    as published by the Free Software Foundation; either version 2
   13    of the License, or (at your option) any later version.
   14
   15    This program is distributed in the hope that it will be useful,
   16    but WITHOUT ANY WARRANTY; without even the implied warranty of
   17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18    GNU General Public License for more details.
   19
   20    You should have received a copy of the GNU Lesser General Public
   21    License along with this library; if not, write to the Free Software
   22    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23
   24    As a special exception, if you link this library with other files,
   25    compiled with a Free Software compiler, to produce an executable, this
   26    library does not by itself cause the resulting executable to be covered
   27    by the GNU General Public License. This exception does not however
   28    invalidate any other reasons why the executable file might be covered by
   29    the GNU General Public License.
   30*/
   31
   32:- module(serql_runtime,
   33	  [ serql_compare/3,		% +Comparison, +Left, +Right
   34	    serql_eval/2,		% +Term, -Evaluated
   35	    serql_member_statement/2	% -Triple, +List
   36	  ]).   37:- use_module(library(xsdp_types)).   38:- use_module(library(debug)).   39:- use_module(library('semweb/rdf_db')).   40
   41
   42/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   43This module provides runtime support for running compiled SeRQL queries.
   44I.e. it defines special constructs that  may   be  emitted  by the SeRQL
   45compiler and optimizer. Predicates common to   all  query languages have
   46been moved to rdfql_runtime.pl
   47- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
   48
   49
   50		 /*******************************
   51		 *	   RUNTIME TESTS	*
   52		 *******************************/
   53
   54%%	serql_compare(+Op, +Left, +Right)
   55%	
   56%	Handle numerical and textual comparison of literals.  Some work
   57%	must be done at compiletime.
   58
   59serql_compare(Op, L, R) :-
   60	serql_eval(L, VL),
   61	serql_eval(R, VR), !,
   62	do_compare(Op, VL, VR).
   63
   64do_compare(like, literal(Value), Pattern) :- !,
   65	to_string(Value, String),
   66	rdf_match_label(like, Pattern, String).
   67do_compare(like, Resource, Pattern) :- !,
   68	atom(Resource),
   69	rdf_match_label(like, Pattern, Resource).
   70do_compare(=, X, X) :- !.
   71do_compare(=, literal(X), query(X)) :- !.
   72do_compare(=, X, query(X)) :- !.
   73do_compare(\=, X, Y) :- !,
   74	\+ do_compare(=, X, Y).
   75do_compare(Op, literal(Data), query(Query)) :-
   76	catch(to_number(Query, Right, TypeQ), _, fail), !,
   77	(   nonvar(TypeQ), atom(Data)
   78	->  catch(xsdp_convert(TypeQ, [Data], Left), _, fail)
   79	;   catch(to_number(Data, Left, TypeD), _, fail),
   80	    serql_subsumes(TypeQ, TypeD)
   81	),
   82	cmp_nums(Op, Left, Right).
   83do_compare(Op, literal(Data), query(Query)) :- !,
   84	(   atom(Query)			% plain text
   85	->  atom(Data),			% TBD: Lang and Type
   86	    cmp_strings(Op, Data, Query)
   87	).
   88do_compare(Op, query(Query), literal(Data)) :- !,
   89	inverse_op(Op, Inverse),
   90	do_compare(Inverse, literal(Data), query(Query)).
   91do_compare(Op, literal(Value), literal(Number)) :-
   92	catch(to_number(Value, Left, TypeL), _, fail),
   93	catch(to_number(Number, Right, TypeR), _, fail),
   94	TypeL == TypeR,
   95	cmp_nums(Op, Left, Right).
   96
   97serql_eval(Var, X) :-
   98	var(Var), !,
   99	X = '$null$'.
  100serql_eval(lang(X), Lang) :- !,
  101	lang(X, Lang).
  102serql_eval(datatype(X), Type) :- !,
  103	datatype(X, Type).
  104serql_eval(label(X), Lang) :- !,
  105	label(X, Lang).
  106serql_eval(X, X).
  107
  108%%	lang(+Literal, -Lang) is det.
  109%%	datatype(+Literal, -DataType) is det.
  110%%	label(+Literal, -Label) is det.
  111%	
  112%	Defined functions on literals.  
  113
  114lang(literal(lang(Lang0, _)), Lang) :-
  115     nonvar(Lang0), !,
  116     Lang = literal(Lang0).
  117lang(_, '$null$').
  118
  119datatype(literal(type(Type0, _)), Type) :-
  120     nonvar(Type0), !,
  121     Type = Type0.
  122datatype(_, '$null$').
  123
  124label(literal(lang(_, Label0)), Label) :-
  125     nonvar(Label0), !,
  126     Label = literal(Label0).
  127label(literal(Label0), Label) :-
  128     nonvar(Label0), !,
  129     Label = literal(Label0).
  130label(_, '$null$').
  131
  132
  133cmp_nums(=, L, R)  :- L =:= R.
  134cmp_nums(=<, L, R) :- L =< R.
  135cmp_nums(<, L, R)  :- L < R.
  136cmp_nums(>, L, R)  :- L > R.
  137cmp_nums(>=, L, R) :- L >= R.
  138
  139cmp_strings(Op, S1, S2) :-
  140	atom(S1), atom(S2),
  141	compare(Op, S1, S2).
  142
  143inverse_op(=, =).
  144inverse_op(=<, >).
  145inverse_op(<, >=).
  146inverse_op(>, =<).
  147inverse_op(>=, <).
  148
  149to_number(type(Type, String), Num, Type) :- !,	% TBD: Check type
  150	atom_number(String, Num).
  151to_number(lang(_Lang, String), Num, _) :- !,
  152	atom_number(String, Num).
  153to_number(String, Num, _) :-
  154	assertion(atom(String)),
  155	atom_number(String, Num).
  156
  157%%	serql_subsumes(QueryType, DataType)
  158
  159serql_subsumes(Q, Var) :-		% odd rule!
  160	nonvar(Q),
  161	var(Var), !.
  162serql_subsumes(_, Var) :-		% odd rule!
  163	var(Var), !, fail.
  164serql_subsumes(Var, _) :-		% type is not specified in query
  165	var(Var), !.
  166serql_subsumes(Query, Data) :-
  167	xsdp_subtype_of(Data, Query).
  168
  169to_string(lang(_, String), String) :- !.
  170to_string(type(_, String), String) :- !.
  171to_string(String, String) :-
  172	atom(String).
  173
  174%%	serql_member_statement(-Triple, +List)
  175%	
  176%	Get the individual triples from  the   original  reply. Used for
  177%	CONSTRUCT queries. As handling optional matches is different int
  178%	SeRQL compared to SPARQL, the selection  is in the SeRQL runtime
  179%	module.
  180
  181serql_member_statement(RDF, [H|_]) :-
  182	member_statement2(RDF, H).
  183serql_member_statement(RDF, [_|T]) :-
  184	serql_member_statement(RDF, T).
  185
  186member_statement2(RDF, optional(True, Statements)) :- !,
  187	True = true,
  188	serql_member_statement(RDF, Statements).
  189member_statement2(RDF, RDF)