3 #include "mutableset.h"
10 #include "satencoder.h"
11 #include "sattranslator.h"
13 #include "orderencoder.h"
14 #include "polarityassignment.h"
16 CSolver::CSolver() : unsat(false) {
17 constraints = allocDefHashSetBoolean();
19 satEncoder = allocSATEncoder(this);
22 /** This function tears down the solver and the entire AST */
25 deleteHashSetBoolean(constraints);
27 uint size = allBooleans.getSize();
28 for (uint i = 0; i < size; i++) {
29 delete allBooleans.get(i);
32 size = allSets.getSize();
33 for (uint i = 0; i < size; i++) {
34 delete allSets.get(i);
37 size = allElements.getSize();
38 for (uint i = 0; i < size; i++) {
39 delete allElements.get(i);
42 size = allTables.getSize();
43 for (uint i = 0; i < size; i++) {
44 delete allTables.get(i);
47 size = allPredicates.getSize();
48 for (uint i = 0; i < size; i++) {
49 delete allPredicates.get(i);
52 size = allOrders.getSize();
53 for (uint i = 0; i < size; i++) {
54 delete allOrders.get(i);
57 size = allFunctions.getSize();
58 for (uint i = 0; i < size; i++) {
59 delete allFunctions.get(i);
62 deleteSATEncoder(satEncoder);
66 Set *CSolver::createSet(VarType type, uint64_t *elements, uint numelements) {
67 Set *set = new Set(type, elements, numelements);
72 Set *CSolver::createRangeSet(VarType type, uint64_t lowrange, uint64_t highrange) {
73 Set *set = new Set(type, lowrange, highrange);
78 MutableSet *CSolver::createMutableSet(VarType type) {
79 MutableSet *set = new MutableSet(type);
84 void CSolver::addItem(MutableSet *set, uint64_t element) {
85 set->addElementMSet(element);
88 uint64_t CSolver::createUniqueItem(MutableSet *set) {
89 uint64_t element = set->low++;
90 set->addElementMSet(element);
94 Element *CSolver::getElementVar(Set *set) {
95 Element *element = new ElementSet(set);
96 allElements.push(element);
100 Element *CSolver::getElementConst(VarType type, uint64_t value) {
101 Element *element = new ElementConst(value, type);
102 allElements.push(element);
106 Boolean *CSolver::getBooleanVar(VarType type) {
107 Boolean *boolean = new BooleanVar(type);
108 allBooleans.push(boolean);
112 Function *CSolver::createFunctionOperator(ArithOp op, Set **domain, uint numDomain, Set *range,OverFlowBehavior overflowbehavior) {
113 Function *function = new FunctionOperator(op, domain, numDomain, range, overflowbehavior);
114 allFunctions.push(function);
118 Predicate *CSolver::createPredicateOperator(CompOp op, Set **domain, uint numDomain) {
119 Predicate *predicate = new PredicateOperator(op, domain,numDomain);
120 allPredicates.push(predicate);
124 Predicate *CSolver::createPredicateTable(Table *table, UndefinedBehavior behavior) {
125 Predicate *predicate = new PredicateTable(table, behavior);
126 allPredicates.push(predicate);
130 Table *CSolver::createTable(Set **domains, uint numDomain, Set *range) {
131 Table *table = new Table(domains,numDomain,range);
132 allTables.push(table);
136 Table *CSolver::createTableForPredicate(Set **domains, uint numDomain) {
137 return createTable(domains, numDomain, NULL);
140 void CSolver::addTableEntry(Table *table, uint64_t *inputs, uint inputSize, uint64_t result) {
141 table->addNewTableEntry(inputs, inputSize, result);
144 Function *CSolver::completeTable(Table *table, UndefinedBehavior behavior) {
145 Function *function = new FunctionTable(table, behavior);
146 allFunctions.push(function);
150 Element *CSolver::applyFunction(Function *function, Element **array, uint numArrays, Boolean *overflowstatus) {
151 Element *element = new ElementFunction(function,array,numArrays,overflowstatus);
152 allElements.push(element);
156 Boolean *CSolver::applyPredicate(Predicate *predicate, Element **inputs, uint numInputs) {
157 return applyPredicateTable(predicate, inputs, numInputs, NULL);
160 Boolean *CSolver::applyPredicateTable(Predicate *predicate, Element **inputs, uint numInputs, Boolean *undefinedStatus) {
161 Boolean *boolean = new BooleanPredicate(predicate, inputs, numInputs, undefinedStatus);
162 allBooleans.push(boolean);
166 Boolean *CSolver::applyLogicalOperation(LogicOp op, Boolean **array, uint asize) {
167 return new BooleanLogic(this, op, array, asize);
170 void CSolver::addConstraint(Boolean *constraint) {
171 addHashSetBoolean(constraints, constraint);
174 Order *CSolver::createOrder(OrderType type, Set *set) {
175 Order *order = new Order(type, set);
176 allOrders.push(order);
180 Boolean *CSolver::orderConstraint(Order *order, uint64_t first, uint64_t second) {
181 Boolean *constraint = new BooleanOrder(order, first, second);
182 allBooleans.push(constraint);
186 int CSolver::startEncoding() {
187 naiveEncodingDecision(this);
188 computePolarities(this);
190 encodeAllSATEncoder(this, satEncoder);
191 int result = solveCNF(satEncoder->cnf);
192 model_print("sat_solver's result:%d\tsolutionSize=%d\n", result, satEncoder->cnf->solver->solutionsize);
193 for (int i = 1; i <= satEncoder->cnf->solver->solutionsize; i++) {
194 model_print("%d, ", satEncoder->cnf->solver->solution[i]);
200 uint64_t CSolver::getElementValue(Element *element) {
201 switch (GETELEMENTTYPE(element)) {
205 return getElementValueSATTranslator(this, element);
212 bool CSolver::getBooleanValue(Boolean *boolean) {
213 switch (GETBOOLEANTYPE(boolean)) {
215 return getBooleanVariableValueSATTranslator(this, boolean);
222 HappenedBefore CSolver::getOrderConstraintValue(Order *order, uint64_t first, uint64_t second) {
223 return getOrderConstraintValueSATTranslator(this, order, first, second);