
:- ensure_loaded(library('semweb/rdf_db')).

go :-
	thread_create(go1, T1, []),
	thread_create(go1, T2, []),
	thread_create(go1, T3, []),
	thread_create(go1, T4, []),
	thread_create(go1, T5, []),
	thread_join(T1, true),
	thread_join(T2, true),
	thread_join(T3, true),
	thread_join(T4, true),
	thread_join(T5, true).


go1 :-
	flag(r, _, 0),
	flag(c, _, 0),
	do_random(1000).

next(N, Max) :-
	N is random(Max),
	N >= 0,
	N < Max.
%	flag(c, I, I+1),
%	format('used(~w, ~w).~n', [I, N]).

next2(N, Max) :-
	% stupid little "random" number generator
	flag(r, Old, Old),
	Next is ((Old*113 + 117) mod 65535),
	flag(r, _, Next),
	N is Next mod Max,
	N >= 0,
	N < Max.


/*
  c=0
  while read x < t; do
     echo "used($c, $x)." >> used.pl
     c=`expr $c + 1`
  done
  
  */

next3(N, _Max) :-
	flag(c, I, I+1),
	used(I, N).

do_random(N) :-
	MM is N mod 100,
	(   MM = 0
	->  rdf_statistics(triples(_Triples)) %,
	    % format('Count ~w, Triples ~w.~n', [N, Triples])
	;   true
	),
	next(Op, 4),
	rans(Subject),
	ranp(Predicate),
	rano(Object),
	rans(Graph),
	do(Op, Subject, Predicate, Object, Graph),
	N1 is N - 1,
	(   N > 1
	->  do_random(N1)
	;   true
	).

do(0, S, P, O, G) :- rdf_transaction(rdf_assert(S,P,O,G)).
do(1, S, P, O, G) :- rdf_transaction(rdf_retractall(S,P,O,G)).
do(2, S, _P, _O, _G) :- findall(x, mt_rdf(S, _, _, _), _).
do(3, S, P, _O, G) :- findall(x, mt_rdf(S, P, _, G), _).

rans(X) :-
	next(I, 4),
	rs(I, X).
rs(0, a).
rs(1, b).
rs(2, c).
rs(3, d).

ranp(X) :-
	next(I, 4),
	rp(I, X).
rp(0, a).
rp(1, p1).
rp(2, p2).
rp(3, p3).

rano(X) :-
	next(I, 8),
	ro(I, X).
ro(0, a).
ro(1, b).
ro(2, c).
ro(3, p1).
ro(4, literal(1)).
ro(5, literal(hello_world)).
ro(6, literal(bye)).
ro(7, d).

	
	
	
