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

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