-
Notifications
You must be signed in to change notification settings - Fork 15
/
rnd_test.pl
294 lines (250 loc) · 6.93 KB
/
rnd_test.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: J.Wielemaker@vu.nl
WWW: http://www.swi-prolog.org
Copyright (c) 2006-2011, University of Amsterdam
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
:- module(rdf_random_test,
[ concur/2, % +Threads, +Actions
go/0,
go/1, % +Actions
record/1, % +Actions
replay/1 % +Actions
]).
:- asserta(user:file_search_path(foreign, '.')).
:- use_module(rdf_db).
:- use_module(library(thread)).
:- use_module(library(debug)).
replay_file('rnd.reply').
%! concur(+Threads:int, +Actions:int) is det.
%
% Create _N_ Threads, each performing Actions using go/1.
concur(1, Actions) :-
!,
go(Actions).
concur(Threads, Actions) :-
create_threads(Threads, go(Actions), Ids),
wait(Ids).
create_threads(0, _, []) :- !.
create_threads(N, G, [Id|T]) :-
thread_create(G, Id, []),
N2 is N - 1,
create_threads(N2, G, T).
wait([]).
wait([H|T]) :-
thread_join(H, Result),
( Result == true
-> true
; format('ERROR from ~w: ~w~n', [H, Result])
),
wait(T).
%! go is det.
%! go(+N) is det.
%
% Perform N random operations on the database.
go :-
go(20000).
go(N) :-
nb_setval(rnd_file, none),
do_random(N),
rdf_statistics(triples(T)),
rdf_predicate_property(rdfs:subPropertyOf, triples(SP)),
format('~D triples; property hierarchy complexity: ~D~n', [T, SP]).
%! record(+N)
%
% As go/1, but record generated random numbers in the file
% specified with replay_file/1.
record(N) :-
replay_file(File),
open(File, write, Out),
nb_setval(rnd_file, out(Out)),
do_random(N).
%! replay(+N)
%
% Replay first N actions recorded using record/1. N is normally
% the same as used for record/1.
replay(N) :-
replay_file(File),
open(File, read, In),
nb_setval(rnd_file, in(In)),
do_random(N).
%! next(-N, +Max)
%
% Produce a random number 1 =< N <= Max. During record/1, write
% to file. Using replay/1, read from file.
next(N, Max) :-
nb_getval(rnd_file, X),
( X == none
-> N is random(Max)+1
; X = in(Fd)
-> read(Fd, N)
; X = out(Fd),
N is random(Max)+1,
format(Fd, '~q.~n', [N]),
flush_output(Fd)
).
%! do_random(N) is det.
%
% Take a random action on the database.
do_random(N) :-
nb_setval(line, 1),
random_actions(N).
random_actions(N) :-
MM is N mod 100,
( MM = 0
-> rdf_statistics(triples(Triples)),
debug(count, 'Count ~w, Triples ~w', [N, Triples])
; true
),
next(Op, 10),
rans(Subject),
ranp(Predicate),
rano(Object),
rang(Graph),
do(Op, Subject, Predicate, Object, Graph),
N1 is N - 1,
( N > 1
-> random_actions(N1)
; true
).
%! do(+Operation, +Subject, +Predicate, +Object, +Graph) is det.
%
% Execute an operation on Graph.
%
% @tbd Test update
do(1, S, P, O, G) :-
debug(bug(S,P,O), 'ASSERT(~q,~q,~q,~q)', [S,P,O,G]),
rdf_assert(S,P,O,G).
do(2, S, P, O, G) :-
debug(bug(S,P,O), 'RETRACTALL(~q,~q,~q,~q)', [S,P,O,G]),
rdf_retractall(S,P,O,G).
do(3, S, _P, _O, _G) :- rdf_s(S). % allow profiling
do(4, S, P, _O, _G) :- rdf_sp(S, P).
do(5, S, _P, _O, _G) :- has_s(S).
do(6, S, P, _O, _G) :- has_sp(S, P).
do(7, S, P, _O, _G) :- reach_sp(S, P).
do(8, _S, P, O, _G) :- reach_po(P, O).
do(9, _, P, _, G) :- % add a random subproperty below me
repeat,
ranp(P2),
P2 \== P,
!,
rdf_assert(P2, rdfs:subPropertyOf, P, G),
debug(subPropertyOf, 'Added ~p rdfs:subPropertyOf ~p~n', [P2, P]).
do(10, _, P, _, G) :- % randomly delete a subproperty
( rdf(_, rdfs:subPropertyOf, P)
-> repeat,
ranp(P2),
P2 \== P,
rdf(P2, rdfs:subPropertyOf, P),
!,
debug(subPropertyOf, 'Delete ~p rdfs:subPropertyOf ~p~n', [P2, P]),
rdf_retractall(P2, rdfs:subPropertyOf, P, G)
; true
).
rdf_s(S) :-
forall(rdf(S, _, _), true).
rdf_sp(S, P) :-
forall(rdf(S, P, _), true).
has_s(S) :-
forall(rdf_has(S, _, _), true).
has_sp(S, P) :-
forall(rdf_has(S, P, _), true).
reach_sp(S, P) :-
forall(rdf_reachable(S, P, _), true).
reach_po(P, O) :-
( atom(O)
-> forall(rdf_reachable(_, P, O), true)
; true
).
%! rans(-Subject) is det.
%
% Generate a random subject.
rans(X) :-
next(I, 4),
rs(I, X).
rs(1, a).
rs(2, b).
rs(3, c).
rs(4, d).
%! ranp(-Predicate) is det.
%
% Generate a random predicate.
ranp(X) :-
next(I, 4),
rp(I, X).
rp(1, a).
rp(2, p1).
rp(3, p2).
rp(4, p3).
%! rano(-Object) is det.
%
% Generate a random object.
rano(X) :-
next(I, 13),
ro(I, X).
ro(1, a).
ro(2, b).
ro(3, c).
ro(4, p1).
ro(5, literal(1)).
ro(6, literal(hello_world)).
ro(7, literal(bye)).
ro(8, literal(lang(en, bye))).
ro(9, literal(lang(nl, bye))).
ro(10, d).
ro(11, R) :-
next(I, 1000),
atom_concat(r, I, R).
ro(12, literal(L)) :-
next(I, 1000),
atom_concat(l, I, L).
ro(13, literal(lang(Lang, L))) :-
next(I, 1000),
atom_concat(l, I, L),
ranl(Lang).
ranl(Lang) :-
next(I, 2),
rl(I, Lang).
rl(1, en).
rl(2, nl).
%! rang(-Graph) is det.
%
% Generate a random graph.
graph_count(200).
rang(X:Line) :-
graph_count(Count),
next(I, Count),
rg(I, X),
Line = 1.
% line(Line).
term_expansion(rg(x,x), Clauses) :-
graph_count(Count),
findall(rg(I,N), (between(1, Count, I), atom_concat(g,I,N)), Clauses).
rg(x,x).
line(Line) :-
nb_getval(line, Line),
NL is Line+1,
nb_setval(line, NL).