00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #ifndef _CVC_lite__variable_h_
00035 #define _CVC_lite__variable_h_
00036
00037 #include "expr.h"
00038
00039 namespace CVCL {
00040
00041 class VariableManager;
00042 class VariableValue;
00043 class Clause;
00044 class SearchEngineRules;
00045
00046
00047 class Variable {
00048 private:
00049 VariableValue* d_val;
00050
00051 Theorem deriveThmRec(bool checkAssump) const;
00052 public:
00053
00054 Variable(): d_val(NULL) { }
00055
00056
00057 Variable(VariableManager* vm, const Expr& e);
00058
00059 Variable(const Variable& l);
00060
00061 ~Variable();
00062
00063 Variable& operator=(const Variable& l);
00064
00065 bool isNull() const { return d_val == NULL; }
00066
00067
00068
00069
00070
00071 const Expr& getExpr() const;
00072
00073
00074 const Expr& getNegExpr() const;
00075
00076
00077 int getValue() const;
00078
00079
00080 int getScope() const;
00081 const Theorem& getTheorem() const;
00082 const Clause& getAntecedent() const;
00083
00084
00085 int getAntecedentIdx() const;
00086
00087
00088
00089 const Theorem& getAssumpThm() const;
00090
00091
00092
00093 void setValue(int val, const Clause& c, int idx);
00094
00095
00096
00097 void setValue(const Theorem& thm);
00098 void setValue(const Theorem& thm, int scope);
00099
00100 void setAssumpThm(const Theorem& a, int scope);
00101
00102
00103
00104
00105 Theorem deriveTheorem() const;
00106
00107
00108 unsigned& count(bool neg);
00109 unsigned& countPrev(bool neg);
00110 int& score(bool neg);
00111 bool& added(bool neg);
00112
00113 unsigned count(bool neg) const;
00114 unsigned countPrev(bool neg) const;
00115 int score(bool neg) const;
00116 bool added(bool neg) const;
00117
00118 std::vector<std::pair<Clause, int> >& wp(bool neg) const;
00119
00120 friend bool operator==(const Variable& l1, const Variable& l2) {
00121 return l1.d_val == l2.d_val;
00122 }
00123
00124 friend std::ostream& operator<<(std::ostream& os, const Variable& l);
00125 std::string toString() const;
00126 };
00127
00128 class Literal {
00129 private:
00130 Variable d_var;
00131 bool d_negative;
00132 public:
00133
00134 Literal(const Variable& v, bool positive = true)
00135 : d_var(v), d_negative(!positive) { }
00136
00137 Literal(): d_negative(false) { }
00138
00139
00140 Literal(VariableManager* vm, const Expr& e)
00141 : d_var(vm, (e.isNot())? e[0] : e), d_negative(e.isNot()) { }
00142 Variable& getVar() { return d_var; }
00143 const Variable& getVar() const { return d_var; }
00144 bool isPositive() const { return !d_negative; }
00145 bool isNegative() const { return d_negative; }
00146 bool isNull() const { return d_var.isNull(); }
00147
00148 const Expr& getExpr() const {
00149 if(d_negative) return d_var.getNegExpr();
00150 else return d_var.getExpr();
00151 }
00152 int getValue() const {
00153 if(d_negative) return -(d_var.getValue());
00154 else return d_var.getValue();
00155 }
00156 int getScope() const { return getVar().getScope(); }
00157
00158
00159
00160
00161 void setValue(const Theorem& thm) {
00162 d_var.setValue(thm, thm.getScope());
00163 }
00164 void setValue(const Theorem& thm, int scope) {
00165 d_var.setValue(thm, scope);
00166 }
00167 const Theorem& getTheorem() const { return d_var.getTheorem(); }
00168
00169
00170
00171
00172 Theorem deriveTheorem() const { return d_var.deriveTheorem(); }
00173
00174 unsigned& count() { return d_var.count(d_negative); }
00175 unsigned& countPrev() { return d_var.countPrev(d_negative); }
00176 int& score() { return d_var.score(d_negative); }
00177 bool& added() { return d_var.added(d_negative); }
00178
00179 unsigned count() const { return d_var.count(d_negative); }
00180 unsigned countPrev() const { return d_var.countPrev(d_negative); }
00181 int score() const { return d_var.score(d_negative); }
00182 bool added() const { return d_var.added(d_negative); }
00183
00184 std::vector<std::pair<Clause, int> >& wp() const
00185 { return d_var.wp(d_negative); }
00186
00187 friend std::ostream& operator<<(std::ostream& os, const Literal& l);
00188 std::string toString() const;
00189
00190 friend bool operator==(const Literal& l1, const Literal& l2) {
00191 return (l1.d_negative == l2.d_negative && l1.d_var==l1.d_var);
00192 }
00193 };
00194
00195
00196 inline Literal operator!(const Variable& v) {
00197 return Literal(v, false);
00198 }
00199
00200 inline Literal operator!(const Literal& l) {
00201 return Literal(l.getVar(), l.isNegative());
00202 }
00203
00204 std::ostream& operator<<(std::ostream& os, const Literal& l);
00205
00206 }
00207
00208
00209 #include "clause.h"
00210
00211 namespace CVCL {
00212
00213
00214 class VariableValue {
00215 friend class Variable;
00216 friend class VariableManager;
00217 private:
00218 VariableManager* d_vm;
00219 int d_refcount;
00220
00221 Expr d_expr;
00222
00223 Expr d_neg;
00224
00225
00226
00227
00228 unsigned d_count;
00229 unsigned d_countPrev;
00230 int d_score;
00231
00232 unsigned d_negCount;
00233 unsigned d_negCountPrev;
00234 int d_negScore;
00235
00236 bool d_added;
00237 bool d_negAdded;
00238
00239 std::vector<std::pair<Clause, int> > d_wp;
00240 std::vector<std::pair<Clause, int> > d_negwp;
00241
00242
00243
00244
00245 CDO<int>* d_val;
00246 CDO<int>* d_scope;
00247
00248 CDO<Theorem>* d_thm;
00249 CDO<Clause>* d_ante;
00250 CDO<int>* d_anteIdx;
00251 CDO<Theorem>* d_assump;
00252
00253 VariableValue(VariableManager* vm, const Expr& e)
00254 : d_vm(vm), d_refcount(0), d_expr(e),
00255 d_count(0), d_countPrev(0), d_score(0),
00256 d_negCount(0), d_negCountPrev(0), d_negScore(0),
00257 d_added(false), d_negAdded(false),
00258 d_val(NULL), d_scope(NULL), d_thm(NULL),
00259 d_ante(NULL), d_anteIdx(NULL), d_assump(NULL) { }
00260 public:
00261 ~VariableValue();
00262
00263 const Expr& getExpr() const { return d_expr; }
00264
00265 const Expr& getNegExpr() const {
00266 if(d_neg.isNull()) {
00267 const_cast<VariableValue*>(this)->d_neg
00268 = d_expr.negate();
00269 }
00270 return d_neg;
00271 }
00272
00273 int getValue() const {
00274 if(d_val==NULL) return 0;
00275 else return d_val->get();
00276 }
00277
00278 int getScope() const {
00279 if(d_scope==NULL) return 0;
00280 else return d_scope->get();
00281 }
00282
00283 const Theorem& getTheorem() const {
00284 static Theorem null;
00285 if(d_thm==NULL) return null;
00286 else return d_thm->get();
00287 }
00288
00289 const Clause& getAntecedent() const {
00290 static Clause null;
00291 if(d_ante==NULL) return null;
00292 else return d_ante->get();
00293 }
00294
00295 int getAntecedentIdx() const {
00296 if(d_anteIdx==NULL) return 0;
00297 else return d_anteIdx->get();
00298 }
00299
00300 const Theorem& getAssumpThm() const {
00301 static Theorem null;
00302 if(d_assump==NULL) return null;
00303 else return d_assump->get();
00304 }
00305
00306
00307
00308 void setValue(int val, const Clause& c, int idx);
00309
00310
00311 void setValue(const Theorem& thm, int scope);
00312
00313 void setAssumpThm(const Theorem& a, int scope);
00314
00315
00316 unsigned& count(bool neg) {
00317 if(neg) return d_negCount;
00318 else return d_count;
00319 }
00320 unsigned& countPrev(bool neg) {
00321 if(neg) return d_negCountPrev;
00322 else return d_countPrev;
00323 }
00324 int& score(bool neg) {
00325 if(neg) return d_negScore;
00326 else return d_score;
00327 }
00328 bool& added(bool neg) {
00329 if(neg) return d_negAdded;
00330 else return d_added;
00331 }
00332
00333
00334 void* operator new(size_t size, MemoryManager* mm) {
00335 return mm->newData(size);
00336 }
00337 void operator delete(void*) { }
00338
00339
00340 friend std::ostream& operator<<(std::ostream& os, const VariableValue& v);
00341 friend bool operator==(const VariableValue& v1, const VariableValue& v2) {
00342 return v1.d_expr == v2.d_expr;
00343 }
00344 };
00345
00346
00347 inline unsigned& Variable::count(bool neg) { return d_val->count(neg); }
00348 inline unsigned& Variable::countPrev(bool neg)
00349 { return d_val->countPrev(neg); }
00350 inline int& Variable::score(bool neg) { return d_val->score(neg); }
00351 inline bool& Variable::added(bool neg) { return d_val->added(neg); }
00352
00353 inline unsigned Variable::count(bool neg) const { return d_val->count(neg); }
00354 inline unsigned Variable::countPrev(bool neg) const
00355 { return d_val->countPrev(neg); }
00356 inline int Variable::score(bool neg) const { return d_val->score(neg); }
00357 inline bool Variable::added(bool neg) const { return d_val->added(neg); }
00358
00359 inline std::vector<std::pair<Clause, int> >& Variable::wp(bool neg) const {
00360 if(neg) return d_val->d_negwp;
00361 else return d_val->d_wp;
00362 }
00363
00364
00365 class VariableManagerNotifyObj;
00366
00367
00368 class VariableManager {
00369 friend class Variable;
00370 friend class VariableValue;
00371 private:
00372 ContextManager* d_cm;
00373 MemoryManager* d_mm;
00374 SearchEngineRules* d_rules;
00375 VariableManagerNotifyObj* d_notifyObj;
00376
00377
00378
00379
00380 bool d_disableGC;
00381
00382 bool d_postponeGC;
00383
00384 std::vector<VariableValue*> d_deleted;
00385
00386
00387 class HashLV {
00388 public:
00389 size_t operator()(VariableValue* v) const { return v->getExpr().hash(); }
00390 };
00391 class EqLV {
00392 public:
00393 bool operator()(const VariableValue* lv1, const VariableValue* lv2) const
00394 { return lv1->getExpr() == lv2->getExpr(); }
00395 };
00396
00397
00398 typedef std::hash_set<VariableValue*, HashLV, EqLV> VariableValueSet;
00399 VariableValueSet d_varSet;
00400
00401
00402 VariableValue* newVariableValue(const Expr& e);
00403
00404 public:
00405
00406 VariableManager(ContextManager* cm, SearchEngineRules* rules,
00407 const std::string& mmFlag);
00408
00409 ~VariableManager();
00410
00411
00412 void gc(VariableValue* v);
00413
00414 void postponeGC() { d_postponeGC = true; }
00415
00416 void resumeGC();
00417
00418 ContextManager* getCM() const { return d_cm; }
00419 SearchEngineRules* getRules() const { return d_rules; }
00420
00421 };
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437 class VariableManagerNotifyObj: public ContextNotifyObj {
00438 VariableManager* d_vm;
00439 public:
00440
00441 VariableManagerNotifyObj(VariableManager* vm, Context* cxt)
00442 : ContextNotifyObj(cxt), d_vm(vm) { }
00443
00444 void notifyPre(void) { d_vm->postponeGC(); }
00445 void notify(void) { d_vm->resumeGC(); }
00446 };
00447
00448
00449 }
00450 #endif