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
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 #ifndef GROUNDCLAUSE_H_JUN_26_2005
00067 #define GROUNDCLAUSE_H_JUN_26_2005
00068
00069 #include <cfloat>
00070 #include <ext/hash_set>
00071 using namespace __gnu_cxx;
00072 #include "hasharray.h"
00073 #include "array.h"
00074 #include "hash.h"
00075 #include <map>
00076
00077 using namespace std;
00078
00079
00080 typedef map<int,int> IntPair;
00081 typedef IntPair::iterator IntPairItr;
00083
00084
00085 const double HARD_GROUNDCLAUSE_WT = DBL_MAX;
00086
00087
00088 class Domain;
00089 class Clause;
00090 class GroundPredicate;
00091 class HashGroundPredicate;
00092 class EqualGroundPredicate;
00093
00094
00095 typedef HashArray<GroundPredicate*, HashGroundPredicate, EqualGroundPredicate>
00096 GroundPredicateHashArray;
00097
00101 class GroundClause
00102 {
00103 public:
00104
00105 GroundClause(const Clause* const & c,
00106 GroundPredicateHashArray* const & gndPredHashArray);
00107
00108 ~GroundClause()
00109 {
00110 if (gndPredIndexes_) delete gndPredIndexes_;
00111 if (parentWtPtrs_) delete parentWtPtrs_;
00112 if (foClauseFrequencies_) delete foClauseFrequencies_;
00113 }
00114
00115 void deleteParentWtPtrs()
00116 {
00117 if (parentWtPtrs_)
00118 {
00119 for (int i = 0; i < parentWtPtrs_->size(); i++)
00120 delete (*parentWtPtrs_)[i];
00121 delete parentWtPtrs_;
00122 }
00123 parentWtPtrs_ = NULL;
00124 }
00125
00126 void deleteFoClauseFrequencies()
00127 {
00128 if (foClauseFrequencies_) delete foClauseFrequencies_;
00129 foClauseFrequencies_ = NULL;
00130 }
00131
00132 void addWt(const double& wt)
00133 { if (wt_ == HARD_GROUNDCLAUSE_WT) return; wt_ += wt; }
00134
00135 void setWt(const double& wt)
00136 { if (wt_ == HARD_GROUNDCLAUSE_WT) return; wt_ = wt; }
00137
00138 double getWt() const { return wt_; }
00139
00140 void setWtToHardWt() { wt_ = HARD_GROUNDCLAUSE_WT; }
00141 bool isHardClause() const { return (wt_ == HARD_GROUNDCLAUSE_WT); }
00142
00143 int getNumGroundPredicates() const { return gndPredIndexes_->size(); }
00144
00145 const GroundPredicate* getGroundPredicate(const int& i,
00146 GroundPredicateHashArray* const & gndPredHashArray) const
00147 {
00148 return (*gndPredHashArray)[abs((*gndPredIndexes_)[i]) - 1];
00149 }
00150
00157 void appendToGndPreds(GroundPredicateHashArray* const & gndPredHashArray);
00158
00159 bool getGroundPredicateSense(const int& i) const
00160 { return ((*gndPredIndexes_)[i] > 0); }
00161
00162 void setGroundPredicateIndex(const int& i, const int& gndPredIdx)
00163 { (*gndPredIndexes_)[i] = gndPredIdx; }
00164
00165 int getGroundPredicateIndex(const int& i) const
00166 { return (*gndPredIndexes_)[i]; }
00167
00168 const Array<int>* getGndPredIndexes() const
00169 {
00170 return gndPredIndexes_;
00171 }
00172
00173 void appendParentWtPtr(const double* const & wtPtr)
00174 {
00175 if (parentWtPtrs_ == NULL)
00176 parentWtPtrs_ = new Array<const double*>;
00177 parentWtPtrs_->append(wtPtr);
00178 }
00179
00180 void setWtToSumOfParentWts()
00181 {
00182 assert(parentWtPtrs_);
00183 wt_ = 0;
00184 for (int i = 0; i < parentWtPtrs_->size(); i++)
00185 wt_ += *((*parentWtPtrs_)[i]);
00186 }
00187
00188 IntPair *getClauseFrequencies()
00189 {
00190 return foClauseFrequencies_;
00191 }
00192
00193 int getClauseFrequency(int clauseno)
00194 {
00195 if (!foClauseFrequencies_) return 0;
00196 IntPairItr itr = foClauseFrequencies_->find(clauseno);
00197 if (itr == foClauseFrequencies_->end())
00198 return 0;
00199 else
00200 return itr->second;
00201 }
00202
00203 void incrementClauseFrequency(int clauseno, int increment)
00204 {
00205 if (!foClauseFrequencies_)
00206 foClauseFrequencies_ = new IntPair;
00207 IntPairItr itr = foClauseFrequencies_->find(clauseno);
00208 if (itr == foClauseFrequencies_->end())
00209 foClauseFrequencies_->insert(IntPair::value_type(clauseno, increment));
00210 else
00211 itr->second += increment;
00212 }
00213
00214 size_t hashCode() { return hashCode_; }
00215
00216 bool same(const GroundClause* const & gc)
00217 {
00218 if (this == gc) return true;
00219 if (gndPredIndexes_->size() != gc->getGndPredIndexes()->size())
00220 {
00221 return false;
00222 }
00223 return (memcmp(gndPredIndexes_->getItems(),
00224 gc->getGndPredIndexes()->getItems(),
00225 (gndPredIndexes_->size())*sizeof(int)) == 0);
00226 }
00227
00228 void printWithoutWt(ostream& out) const;
00229 void print(ostream& out) const;
00230
00231 ostream& print(ostream& out, const Domain* const& domain,
00232 const bool& withWt, const bool& asInt,
00233 const bool& withStrVar,
00234 const GroundPredicateHashArray* const & predHashArray) const;
00235
00236 ostream& printWithoutWt(ostream& out, const Domain* const & domain,
00237 const GroundPredicateHashArray* const & predHashArray) const;
00238
00239 ostream&
00240 printWithoutWtWithStrVar(ostream& out, const Domain* const & domain,
00241 const GroundPredicateHashArray* const & predHashArray) const;
00242
00243 ostream& printWithWtAndStrVar(ostream& out, const Domain* const& domain,
00244 const GroundPredicateHashArray* const & predHashArray) const;
00245
00246 ostream& print(ostream& out, const Domain* const& domain,
00247 const GroundPredicateHashArray* const & predHashArray) const;
00248
00249 ostream& printWithoutWtWithStrVarAndPeriod(ostream& out,
00250 const Domain* const& domain,
00251 const GroundPredicateHashArray* const & predHashArray) const;
00252
00253 double sizeKB();
00254
00255 private:
00256
00257 size_t hashCode_;
00258 Array<int>* gndPredIndexes_;
00259
00260
00261
00262 double wt_;
00263
00264
00265 IntPair* foClauseFrequencies_;
00266
00267
00268
00269
00270 Array<const double*>* parentWtPtrs_;
00271
00272 };
00273
00274
00276
00277
00278 class HashGroundClause
00279 {
00280 public:
00281 size_t operator()(GroundClause* const & gc) const { return gc->hashCode(); }
00282 };
00283
00284
00285 class EqualGroundClause
00286 {
00287 public:
00288 bool operator()(GroundClause* const & c1, GroundClause* const & c2) const
00289 { return c1->same(c2); }
00290 };
00291
00292
00294
00295 typedef HashArray<GroundClause*, HashGroundClause, EqualGroundClause>
00296 GroundClauseHashArray;
00297
00298 typedef hash_set<GroundClause*, HashGroundClause, EqualGroundClause>
00299 GroundClauseSet;
00300
00301
00302 #endif