00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
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 {
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_;
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;
00178 }
00179 }
00180 else
00181 {
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;
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_;
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;
00206 }
00207 }
00208 else
00209 {
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;
00216 }
00217 else
00218 function_->print(out, domain);
00219 }
00220 }