3 #include "mutableset.h"
10 #include "satencoder.h"
11 #include "sattranslator.h"
13 #include "polarityassignment.h"
14 #include "orderdecompose.h"
16 CSolver::CSolver() : unsat(false) {
18 satEncoder = allocSATEncoder(this);
21 /** This function tears down the solver and the entire AST */
24 uint size = allBooleans.getSize();
25 for (uint i = 0; i < size; i++) {
26 delete allBooleans.get(i);
29 size = allSets.getSize();
30 for (uint i = 0; i < size; i++) {
31 delete allSets.get(i);
34 size = allElements.getSize();
35 for (uint i = 0; i < size; i++) {
36 delete allElements.get(i);
39 size = allTables.getSize();
40 for (uint i = 0; i < size; i++) {
41 delete allTables.get(i);
44 size = allPredicates.getSize();
45 for (uint i = 0; i < size; i++) {
46 delete allPredicates.get(i);
49 size = allOrders.getSize();
50 for (uint i = 0; i < size; i++) {
51 delete allOrders.get(i);
54 size = allFunctions.getSize();
55 for (uint i = 0; i < size; i++) {
56 delete allFunctions.get(i);
59 deleteSATEncoder(satEncoder);
63 CSolver * CSolver::clone() {
64 CSolver * copy=new CSolver();
66 HSIteratorBoolean * it=getConstraints();
67 while(it->hasNext()) {
68 Boolean *b=it->next();
75 Set *CSolver::createSet(VarType type, uint64_t *elements, uint numelements) {
76 Set *set = new Set(type, elements, numelements);
81 Set *CSolver::createRangeSet(VarType type, uint64_t lowrange, uint64_t highrange) {
82 Set *set = new Set(type, lowrange, highrange);
87 MutableSet *CSolver::createMutableSet(VarType type) {
88 MutableSet *set = new MutableSet(type);
93 void CSolver::addItem(MutableSet *set, uint64_t element) {
94 set->addElementMSet(element);
97 uint64_t CSolver::createUniqueItem(MutableSet *set) {
98 uint64_t element = set->low++;
99 set->addElementMSet(element);
103 Element *CSolver::getElementVar(Set *set) {
104 Element *element = new ElementSet(set);
105 allElements.push(element);
109 Element *CSolver::getElementConst(VarType type, uint64_t value) {
110 uint64_t array[] = {value};
111 Set * set = new Set(type, array, 1);
113 Element *element = new ElementConst(value, type, set);
114 allElements.push(element);
118 Boolean *CSolver::getBooleanVar(VarType type) {
119 Boolean *boolean = new BooleanVar(type);
120 allBooleans.push(boolean);
124 Function *CSolver::createFunctionOperator(ArithOp op, Set **domain, uint numDomain, Set *range,OverFlowBehavior overflowbehavior) {
125 Function *function = new FunctionOperator(op, domain, numDomain, range, overflowbehavior);
126 allFunctions.push(function);
130 Predicate *CSolver::createPredicateOperator(CompOp op, Set **domain, uint numDomain) {
131 Predicate *predicate = new PredicateOperator(op, domain,numDomain);
132 allPredicates.push(predicate);
136 Predicate *CSolver::createPredicateTable(Table *table, UndefinedBehavior behavior) {
137 Predicate *predicate = new PredicateTable(table, behavior);
138 allPredicates.push(predicate);
142 Table *CSolver::createTable(Set **domains, uint numDomain, Set *range) {
143 Table *table = new Table(domains,numDomain,range);
144 allTables.push(table);
148 Table *CSolver::createTableForPredicate(Set **domains, uint numDomain) {
149 return createTable(domains, numDomain, NULL);
152 void CSolver::addTableEntry(Table *table, uint64_t *inputs, uint inputSize, uint64_t result) {
153 table->addNewTableEntry(inputs, inputSize, result);
156 Function *CSolver::completeTable(Table *table, UndefinedBehavior behavior) {
157 Function *function = new FunctionTable(table, behavior);
158 allFunctions.push(function);
162 Element *CSolver::applyFunction(Function *function, Element **array, uint numArrays, Boolean *overflowstatus) {
163 Element *element = new ElementFunction(function,array,numArrays,overflowstatus);
164 allElements.push(element);
168 Boolean *CSolver::applyPredicate(Predicate *predicate, Element **inputs, uint numInputs) {
169 return applyPredicateTable(predicate, inputs, numInputs, NULL);
172 Boolean *CSolver::applyPredicateTable(Predicate *predicate, Element **inputs, uint numInputs, Boolean *undefinedStatus) {
173 BooleanPredicate *boolean = new BooleanPredicate(predicate, inputs, numInputs, undefinedStatus);
174 allBooleans.push(boolean);
178 Boolean *CSolver::applyLogicalOperation(LogicOp op, Boolean **array, uint asize) {
179 Boolean *boolean = new BooleanLogic(this, op, array, asize);
180 allBooleans.push(boolean);
184 void CSolver::addConstraint(Boolean *constraint) {
185 constraints.add(constraint);
188 Order *CSolver::createOrder(OrderType type, Set *set) {
189 Order *order = new Order(type, set);
190 allOrders.push(order);
194 Boolean *CSolver::orderConstraint(Order *order, uint64_t first, uint64_t second) {
195 Boolean *constraint = new BooleanOrder(order, first, second);
196 allBooleans.push(constraint);
200 int CSolver::startEncoding() {
201 computePolarities(this);
203 naiveEncodingDecision(this);
204 encodeAllSATEncoder(this, satEncoder);
205 int result = solveCNF(satEncoder->cnf);
206 model_print("sat_solver's result:%d\tsolutionSize=%d\n", result, satEncoder->cnf->solver->solutionsize);
207 for (int i = 1; i <= satEncoder->cnf->solver->solutionsize; i++) {
208 model_print("%d, ", satEncoder->cnf->solver->solution[i]);
214 uint64_t CSolver::getElementValue(Element *element) {
215 switch (GETELEMENTTYPE(element)) {
219 return getElementValueSATTranslator(this, element);
226 bool CSolver::getBooleanValue(Boolean *boolean) {
227 switch (GETBOOLEANTYPE(boolean)) {
229 return getBooleanVariableValueSATTranslator(this, boolean);
236 HappenedBefore CSolver::getOrderConstraintValue(Order *order, uint64_t first, uint64_t second) {
237 return getOrderConstraintValueSATTranslator(this, order, first, second);