1 | :- multifile generate/2. | |
2 | :- multifile shrink/3. | |
3 | ||
4 | :- use_module(library(lists),[nth1/4, is_list/1]). | |
5 | :- use_module(library(random)). | |
6 | ||
7 | generate(prob_ast_identifier(Type),b(identifier(Name),Type,[])) :- | |
8 | Type = record(_) , ! , | |
9 | generate(atom([size:5,alph]),Name). | |
10 | ||
11 | generate(prob_ast_identifier(Type),b(identifier(Name),NType,[])) :- | |
12 | generate(atom([size:5,alph]),Name) , | |
13 | inner_type(Type,Inner,Outter) , | |
14 | generate_id_or_ast_aux2(Type,Inner,Outter,NType). | |
15 | ||
16 | % either generate an identifier or an expression | |
17 | generate(id_or_ast(Type),Value) :- | |
18 | random(0,4,R) , | |
19 | R =< 2 , ! , | |
20 | % remove options for type of identifier | |
21 | % argument Type is like set(integer([small])) | |
22 | inner_type(Type,Inner,Outter) , | |
23 | generate_id_or_ast_aux2(Type,Inner,Outter,NType) , | |
24 | generate(prob_ast_identifier(NType),Value). | |
25 | ||
26 | generate(id_or_ast(Type),Value) :- | |
27 | inner_type(Type,Inner,Outter) , | |
28 | generate_id_or_ast_aux(Type,Inner,Outter,NType) , | |
29 | generate(NType,Value). | |
30 | ||
31 | % generate constraints for a list of identifier nodes | |
32 | generate(prob_ast_id_constraints(IDs),ConstraintPred) :- | |
33 | generate(prob_ast_id_constraints(IDs,[]),ConstraintPred). | |
34 | ||
35 | generate(prob_ast_id_constraints(b(Name,integer,Info),Options),ConstraintPred) :- | |
36 | generate(prob_ast_set_expr(interval,Options),Interval) , | |
37 | ConstraintPred = b(member(b(Name,integer,Info),Interval),pred,[]). | |
38 | ||
39 | generate(prob_ast_id_constraints(b(Name,set(InnerType),Info),_),ConstraintPred) :- | |
40 | random_member(Pred,[equal,less,greater,greater_equal,less_equal]) , | |
41 | generate(prob_ast_integer([between(1,10)]),Integer) , | |
42 | Identifier = b(Name,set(InnerType),Info) , | |
43 | NewNode =.. [Pred,b(card(Identifier),integer,[]),Integer] , | |
44 | get_type_constraint(Identifier,InnerType,TypeConstraint) , | |
45 | ConstraintPred = b(conjunct(b(NewNode,pred,[]),TypeConstraint),pred,[]). | |
46 | ||
47 | generate(prob_ast_id_constraints(b(Name,seq(InnerType),Info),_),ConstraintPred) :- | |
48 | random_member(Pred,[equal,less,greater,greater_equal,less_equal]) , | |
49 | generate(prob_ast_integer([between(1,10)]),Integer) , | |
50 | NewNode =.. [Pred,b(size(b(Name,seq(InnerType),Info)),integer,[]),Integer] , | |
51 | ConstraintPred = b(NewNode,pred,[]). | |
52 | ||
53 | generate(prob_ast_id_constraints([IDNode],Options),ConstraintPred) :- | |
54 | generate(prob_ast_id_constraints(IDNode,Options),ConstraintPred). | |
55 | ||
56 | generate(prob_ast_id_constraints([IDNode1,IDNode2|R],Options),ConstraintPred) :- | |
57 | generate(prob_ast_id_constraints(IDNode1,Options),Constraint1) , | |
58 | generate(prob_ast_id_constraints(IDNode2,Options),Constraint2) , | |
59 | generate_identifier_constraint_aux(Constraint1,Constraint2,R,Options,ConstraintPred). | |
60 | % skip boolean, string | |
61 | generate(prob_ast_id_constraints(_,_),b(truth,pred,[])). | |
62 | ||
63 | generate_identifier_constraint_aux(Constraint1,Constraint2,R,Options,ConstraintPred) :- | |
64 | R \= [] , | |
65 | generate(prob_ast_id_constraints(R,Options),RConstraints) , | |
66 | ConstraintPred = b(conjunct(b(conjunct(Constraint1,Constraint2),pred,[]),RConstraints),pred,[]). | |
67 | generate_identifier_constraint_aux(Constraint1,Constraint2,[],_,ConstraintPred) :- | |
68 | ConstraintPred = b(conjunct(Constraint1,Constraint2),pred,[]). | |
69 | ||
70 | get_type_constraint(Identifier,InnerType,TypeConstraint) :- | |
71 | get_set_type(InnerType,SetType) , ! , | |
72 | TypeConstraint = b(subset(Identifier,SetType),pred,[]). | |
73 | get_type_constraint(_,_,b(truth,pred,[])). | |
74 | ||
75 | get_set_type(InnerType,SetType) :- | |
76 | ground(InnerType) , ! , | |
77 | get_set_type_aux(0,InnerType,SetType). | |
78 | get_set_type(_,SetType) :- | |
79 | random(0,3,R) , | |
80 | get_set_type_aux(R,_,SetType). | |
81 | get_set_type_aux(0,integer,b(integer_set('INTEGER'),set(integer),[])). | |
82 | get_set_type_aux(1,integer,b(integer_set('NATURAL'),set(integer),[])). | |
83 | get_set_type_aux(2,integer,b(integer_set('NATURAL1'),set(integer),[])). | |
84 | get_set_type_aux_(set(Type),b(pow_subset(Node),set(Type),[])) :- | |
85 | get_set_type(Type,Node). | |
86 | ||
87 | generate_id_or_ast_aux(_,InnerType,OutterType,NType) :- | |
88 | \+is_list(InnerType) , | |
89 | NewInner =.. [InnerType,[]] , | |
90 | surround_type(NewInner,OutterType,Temp) , | |
91 | gen_type(Temp,ast,NType). | |
92 | generate_id_or_ast_aux(Type,InnerType,_,NType) :- | |
93 | is_list(InnerType) , | |
94 | gen_type(Type,ast,NType). | |
95 | ||
96 | generate_id_or_ast_aux2(_,InnerType,OutterType,NType) :- | |
97 | is_list(InnerType) , | |
98 | length(OutterType,L) , | |
99 | nth1(L,OutterType,NewInner,NewOutter) , | |
100 | surround_type(NewInner,NewOutter,NType) . | |
101 | generate_id_or_ast_aux2(_,InnerType,OutterType,NType) :- | |
102 | compound(InnerType) , | |
103 | % split inner to get rid of options | |
104 | InnerType =.. [NewInner|_] , | |
105 | surround_type(NewInner,OutterType,NType). | |
106 | generate_id_or_ast_aux2(Type,InnerType,_,Type) :- | |
107 | \+is_list(InnerType). | |
108 | ||
109 | % don't shrink identifier | |
110 | shrink(prob_ast_identifier(_),Value,Value). |