Edits to merge
[satune.git] / src / AST / boolean.cc
index 3e05452019f17d6a576e68a610889ed2b81f4187..fff3658abb16bc404bd0d40f8c3f9c6db5d593aa 100644 (file)
@@ -3,6 +3,7 @@
 #include "csolver.h"
 #include "element.h"
 #include "order.h"
+#include "predicate.h"
 
 Boolean::Boolean(ASTNodeType _type) :
        ASTNode(_type),
@@ -11,6 +12,11 @@ Boolean::Boolean(ASTNodeType _type) :
        parents() {
 }
 
+BooleanConst::BooleanConst(bool _isTrue) :
+       Boolean(BOOLCONST),
+       istrue(_isTrue) {
+}
+
 BooleanVar::BooleanVar(VarType t) :
        Boolean(BOOLEANVAR),
        vtype(t),
@@ -25,24 +31,181 @@ BooleanOrder::BooleanOrder(Order *_order, uint64_t _first, uint64_t _second) :
        order->constraints.push(this);
 }
 
-BooleanPredicate::BooleanPredicate(Predicate *_predicate, Element **_inputs, uint _numInputs, Boolean *_undefinedStatus) :
+BooleanPredicate::BooleanPredicate(Predicate *_predicate, Element **_inputs, uint _numInputs, BooleanEdge _undefinedStatus) :
        Boolean(PREDICATEOP),
        predicate(_predicate),
+       encoding(this),
        inputs(_inputs, _numInputs),
        undefStatus(_undefinedStatus) {
-       for (uint i = 0; i < _numInputs; i++) {
-               GETELEMENTPARENTS(_inputs[i])->push(this);
-       }
-       initPredicateEncoding(&encoding, this);
 }
 
-BooleanLogic::BooleanLogic(CSolver *solver, LogicOp _op, Boolean **array, uint asize) :
+BooleanLogic::BooleanLogic(CSolver *solver, LogicOp _op, BooleanEdge *array, uint asize) :
        Boolean(LOGICOP),
        op(_op),
+       replaced(false),
        inputs(array, asize) {
-       solver->allBooleans.push(this);
 }
 
