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
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 {
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_;
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;
00177 }
00178 }
00179 else
00180 {
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;
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_;
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;
00205 }
00206 }
00207 else
00208 {
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;
00215 }
00216 else
00217 function_->print(out, domain);
00218 }
00219 }