comparison cpack/dml/lib/dataset.pl @ 0:718306e29690 tip

commiting public release
author Daniel Wolff
date Tue, 09 Feb 2016 21:05:06 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:718306e29690
1 /* Part of DML (Digital Music Laboratory)
2 Copyright 2014-2015 Samer Abdallah, University of London
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 :- module(dataset,
20 [ dataset/5
21 , dataset_size/2
22 , dataset_query/2
23 , dataset_query_dv/3
24 , dataset_item/2
25 , dataset_items/2
26 , dataset_query_id/3
27 , random_subset/4
28 ]).
29
30 /** <module> Definition and memoising of datasets */
31
32 :- use_module(library(memo)).
33 :- use_module(library(semweb/rdf_db)).
34 :- use_module(library(sandbox)).
35 :- use_module(library(typedef)).
36
37 :- type natural == nonneg.
38 :- persistent_memo dataset(+class:ground,+dbv:ground,-id:ground,-size:natural, -items:list).
39 :- persistent_memo term_hash_id(+object:any, +hash:atom, -id:atom).
40 :- persistent_memo random_subset( +domain:nonneg, +size:nonneg, +index:nonneg, -set:list(nonneg)).
41
42 random_subset(N,K,_,Indices) :- randset(K,N,Indices).
43
44
45 :- initialization memo_attach(memo(datasets),[]).
46
47 :- meta_predicate dataset(1,+,-,-,-), dataset_query_id(1,+,-).
48
49 sandbox:safe_meta(dataset(G,_,_,_,_),[G]).
50 sandbox:safe_meta(dataset_query_id(G,_,_),[G]).
51
52 %% dataset( +Generator:pred(A), +DBVersion, -ID:atom, -Size:natural, -Items:list(A)) is det.
53 %
54 % Note: Generator must be ground.
55 dataset(Generator,_,ID,Size,Items) :-
56 setof(X, call(Generator,X), Items),
57 length(Items,Size),
58 variant_sha1(Items,Hash),
59 term_hash_id(Items,Hash,ID).
60
61 term_hash_id(X,Hash,ID) :-
62 ( browse(term_hash_id(X,Hash,ID)) -> true % makes it safe to call in compute mode
63 ; (ID=Hash; between(1,100,I), variant_sha1(Hash-I,ID)),
64 \+browse(term_hash_id(_,_,ID))
65 ; throw(unable_to_unique_id(X))
66 ).
67
68
69
70 %% dataset_query_id(+Query:class,+DBVersion:ground,-ID:ground) is det.
71 % Get ID for given query and database version. If this query has been
72 % requested before, the previously generated ID will be unified with ID.
73 % Otherwise, a new ID will be created and the list of items stored in
74 % the persistent Prolog database. An empty dataset results in an
75 % exception.
76 dataset_query_id(Q,V,ID) :-
77 dif(Status,fail),
78 memo(dataset(Q,V,ID,_,_),_-Status).
79
80 %% dataset_size(-ID:ground,-Size:natural) is nondet.
81 %% dataset_size(+ID:ground,-Size:natural) is semidet.
82 % True when Size is the number of items in existing dataset with id ID.
83 dataset_size(ID,Size) :- distinct(current_dataset(ID,Size,_)).
84
85 %% dataset_items(-ID,-Items:list) is nondet.
86 %% dataset_items(+ID,-Items:list) is semidet.
87 % True when Items is the list of items in existing dataset with id ID.
88 dataset_items(ID,Items) :- distinct(current_dataset(ID,_,Items)).
89
90 %% dataset_item(-ID,-Item) is nondet.
91 %% dataset_item(+ID,-Item) is nondet.
92 % True when dataset ID contains Item.
93 dataset_item(ID,Item) :- dataset_items(ID,Items), member(Item,Items).
94
95 %% dataset_query(-ID,-Query) is nondet.
96 %% dataset_query(+ID,-Query) is nondet.
97 % True when dataset ID contains Item.
98 dataset_query(ID,Q) :- browse(dataset(Q,_,ID,_,_)).
99
100 %% dataset_query_dv(-ID,-Query,-DV) is nondet.
101 %% dataset_query_dv(+ID,-Query,-DV) is nondet.
102 % True when dataset ID contains Item.
103 dataset_query_dv(ID,Q,DV) :- browse(dataset(Q,DV,ID,_,_)).
104
105 current_dataset(ID,Size,Items) :- browse(dataset(_,_,ID,Size,Items)).