Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "wire/core/PropertySet.h"
00010 #include "wire/core/Property.h"
00011 #include "wire/models/FixedState.h"
00012
00013 using namespace std;
00014
00015 namespace mhf {
00016
00017 int PropertySet::N_PROPERTY_SET = 0;
00018
00019 PropertySet::PropertySet(Time timestamp) : timestamp_(timestamp) {
00020 ++N_PROPERTY_SET;
00021 }
00022
00023 PropertySet::~PropertySet() {
00024 for(map<Attribute, Property*>::iterator it = properties_.begin(); it != properties_.end(); ++it) {
00025 delete it->second;
00026 }
00027
00028 --N_PROPERTY_SET;
00029 }
00030
00031 PropertySet::PropertySet(const PropertySet& orig) : timestamp_(orig.timestamp_) {
00032 for(map<Attribute, Property*>::const_iterator it = orig.properties_.begin(); it != orig.properties_.end(); ++it) {
00033 properties_[it->first] = it->second->clone();
00034 }
00035 }
00036
00037 PropertySet* PropertySet::clone() const {
00038 return new PropertySet(*this);
00039 }
00040
00041 void PropertySet::addProperty(const Attribute& att, const pbl::PDF& value) {
00042 map<Attribute, Property*>::iterator it = properties_.find(att);
00043 if (it == properties_.end()) {
00044 properties_[att] = new Property(att, FixedState(value));
00045 } else {
00046 delete it->second;
00047 it->second = new Property(att, FixedState(value));
00048 }
00049 }
00050
00051 void PropertySet::addProperty(const string& att, const pbl::PDF& value) {
00052 addProperty(AttributeConv::attribute(att), value);
00053 }
00054
00055 void PropertySet::addProperty(const Attribute& att, const IStateEstimator& estimator) {
00056 map<Attribute, Property*>::iterator it = properties_.find(att);
00057 if (it == properties_.end()) {
00058 properties_[att] = new Property(att, estimator);
00059 } else {
00060 delete it->second;
00061 it->second = new Property(att, estimator);
00062 }
00063 }
00064
00065 void PropertySet::addProperty(Property* property) {
00066 map<Attribute, Property*>::iterator it = properties_.find(property->getAttribute());
00067 if (it == properties_.end()) {
00068 properties_[property->getAttribute()] = property;
00069 } else {
00070 delete it->second;
00071 it->second = property;
00072 }
00073 }
00074
00075 const Property* PropertySet::getProperty(const Attribute& attribute) const {
00076 map<Attribute, Property*>::const_iterator it = properties_.find(attribute);
00077 if (it != properties_.end()) {
00078 return it->second;
00079 }
00080 return 0;
00081 }
00082
00083 Property* PropertySet::getProperty(const Attribute& attribute) {
00084 map<Attribute, Property*>::iterator it = properties_.find(attribute);
00085 if (it != properties_.end()) {
00086 return it->second;
00087 }
00088 return 0;
00089 }
00090
00091 const Property* PropertySet::getProperty(const std::string& attribute) const {
00092 return getProperty(AttributeConv::attribute(attribute));
00093 }
00094
00095 void PropertySet::propagate(const Time& time) {
00096 if (fabs(time - timestamp_) < 0.001) {
00097 return;
00098 }
00099
00100 for(map<Attribute, Property*>::iterator it = properties_.begin(); it != properties_.end(); ++it) {
00101 it->second->propagate(time);
00102 }
00103
00104 timestamp_ = time;
00105 }
00106
00107 void PropertySet::update(const pbl::PDF& z, const Time& time) {
00108 assert(false);
00109 }
00110
00111 void PropertySet::reset() {
00112 for(map<Attribute, Property*>::iterator it = properties_.begin(); it != properties_.end(); ++it) {
00113 it->second->reset();
00114 }
00115 }
00116
00117 const pbl::PDF& PropertySet::getValue() const {
00118 assert(false);
00119 }
00120
00121 double PropertySet::getLikelihood(const PropertySet& P) const {
00122 double likelihood = 1;
00123
00124 const map<Attribute, Property*>& other_props = P.properties_;
00125
00126 for(map<Attribute, Property*>::const_iterator it = other_props.begin(); it != other_props.end(); ++it) {
00127
00128 const Attribute& attribute = it->first;
00129 const Property* other_prop = it->second;
00130
00131 const Property* this_prop = getProperty(attribute);
00132
00133 if (this_prop) {
00134
00135
00136
00137
00138
00139
00140
00141
00142 likelihood *= this_prop->getLikelihood(other_prop->getValue());
00143 } else {
00144 printf("Error during likelihood calculation: property '%s' is not in property set.\n", AttributeConv::attribute_str(attribute).c_str());
00145
00146 printf("This (%p) constains:\n", this);
00147 for(map<Attribute, Property*>::const_iterator it = properties_.begin(); it != properties_.end(); ++it) {
00148 printf(" - %s\n", AttributeConv::attribute_str(it->first).c_str());
00149 }
00150
00151 printf("Other (%p) constains:\n", &P);
00152 for(map<Attribute, Property*>::const_iterator it = other_props.begin(); it != other_props.end(); ++it) {
00153 printf(" - %s\n", AttributeConv::attribute_str(it->first).c_str());
00154 }
00155 }
00156
00157 }
00158
00159 return likelihood;
00160 }
00161
00162 const std::map<Attribute, Property*>& PropertySet::getPropertyMap() const {
00163 return properties_;
00164 }
00165
00166 Time PropertySet::getTimestamp() const {
00167 return timestamp_;
00168 }
00169
00170 string PropertySet::toString() const {
00171 stringstream s;
00172 for(map<Attribute, Property*>::const_iterator it = properties_.begin(); it != properties_.end(); ++it) {
00173 s << " - " << AttributeConv::attribute_str(it->first) << endl;
00174 }
00175 return s.str();
00176 }
00177
00178 }