Add a Techno::getRule() method and simply Techno::getValue() & Techno::getValueAsString() methods

This commit is contained in:
Damien Dupuis 2010-06-15 11:52:13 +00:00
parent d0b2c0c6eb
commit e3bdbfb7f0
3 changed files with 15 additions and 36 deletions

View File

@ -13,7 +13,8 @@ void translator(DTRException const& e) {
} }
// specify that Techno::getValue & Techno::getValueAsString methods have optional arguments // specify that Techno::getRule & Techno::getValue & Techno::getValueAsString methods have optional arguments
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(getRule_overloads , getRule , 1, 3);
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(getValue_overloads , getValue , 1, 3); BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(getValue_overloads , getValue , 1, 3);
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(getValueAsString_overloads, getValueAsString, 1, 3); BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(getValueAsString_overloads, getValueAsString, 1, 3);
// specify that Techno::addRule method has optional arguments // specify that Techno::addRule method has optional arguments
@ -49,7 +50,8 @@ BOOST_PYTHON_MODULE(pyDTR) {
// accessors // accessors
.def("getName" , &Techno::getName) .def("getName" , &Techno::getName)
.def("getUnit" , &Techno::getUnit) .def("getUnit" , &Techno::getUnit)
.def("getValue" , &Techno::getValue, getValue_overloads()) .def("getRule" , &Techno::getRule , getRule_overloads()[return_value_policy<reference_existing_object>()])
.def("getValue" , &Techno::getValue , getValue_overloads())
.def("getValueAsString", &Techno::getValueAsString, getValueAsString_overloads()) .def("getValueAsString", &Techno::getValueAsString, getValueAsString_overloads())
// modifiers // modifiers

View File

@ -46,7 +46,7 @@ ARule* Techno::addARule (Name name, double value, Name ref, Name layer1, Name la
return arule; return arule;
} }
double Techno::getValue(Name name, Name layer1, Name layer2) { Rule* Techno::getRule(Name name, Name layer1, Name layer2) {
bool testL1 = (layer1 == Name("")) ? false : true; bool testL1 = (layer1 == Name("")) ? false : true;
bool testL2 = (layer2 == Name("")) ? false : true; bool testL2 = (layer2 == Name("")) ? false : true;
@ -57,18 +57,18 @@ double Techno::getValue(Name name, Name layer1, Name layer2) {
if (rule->getLayer1() == layer1) { if (rule->getLayer1() == layer1) {
if (testL2) { if (testL2) {
if (rule->getLayer2() == layer2) { if (rule->getLayer2() == layer2) {
return rule->getValue(); return rule;
} }
} else { } else {
return rule->getValue(); return rule;
} }
} }
} else { } else {
return rule->getValue(); return rule;
} }
} }
} }
string error ("[ERROR] Could not found rule: "); string error ("[ERROR] Could not find rule: ");
error += name.getString(); error += name.getString();
error += "."; error += ".";
error += layer1.getString(); error += layer1.getString();
@ -78,36 +78,12 @@ double Techno::getValue(Name name, Name layer1, Name layer2) {
throw DTRException(error); throw DTRException(error);
} }
string Techno::getValueAsString(Name name, Name layer1, Name layer2) { double Techno::getValue(Name name, Name layer1, Name layer2) {
bool testL1 = (layer1 == Name("")) ? false : true; return getRule(name, layer1, layer2)->getValue();
bool testL2 = (layer2 == Name("")) ? false : true; }
for (size_t i = 0 ; i < _rules.size() ; i++) { string Techno::getValueAsString(Name name, Name layer1, Name layer2) {
Rule* rule = _rules[i]; return getRule(name, layer1, layer2)->getValueAsString();
if (rule->getName() == name) {
if (testL1) {
if (rule->getLayer1() == layer1) {
if (testL2) {
if (rule->getLayer2() == layer2) {
return rule->getValueAsString();
}
} else {
return rule->getValueAsString();
}
}
} else {
return rule->getValueAsString();
}
}
}
string error ("[ERROR] Could not found rule: ");
error += name.getString();
error += ".";
error += layer1.getString();
error += ".";
error += layer2.getString();
error += ".";
throw DTRException(error);
} }
Techno* Techno::readFromFile(const string filePath) { Techno* Techno::readFromFile(const string filePath) {

View File

@ -31,6 +31,7 @@ class Techno {
Rule* addRule (Name name, double value, Name ref, Name layer1=Name(""), Name layer2=Name("")); Rule* addRule (Name name, double value, Name ref, Name layer1=Name(""), Name layer2=Name(""));
ARule* addARule(Name name, double value, Name ref, Name layer1 , Name layer2); ARule* addARule(Name name, double value, Name ref, Name layer1 , Name layer2);
Rule* getRule(Name name, Name layer1=Name(""), Name layer2=Name(""));
double getValue(Name name, Name layer1=Name(""), Name layer2=Name("")); double getValue(Name name, Name layer1=Name(""), Name layer2=Name(""));
std::string getValueAsString(Name name, Name layer1=Name(""), Name layer2=Name("")); std::string getValueAsString(Name name, Name layer1=Name(""), Name layer2=Name(""));