annotate jamendo/sparql-archived/SeRQL/rdfql_runtime.pl @ 27:d95e683fbd35 tip

Enable CORS on urispace redirects as well
author Chris Cannam
date Tue, 20 Feb 2018 14:52:02 +0000
parents df9685986338
children
rev   line source
Chris@0 1 /* $Id$
Chris@0 2
Chris@0 3 Part of SWI-Prolog
Chris@0 4
Chris@0 5 Author: Jan Wielemaker
Chris@0 6 E-mail: wielemak@science.uva.nl
Chris@0 7 WWW: http://www.swi-prolog.org
Chris@0 8 Copyright (C): 1985-2005, University of Amsterdam
Chris@0 9
Chris@0 10 This program is free software; you can redistribute it and/or
Chris@0 11 modify it under the terms of the GNU General Public License
Chris@0 12 as published by the Free Software Foundation; either version 2
Chris@0 13 of the License, or (at your option) any later version.
Chris@0 14
Chris@0 15 This program is distributed in the hope that it will be useful,
Chris@0 16 but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 18 GNU General Public License for more details.
Chris@0 19
Chris@0 20 You should have received a copy of the GNU Lesser General Public
Chris@0 21 License along with this library; if not, write to the Free Software
Chris@0 22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Chris@0 23
Chris@0 24 As a special exception, if you link this library with other files,
Chris@0 25 compiled with a Free Software compiler, to produce an executable, this
Chris@0 26 library does not by itself cause the resulting executable to be covered
Chris@0 27 by the GNU General Public License. This exception does not however
Chris@0 28 invalidate any other reasons why the executable file might be covered by
Chris@0 29 the GNU General Public License.
Chris@0 30 */
Chris@0 31
Chris@0 32 :- module(rdfql_runtime,
Chris@0 33 [ rdfql_carthesian/1, % +Bags
Chris@0 34 rdfql_is_literal/1, % +Node
Chris@0 35 rdfql_is_resource/1, % +Node
Chris@0 36
Chris@0 37 rdfql_bind_null/1, % +List
Chris@0 38 rdfql_cond_bind_null/1, % +List
Chris@0 39
Chris@0 40 % SeRQL support
Chris@0 41 serql_compare/3, % +Comparison, +Left, +Right
Chris@0 42 serql_eval/2, % +Term, -Evaluated
Chris@0 43 serql_member_statement/2, % -Triple, +List
Chris@0 44
Chris@0 45 % SPAQRL support
Chris@0 46 sparql_true/1, % +Term
Chris@0 47 sparql_eval/2 % +Expression, -Result
Chris@0 48 ]).
Chris@0 49 :- use_module(library(nb_set)).
Chris@0 50 :- use_module(library(debug)).
Chris@0 51 :- use_module(serql_runtime).
Chris@0 52 :- use_module(sparql_runtime).
Chris@0 53
Chris@0 54 :- meta_predicate
Chris@0 55 rdfql_carthesian(:),
Chris@0 56 solve_bags(:,+,-).
Chris@0 57
Chris@0 58 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Chris@0 59 This module provides runtime support for running compiled queries. I.e.
Chris@0 60 it defines special constructs that may be emitted by the compiler and
Chris@0 61 optmizer that are common to all query languages. Language specific
Chris@0 62 runtime support is in serql_runtime.pl and sparql_runtime.pl
Chris@0 63 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
Chris@0 64
Chris@0 65 /*******************************
Chris@0 66 * CARTHESIAN PRODUCT *
Chris@0 67 *******************************/
Chris@0 68
Chris@0 69 rdfql_carthesian(Bags) :-
Chris@0 70 solve_bags(Bags, 1, Sets),
Chris@0 71 ( debugging(carthesian_size)
Chris@0 72 -> solution_set_size(Sets, Size),
Chris@0 73 debug(carthesian_size, 'Total size = ~D; NO select', [Size])
Chris@0 74 ; true
Chris@0 75 ),
Chris@0 76 ( debugging(carthesian_no_select)
Chris@0 77 -> true
Chris@0 78 ; carthesian_select(Sets)
Chris@0 79 ).
Chris@0 80
Chris@0 81 solve_bags([], _, []).
Chris@0 82 solve_bags([bag(Vars, Goal)|T0], N, [set(Templ,Set,Size)|T]) :-
Chris@0 83 Templ =.. [v|Vars],
Chris@0 84 empty_nb_set(Set),
Chris@0 85 ( Goal,
Chris@0 86 add_nb_set(Templ, Set),
Chris@0 87 fail
Chris@0 88 ; true
Chris@0 89 ),
Chris@0 90 size_nb_set(Set, Size),
Chris@0 91 debug(carthesian_bags, 'Bag ~d: solution size = ~D', [N, Size]),
Chris@0 92 Size > 0,
Chris@0 93 N2 is N + 1,
Chris@0 94 solve_bags(T0, N2, T).
Chris@0 95
Chris@0 96 carthesian_select([]).
Chris@0 97 carthesian_select([set(Templ,Set,_)|T]) :-
Chris@0 98 gen_nb_set(Set, Templ),
Chris@0 99 carthesian_select(T).
Chris@0 100
Chris@0 101 solution_set_size([], 0).
Chris@0 102 solution_set_size([set(_,_,Len)|T], Size) :-
Chris@0 103 ( T == []
Chris@0 104 -> Size = Len
Chris@0 105 ; solution_set_size(T, Size0),
Chris@0 106 Size is Len * Size0
Chris@0 107 ).
Chris@0 108
Chris@0 109
Chris@0 110 /*******************************
Chris@0 111 * NULL HANDLING *
Chris@0 112 *******************************/
Chris@0 113
Chris@0 114 rdfql_cond_bind_null([]).
Chris@0 115 rdfql_cond_bind_null([H|T]) :-
Chris@0 116 ( var(H)
Chris@0 117 -> H = '$null$'
Chris@0 118 ; true
Chris@0 119 ),
Chris@0 120 rdfql_cond_bind_null(T).
Chris@0 121
Chris@0 122
Chris@0 123 rdfql_bind_null([]).
Chris@0 124 rdfql_bind_null(['$null$'|T]) :-
Chris@0 125 rdfql_bind_null(T).
Chris@0 126
Chris@0 127
Chris@0 128 rdfql_is_literal(literal(_)).
Chris@0 129 rdfql_is_resource(X) :-
Chris@0 130 atom(X).