intclause.h

00001 /*
00002  * All of the documentation and software included in the
00003  * Alchemy Software is copyrighted by Stanley Kok, Parag
00004  * Singla, Matthew Richardson, Pedro Domingos, Marc
00005  * Sumner and Hoifung Poon.
00006  * 
00007  * Copyright [2004-07] Stanley Kok, Parag Singla, Matthew
00008  * Richardson, Pedro Domingos, Marc Sumner and Hoifung
00009  * Poon. All rights reserved.
00010  * 
00011  * Contact: Pedro Domingos, University of Washington
00012  * (pedrod@cs.washington.edu).
00013  * 
00014  * Redistribution and use in source and binary forms, with
00015  * or without modification, are permitted provided that
00016  * the following conditions are met:
00017  * 
00018  * 1. Redistributions of source code must retain the above
00019  * copyright notice, this list of conditions and the
00020  * following disclaimer.
00021  * 
00022  * 2. Redistributions in binary form must reproduce the
00023  * above copyright notice, this list of conditions and the
00024  * following disclaimer in the documentation and/or other
00025  * materials provided with the distribution.
00026  * 
00027  * 3. All advertising materials mentioning features or use
00028  * of this software must display the following
00029  * acknowledgment: "This product includes software
00030  * developed by Stanley Kok, Parag Singla, Matthew
00031  * Richardson, Pedro Domingos, Marc Sumner and Hoifung
00032  * Poon in the Department of Computer Science and
00033  * Engineering at the University of Washington".
00034  * 
00035  * 4. Your publications acknowledge the use or
00036  * contribution made by the Software to your research
00037  * using the following citation(s): 
00038  * Stanley Kok, Parag Singla, Matthew Richardson and
00039  * Pedro Domingos (2005). "The Alchemy System for
00040  * Statistical Relational AI", Technical Report,
00041  * Department of Computer Science and Engineering,
00042  * University of Washington, Seattle, WA.
00043  * http://www.cs.washington.edu/ai/alchemy.
00044  * 
00045  * 5. Neither the name of the University of Washington nor
00046  * the names of its contributors may be used to endorse or
00047  * promote products derived from this software without
00048  * specific prior written permission.
00049  * 
00050  * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF WASHINGTON
00051  * AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
00052  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00053  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00054  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY
00055  * OF WASHINGTON OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
00056  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00057  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00058  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00059  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00060  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00061  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00062  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
00063  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 //const double HARD_INTCLAUSE_WT = DBL_MAX;
00082 //const double HARD_INTCLAUSE_WT = 10.0;
00083 
00084 class IntClause
00085 {
00086  public:
00087 
00088   IntClause(const Clause* const & c, PredicateHashArray* const & predHashArray);
00089 
00090     // Copy constructor
00091   IntClause(const IntClause& ic);
00092 
00093   ~IntClause()
00094   {
00095         //if(intArrRep_) delete intArrRep_;
00096   }
00097  
00098   void deleteIntPredicates()
00099   {
00100         if (intPreds_) { intPreds_->compress(); delete intPreds_; }
00101   }
00102 
00103 //  void addWt(const double& wt) 
00104 //  { if (wt_ == LWInfo::HARD_WT) return; wt_ += wt; }
00105 
00106 //  void setWt(const double& wt) 
00107 //  { if (wt_ == LWInfo::HARD_WT) return; wt_ = wt; }
00108 
00109   void addWt(const double& wt);
00110   void setWt(const double& wt);
00111 
00112   double getWt() const { return wt_; }
00113 
00114 //  void setWtToHardWt() { wt_ = LWInfo::HARD_WT; }
00115 
00116 //  bool isHardClause() const { return (wt_ == LWInfo::HARD_WT); }
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   const Array<unsigned int>* getIntArrRep() const
00134   { return intArrRep_; }
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   //Array<unsigned int>* intArrRep_;  
00250   
00251   Array<int>* intPreds_;
00252   
00253   // overloaded to indicate whether this is a hard clause
00254   // if this is a hard clause, wt_ is set to LWInfo::HARD_WT
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 

Generated on Tue Jan 16 05:30:02 2007 for Alchemy by  doxygen 1.5.1