Edits to merge
[satune.git] / src / AST / boolean.cc
index 256ab5af2b453e4b26d57a5e45d2ee561df779bf..fff3658abb16bc404bd0d40f8c3f9c6db5d593aa 100644 (file)
 #include "csolver.h"
 #include "element.h"
 #include "order.h"
+#include "predicate.h"
 
-Boolean *allocBooleanVar(VarType t) {
-       BooleanVar *This = (BooleanVar *) ourmalloc(sizeof (BooleanVar));
-       GETBOOLEANTYPE(This) = BOOLEANVAR;
-       GETBOOLEANVALUE(This) = BV_UNDEFINED;
-       GETBOOLEANPOLARITY(This) = P_UNDEFINED;
-       This->vtype = t;
-       This->var = E_NULL;
-       initDefVectorBoolean(GETBOOLEANPARENTS(This));
-       return &This->base;
-}
-
-Boolean *allocBooleanOrder(Order *order, uint64_t first, uint64_t second) {
-       BooleanOrder *This = (BooleanOrder *) ourmalloc(sizeof (BooleanOrder));
-       GETBOOLEANTYPE(This) = ORDERCONST;
-       GETBOOLEANVALUE(This) = BV_UNDEFINED;
-       GETBOOLEANPOLARITY(This) = P_UNDEFINED;
-       This->order = order;
-       This->first = first;
-       This->second = second;
-       pushVectorBooleanOrder(&order->constraints, This);
-       initDefVectorBoolean(GETBOOLEANPARENTS(This));
-       return &This->base;
-}
-
-Boolean *allocBooleanPredicate(Predicate *predicate, Element **inputs, uint numInputs, Boolean *undefinedStatus) {
-       BooleanPredicate *This = (BooleanPredicate *) ourmalloc(sizeof(BooleanPredicate));
-       GETBOOLEANTYPE(This) = PREDICATEOP;
-       GETBOOLEANVALUE(This) = BV_UNDEFINED;
-       GETBOOLEANPOLARITY(This) = P_UNDEFINED;
-       This->predicate = predicate;
-       initArrayInitElement(&This->inputs, inputs, numInputs);
-       initDefVectorBoolean(GETBOOLEANPARENTS(This));
-
-       for (uint i = 0; i < numInputs; i++) {
-               pushVectorASTNode(GETELEMENTPARENTS(inputs[i]), (ASTNode *)This);
+Boolean::Boolean(ASTNodeType _type) :
+       ASTNode(_type),
+       polarity(P_UNDEFINED),
+       boolVal(BV_UNDEFINED),
+       parents() {
+}
+
+BooleanConst::BooleanConst(bool _isTrue) :
+       Boolean(BOOLCONST),
+       istrue(_isTrue) {
+}
+
+BooleanVar::BooleanVar(VarType t) :
+       Boolean(BOOLEANVAR),
+       vtype(t),
+       var(E_NULL) {
+}
+
+BooleanOrder::BooleanOrder(Order *_order, uint64_t _first, uint64_t _second) :
+       Boolean(ORDERCONST),
+       order(_order),
+       first(_first),
+       second(_second) {
+       order->constraints.push(this);
+}
+
+BooleanPredicate::BooleanPredicate(Predicate *_predicate, Element **_inputs, uint _numInputs, BooleanEdge _undefinedStatus) :
+       Boolean(PREDICATEOP),
+       predicate(_predicate),
+       encoding(this),
+       inputs(_inputs, _numInputs),
+       undefStatus(_undefinedStatus) {
+}
+
+BooleanLogic::BooleanLogic(CSolver *solver, LogicOp _op, BooleanEdge *array, uint asize) :
+       Boolean(LOGICOP),
+       op(_op),
+       replaced(false),
+       inputs(array, asize) {
+}
+
+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();
        }
-       initPredicateEncoding(&This->encoding, (Boolean *) This);
-       This->undefStatus = undefinedStatus;
-       return &This->base;
-}
-
-Boolean *allocBooleanLogicArray(CSolver *solver, LogicOp op, Boolean **array, uint asize) {
-       BooleanLogic *This = (BooleanLogic *) ourmalloc(sizeof(BooleanLogic));
-       GETBOOLEANTYPE(This) = LOGICOP;
-       GETBOOLEANVALUE(This) = BV_UNDEFINED;
-       GETBOOLEANPOLARITY(This) = P_UNDEFINED;
-       This->op = op;
-       initDefVectorBoolean(GETBOOLEANPARENTS(This));
-       initArrayInitBoolean(&This->inputs, array, asize);
-       pushVectorBoolean(solver->allBooleans, (Boolean *) This);
-       return &This->base;
-}
-
-void deleteBoolean(Boolean *This) {
-       switch (GETBOOLEANTYPE(This)) {
-       case PREDICATEOP: {
-               BooleanPredicate *bp = (BooleanPredicate *)This;
-               deleteInlineArrayElement(&bp->inputs );
-               deleteFunctionEncoding(&bp->encoding);
-               break;
+       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);
        }
-       case LOGICOP: {
-               BooleanLogic *bl = (BooleanLogic *) This;
-               deleteInlineArrayBoolean(&bl->inputs);
-               break;
+       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*));
        }
-       default:
-               break;
+}
+
+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();
        }
-       deleteVectorArrayBoolean(GETBOOLEANPARENTS(This));
-       ourfree(This);
+       model_print("}\n");
 }
+