variable.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, Hoifung Poon, and Daniel Lowd.
00006  * 
00007  * Copyright [2004-08] Stanley Kok, Parag Singla, Matthew
00008  * Richardson, Pedro Domingos, Marc Sumner, Hoifung
00009  * Poon, and Daniel Lowd. 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, Hoifung
00032  * Poon, and Daniel Lowd 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 //NOTE: A better name for this class is needed. Variable may be a little
00067 //misleading
00068 #ifndef VARIABLE_H_JAN_2008
00069 #define VARIABLE_H_JAN_2008
00070 
00071 #include "util.h"
00072 #include "predicate.h"
00073 
00074 
00079 class PredicateTerm
00080 {
00081  public:
00082   PredicateTerm(int & predId, int & termno)
00083   {
00084     predId_ = predId;
00085     termno_ = termno;
00086   }
00087 
00088   int getPredId() { return predId_;}
00089 
00090   int getTermno() { return termno_;}
00091 
00092  private:
00093   int predId_;
00094   int termno_;
00095 };
00096 
00097 
00103 class Variable
00104 {
00105  public:
00106   Variable(Clause* const & clause, int varId, Predicate* const & pred, 
00107            int& termno, int& eqClassId, const Array<int>* const & constants)
00108   {
00109     clause_ = clause;
00110     pred_ = pred;
00111     varId_ = varId;
00112     termno_ = termno;
00113     eqClassId_ = eqClassId; 
00114     tiedVars_ = new Array<Variable*>();
00115     tiedVars_->append(this);
00116     representative_ = true;
00117     implicitConstants_ = new IntHashArray();
00118 
00119       //all constants are initially implicit
00120     for (int i = 0; i < constants->size(); i++)
00121     {
00122       implicitConstants_->append((*constants)[i]);
00123     }
00124   }
00125 
00126   Array<Variable*>* getTiedVariables() {return tiedVars_;} 
00127           
00128   Clause * getClause(){return clause_;}
00129   int getPredId() {return pred_->getId();}
00130   int getVarId() {return varId_;}
00131   int getTermno() {return termno_;}
00132   int getEqClassId() {return eqClassId_;}
00133   bool isRepresentative() {return representative_;}
00134   int getNumTiedVariables() {return tiedVars_->size();}
00135   Variable * getTiedVariable(int pos) {return (*tiedVars_)[pos];}
00136 
00137     //update the clausevarids tied to this object
00138   void setTiedVars(Array<Variable *> * const & newTiedVars)
00139   {
00140     tiedVars_ = newTiedVars;
00141   }
00142 
00143   void setEqClassId(int id) { eqClassId_ = id;}
00144 
00145   void setRepresentative(bool b) { representative_ = b;}
00146 
00147   bool hasSameEqClass(Variable * const & var)
00148   {
00149     return (eqClassId_ == var->getEqClassId());
00150   }
00151 
00152   bool same(Variable * const & var)
00153   {
00154     return
00155       //either they are the same variable (not the constant) in a clause
00156         (clause_->same(var->getClause()) &&
00157      varId_ == var->getVarId() && varId_ < 0) || 
00158       //or some variable in a predicate
00159     ((pred_->getId() == var->getPredId()) && (termno_ == var->getTermno()));
00160   }
00161 
00162     //merge two Variable objects (and the Vars tied to them)
00163   void merge(Variable * const & var)
00164   {
00165     if (hasSameEqClass(var))
00166       return;
00167     tiedVars_->append(var->tiedVars_);
00168     delete var->tiedVars_;
00169 
00170     Variable *linkedVar;
00171       //copy over stuff to all the variables linked to his variable
00172     for (int i = 0; i < tiedVars_->size(); i++)
00173     {
00174       linkedVar = (*tiedVars_)[i];
00175       linkedVar->setEqClassId(eqClassId_);
00176       linkedVar->setTiedVars(tiedVars_);
00177       linkedVar->setRepresentative(false);
00178     }
00179     representative_ = true;
00180   }
00181 
00182   void print(ostream& out, const Domain* const & domain)
00183   {
00184     out<<"Class id: "<<eqClassId_<<endl;
00185     out<<pred_->getName()<<" :"<<termno_<<endl;
00186      cout<<endl;
00187   }
00188 
00189   void printAll(ostream& out, const Domain* const & domain)
00190   {
00191     for (int i = 0; i < tiedVars_->size(); i++)
00192     {
00193       (*tiedVars_)[i]->print(out,domain);
00194     }
00195   }
00196 
00197 /****************************************************************************/
00198 // Functions relating to manipulation of constants
00199 /****************************************************************************/
00200 
00201   bool isImplicit(int constantId)
00202   {
00203     return implicitConstants_->find(constantId) >= 0;
00204   }
00205 
00206   int getNumImplicitConstants() { return implicitConstants_->size();}
00207 
00208   int getImplicitIndex(int constantId)
00209   {
00210     return implicitConstants_->find(constantId);
00211   }
00212 
00213     //remove this constant from the list of implicit constants
00214   void removeImplicit(int constantId)
00215   {
00216     int index = implicitConstants_->find(constantId);
00217     if (index >= 0)
00218       implicitConstants_->removeItem(index);
00219   }
00220 
00221   int getImplicitConstant(int index)
00222   {
00223     assert(implicitConstants_->size() > index);
00224     return (*implicitConstants_)[index]; 
00225   }
00226 
00227     //first constant of the implicit array is taken as the place holder for
00228     //the whole set
00229   bool isImplicitPlaceHolder(int constantId)
00230   {
00231     if (implicitConstants_->size() <= 0)
00232       return false;
00233     return constantId == (*implicitConstants_)[0];
00234   }
00235 
00236   void printImplicitConstants(ostream & out, Domain * const & domain)
00237   {
00238     for (int i = 0; i < implicitConstants_->size(); i++)
00239     {
00240       out<<(*implicitConstants_)[i]<<" ";
00241     }
00242   }
00243 
00244  private:
00245   Clause *clause_;
00246   Predicate *pred_;
00247   int varId_;
00248   int termno_;
00249   Array<Variable *> *tiedVars_;
00250   int eqClassId_;
00251   bool representative_;
00252 
00253     //constants which are represented implicitly
00254   IntHashArray * implicitConstants_;
00255 };
00256 
00257 
00258 class HashPredicateTerm
00259 {
00260  public:
00261   size_t operator()(PredicateTerm *pt) const
00262   {
00263     return 31*pt->getPredId()+ pt->getTermno();
00264   }
00265 };
00266 
00267 
00268 class EqualPredicateTerm
00269 {
00270  public:
00271   bool operator()(PredicateTerm * const & pt1,
00272                   PredicateTerm * const & pt2) const
00273   {
00274     return (pt1->getPredId() == pt2->getPredId()) &&
00275            (pt1->getTermno() == pt2->getTermno());
00276   }
00277 };
00278 
00279 #endif
00280 

Generated on Sun Jun 7 11:55:13 2009 for Alchemy by  doxygen 1.5.1