predicatetemplate.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 PREDICATETEMPLATE_JUN_22_2005
00067 #define PREDICATETEMPLATE_JUN_22_2005
00068 
00069 #include <ctype.h>
00070 #include "array.h"
00071 
00072 class Domain;
00073 
00074 
00075 class PredicateTemplate
00076 {
00077  public:
00078   static const char* EMPTY_NAME; // name of empty template
00079   //Internal predicate names
00080   static const char* EQUAL_NAME; // name of template representing '='
00081   static const char* GT_NAME; // name of template representing '>'
00082   static const char* LT_NAME; // name of template representing '<'
00083   static const char* GTEQ_NAME; // name of template representing '>='
00084   static const char* LTEQ_NAME; // name of template representing '<='
00085   static const char* SUBSTR_NAME; // name of template representing 'substring'
00086   static const char* ANY_TYPE_NAME;   // name of any type
00087   static const char* INT_TYPE_NAME;   // name of type representing integers
00088   static const char* STRING_TYPE_NAME;   // name of type representing strings
00089   // Start of the predicate name used for replacing functions
00090   static const char* ZZ_RETURN_PREFIX;
00091   
00092   // Internal function names
00093   static const char* SUCC_NAME; // name of template representing 'succ'
00094   static const char* PLUS_NAME; // name of template representing '+'
00095   static const char* MINUS_NAME; // name of template representing '-'
00096   static const char* TIMES_NAME; // name of template representing '*'
00097   static const char* DIVIDEDBY_NAME; // name of template representing '/'
00098   static const char* MOD_NAME; // name of template representing '%'
00099   static const char* CONCAT_NAME; // name of template representing 'concat'
00100  private:
00101   enum { NONE = 0, EQUAL_PRED = 1, EQUAL_PRED_WITH_TYPE = 2 };
00102   enum { INT_PRED = 1, INT_PRED_WITH_TYPE = 2 };
00103 
00104  public:
00105   PredicateTemplate() : id_(-1), name_(NULL), termTypesAsInt_(new Array<int>), 
00106                         termTypesAsStr_(new Array<const char*>),
00107                         termsUnique_(new Array<bool>), numGnd_(-1.0), 
00108                         equalPred_(NONE), intPred_(NONE) {}
00109   
00110   virtual ~PredicateTemplate() 
00111   { 
00112     delete [] name_;
00113     for (int i = 0; i < termTypesAsStr_->size(); i++)
00114       delete [] (*termTypesAsStr_)[i];
00115     delete termTypesAsInt_; 
00116     delete termTypesAsStr_; 
00117     delete termsUnique_;
00118   }
00119 
00120 
00121     // returns EQUAL_NAME append with _typeName
00122   static string createEqualPredTypeName(const char* const & typeName)
00123   {
00124     string ptName;
00125     ptName.append(PredicateTemplate::EQUAL_NAME).append("_").append(typeName);
00126     return ptName;
00127   }
00128 
00129     // returns predName append with _typeName
00130   static string createInternalPredTypeName(const char* const & predName,
00131                                                                                    const char* const & typeName)
00132   {
00133     string ptName;
00134     ptName.append(predName).append("_").append(typeName);
00135     return ptName;
00136   }
00137   
00138         // returns array with internal predicate names appended with _typeName
00139   static Array<string> createInternalPredTypeNames(const char* const & typeName)
00140   {
00141         Array<string> predTypeNames;
00142         
00143         string gtName;
00144         gtName.append(PredicateTemplate::GT_NAME).append("_").append(typeName);
00145         predTypeNames.append(gtName);
00146         
00147         string ltName;
00148         ltName.append(PredicateTemplate::LT_NAME).append("_").append(typeName);
00149         predTypeNames.append(ltName);
00150         
00151         string gteqName;
00152         gteqName.append(PredicateTemplate::GTEQ_NAME).append("_").append(typeName);
00153         predTypeNames.append(gteqName);
00154         
00155         string lteqName;
00156         lteqName.append(PredicateTemplate::LTEQ_NAME).append("_").append(typeName);
00157         predTypeNames.append(lteqName);
00158         
00159         string substrName;
00160         substrName.append(PredicateTemplate::SUBSTR_NAME).append("_").append(typeName);
00161         predTypeNames.append(substrName);
00162         
00163         return predTypeNames;
00164   }
00165 
00166   static bool isEqualPredName(const char* predName)
00167   { return (strncmp(predName, EQUAL_NAME, strlen(EQUAL_NAME))==0); }
00168 
00169   static bool isInternalPredicateTemplateName(const char* predName)
00170   { 
00171         return (strncmp(predName, GT_NAME, strlen(GT_NAME)) == 0 || 
00172                         strncmp(predName, LT_NAME, strlen(LT_NAME)) == 0 ||
00173                         strncmp(predName, GTEQ_NAME, strlen(GTEQ_NAME)) == 0 ||
00174                         strncmp(predName, LTEQ_NAME, strlen(LTEQ_NAME)) == 0 ||
00175                         strncmp(predName, SUBSTR_NAME, strlen(SUBSTR_NAME)) == 0);
00176   }
00177 
00178 
00179   bool isEqualPredicateTemplate() const 
00180   { return (equalPred_ == EQUAL_PRED || equalPred_ == EQUAL_PRED_WITH_TYPE); }
00181 
00182   bool isEmptyPredicateTemplate() const 
00183   { return (strcmp(name_, EMPTY_NAME) == 0); }
00184 
00185   bool isInternalPredicateTemplate() const 
00186   { 
00187         return (strncmp(name_, GT_NAME, strlen(GT_NAME)) == 0 || 
00188                         strncmp(name_, LT_NAME, strlen(LT_NAME)) == 0 ||
00189                         strncmp(name_, GTEQ_NAME, strlen(GTEQ_NAME)) == 0 ||
00190                         strncmp(name_, LTEQ_NAME, strlen(LTEQ_NAME)) == 0 ||
00191                         strncmp(name_, SUBSTR_NAME, strlen(SUBSTR_NAME)) == 0);
00192   }
00193 
00194   bool isInternalPredicateTemplateWithoutType() const 
00195   { 
00196         return (strcmp(name_, GT_NAME) == 0 || 
00197                         strcmp(name_, LT_NAME) == 0 ||
00198                         strcmp(name_, GTEQ_NAME) == 0 ||
00199                         strcmp(name_, LTEQ_NAME) == 0 ||
00200                         strcmp(name_, SUBSTR_NAME) == 0);
00201   }
00202 
00203   bool isPredicateTemplateFromInternalFunction() const
00204   {
00205         return (strcmp(name_, string(ZZ_RETURN_PREFIX).append(string(SUCC_NAME)).c_str()) == 0 ||
00206                         strcmp(name_, string(ZZ_RETURN_PREFIX).append(string(PLUS_NAME)).c_str()) == 0 ||
00207                         strcmp(name_, string(ZZ_RETURN_PREFIX).append(string(MINUS_NAME)).c_str()) == 0 ||
00208                         strcmp(name_, string(ZZ_RETURN_PREFIX).append(string(TIMES_NAME)).c_str()) == 0 ||
00209                         strcmp(name_, string(ZZ_RETURN_PREFIX).append(string(DIVIDEDBY_NAME)).c_str()) == 0 ||
00210                         strcmp(name_, string(ZZ_RETURN_PREFIX).append(string(MOD_NAME)).c_str()) == 0 ||
00211                         strcmp(name_, string(ZZ_RETURN_PREFIX).append(string(CONCAT_NAME)).c_str()) == 0);
00212   }
00213   
00214   bool isPredicateTemplateFromFunction() const
00215   {
00216         if (strlen(name_) > strlen(ZZ_RETURN_PREFIX))
00217           return (strncmp(name_, ZZ_RETURN_PREFIX, strlen(ZZ_RETURN_PREFIX)) == 0);
00218         return false;
00219   }
00220   
00221   static string translateEqualPredicateName(const string& eqPredName)
00222   {
00223     if (strncmp(eqPredName.c_str(), "same", 4) != 0) return "";
00224     string typeStr = eqPredName.substr(4, eqPredName.length()-4);
00225     string newName = PredicateTemplate::EQUAL_NAME;
00226     newName.append("_").append(typeStr);
00227     return newName;
00228   }
00229 
00230 
00231   void setId(const int& id)  { id_ = id; }
00232 
00233   int getId() const { return id_; }
00234 
00235 
00236     // Caller is responsible for deleting name if required.
00237   void setName(const char* const & name) 
00238   { 
00239     if (name_) delete [] name_;
00240     name_ = new char[strlen(name)+1]; 
00241     strcpy(name_, name);
00242 
00243     unsigned int len = strlen(EQUAL_NAME);
00244     if (strncmp(name_, EQUAL_NAME, len)==0 && strlen(name_) > len)
00245       equalPred_ = EQUAL_PRED_WITH_TYPE;
00246     else
00247     if (strncmp(name_,EQUAL_NAME, len)==0) 
00248       equalPred_ = EQUAL_PRED;
00249     else 
00250       equalPred_ = NONE;
00251   }
00252 
00253   
00254   bool isEqualPred() const 
00255   { return (equalPred_ == EQUAL_PRED || equalPred_ == EQUAL_PRED_WITH_TYPE); }
00256 
00257 
00258   bool isEqualPredWithType() const
00259   { return (equalPred_ == EQUAL_PRED_WITH_TYPE); }
00260 
00261     // Caller should not delete returned const char*.
00262   const char* const getName() const { return name_; }
00263 
00264 
00265   int getNumTerms() const 
00266   {
00267     assert(termTypesAsStr_->size() == termTypesAsInt_->size());
00268     return termTypesAsInt_->size(); 
00269   }
00270 
00271 
00272     // Caller is responsible for deleting typeName if required.
00273     // returns true if termType is successfully added; false otherwise
00274   bool appendTermType(const char* const & typeName, const bool& isUnique,
00275                       const Domain* const & domain);
00276 
00277     // returns true if termType is successfully added; false otherwise
00278   bool appendTermType(const int& typeId, const bool& isUnique, 
00279                       const Domain* const & domain);
00280                       
00281 
00282     // Caller should not delete the returned Array<const char&>* nor modify its
00283     // contents.
00284   const Array<const char*>* getTermTypesAsStr() const 
00285   { return termTypesAsStr_; }
00286 
00287 
00288     // Caller should not delete the returned const char* nor modify its
00289     // contents.
00290   const char* getTermTypeAsStr(const int& idx) const 
00291   { return (*termTypesAsStr_)[idx]; }
00292 
00293 
00294     // Caller should not delete the returned Array<int>* nor modify its
00295     // contents.
00296   const Array<int>* getTermTypesAsInt() const 
00297   { return termTypesAsInt_; }
00298 
00299 
00300   int getTermTypeAsInt(const int& idx) const 
00301   { return (*termTypesAsInt_)[idx]; }
00302   
00303 
00304     // Caller should not delete the returned Array<bool>* nor modify its
00305     // contents.
00306   const Array<bool>* getTermsUnique() const { return termsUnique_; }
00307 
00308 
00309   const bool termIsUnique(const int& idx) const 
00310   { return (*termsUnique_)[idx]; }
00311 
00312 
00313   virtual ostream& print(ostream& out) const
00314   {
00315     out << name_ << "(";
00316     for (int i = 0; i < termTypesAsStr_->size(); i++)
00317     {
00318       out << (*termTypesAsStr_)[i]; 
00319       out << ((i!=termTypesAsStr_->size()-1)?",":")");
00320     }
00321     return out;
00322   }
00323 
00324 
00325   virtual ostream& printWithStrVar(ostream& out) const
00326   {
00327     out << name_ << "(";
00328     for (int i = 0; i < termTypesAsStr_->size(); i++)
00329     {
00330       out << "a" << i+1; 
00331       out << ((i!=termTypesAsStr_->size()-1)?",":")");
00332     }
00333     return out;
00334   }
00335 
00336 
00337  protected:
00338   int id_;     // id of predicate
00339   char* name_; // name of predicate
00340   Array<int>* termTypesAsInt_;
00341   Array<const char*>* termTypesAsStr_;
00342   Array<bool>* termsUnique_;
00343   double numGnd_;
00344   
00345  private:
00346   int equalPred_;
00347   int intPred_;
00348 };
00349 
00350 
00351 inline
00352 ostream& operator<<(ostream& o, const PredicateTemplate& p) {return p.print(o);}
00353 
00354 
00355 #endif

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