1 | :- multifile generate/2. | |
2 | :- multifile shrink/3. | |
3 | ||
4 | :- use_module(library(random),[random_member/2]). | |
5 | ||
6 | generate(prob_ast_set_expr,Value) :- | |
7 | generate(prob_ast_set_expr([]),Value). | |
8 | ||
9 | % Options: | |
10 | % id to enable random generation of identifier | |
11 | % all expressions are well-defined unless option not-well-defined-values is given | |
12 | ||
13 | % Expr :: set(_) | |
14 | generate(prob_ast_set_expr(Options),Value) :- | |
15 | is_list(Options) , | |
16 | random_member(Expr,[interval,union,intersection,set_subtraction,bool_set,string_set, | |
17 | general_union,general_intersection,empty_set]) , | |
18 | generate(prob_ast_set_expr(Expr,Options),Value). | |
19 | ||
20 | generate(prob_ast_set_expr(interval,Options),b(Interval,set(integer),[])) :- | |
21 | ( member(posInterval,Options) | |
22 | -> generate(prob_ast_integer([between(0,15)|Options]),Node1) | |
23 | ; member(negInterval,Options) | |
24 | -> generate(prob_ast_integer([between(-15,0)|Options]),Node1) | |
25 | ; generate(prob_ast_integer([between(-15,15)|Options]),Node1)) , | |
26 | Node1 = b(integer(Value1),_,_) , | |
27 | (member(maxInterval:S,Options) | |
28 | -> % interval with given maximum size | |
29 | random(0,S,R) , | |
30 | Value2 is Value1 + R , | |
31 | Node2 = b(integer(Value2),integer,[]) | |
32 | ; generate(prob_ast_integer([small|Options]),Node2) , | |
33 | Node2 = b(integer(Value2),_,_)) , | |
34 | % get value from ast node | |
35 | (Value1 =< Value2 | |
36 | -> Interval = interval(Node1,Node2) | |
37 | ; Interval = interval(Node2,Node1)). | |
38 | ||
39 | generate(prob_ast_set_expr(empty_set,_),b(set_extension([]),set(InnerType),[])) :- | |
40 | generate(prob_type, InnerType). | |
41 | ||
42 | generate(prob_ast_set_expr(bool_set,Options),Value) :- | |
43 | (member(id,Options) | |
44 | -> generate(id_or_ast(set(boolean([]))),Value) | |
45 | ; generate(prob_ast_set(boolean([]),Options),Value)). | |
46 | ||
47 | generate(prob_ast_set_expr(string_set,Options),Value) :- | |
48 | (member(id,Options) | |
49 | -> generate(id_or_ast(set(string([]))),Value) | |
50 | ; generate(prob_ast_set(string([]),Options),Value)). | |
51 | ||
52 | generate(prob_ast_set_expr(Expr,Options),b(NewPred,set(Type),[])) :- | |
53 | member(Expr,[union,intersection,set_subtraction]) , | |
54 | generate(prob_ast_set(Options),Set1) , | |
55 | Set1 = b(_,set(Type),_) , | |
56 | inner_type(Type,Inner,Outter) , | |
57 | NewInner =.. [Inner,[]] , % no options | |
58 | surround_type(NewInner,Outter,NType) , ! , | |
59 | (member(id,Options) , Type \= empty -> | |
60 | generate(id_or_ast(set(NType)),Set2) | |
61 | ; generate(prob_ast_set(NType,Options),Set2)) , | |
62 | NewPred =.. [Expr,Set1,Set2]. | |
63 | ||
64 | generate(prob_ast_set_expr(Expr,Options),b(NewPred,Type,[])) :- | |
65 | member(Expr,[general_union,general_intersection]) , | |
66 | generate(ground_type,SetType) , | |
67 | (member(id,Options) | |
68 | -> generate(id_or_ast(set(SetType)),Set) | |
69 | ; generate(prob_ast_set(SetType,Options),Set)) , | |
70 | Set = b(_,Type,_) , | |
71 | SetOfSet = b(set_extension([Set]),set(Type),[]) , | |
72 | NewPred =.. [Expr,SetOfSet]. | |
73 | ||
74 | shrink(Type,Expression,Shrunken) :- | |
75 | Type =.. [prob_ast_set_expr|_] , | |
76 | minimize_set_expr(Expression,Shrunken). | |
77 | ||
78 | shrink(Type,Value,Shrunken) :- | |
79 | Type =.. [prob_ast_set_expr|_] , | |
80 | % defined in prob_ast_minimize_int_expr.pl | |
81 | get_inner_expr(Value,Shrunken). |