term.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 TERM_H_JUN_26_2005
00067 #define TERM_H_JUN_26_2005
00068 
00069 #include <climits>
00070 #include <stdlib.h>
00071 #include <ostream>
00072 using namespace std;
00073 #include "array.h"
00074 
00075 class Function; 
00076 class Domain;
00077 
00078   //Represents a constant, variable, or function.
00079   //Ensure that the dirty_ bit is consistently updated.
00080 class Term
00081 {
00082  public:
00083   enum { NONE = 0, CONSTANT = 1, VARIABLE = 2, FUNCTION = 3 };
00084 
00085  public:
00086   Term() : id_(-1), function_(NULL), intArrRep_(NULL), dirty_(true), 
00087            parent_(NULL), parentIsPred_(true) {}
00088 
00089   Term(void* const & parent, const bool& parentIsPred) 
00090     : id_(-1), function_(NULL), intArrRep_(NULL), dirty_(true), 
00091       parent_(parent), parentIsPred_(parentIsPred) {}
00092   
00093   Term(const int& id) 
00094     : id_(id), function_(NULL), intArrRep_(NULL), dirty_(true), parent_(NULL), 
00095       parentIsPred_(true) {}
00096 
00097   Term(const int& id, void* const & parent, const bool& parentIsPred) 
00098     : id_(id), function_(NULL), intArrRep_(NULL), dirty_(true), 
00099       parent_(parent), parentIsPred_(parentIsPred) {}
00100 
00101   Term(Function* f) : id_(-1), function_(f), intArrRep_(NULL), dirty_(true),
00102                       parent_(NULL), parentIsPred_(true) {}
00103 
00104   Term(Function* f, void* const & parent, const bool& parentIsPred) 
00105     : id_(-1), function_(f), intArrRep_(NULL), dirty_(true), 
00106       parent_(parent), parentIsPred_(parentIsPred) {}
00107 
00108   Term(const Term& t);
00109 
00110   Term(const Term& t, void* const & parent, const bool& parentIsPred);
00111 
00112   ~Term();
00113 
00114 
00115     //NOTE: if function is supported include the size of *function_
00116   double sizeMB() const
00117   { return (fixedSizeB_ + intArrRep_->size()*sizeof(int))/1000000.0; }
00118 
00119 
00120   static void computeFixedSizeB()
00121   { fixedSizeB_ = sizeof(Term) + sizeof(Array<int>); }
00122 
00123 
00124     //NOTE: if function is supported, compress function_
00125   void compress() { if (intArrRep_) intArrRep_->compress(); }
00126 
00127 
00128   void setDirty();
00129   bool isDirty() const { return dirty_; }
00130 
00131     // Returns CONSTANT, VARIABLE or FUNCTION.
00132   int getType() const
00133   {
00134     if (id_ >= 0) return CONSTANT;
00135     if (function_ != NULL) return FUNCTION;
00136     return VARIABLE;
00137   }
00138 
00139   
00140   void setParent(void* const parent, const bool& parentIsPred)
00141   { parent_ = parent; parentIsPred_ = parentIsPred; setDirty(); }
00142 
00143 
00144   void* getParent(bool& parentIsPred) const
00145   { parentIsPred = parentIsPred_; return parent_; }
00146 
00147   
00148   void setId(const int& id) 
00149   { 
00150     id_ = id; 
00151     setDirty(); 
00152   }
00153 
00154   int getId() const { return id_; }
00155   //int* getIdPtr() { return &id_; }
00156 
00157   void revertToBeFunction() { assert(function_); id_ = -1; }
00158 
00159   void setFunction(Function* const & f) { function_ = f; setDirty(); }
00160   Function* getFunction() const { return function_; }
00161 
00162   bool isConstant() const { return id_ >= 0; }
00163   bool isGrounded() const { return id_ >= 0; }
00164 
00165     // represent this term as an Array<int> and append it to rep
00166   void appendIntArrRep(Array<int>& rep) 
00167   {
00168     if (dirty_) computeAndStoreIntArrRep();
00169     rep.append(*intArrRep_);
00170   }
00171 
00172 
00173   bool same(Term* const & t)
00174   {
00175     if (this==t) return true;
00176     const Array<int>* tArr  = t->getIntArrRep();
00177     const Array<int>* myArr = getIntArrRep();
00178     if (myArr->size() != tArr->size()) return false;
00179     const int* tItems  = t->getIntArrRep()->getItems();
00180     const int* myItems = getIntArrRep()->getItems();
00181     return (memcmp(myItems, tItems, myArr->size()*sizeof(int))==0); 
00182   }
00183 
00184   
00185   void printAsInt(ostream& out) const;
00186   void printWithStrVar(ostream& out, const Domain* const & domain) const;
00187   void print(ostream& out, const Domain* const & domain) const;
00188 
00189 
00190  private:
00191   void copy(const Term& t);
00192   bool noDirtyFunction();
00193 
00194   const Array<int>* getIntArrRep() 
00195   { if (dirty_) computeAndStoreIntArrRep(); return intArrRep_; }
00196 
00197   void computeAndStoreIntArrRep();
00198   
00199  private:
00200     // if it is a constant, id_ >= 0 and function_ == NULL;
00201     // if it is a variable, id_ < 0  and function_ == NULL;
00202     // if it is a function whose returned value has not been determined, 
00203     //    function_ != NULL and id_ < 0;
00204     // if it is a function whose returned value has been determined, 
00205     //    function_ != NULL and id_ >= 0;
00206   int id_;
00207   Function* function_; // if non-null, then Term is a function
00208   Array<int>* intArrRep_; 
00209   bool dirty_;
00210   
00211   void* parent_;  // not owned by Term, so do not delete in destructor
00212   bool parentIsPred_;
00213 
00214   static double fixedSizeB_;
00215 };
00216 
00217 #endif

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