annotate jamendo/sparql-archived/SeRQL/rdfql_util.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): 2004-2006, 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_util,
Chris@0 33 [ select_results/6, % +Distinct, +Offset, +Limit,
Chris@0 34 % :SortBy, -Result, :Goal
Chris@0 35 entailment_module/2 % +Entailment, -Module
Chris@0 36 ]).
Chris@0 37 :- use_module(library(nb_set)).
Chris@0 38
Chris@0 39 :- module_transparent
Chris@0 40 select_results/6,
Chris@0 41 select_results/5.
Chris@0 42
Chris@0 43 %% select_results(+Distinct, +Offset, +Limit, :SortBy, -Result, :Goal)
Chris@0 44 %
Chris@0 45 % Select results for the template Result on backtracking over
Chris@0 46 % Goal.
Chris@0 47 %
Chris@0 48 % @param Distinct
Chris@0 49 % Iff 'distinct', only consider distinct solutions
Chris@0 50 %
Chris@0 51 % @param Offset
Chris@0 52 % Skip the first Offset results. Offset is applied after
Chris@0 53 % Distinct and SortBy
Chris@0 54 %
Chris@0 55 % @param Limit
Chris@0 56 % Only return the first Limit results. Limit is applied
Chris@0 57 % after Distinct, SortBy and Offset. The value 'inf'
Chris@0 58 % returns all values.
Chris@0 59 %
Chris@0 60 % @param SortBy
Chris@0 61 % Either 'unsorted' or the name of a predicate that sorts
Chris@0 62 % the result-set by calling call(SortBy, UnSorted, Sorted)
Chris@0 63
Chris@0 64 select_results(Distinct, Offset, Limit, unsorted, Result, Goal) :- !,
Chris@0 65 select_results(Distinct, Offset, Limit, Result, Goal).
Chris@0 66 select_results(Distinct, Offset, Limit, order_by(Cols), Result, Goal) :- !,
Chris@0 67 term_variables(Cols, Vars),
Chris@0 68 SortKey =.. [v|Vars],
Chris@0 69 findall(SortKey-Result, Goal, Results0),
Chris@0 70 ( Distinct == distinct
Chris@0 71 -> sort(Results0, Results1)
Chris@0 72 ; keysort(Results0, Results1)
Chris@0 73 ),
Chris@0 74 apply_offset(Offset, Results1, Results2),
Chris@0 75 apply_limit(Limit, Results2, Results),
Chris@0 76 member(_Key-Result, Results).
Chris@0 77
Chris@0 78 %% select_results(+Distinct, +Offset, +Limit, -Result, :Goal)
Chris@0 79 %
Chris@0 80 % Unsorted version. In this case we can avoid first collecting all
Chris@0 81 % results.
Chris@0 82
Chris@0 83 select_results(distinct, Offset, Limit, Result, Goal) :- !,
Chris@0 84 term_variables(Result, Vars),
Chris@0 85 V =.. [v|Vars],
Chris@0 86 empty_nb_set(Set),
Chris@0 87 Counter = count(0),
Chris@0 88 Goal,
Chris@0 89 add_nb_set(V, Set, New),
Chris@0 90 New == true,
Chris@0 91 apply_limit(Counter, Offset, Limit, Last),
Chris@0 92 ( Last == true -> ! ; true ).
Chris@0 93 select_results(_, 0, inf, _, G) :- !,
Chris@0 94 G.
Chris@0 95 select_results(_, Offset, Limit, _, G) :- !,
Chris@0 96 Counter = count(0),
Chris@0 97 G,
Chris@0 98 apply_limit(Counter, Offset, Limit, Last),
Chris@0 99 ( Last == true -> ! ; true ).
Chris@0 100
Chris@0 101 apply_limit(_, 0, inf, false) :- !.
Chris@0 102 apply_limit(Counter, Offset, Limit, Last) :-
Chris@0 103 arg(1, Counter, N0),
Chris@0 104 N is N0 + 1,
Chris@0 105 nb_setarg(1, Counter, N),
Chris@0 106 N > Offset,
Chris@0 107 ( Limit \== inf,
Chris@0 108 N =:= Offset+Limit
Chris@0 109 -> Last = true
Chris@0 110 ; Last = false
Chris@0 111 ).
Chris@0 112
Chris@0 113 apply_offset(0, List, List) :- !.
Chris@0 114 apply_offset(N, [_|T], List) :-
Chris@0 115 N > 0,
Chris@0 116 N2 is N - 1,
Chris@0 117 apply_offset(N2, T, List).
Chris@0 118
Chris@0 119 apply_limit(inf, List, List) :- !.
Chris@0 120 apply_limit(0, _, []).
Chris@0 121 apply_limit(N, [H|T0], [H|T]) :-
Chris@0 122 N > 0,
Chris@0 123 N2 is N - 1,
Chris@0 124 apply_offset(N2, T0, T).
Chris@0 125
Chris@0 126
Chris@0 127 /*******************************
Chris@0 128 * ENTAILMENT *
Chris@0 129 *******************************/
Chris@0 130
Chris@0 131 %% entailment_module(+Entailment, -Module)
Chris@0 132 %
Chris@0 133 % Find the Prolog module implementing the entailment rules for a
Chris@0 134 % semantic web language.
Chris@0 135
Chris@0 136 entailment_module(Entailment, Module) :-
Chris@0 137 serql:entailment(Entailment, Module), !.
Chris@0 138 entailment_module(Entailment, _) :-
Chris@0 139 throw(error(existence_error(entailment, Entailment), _)).
Chris@0 140
Chris@0 141 :- multifile
Chris@0 142 serql:entailment/2.
Chris@0 143