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

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