Adding Transformer and adding integerencoding to it + More OOP
[satune.git] / src / csolver.h
1 #ifndef CSOLVER_H
2 #define CSOLVER_H
3 #include "classlist.h"
4 #include "ops.h"
5 #include "structs.h"
6 #include "asthash.h"
7
8 class CSolver {
9 public:
10         CSolver();
11         ~CSolver();
12
13         /** This function creates a set containing the elements passed in the array. */
14         Set *createSet(VarType type, uint64_t *elements, uint num);
15
16         /** This function creates a set from lowrange to highrange (inclusive). */
17
18         Set *createRangeSet(VarType type, uint64_t lowrange, uint64_t highrange);
19
20         /** This function creates a mutable set. */
21
22         MutableSet *createMutableSet(VarType type);
23
24         /** This function adds a new item to a set. */
25
26         void addItem(MutableSet *set, uint64_t element);
27
28         /** This function adds a new unique item to the set and returns it.
29             This function cannot be used in conjunction with manually adding
30             items to the set. */
31
32         uint64_t createUniqueItem(MutableSet *set);
33
34         /** This function creates an element variable over a set. */
35
36         Element *getElementVar(Set *set);
37
38         /** This function creates an element constrant. */
39         Element *getElementConst(VarType type, uint64_t value);
40
41         /** This function creates a boolean variable. */
42
43         Boolean *getBooleanVar(VarType type);
44
45         /** This function creates a function operator. */
46
47         Function *createFunctionOperator(ArithOp op, Set **domain, uint numDomain, Set *range,
48                                                                                                                                          OverFlowBehavior overflowbehavior);
49
50         /** This function creates a predicate operator. */
51
52         Predicate *createPredicateOperator(CompOp op, Set **domain, uint numDomain);
53
54         Predicate *createPredicateTable(Table *table, UndefinedBehavior behavior);
55
56         /** This function creates an empty instance table.*/
57
58         Table *createTable(Set **domains, uint numDomain, Set *range);
59
60         Table *createTableForPredicate(Set **domains, uint numDomain);
61         /** This function adds an input output relation to a table. */
62
63         void addTableEntry(Table *table, uint64_t *inputs, uint inputSize, uint64_t result);
64
65         /** This function converts a completed table into a function. */
66
67         Function *completeTable(Table *, UndefinedBehavior behavior);
68
69         /** This function applies a function to the Elements in its input. */
70
71         Element *applyFunction(Function *function, Element **array, uint numArrays, Boolean *overflowstatus);
72
73         /** This function applies a predicate to the Elements in its input. */
74
75         Boolean *applyPredicateTable(Predicate *predicate, Element **inputs, uint numInputs, Boolean *undefinedStatus);
76
77         Boolean *applyPredicate(Predicate *predicate, Element **inputs, uint numInputs);
78
79         /** This function applies a logical operation to the Booleans in its input. */
80
81         Boolean *applyLogicalOperation(LogicOp op, Boolean **array, uint asize);
82
83         /** This function adds a boolean constraint to the set of constraints
84             to be satisfied */
85
86         void addConstraint(Boolean *constraint);
87
88         /** This function instantiates an order of type type over the set set. */
89         Order *createOrder(OrderType type, Set *set);
90
91         /** This function instantiates a boolean on two items in an order. */
92         Boolean *orderConstraint(Order *order, uint64_t first, uint64_t second);
93
94         /** When everything is done, the client calls this function and then csolver starts to encode*/
95         int startEncoding();
96
97         /** After getting the solution from the SAT solver, client can get the value of an element via this function*/
98         uint64_t getElementValue(Element *element);
99
100         /** After getting the solution from the SAT solver, client can get the value of a boolean via this function*/
101         bool getBooleanValue(Boolean *boolean);
102
103         HappenedBefore getOrderConstraintValue(Order *order, uint64_t first, uint64_t second);
104
105         void setUnSAT() { unsat = true; }
106
107         bool isUnSAT() { return unsat; }
108
109         Vector<Order *> *getOrders() { return &allOrders;}
110
111         Tuner *getTuner() { return tuner; }
112         Transformer* getTransformer() {return transformer;}
113         
114         HSIteratorBoolean *getConstraints() { return constraints.iterator(); }
115
116         SATEncoder *getSATEncoder() {return satEncoder;}
117
118         void replaceBooleanWithTrue(Boolean *bexpr);
119         void replaceBooleanWithFalse(Boolean *bexpr);
120         void replaceBooleanWithBoolean(Boolean *oldb, Boolean *newb);
121         CSolver *clone();
122         void autoTune(uint budget);
123
124         void setTuner(Transformer * _transformer) {  transformer = _transformer; }
125         void setTuner(Tuner * _tuner) { tuner = _tuner; }
126         long long getElapsedTime() { return elapsedTime; }
127         long long getEncodeTime();
128         long long getSolveTime();
129         
130         MEMALLOC;
131
132 private:
133         void handleXORFalse(BooleanLogic *bexpr, Boolean *child);
134         void handleIMPLIESTrue(BooleanLogic *bexpr, Boolean *child);
135         void handleIMPLIESFalse(BooleanLogic *bexpr, Boolean *child);
136         void handleANDTrue(BooleanLogic *bexpr, Boolean *child);
137         void handleORFalse(BooleanLogic *bexpr, Boolean *child);
138
139         /** This is a vector of constraints that must be satisfied. */
140         HashSetBoolean constraints;
141
142         /** This is a vector of all boolean structs that we have allocated. */
143         Vector<Boolean *> allBooleans;
144
145         /** This is a vector of all set structs that we have allocated. */
146         Vector<Set *> allSets;
147
148         /** This is a vector of all element structs that we have allocated. */
149         Vector<Element *> allElements;
150
151         /** This is a vector of all predicate structs that we have allocated. */
152         Vector<Predicate *> allPredicates;
153
154         /** This is a vector of all table structs that we have allocated. */
155         Vector<Table *> allTables;
156
157         /** This is a vector of all order structs that we have allocated. */
158         Vector<Order *> allOrders;
159
160         /** This is a vector of all function structs that we have allocated. */
161         Vector<Function *> allFunctions;
162
163         /** These two tables are used for deduplicating entries. */
164         BooleanMatchMap boolMap;
165         ElementMatchMap elemMap;
166         
167         SATEncoder *satEncoder;
168         bool unsat;
169         Tuner *tuner;
170         Transformer* transformer;
171         long long elapsedTime;
172 };
173 #endif