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 #include "groundclause.h"
00067 #include "groundpredicate.h"
00068 #include "clause.h"
00069
00070 GroundClause::GroundClause(const Clause* const & c,
00071 GroundPredicateHashArray* const & gndPredHashArray)
00072 : wt_(c->getWt()), foClauseFrequencies_(NULL), parentWtPtrs_(NULL)
00073 {
00074 int numPreds = c->getNumPredicates();
00075 gndPredIndexes_ = new Array<int>;
00076
00077 Array<unsigned int>* intArrRep = new Array<unsigned int>;
00078
00079
00080 for (int i = 0; i < numPreds; i++)
00081 {
00082 bool deletePred = false;
00083 Predicate* pred = c->getPredicate(i);
00084 GroundPredicate* gndPred = new GroundPredicate(pred);
00085 int index = gndPredHashArray->find(gndPred);
00086 if (index < 0 )
00087 {
00088 index = gndPredHashArray->append(gndPred) + 1;
00089 }
00090 else
00091 {
00092 deletePred = true;
00093 index++;
00094 }
00095
00096 int wlit;
00097 if (pred->getSense())
00098 {
00099 wlit = index;
00100 intArrRep->append(1);
00101 }
00102 else
00103 {
00104 wlit = -index;
00105 intArrRep->append((unsigned int)0);
00106 }
00107 intArrRep->append(index);
00108 gndPredIndexes_->append(wlit);
00109
00110 if (deletePred) delete gndPred;
00111 }
00112
00113 hashCode_ = Hash::hash(*intArrRep);
00114 delete intArrRep;
00115 gndPredIndexes_->compress();
00116 }
00117
00124 void GroundClause::appendToGndPreds(
00125 GroundPredicateHashArray* const & gndPredHashArray)
00126 {
00127
00128 for (int i = 0; i < gndPredIndexes_->size(); i++)
00129 {
00130 bool sense = ((*gndPredIndexes_)[i] > 0);
00131 int index = abs((*gndPredIndexes_)[i]) - 1;
00132
00133 bool ok = (*gndPredHashArray)[index]->appendGndClause(this, sense);
00134 assert(ok); ok = true;
00135 }
00136 }
00137
00138 void GroundClause::printWithoutWt(ostream& out) const
00139 {
00140 for (int i = 0; i < gndPredIndexes_->size(); i++)
00141 {
00142 out << (*gndPredIndexes_)[i];
00143 if (i < gndPredIndexes_->size() - 1) out << " v ";
00144 }
00145 }
00146
00147 void GroundClause::print(ostream& out) const
00148 { out << wt_ << " "; printWithoutWt(out); }
00149
00150 ostream&
00151 GroundClause::print(ostream& out, const Domain* const& domain,
00152 const bool& withWt, const bool& asInt,
00153 const bool& withStrVar,
00154 const GroundPredicateHashArray* const & predHashArray) const
00155 {
00156 if (withWt) out << wt_ << " ";
00157
00158 Array<Predicate*> eqPreds;
00159 Array<Predicate*> internalPreds;
00160 for (int i = 0; i < gndPredIndexes_->size(); i++)
00161 {
00162 int index = (*gndPredIndexes_)[i];
00163 Predicate* pred =
00164 (*predHashArray)[abs(index)-1]->createEquivalentPredicate(domain);
00165
00166
00167 assert(pred);
00168 if (index < 0) pred->setSense(false);
00169 else pred->setSense(true);
00170
00171 if (pred->isEqualPred())
00172 eqPreds.append(pred);
00173 else
00174 if (pred->isInternalPred())
00175 internalPreds.append(pred);
00176 else
00177 {
00178 if (asInt) pred->printAsInt(out);
00179 else if (withStrVar) pred->printWithStrVar(out, domain);
00180 else pred->print(out,domain);
00181 if (i < gndPredIndexes_->size() - 1 || !eqPreds.empty() ||
00182 !internalPreds.empty()) out << " v ";
00183 delete pred;
00184 }
00185 }
00186
00187 for (int i = 0; i < eqPreds.size(); i++)
00188 {
00189 if (asInt) eqPreds[i]->printAsInt(out);
00190 else if (withStrVar) eqPreds[i]->printWithStrVar(out,domain);
00191 else eqPreds[i]->print(out,domain);
00192 out << ((i != eqPreds.size()-1 || !internalPreds.empty())?" v ":"");
00193 delete eqPreds[i];
00194 }
00195
00196 for (int i = 0; i < internalPreds.size(); i++)
00197 {
00198 if (asInt) internalPreds[i]->printAsInt(out);
00199 else if (withStrVar) internalPreds[i]->printWithStrVar(out,domain);
00200 else internalPreds[i]->print(out,domain);
00201 out << ((i!=internalPreds.size()-1)?" v ":"");
00202 delete internalPreds[i];
00203 }
00204
00205 return out;
00206 }
00207
00208
00209 ostream&
00210 GroundClause::printWithoutWt(ostream& out, const Domain* const & domain,
00211 const GroundPredicateHashArray* const & predHashArray) const
00212 { return print(out, domain, false, false, false, predHashArray); }
00213
00214 ostream&
00215 GroundClause::printWithoutWtWithStrVar(ostream& out,
00216 const Domain* const & domain,
00217 const GroundPredicateHashArray* const & predHashArray) const
00218 { return print(out, domain, false, false, true, predHashArray); }
00219
00220 ostream&
00221 GroundClause::printWithWtAndStrVar(ostream& out, const Domain* const& domain,
00222 const GroundPredicateHashArray* const & predHashArray) const
00223 { return print(out, domain, true, false, true, predHashArray); }
00224
00225 ostream&
00226 GroundClause::print(ostream& out, const Domain* const& domain,
00227 const GroundPredicateHashArray* const & predHashArray) const
00228 { return print(out, domain, true, false, false, predHashArray); }
00229
00230 ostream&
00231 GroundClause::printWithoutWtWithStrVarAndPeriod(ostream& out,
00232 const Domain* const& domain,
00233 const GroundPredicateHashArray* const & predHashArray) const
00234 {
00235 printWithoutWtWithStrVar(out, domain, predHashArray);
00236 if (isHardClause()) out << ".";
00237 return out;
00238 }
00239
00240
00244 double GroundClause::sizeKB()
00245 {
00246 double size = 0;
00247
00248 if (gndPredIndexes_)
00249 size += (gndPredIndexes_->size()*sizeof(int) / 1024.0);
00250
00251 size += (sizeof(double) / 1024.0);
00252
00253 if (parentWtPtrs_)
00254 size += (parentWtPtrs_->size()*sizeof(const double*) / 1024.0);
00255
00256 if (foClauseFrequencies_)
00257 size += (foClauseFrequencies_->size()*2*sizeof(int) / 1024.0);
00258
00259 return size;
00260 }
00261