Add Const API to frontend
[satune.git] / src / csolver.c
1 #include "csolver.h"
2 #include "set.h"
3 #include "mutableset.h"
4 #include "element.h"
5 #include "boolean.h"
6 #include "predicate.h"
7 #include "order.h"
8 #include "table.h"
9 #include "function.h"
10 #include "satencoder.h"
11 #include "sattranslator.h"
12
13 CSolver * allocCSolver() {
14         CSolver * This=(CSolver *) ourmalloc(sizeof(CSolver));
15         This->constraints=allocDefVectorBoolean();
16         This->allBooleans=allocDefVectorBoolean();
17         This->allSets=allocDefVectorSet();
18         This->allElements=allocDefVectorElement();
19         This->allPredicates = allocDefVectorPredicate();
20         This->allTables = allocDefVectorTable();
21         This->allOrders = allocDefVectorOrder();
22         This->allFunctions = allocDefVectorFunction();
23         This->satEncoder = allocSATEncoder();
24         return This;
25 }
26
27 /** This function tears down the solver and the entire AST */
28
29 void deleteSolver(CSolver *This) {
30         deleteVectorBoolean(This->constraints);
31
32         uint size=getSizeVectorBoolean(This->allBooleans);
33         for(uint i=0;i<size;i++) {
34                 deleteBoolean(getVectorBoolean(This->allBooleans, i));
35         }
36         deleteVectorBoolean(This->allBooleans);
37
38         size=getSizeVectorSet(This->allSets);
39         for(uint i=0;i<size;i++) {
40                 deleteSet(getVectorSet(This->allSets, i));
41         }
42         deleteVectorSet(This->allSets);
43
44         size=getSizeVectorElement(This->allElements);
45         for(uint i=0;i<size;i++) {
46                 deleteElement(getVectorElement(This->allElements, i));
47         }
48         deleteVectorElement(This->allElements);
49
50         size=getSizeVectorTable(This->allTables);
51         for(uint i=0;i<size;i++) {
52                 deleteTable(getVectorTable(This->allTables, i));
53         }
54         deleteVectorTable(This->allTables);
55
56         size=getSizeVectorPredicate(This->allPredicates);
57         for(uint i=0;i<size;i++) {
58                 deletePredicate(getVectorPredicate(This->allPredicates, i));
59         }
60         deleteVectorPredicate(This->allPredicates);
61
62         size=getSizeVectorOrder(This->allOrders);
63         for(uint i=0;i<size;i++) {
64                 deleteOrder(getVectorOrder(This->allOrders, i));
65         }
66         deleteVectorOrder(This->allOrders);
67
68         size=getSizeVectorFunction(This->allFunctions);
69         for(uint i=0;i<size;i++) {
70                 deleteFunction(getVectorFunction(This->allFunctions, i));
71         }
72         deleteVectorFunction(This->allFunctions);
73         deleteSATEncoder(This->satEncoder);
74         ourfree(This);
75 }
76
77 Set * createSet(CSolver * This, VarType type, uint64_t * elements, uint numelements) {
78         Set * set=allocSet(type, elements, numelements);
79         pushVectorSet(This->allSets, set);
80         return set;
81 }
82
83 Set * createRangeSet(CSolver * This, VarType type, uint64_t lowrange, uint64_t highrange) {
84         Set * set=allocSetRange(type, lowrange, highrange);
85         pushVectorSet(This->allSets, set);
86         return set;
87 }
88
89 MutableSet * createMutableSet(CSolver * This, VarType type) {
90         MutableSet * set=allocMutableSet(type);
91         pushVectorSet(This->allSets, set);
92         return set;
93 }
94
95 void addItem(CSolver *This, MutableSet * set, uint64_t element) {
96         addElementMSet(set, element);
97 }
98
99 uint64_t createUniqueItem(CSolver *This, MutableSet * set) {
100         uint64_t element=set->low++;
101         addElementMSet(set, element);
102         return element;
103 }
104
105 Element * getElementVar(CSolver *This, Set * set) {
106         Element * element=allocElementSet(set);
107         pushVectorElement(This->allElements, element);
108         return element;
109 }
110
111 Element * getElementConst(CSolver *This, VarType type, uint64_t value) {
112         Element * element=allocElementConst(value, type);
113         pushVectorElement(This->allElements, element);
114         return element;
115 }
116
117 Boolean * getBooleanVar(CSolver *This, VarType type) {
118         Boolean* boolean= allocBooleanVar(type);
119         pushVectorBoolean(This->allBooleans, boolean);
120         return boolean;
121 }
122
123 Function * createFunctionOperator(CSolver *This, ArithOp op, Set ** domain, uint numDomain, Set * range,OverFlowBehavior overflowbehavior) {
124         Function* function = allocFunctionOperator(op, domain, numDomain, range, overflowbehavior);
125         pushVectorFunction(This->allFunctions, function);
126         return function;
127 }
128
129 Predicate * createPredicateOperator(CSolver *This, CompOp op, Set ** domain, uint numDomain) {
130         Predicate* predicate= allocPredicateOperator(op, domain,numDomain);
131         pushVectorPredicate(This->allPredicates, predicate);
132         return predicate;
133 }
134
135 Table * createTable(CSolver *This, Set **domains, uint numDomain, Set * range) {
136         Table* table= allocTable(domains,numDomain,range);
137         pushVectorTable(This->allTables, table);
138         return table;
139 }
140
141 void addTableEntry(CSolver *This, Table* table, uint64_t* inputs, uint inputSize, uint64_t result) {
142         addNewTableEntry(table,inputs, inputSize,result);
143 }
144
145 Function * completeTable(CSolver *This, Table * table) {
146         Function* function = allocFunctionTable(table);
147         pushVectorFunction(This->allFunctions,function);
148         return function;
149 }
150
151 Element * applyFunction(CSolver *This, Function * function, Element ** array, uint numArrays, Boolean * overflowstatus) {
152         Element* element= allocElementFunction(function,array,numArrays,overflowstatus);
153         pushVectorElement(This->allElements, element);
154         return element;
155 }
156
157 Boolean * applyPredicate(CSolver *This, Predicate * predicate, Element ** inputs, uint numInputs) {
158         Boolean* boolean= allocBooleanPredicate(predicate, inputs, numInputs);
159         pushVectorBoolean(This->allBooleans, boolean);
160         return boolean;
161 }
162
163 Boolean * applyLogicalOperation(CSolver *This, LogicOp op, Boolean ** array, uint asize) {
164         return allocBooleanLogicArray(This, op, array, asize);
165 }
166
167 void addConstraint(CSolver *This, Boolean * constraint) {
168         pushVectorBoolean(This->constraints, constraint);
169 }
170
171 Order * createOrder(CSolver *This, OrderType type, Set * set) {
172         Order* order = allocOrder(type, set);
173         pushVectorOrder(This->allOrders, order);
174         return order;
175 }
176
177 Boolean * orderConstraint(CSolver *This, Order * order, uint64_t first, uint64_t second) {
178         Boolean* constraint = allocBooleanOrder(order, first, second);
179         pushVectorBoolean(This->allBooleans,constraint);
180         return constraint;
181 }
182
183 int startEncoding(CSolver* This){
184         naiveEncodingDecision(This);
185         SATEncoder* satEncoder = This->satEncoder;
186         encodeAllSATEncoder(This, satEncoder);
187         int result= solveCNF(satEncoder->cnf);
188         model_print("sat_solver's result:%d\tsolutionSize=%d\n", result, satEncoder->cnf->solver->solutionsize);
189         for(uint i=1; i<=satEncoder->cnf->solver->solutionsize; i++){
190                 model_print("%d, ", satEncoder->cnf->solver->solution[i]);
191         }
192         model_print("\n");
193         return result;
194 }
195
196 uint64_t getElementValue(CSolver* This, Element* element){
197         switch(GETELEMENTTYPE(element)){
198                 case ELEMSET:
199                         return getElementValueSATTranslator(This, element);
200                 default:
201                         ASSERT(0);
202         }
203         exit(-1);
204 }
205
206 bool getBooleanValue( CSolver* This , Boolean* boolean){
207         switch(GETBOOLEANTYPE(boolean)){
208                 case BOOLEANVAR:
209                         return getBooleanVariableValueSATTranslator(This, boolean);
210                 default:
211                         ASSERT(0);
212         }
213         exit(-1);
214 }