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 INTCLAUSE_H_JAN_13_2006
00067 #define INTCLAUSE_H_JAN_13_2006
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 "predicate.h"
00076
00077 class Domain;
00078 class Clause;
00079 class Database;
00080
00081
00082
00083
00084 class IntClause
00085 {
00086 public:
00087
00088 IntClause(const Clause* const & c, PredicateHashArray* const & predHashArray);
00089
00090
00091 IntClause(const IntClause& ic);
00092
00093 ~IntClause()
00094 {
00095
00096 }
00097
00098 void deleteIntPredicates()
00099 {
00100 if (intPreds_) { intPreds_->compress(); delete intPreds_; }
00101 }
00102
00103
00104
00105
00106
00107
00108
00109 void addWt(const double& wt);
00110 void setWt(const double& wt);
00111
00112 double getWt() const { return wt_; }
00113
00114
00115
00116
00117
00118 void setWtToHardWt();
00119 bool isHardClause() const;
00120
00121 int getNumIntPredicates() const { return intPreds_->size(); }
00122
00123 const int getIntPredicate(const int& i) const
00124 { return (*intPreds_)[i]; }
00125
00126 const Array<int>* getIntPredicates() const
00127 { return intPreds_; }
00128
00129 bool isSatisfied(const PredicateHashArray* const & predHashArray,
00130 const Database* const & db) const;
00131
00132
00133
00134
00135
00136
00137 size_t hashCode() { return hashCode_;}
00138
00139 bool same(const IntClause* const & c)
00140 {
00141 if(this == c) return true;
00142
00143 if (intPreds_->size() != c->getIntPredicates()->size()) return false;
00144
00145 const int* myItems = intPreds_->getItems();
00146 const int* cItems = c->getIntPredicates()->getItems();
00147
00148 return (memcmp(myItems,cItems,(intPreds_->size())*sizeof(int)) == 0);
00149 }
00150
00151 void printWithoutWt(ostream& out) const
00152 {
00153 for (int i = 0; i < intPreds_->size(); i++)
00154 {
00155 out << (*intPreds_)[i];
00156 if (i < intPreds_->size()-1) out << " v ";
00157 }
00158 }
00159
00160 void print(ostream& out) const
00161 { out << wt_ << " "; printWithoutWt(out); }
00162
00163
00164 ostream& print(ostream& out, const Domain* const& domain,
00165 const bool& withWt, const bool& asInt,
00166 const bool& withStrVar,
00167 const PredicateHashArray* const & predHashArray) const
00168 {
00169 if (withWt) out << wt_ << " ";
00170
00171 Array<Predicate*> eqPreds;
00172 Array<Predicate*> internalPreds;
00173 for (int i = 0; i < intPreds_->size(); i++)
00174 {
00175 int index = (*intPreds_)[i];
00176 Predicate* pred = new Predicate((*(*predHashArray)[abs(index)-1]));
00177 assert(pred);
00178 if (index < 0) pred->setSense(false);
00179 else pred->setSense(true);
00180
00181 if (pred->isEqualPred())
00182 eqPreds.append(pred);
00183 else
00184 if (pred->isInternalPred())
00185 internalPreds.append(pred);
00186 else
00187 {
00188 if (asInt) pred->printAsInt(out);
00189 else if (withStrVar) pred->printWithStrVar(out, domain);
00190 else pred->print(out,domain);
00191 if (i < intPreds_->size()-1 || !eqPreds.empty() ||
00192 !internalPreds.empty()) out << " v ";
00193 delete pred;
00194 }
00195 }
00196
00197 for (int i = 0; i < eqPreds.size(); i++)
00198 {
00199 if (asInt) eqPreds[i]->printAsInt(out);
00200 else if (withStrVar) eqPreds[i]->printWithStrVar(out,domain);
00201 else eqPreds[i]->print(out,domain);
00202 out << ((i != eqPreds.size()-1 || !internalPreds.empty())?" v ":"");
00203 delete eqPreds[i];
00204 }
00205
00206 for (int i = 0; i < internalPreds.size(); i++)
00207 {
00208 if (asInt) internalPreds[i]->printAsInt(out);
00209 else if (withStrVar) internalPreds[i]->printWithStrVar(out,domain);
00210 else internalPreds[i]->print(out,domain);
00211 out << ((i!=internalPreds.size()-1)?" v ":"");
00212 delete internalPreds[i];
00213 }
00214
00215 return out;
00216 }
00217
00218
00219 ostream& printWithoutWt(ostream& out, const Domain* const & domain,
00220 const PredicateHashArray* const & predHashArray) const
00221 { return print(out, domain, false, false, false, predHashArray); }
00222
00223 ostream&
00224 printWithoutWtWithStrVar(ostream& out, const Domain* const & domain,
00225 const PredicateHashArray* const & predHashArray) const
00226 { return print(out, domain, false, false, true, predHashArray); }
00227
00228 ostream& printWithWtAndStrVar(ostream& out, const Domain* const& domain,
00229 const PredicateHashArray* const & predHashArray) const
00230 { return print(out, domain, true, false, true, predHashArray); }
00231
00232 ostream& print(ostream& out, const Domain* const& domain,
00233 const PredicateHashArray* const & predHashArray) const
00234 { return print(out, domain, true, false, false, predHashArray); }
00235
00236 ostream& printWithoutWtWithStrVarAndPeriod(ostream& out,
00237 const Domain* const& domain,
00238 const PredicateHashArray* const & predHashArray) const
00239 {
00240 printWithoutWtWithStrVar(out, domain, predHashArray);
00241 if (isHardClause()) out << ".";
00242 return out;
00243 }
00244
00245
00246 private:
00247 size_t hashCode_;
00248
00249
00250
00251 Array<int>* intPreds_;
00252
00253
00254
00255 double wt_;
00256
00257 };
00258
00260
00261
00262 class HashIntClause
00263 {
00264 public:
00265 size_t operator()(IntClause* const & c) const { return c->hashCode(); }
00266 };
00267
00268
00269 class EqualIntClause
00270 {
00271 public:
00272 bool operator()(IntClause* const & c1, IntClause* const & c2) const
00273 { return c1->same(c2); }
00274 };
00275
00276
00278
00279 typedef HashArray<IntClause*, HashIntClause, EqualIntClause>
00280 IntClauseHashArray;
00281
00282
00283 #endif
00284