1 | :- multifile generate/2. | |
2 | :- multifile shrink/3. | |
3 | ||
4 | generate(prob_ast_sat(Options),ResultPred) :- | |
5 | generate(prob_ast_pred(Options),Pred) , | |
6 | get_id_amount_from_options(Options,AmountOfIDs) , | |
7 | adapt_id_amount(AmountOfIDs,Amount) , | |
8 | set_wd_option(Options, WD), | |
9 | insert_identifier_to_ast(WD,Pred,Amount,NewPred) , % defined in 'snippets.pl' | |
10 | set_identifier_constraints(NewPred,Options,ConstrainedPred) , | |
11 | extend_predicate_if_necessary(ConstrainedPred,AmountOfIDs,Options,ResultPred). | |
12 | ||
13 | set_identifier_constraints(Pred,Options,b(conjunct(Pred,Constraints),pred,[])) :- | |
14 | bsyntaxtree:find_typed_identifier_uses(Pred,[],ListOfIdentifier) , | |
15 | generate(prob_ast_id_constraints(ListOfIdentifier,Options),Constraints). | |
16 | ||
17 | get_id_amount_from_options(Options,AmountOfIDs) :- | |
18 | get_min_max_and_type(Options,(Min,Max),interval) , ! , | |
19 | NMax is Max + 1 , | |
20 | random(Min,NMax,AmountOfIDs). | |
21 | get_id_amount_from_options(Options,AmountOfIDs) :- | |
22 | get_min_max_and_type(Options,(Min,_),min) , ! , | |
23 | random(0,10,R) , | |
24 | AmountOfIDs is Min + R. | |
25 | get_id_amount_from_options(Options,AmountOfIDs) :- | |
26 | get_min_max_and_type(Options,(_,Max),max) , ! , | |
27 | random(0,Max,AmountOfIDs). | |
28 | get_id_amount_from_options(_,AmountOfIDs) :- | |
29 | random(0,10,AmountOfIDs). | |
30 | ||
31 | order_min_max(Min,Max,Min,Max) :- | |
32 | Min =< Max. | |
33 | order_min_max(Min,Max,Max,Min) :- | |
34 | Min > Max. | |
35 | ||
36 | get_min_max_and_type(Options,(NMin,NMax),interval) :- | |
37 | subset([minID:Min,maxID:Max],Options) , | |
38 | order_min_max(Min,Max,NMin,NMax). | |
39 | get_min_max_and_type(Options,(Min,_),min) :- | |
40 | member(minID:Min,Options). | |
41 | get_min_max_and_type(Options,(_,Max),max) :- | |
42 | member(maxID:Max,Options). | |
43 | ||
44 | % generate new predicate to conjunct with the current one if the minimum | |
45 | % amount of identifiers is not reached | |
46 | extend_predicate_if_necessary(Pred,AmountOfIDs,Options,ResultPred) :- | |
47 | bsyntaxtree:find_identifier_uses(Pred,[],IDs) , | |
48 | length(IDs,CurAmountOfIDs) , | |
49 | CurAmountOfIDs < AmountOfIDs , ! , | |
50 | MissingAmount is AmountOfIDs - CurAmountOfIDs , | |
51 | extend_predicate_if_necessary_aux(Pred,AmountOfIDs,MissingAmount,Options,ResultPred). | |
52 | extend_predicate_if_necessary(Pred,_,_,Pred). | |
53 | ||
54 | extend_predicate_if_necessary_aux(Pred,_,0,_,Pred). | |
55 | extend_predicate_if_necessary_aux(Pred,AmountOfIDs,MissingAmount,Options,ResultPred) :- | |
56 | generate(prob_ast_pred(Options),NewPred) , | |
57 | adapt_id_amount(MissingAmount,Amount) , | |
58 | set_wd_option(Options, WD), | |
59 | insert_identifier_to_ast(WD,NewPred,Amount,NewPredWithIDs) , ! , | |
60 | extend_predicate_if_necessary(b(conjunct(Pred,NewPredWithIDs),pred,[]),AmountOfIDs,Options,ResultPred). | |
61 | ||
62 | adapt_id_amount(Amount,2) :- | |
63 | Amount > 2. | |
64 | adapt_id_amount(Amount,Amount). | |
65 | ||
66 | shrink(Type,Value,Value) :- | |
67 | Type =.. [prob_ast_sat|_]. |