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): 1985-2005, 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(rdfql_runtime, Chris@0: [ rdfql_carthesian/1, % +Bags Chris@0: rdfql_is_literal/1, % +Node Chris@0: rdfql_is_resource/1, % +Node Chris@0: Chris@0: rdfql_bind_null/1, % +List Chris@0: rdfql_cond_bind_null/1, % +List Chris@0: Chris@0: % SeRQL support Chris@0: serql_compare/3, % +Comparison, +Left, +Right Chris@0: serql_eval/2, % +Term, -Evaluated Chris@0: serql_member_statement/2, % -Triple, +List Chris@0: Chris@0: % SPAQRL support Chris@0: sparql_true/1, % +Term Chris@0: sparql_eval/2 % +Expression, -Result Chris@0: ]). Chris@0: :- use_module(library(nb_set)). Chris@0: :- use_module(library(debug)). Chris@0: :- use_module(serql_runtime). Chris@0: :- use_module(sparql_runtime). Chris@0: Chris@0: :- meta_predicate Chris@0: rdfql_carthesian(:), Chris@0: solve_bags(:,+,-). Chris@0: Chris@0: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Chris@0: This module provides runtime support for running compiled queries. I.e. Chris@0: it defines special constructs that may be emitted by the compiler and Chris@0: optmizer that are common to all query languages. Language specific Chris@0: runtime support is in serql_runtime.pl and sparql_runtime.pl Chris@0: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ Chris@0: Chris@0: /******************************* Chris@0: * CARTHESIAN PRODUCT * Chris@0: *******************************/ Chris@0: Chris@0: rdfql_carthesian(Bags) :- Chris@0: solve_bags(Bags, 1, Sets), Chris@0: ( debugging(carthesian_size) Chris@0: -> solution_set_size(Sets, Size), Chris@0: debug(carthesian_size, 'Total size = ~D; NO select', [Size]) Chris@0: ; true Chris@0: ), Chris@0: ( debugging(carthesian_no_select) Chris@0: -> true Chris@0: ; carthesian_select(Sets) Chris@0: ). Chris@0: Chris@0: solve_bags([], _, []). Chris@0: solve_bags([bag(Vars, Goal)|T0], N, [set(Templ,Set,Size)|T]) :- Chris@0: Templ =.. [v|Vars], Chris@0: empty_nb_set(Set), Chris@0: ( Goal, Chris@0: add_nb_set(Templ, Set), Chris@0: fail Chris@0: ; true Chris@0: ), Chris@0: size_nb_set(Set, Size), Chris@0: debug(carthesian_bags, 'Bag ~d: solution size = ~D', [N, Size]), Chris@0: Size > 0, Chris@0: N2 is N + 1, Chris@0: solve_bags(T0, N2, T). Chris@0: Chris@0: carthesian_select([]). Chris@0: carthesian_select([set(Templ,Set,_)|T]) :- Chris@0: gen_nb_set(Set, Templ), Chris@0: carthesian_select(T). Chris@0: Chris@0: solution_set_size([], 0). Chris@0: solution_set_size([set(_,_,Len)|T], Size) :- Chris@0: ( T == [] Chris@0: -> Size = Len Chris@0: ; solution_set_size(T, Size0), Chris@0: Size is Len * Size0 Chris@0: ). Chris@0: Chris@0: Chris@0: /******************************* Chris@0: * NULL HANDLING * Chris@0: *******************************/ Chris@0: Chris@0: rdfql_cond_bind_null([]). Chris@0: rdfql_cond_bind_null([H|T]) :- Chris@0: ( var(H) Chris@0: -> H = '$null$' Chris@0: ; true Chris@0: ), Chris@0: rdfql_cond_bind_null(T). Chris@0: Chris@0: Chris@0: rdfql_bind_null([]). Chris@0: rdfql_bind_null(['$null$'|T]) :- Chris@0: rdfql_bind_null(T). Chris@0: Chris@0: Chris@0: rdfql_is_literal(literal(_)). Chris@0: rdfql_is_resource(X) :- Chris@0: atom(X).