term.cpp

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 #include "term.h"
00067 #include "function.h"
00068 #include "domain.h"
00069 #include <cstring>
00070 
00071 double Term::fixedSizeB_ = -1;
00072 
00073 
00074 Term::Term(const Term& t)
00075 {
00076   parent_ = NULL;
00077   parentIsPred_ = true;
00078   copy(t);
00079 }
00080 
00081 
00082 Term::Term(const Term& t, void* const & parent, const bool& parentIsPred)
00083 {
00084   parent_ = parent;
00085   parentIsPred_ = parentIsPred;
00086   copy(t);
00087 }
00088 
00089 
00090 Term::~Term() 
00091 { 
00092   if (function_) delete function_; 
00093   if (intArrRep_) delete intArrRep_; 
00094 }
00095 
00096 
00097 void Term::copy(const Term& t)
00098 {
00099   id_ = t.id_;
00100 
00101   if (t.function_)  function_ = new Function(*(t.function_), this);
00102   else              function_ = NULL;
00103 
00104   if (t.intArrRep_)  intArrRep_ = new Array<int>(*(t.intArrRep_));
00105   else               intArrRep_ = NULL;  
00106   
00107   dirty_ = t.dirty_;
00108 
00109   if (!dirty_) { assert(noDirtyFunction()); }
00110 
00111   if (parent_ != NULL)
00112   {
00113     if (parentIsPred_) ((Predicate*)parent_)->setDirty();
00114     else               ((Function*)parent_)->setDirty();
00115   }
00116 }
00117 
00118 
00119 bool Term::noDirtyFunction()
00120 {
00121   if (function_ == NULL) return true;
00122   return !(function_->isDirty());
00123 }
00124 
00125 
00126 void Term::setDirty() 
00127 { 
00128   dirty_ = true;
00129   if (parent_ != NULL)
00130   {
00131     if (parentIsPred_) ((Predicate*)parent_)->setDirty();
00132     else               ((Function*)parent_)->setDirty();
00133   }
00134 }
00135 
00136 
00137 void Term::computeAndStoreIntArrRep()
00138 {
00139   dirty_ = false;
00140 
00141   if (intArrRep_ == NULL) intArrRep_ = new Array<int>;
00142   else                    intArrRep_->clear();
00143   
00144   if (function_ == NULL)
00145     intArrRep_->append(id_);
00146   else
00147   {
00148     if (id_ >= 0) intArrRep_->append(id_);
00149     else          function_->appendIntArrRep(*intArrRep_);
00150   }
00151 }
00152 
00153 
00154 void Term::printAsInt(ostream& out) const
00155 {
00156   if (function_ == NULL) 
00157     out << id_;
00158   else 
00159   { //function_ != NULL;
00160     if (id_ >= 0) out << id_;
00161     else          function_->printAsInt(out);
00162   }
00163 }
00164 
00165 
00166 void Term::printWithStrVar(ostream& out, const Domain* const & domain) const
00167 {
00168   if (function_ == NULL)
00169   {
00170     if (id_ < 0) 
00171       out << "a" << -id_;  // variable
00172     else
00173     {
00174       string cn = domain->getConstantName(id_);
00175       string::size_type at = cn.rfind("@");
00176       if (at != string::npos) cn = cn.substr(at+1, cn.length()-at-1);
00177       out << cn; // constant
00178     }
00179   }
00180   else
00181   { //function_ != NULL;
00182     if (id_ >= 0) 
00183     {
00184       string cn = domain->getConstantName(id_);
00185       string::size_type at = cn.rfind("@");
00186       if (at != string::npos) cn = cn.substr(at+1, cn.length()-at-1);
00187       out << cn; // return value is known
00188     }
00189     else          
00190       function_->print(out,domain); 
00191   }
00192 }
00193 
00194 
00195 void Term::print(ostream& out, const Domain* const & domain) const
00196 {
00197   if (function_ == NULL)
00198   {
00199     if (id_ < 0) out << id_;  // variable
00200     else         
00201     {
00202       string cn = domain->getConstantName(id_);
00203       string::size_type at = cn.rfind("@");
00204       if (at != string::npos) cn = cn.substr(at+1, cn.length()-at-1);
00205       out << cn; // constant
00206     }
00207   }
00208   else
00209   { //function_ != NULL;
00210     if (id_ >= 0) 
00211     {
00212       string cn = domain->getConstantName(id_);
00213       string::size_type at = cn.rfind("@");
00214       if (at != string::npos) cn = cn.substr(at+1, cn.length()-at-1);
00215       out << cn; // return value is known
00216     }
00217     else          
00218       function_->print(out, domain); 
00219   }
00220 }

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