1 | :- multifile generate/2. | |
2 | :- multifile shrink/3. | |
3 | ||
4 | % Options: | |
5 | % names:ListOfUsedNames, list:S will generate S records using the same names | |
6 | % size:S of record elements, | |
7 | ||
8 | generate(prob_value_record(Type),Record) :- | |
9 | \+is_list(Type) , | |
10 | generate(prob_value_record(Type,[]),Record). | |
11 | ||
12 | % generate a list of records with the same field names but mixed values | |
13 | generate(prob_value_record(Type,Options),ListOfRecords) :- | |
14 | member(list:Size,Options) , | |
15 | (member(names:NameList,Options) | |
16 | -> true | |
17 | ; random(1,20,R) , | |
18 | generate(list(atom([size:5,alph]),[size:R]),NameList)) , | |
19 | length(ListOfRecords,Size) , | |
20 | maplist(generate(prob_value_record(Type,[names:NameList])),ListOfRecords). | |
21 | ||
22 | % record | |
23 | generate(prob_value_record(Type,Options),rec(Record)) :- | |
24 | member(Type,[integer(_),string(_),boolean(_),any(_)]) , | |
25 | (member(size:Size,Options) | |
26 | -> true | |
27 | ; random(1,20,Size)) , | |
28 | length(NType,Size) , | |
29 | % any or given type | |
30 | (Type = any(InOptions) | |
31 | -> maplist(generate(ground_type(InOptions)),NType) | |
32 | ; % list of single type to use maplist | |
33 | length(NType,Size) , | |
34 | maplist(equal(Type),NType)) , | |
35 | (member(names:NameList,Options) | |
36 | -> length(NameList,NewSize) , | |
37 | length(Record,NewSize) , | |
38 | % size of name list is more substantial than given size | |
39 | length(NNType,NewSize) , | |
40 | maplist(equal(Type),NNType) , | |
41 | maplist(prob_field(value),NNType,NameList,Record) | |
42 | ; length(Record,Size) , | |
43 | maplist(prob_field(value),NType,Record)). | |
44 | ||
45 | % shrink single record | |
46 | shrink(Type,rec(Record),rec(Shrunken)) :- | |
47 | Type =.. [prob_value_record|_] , | |
48 | shrink(list(_),Record,Temp) , | |
49 | % don't shrink to an empty record | |
50 | (Temp = [] -> Shrunken = Record , ! ; Shrunken = Temp). | |
51 | % shrink list of records | |
52 | shrink(Type,ListOfRecords,Shrunken) :- | |
53 | Type =.. [prob_value_record|_] , | |
54 | is_list(ListOfRecords) , | |
55 | maplist(shrink(prob_value_record),ListOfRecords,Shrunken). |