00001 00002 // 00003 // Copyright (c) 2004 Model-based Embedded and Robotic Systems Group 00004 // All Rights Reserved Massachusetts Institute of Technology 00005 // 00006 // This software is provided as is. Model-based Embedded and Robotic Systems 00007 // (MERS) group does not assume any responsibility. 00008 // 00009 // Organization: Model-based Embedded and Robotic Systems Group 00010 // Massachusetts Institute of Technology 00011 // 00013 //# CHECKED MJP 08-07-2002 00014 00015 #include "BFIGConstraintUnit.h" 00016 00017 #include <cassert> 00018 #include <iostream> 00019 00020 #include "BFIGVariable.h" 00021 #include "BFIGAssignment.h" 00022 00024 00025 using namespace Mers::Generators::BFIG; 00026 using Mers::Utils::Lang::String; 00027 using std::ostream; 00028 00030 00031 BFIGConstraintUnit::~BFIGConstraintUnit() 00032 { 00033 } 00034 00036 00037 BFIGConstraintUnit::BFIGConstraintUnit(BFIGAssignment const * a, bool parityBit) 00038 : constraintAssignment(a), parity(parityBit) 00039 { 00040 assert(a); 00041 //constraintAssignment = a; 00042 //parity = parityBit; 00043 } 00044 00046 00047 BFIGConstraintUnit::BFIGConstraintUnit(BFIGConstraintUnit const & cu) 00048 : constraintAssignment(cu.constraintAssignment), parity(cu.parity) 00049 { 00050 assert(constraintAssignment); 00051 //constraintAssignment = cu.constraintAssignment; 00052 //parity = cu.parity; 00053 } 00054 00056 00057 BFIGConstraintUnit 00058 BFIGConstraintUnit::negated() const 00059 { 00060 return BFIGConstraintUnit(constraintAssignment, !parity); 00061 } 00062 00064 00065 Mers::Generators::BFIG::BFIGAssignment const * 00066 BFIGConstraintUnit::getAssignment() const 00067 { 00068 return constraintAssignment; 00069 } 00070 00072 00073 Mers::Generators::BFIG::BFIGVariable const * 00074 BFIGConstraintUnit::getVariable() const 00075 { 00076 if (constraintAssignment == NULL) 00077 return NULL; 00078 return constraintAssignment->getVariable(); 00079 } 00080 00082 00083 unsigned int 00084 BFIGConstraintUnit::getValue() const 00085 { 00086 if (constraintAssignment == NULL) 00087 return 0; 00088 return constraintAssignment->getValue(); 00089 } 00090 00092 00093 String 00094 BFIGConstraintUnit::getValueString() const 00095 { 00096 if (constraintAssignment == NULL) 00097 return "nil"; 00098 return constraintAssignment->getValueString(); 00099 } 00100 00102 00103 unsigned int 00104 BFIGConstraintUnit::getValueInt() const 00105 { 00106 if (constraintAssignment == NULL) 00107 return 0; 00108 return constraintAssignment->getValueInt(); 00109 } 00110 00112 00113 unsigned int 00114 BFIGConstraintUnit::getName() const 00115 { 00116 if (constraintAssignment == NULL) 00117 return 0; 00118 return constraintAssignment->getName(); 00119 } 00120 00122 00123 String 00124 BFIGConstraintUnit::getNameString() const 00125 { 00126 if (constraintAssignment == NULL) 00127 return "nil"; 00128 return constraintAssignment->getNameString(); 00129 } 00130 00132 00133 unsigned int 00134 BFIGConstraintUnit::getNameInt() const 00135 { 00136 if (constraintAssignment == NULL) 00137 return 0; 00138 return constraintAssignment->getNameInt(); 00139 } 00140 00142 00143 bool 00144 BFIGConstraintUnit::getParity() const 00145 { 00146 return parity; 00147 } 00148 00150 00151 /* 00152 bool 00153 BFIGConstraintUnit::isSatisfiedCurrent() const 00154 { 00155 if (constraintAssignment == NULL) 00156 return false; 00157 if (constraintAssignment->getVariable()->getCurrentAssignment() == NULL) 00158 { 00159 cerr << "Warning: ConstraintUnit " << (*this) << " depends on uncertain value!" << endl; 00160 return false; 00161 } 00162 return (constraintAssignment->isCurrent() == getParity()); 00163 } 00164 */ 00165 00167 00168 /* 00169 bool 00170 BFIGConstraintUnit::uncertain() const 00171 { 00172 if (constraintAssignment->getVariable()->getCurrentAssignment() == NULL) 00173 { 00174 return true; 00175 } 00176 return true; 00177 } 00178 */ 00179 00181 00182 /* 00183 bool 00184 BFIGConstraintUnit::maybeSatisfiedCurrent() const 00185 { 00186 if (constraintAssignment == NULL) 00187 return false; 00188 if (constraintAssignment->getVariable()->getCurrentAssignment() == NULL) 00189 { 00190 cerr << "Warning: ConstraintUnit " << (*this) << " depends on uncertain value!" << endl; 00191 return true; 00192 } 00193 return (constraintAssignment->isCurrent() == getParity()); 00194 } 00195 */ 00196 00198 00199 bool 00200 BFIGConstraintUnit::operator<(BFIGConstraintUnit const & c) const 00201 { 00202 assert(getVariable()); 00203 assert(c.getVariable()); 00204 if (getAssignment() == NULL && c.getAssignment() != NULL) 00205 return true; 00206 if (getAssignment() != NULL && c.getAssignment() == NULL) 00207 return false; 00208 if (getAssignment() == NULL && c.getAssignment() == NULL) 00209 return (getParity() && !(c.getParity())); 00210 if (getName() < c.getName()) 00211 return true; 00212 if (getName() > c.getName()) 00213 return false; 00214 if (getValue() < c.getValue()) 00215 return true; 00216 if (getValue() > c.getValue()) 00217 return false; 00218 if (getParity() && !(c.getParity())) 00219 return true; 00220 00221 return false; 00222 } 00223 00225 00226 /*# 00227 //bool 00228 //BFIGConstraintUnit::operator==(BFIGConstraintUnit const * c) const 00229 //{ 00230 // return *this == *c; 00231 //} 00232 */ 00233 00235 00236 bool 00237 BFIGConstraintUnit::operator==(BFIGConstraintUnit const & c) const 00238 { 00239 if (getAssignment() == NULL && c.getAssignment() != NULL) 00240 return false; 00241 if (getAssignment() != NULL && c.getAssignment() == NULL) 00242 return false; 00243 if (getParity() != c.getParity()) 00244 return false; 00245 if (getAssignment() == NULL && c.getAssignment() == NULL) 00246 return true; 00247 return *(getAssignment()) == *(c.getAssignment()); 00248 } 00249 00251 00252 BFIGConstraintUnit const & 00253 BFIGConstraintUnit::operator =(BFIGConstraintUnit const & c) 00254 { 00255 parity = c.parity; 00256 constraintAssignment = c.constraintAssignment; 00257 return *this; 00258 } 00259 00261 00262 String 00263 BFIGConstraintUnit::getDescription() const 00264 { 00265 String res = ""; 00266 if (! getParity()) 00267 res += "!"; 00268 if (getAssignment()) 00269 { 00270 res += getAssignment()->getNameString(); 00271 res += "="; 00272 res += getAssignment()->getValueString(); 00273 } else 00274 { 00275 res += "nil"; 00276 } 00277 return res; 00278 } 00279 00281 00282 namespace Mers { 00283 namespace Generators { 00284 namespace BFIG { 00285 00286 ostream& 00287 operator<<(ostream& os, BFIGConstraintUnit const & c) 00288 { 00289 // for now - seems reasonable. 00290 if (! c.getParity()) 00291 os << "!"; 00292 if (c.getAssignment()) 00293 os << *(c.getAssignment()); 00294 else 00295 os << "nil"; 00296 return os; 00297 } 00298 00299 } 00300 } 00301 } 00302 00304
1.5.0