
pick1(X, Y) :-
	length(X, Max),
	S1 is random(Max),
	nth0(S1, X, Y).

pick10(X, Y) :-
	length(X, Max),
	S0 is random(Max), nth0(S0, X, E0),
	S1 is random(Max), nth0(S1, X, E1),
	S2 is random(Max), nth0(S2, X, E2),
	S3 is random(Max), nth0(S3, X, E3),
	S4 is random(Max), nth0(S4, X, E4),
	S5 is random(Max), nth0(S5, X, E5),
	S6 is random(Max), nth0(S6, X, E6),
	S7 is random(Max), nth0(S7, X, E7),
	S8 is random(Max), nth0(S8, X, E8),
	S9 is random(Max), nth0(S9, X, E9),
	Y = [E0, E1, E2, E3, E4, E5, E6, E7, E8, E9].



/*
   pre rdf_db

/*

   _ user-provided sources
   _ automatically discovered sources
   _ rdf triples
   _ owl sources
   _ self-declared owl ontologies


  re-eval after each load

     self:me sw:sources 221
  
  
*/

sources(S) :-
	findall(S, (
			     manual_source(_,_,_,S),
			     \+ sub_atom(S, 0, _, _, 'blank ')
			   )
			   , L),
	sort(L, L2),
	S=L2.

manual_source(S, P, O, Src) :-
	rxm(( match(S, P, O, G),
	      match(G, retr:loadedFrom, Src, code:retrieve) ))
	;
	rxm(( match(S, P, O, Src) )).


:- rdf_register_ns(rdf,  'http://www.w3.org/1999/02/22-rdf-syntax-ns#').
:- rdf_register_ns(retr, 'http://www.w3.org/2004/03/retr/').
:- rdf_register_ns(code, 'http://www.w3.org/2004/???code???').
:- rdf_register_ns(owl, 'http://www.w3.org/2002/07/owl#').

self_decl_ont(S) :-
	manual_source(S, rdf:type, owl:'Ontology', S).


host(URI, Host) :-
	% assumes Absolute URI (no frag)
	atom_concat('http://', PostScheme, URI),
	(  sub_atom(PostScheme, HostLen, _, _, '/')
	-> sub_atom(PostScheme, 0, HostLen, _, Host)
	;  sub_atom(PostScheme, 0, _, 0, Host)
	).

source_count(Count) :-
	findall(manual_source(Src), manual_source(_S,_P,_O,Src), L),
	sort(L, L2),
	length(L2, Count).

triple_count(Count) :-         % unique triples with unique sources
	findall(manual_source(S,P,O,Src), manual_source(S,P,O,Src), L),
	sort(L, L2),
	length(L2, Count).

fast_triple_count(Count) :-		% unique triples with unique sources
        flag(q, _, 0),
        ((
	rxm(match(_A,_B,_C,D)),
	D\='http://www.w3.org/2004/???code???retrieve',
	flag(q, P, P+1),
	fail
	);true),
	flag(q, Count, Count).

%  0.16 seconds
%  vs 0.22  for ?- time((findall(x, (rxm(match(A,B,C,D)), D\='http://www.w3.org/2004/???code???retrieve'), L), length(L, N))).


subjects(S) :-         % unique triples with unique sources
	findall(S, (
			     manual_source(S,_P,_O,_Src),
			     \+ sub_atom(S, 0, _, _, 'blank ')
			   )
			   , L),
	sort(L, L2),
	S=L2.

subj_count(Count) :-         % unique triples with unique sources
	findall(S, (
			     manual_source(S,_P,_O,_Src),
			     \+ sub_atom(S, 0, _, _, 'blank ')
			   )
			   , L),
	sort(L, L2),
	length(L2, Count).

utriple_count(Count) :-         % unique triples with sources
	findall(manual_source(S,P,O), manual_source(S,P,O,_Src), L),
	sort(L, L2),
	length(L2, Count).

atriple_count(Count) :-         % unique triples with sources
	findall(rdf(S,P,O), rxm(match(S,P,O,_)), L),
	sort(L, L2),
	length(L2, Count).
*/