1 | :- multifile generate/2. | |
2 | :- multifile shrink/3. | |
3 | ||
4 | :- use_module(library(lists),[delete/3]). | |
5 | ||
6 | % Options: | |
7 | % not-well-defined-values, size:S, list, avl or none for random value set | |
8 | ||
9 | % generate any type | |
10 | generate(prob_value_set(Options),Value) :- | |
11 | is_list(Options) , | |
12 | generate(ground_type,Type) , | |
13 | generate(prob_value_set(Type,Options),Value). | |
14 | ||
15 | % no options | |
16 | generate(prob_value_set(Type),Value) :- | |
17 | generate(prob_value_set(Type,[]),Value). | |
18 | ||
19 | generate(prob_value_set(Type,Options),Value) :- | |
20 | (member(not-well-defined,Options) | |
21 | -> delete(Options,not-well-defined,T) , | |
22 | TOptions = [not-well-defined-values|T] | |
23 | ; TOptions = Options) , | |
24 | % only generate sets of the same type for any | |
25 | (Type = any(_) -> generate(ground_type,NType) ; NType = Type) , | |
26 | gen_type(NType,value,GenType) , | |
27 | ( member(size:Size,TOptions) | |
28 | -> Size > 0 , NOptions = TOptions | |
29 | ; random(1,50,Size) , NOptions = [size:Size|TOptions]) , | |
30 | ( member(avl,TOptions) | |
31 | -> % avl tree doesn't have duplicates | |
32 | generate(avl_tree(GenType,NOptions),Temp) , | |
33 | Temp \= empty, | |
34 | Value = avl_set(Temp) | |
35 | ; member(list,TOptions) | |
36 | -> generate(list(GenType,NOptions),Temp) , | |
37 | (member(not-well-defined-values,NOptions) | |
38 | -> Value = Temp | |
39 | ; remove_dups(Temp,Value)) | |
40 | ; random_member(SetType,[avl,list]) , | |
41 | generate(prob_value_set(NType,[SetType|NOptions]),Value)). | |
42 | ||
43 | shrink(Type,avl_set(Value),Shrunken) :- | |
44 | Type =.. [prob_value_set|_] , | |
45 | avl_to_list(Value,AVLList) , | |
46 | findall(Key,member(Key-_,AVLList),List) , | |
47 | ((flattened(List) , \+member(avl_set(_),List)) | |
48 | -> shrink(list,List,NewList) | |
49 | ; maplist(shrink(prob_value_any),List,NewList)) , | |
50 | findall(Key-true,member(Key,NewList),NewAVLList) , | |
51 | ( NewAVLList == [] | |
52 | -> Shrunken = [] | |
53 | ; list_to_avl(NewAVLList, NewAvl), | |
54 | Shrunken = avl_set(NewAvl) | |
55 | ). | |
56 | ||
57 | shrink(Type,Value,Shrunken) :- | |
58 | Type =.. [prob_value_set|_] , | |
59 | shrink(list,Value,Shrunken). |