-BooleanPredicate::~BooleanPredicate() {
-       deleteFunctionEncoding(&encoding);
+BooleanEdge cloneEdge(CSolver *solver, CloneMap *map, BooleanEdge e) {
+       bool isnegated=e.isNegated();
+       Boolean *b=e->clone(solver, map);
+       BooleanEdge be=BooleanEdge(b);
+       return isnegated ? be.negate() : be;
+}
+
+Boolean *BooleanConst::clone(CSolver *solver, CloneMap *map) {
+       return solver->getBooleanTrue().getRaw();
+}
+
+Boolean *BooleanVar::clone(CSolver *solver, CloneMap *map) {
+       Boolean *b = (Boolean *) map->get(this);
+       if (b != NULL)
+               return b;
+       BooleanEdge bvar = solver->getBooleanVar(type);
+       Boolean * base=bvar.getRaw();
+       map->put(this, base);
+       return base;
+}
+
+Boolean *BooleanOrder::clone(CSolver *solver, CloneMap *map) {
+       Order *ordercopy = order->clone(solver, map);
+       return solver->orderConstraint(ordercopy, first, second).getRaw();
+}
+
+Boolean *BooleanLogic::clone(CSolver *solver, CloneMap *map) {
+       BooleanEdge array[inputs.getSize()];
+       for (uint i = 0; i < inputs.getSize(); i++) {
+               array[i] = cloneEdge(solver, map, inputs.get(i));
+       }
+       return solver->applyLogicalOperation(op, array, inputs.getSize()).getRaw();
 }
+
+Boolean *BooleanPredicate::clone(CSolver *solver, CloneMap *map) {
+       Element *array[inputs.getSize()];
+       for (uint i = 0; i < inputs.getSize(); i++) {
+               array[i] = inputs.get(i)->clone(solver, map);
+       }
+       Predicate *pred = predicate->clone(solver, map);
+       BooleanEdge defstatus = undefStatus ? cloneEdge(solver, map, undefStatus) : BooleanEdge();
+
+       return solver->applyPredicateTable(pred, array, inputs.getSize(), defstatus).getRaw();
+}
+
+void BooleanPredicate::updateParents() {
+       for(uint i=0;i < inputs.getSize(); i++) inputs.get(i)->parents.push(this);
+}
+
+void BooleanLogic::updateParents() {
+       for(uint i=0;i < inputs.getSize(); i++) inputs.get(i)->parents.push(this);
+}
+
+void BooleanVar::serialize(Serializer* serializer){
+       if(serializer->isSerialized(this))
+               return;
+       serializer->addObject(this);
+       serializer->mywrite(&type, sizeof(ASTNodeType));
+       BooleanVar* This = this;
+       serializer->mywrite(&This, sizeof(BooleanVar*));
+       serializer->mywrite(&vtype, sizeof(VarType));
+}
+
+void BooleanVar::print(){
+       model_print("BooleanVar:%lu\n", (uintptr_t)this);
+}
+
+void BooleanConst::print(){
+       model_print("BooleanConst:%s\n", istrue?"TRUE" :"FALSE");
+}
+
+void BooleanOrder::serialize(Serializer* serializer){
+       if(serializer->isSerialized(this))
+               return;
+       serializer->addObject(this);
+       order->serialize(serializer);
+       
+       serializer->mywrite(&type, sizeof(ASTNodeType));
+       BooleanOrder* This = this;
+       serializer->mywrite(&This, sizeof(BooleanOrder*));
+       serializer->mywrite(&order, sizeof(Order*));
+       serializer->mywrite(&first, sizeof(uint64_t));
+       serializer->mywrite(&second, sizeof(uint64_t));
+}
+
+void BooleanOrder::print(){
+       model_print("{BooleanOrder: First= %lu, Second = %lu on Order:\n", first, second);
+       order->print();
+       model_print("}\n");
+}
+
+void BooleanPredicate::serialize(Serializer* serializer){
+       if(serializer->isSerialized(this))
+               return;
+       serializer->addObject(this);
+       
+       predicate->serialize(serializer);
+       uint size = inputs.getSize();
+       for(uint i=0; i<size; i++){
+               Element* input = inputs.get(i);
+               input->serialize(serializer);
+       }
+       serializeBooleanEdge(serializer, undefStatus);
+       
+       serializer->mywrite(&type, sizeof(ASTNodeType));
+       BooleanPredicate* This = this;
+       serializer->mywrite(&This, sizeof(BooleanPredicate*));
+       serializer->mywrite(&predicate, sizeof(Predicate *));
+       serializer->mywrite(&size, sizeof(uint));
+       for(uint i=0; i<size; i++){
+               Element *input = inputs.get(i);
+               serializer->mywrite(&input, sizeof(Element *));
+       }
+       Boolean* undefStat = undefStatus!= BooleanEdge(NULL)?undefStatus.getRaw() : NULL;
+       serializer->mywrite(&undefStat, sizeof(Boolean*));
+}
+
+void BooleanPredicate::print(){
+       model_print("{BooleanPredicate:\n");
+       predicate->print();
+       model_print("elements:\n");
+       uint size = inputs.getSize();
+       for(uint i=0; i<size; i++){
+               Element *input = inputs.get(i);
+               input->print();
+       }
+       model_print("}\n");
+}
+
+void BooleanLogic::serialize(Serializer* serializer){
+       if(serializer->isSerialized(this))
+               return;
+       serializer->addObject(this);
+       uint size = inputs.getSize();
+       for(uint i=0; i<size; i++){
+               BooleanEdge input = inputs.get(i);
+               serializeBooleanEdge(serializer, input);
+       }
+       serializer->mywrite(&type, sizeof(ASTNodeType));
+       BooleanLogic* This = this;
+       serializer->mywrite(&This, sizeof(BooleanLogic*));
+       serializer->mywrite(&op, sizeof(LogicOp));
+       serializer->mywrite(&size, sizeof(uint));
+       for(uint i=0; i<size; i++){
+               Boolean* input = inputs.get(i).getRaw();
+               serializer->mywrite(&input, sizeof(Boolean*));
+       }
+}
+
+void BooleanLogic::print(){
+       model_print("{BooleanLogic: %s\n", 
+                                                       op ==SATC_AND? "AND": op == SATC_OR? "OR": op==SATC_NOT? "NOT":
+                                                       op == SATC_XOR? "XOR" : op==SATC_IFF? "IFF" : "IMPLIES");
+       uint size = inputs.getSize();
+       for(uint i=0; i<size; i++){
+               BooleanEdge input = inputs.get(i);
+               if(input.isNegated())
+                       model_print("!");
+               input.getBoolean()->print();
+       }
+       model_print("}\n");
+}
+