Adding getValueAsString method on Rule, ARule and Techno and export it in python module.

This is a convenient method proposed by Corinne Laurey.
This commit is contained in:
Damien Dupuis 2010-06-11 11:27:15 +00:00
parent eac3f7a932
commit 0774138292
4 changed files with 87 additions and 28 deletions

View File

@ -1,3 +1,5 @@
using namespace std;
#include <boost/python.hpp> #include <boost/python.hpp>
using namespace boost::python; using namespace boost::python;
@ -17,11 +19,12 @@ BOOST_PYTHON_MODULE(pyDTR) {
; ;
// class DTR::Rule // class DTR::Rule
class_<Rule>("Rule", init<Name, double, Name, Name, Name>()) class_<Rule>("Rule", init<Name, double, Name, Name, Name, Name>())
// accessors // accessors
.def("getName" , &Rule::getName ) .def("getName" , &Rule::getName )
.def("getType" , &Rule::getType ) .def("getType" , &Rule::getType )
.def("getValue" , &Rule::getValue ) .def("getValue" , &Rule::getValue )
.def("getValueAsString", &Rule::getValueAsString)
.def("getRef" , &Rule::getRef ) .def("getRef" , &Rule::getRef )
.def("getLayer1" , &Rule::getLayer1 ) .def("getLayer1" , &Rule::getLayer1 )
.def("getLayer2" , &Rule::getLayer2 ) .def("getLayer2" , &Rule::getLayer2 )
@ -30,7 +33,7 @@ BOOST_PYTHON_MODULE(pyDTR) {
; ;
// class DTR::ARule derived from DTR::Rule // class DTR::ARule derived from DTR::Rule
class_<ARule, bases<Rule> >("ARule", init<Name, double, Name, Name, Name>()) class_<ARule, bases<Rule> >("ARule", init<Name, double, Name, Name, Name, Name>())
; ;
// class DTR::Techno // class DTR::Techno
@ -43,6 +46,9 @@ BOOST_PYTHON_MODULE(pyDTR) {
.def("getValue" , static_cast<double(Techno::*)(Name )>(&Techno::getValue)) .def("getValue" , static_cast<double(Techno::*)(Name )>(&Techno::getValue))
.def("getValue" , static_cast<double(Techno::*)(Name, Name )>(&Techno::getValue)) .def("getValue" , static_cast<double(Techno::*)(Name, Name )>(&Techno::getValue))
.def("getValue" , static_cast<double(Techno::*)(Name, Name, Name)>(&Techno::getValue)) .def("getValue" , static_cast<double(Techno::*)(Name, Name, Name)>(&Techno::getValue))
.def("getValueAsString", static_cast<string(Techno::*)(Name )>(&Techno::getValueAsString))
.def("getValueAsString", static_cast<string(Techno::*)(Name, Name )>(&Techno::getValueAsString))
.def("getValueAsString", static_cast<string(Techno::*)(Name, Name, Name)>(&Techno::getValueAsString))
; ;
// DTRException translator // DTRException translator

View File

@ -34,14 +34,14 @@ namespace {
namespace DTR { namespace DTR {
Techno::Techno(Name name, Name unit) : _name(name), _unit(unit) {} Techno::Techno(Name name, Name unit) : _name(name), _unit(unit) {}
Rule* Techno::addRule (Name name, double value, Name ref, Name layer1, Name layer2) { Rule* Techno::addRule (Name name, double value, Name valueStr, Name ref, Name layer1, Name layer2) {
Rule* rule = new Rule(name, value, ref, layer1, layer2); Rule* rule = new Rule(name, value, valueStr, ref, layer1, layer2);
_rules.push_back(rule); _rules.push_back(rule);
return rule; return rule;
} }
ARule* Techno::addARule (Name name, double value, Name ref, Name layer1, Name layer2) { ARule* Techno::addARule (Name name, double value, Name valueStr, Name ref, Name layer1, Name layer2) {
ARule* arule = new ARule(name, value, ref, layer1, layer2); ARule* arule = new ARule(name, value, valueStr, ref, layer1, layer2);
_rules.push_back(arule); _rules.push_back(arule);
return arule; return arule;
} }
@ -86,6 +86,46 @@ double Techno::getValue(Name name, Name layer1, Name layer2) {
throw DTRException(error); throw DTRException(error);
} }
string Techno::getValueAsString(Name name) {
return getValueAsString(name, Name(""), Name(""));
}
string Techno::getValueAsString(Name name, Name layer) {
return getValueAsString(name, layer, Name(""));
}
string Techno::getValueAsString(Name name, Name layer1, Name layer2) {
bool testL1 = (layer1 == Name("")) ? false : true;
bool testL2 = (layer2 == Name("")) ? false : true;
for (size_t i = 0 ; i < _rules.size() ; i++) {
Rule* rule = _rules[i];
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) {
LIBXML_TEST_VERSION; LIBXML_TEST_VERSION;
Techno* techno = NULL; Techno* techno = NULL;
@ -136,18 +176,21 @@ Techno* Techno::readFromFile(const string filePath) {
Name layer1 ((const char*)layer1C); Name layer1 ((const char*)layer1C);
Name layer2 ((const char*)layer2C); Name layer2 ((const char*)layer2C);
double value = ::getValue<double>(valueC); double value = ::getValue<double>(valueC);
rule = techno->addRule(name, value, ref, layer1, layer2); Name valueStr ((const char*)valueC);
rule = techno->addRule(name, value, valueStr, ref, layer1, layer2);
} else if (nameC && layerC && valueC && refC) {// rule with only one layer } else if (nameC && layerC && valueC && refC) {// rule with only one layer
Name name ((const char*)nameC); Name name ((const char*)nameC);
Name ref ((const char*)refC); Name ref ((const char*)refC);
Name layer ((const char*)layerC); Name layer ((const char*)layerC);
double value = ::getValue<double>(valueC); double value = ::getValue<double>(valueC);
rule = techno->addRule(name, value, ref, layer); Name valueStr ((const char*)valueC);
rule = techno->addRule(name, value, valueStr, ref, layer);
} else if (nameC && valueC && refC) { // rule without layer } else if (nameC && valueC && refC) { // rule without layer
Name name ((const char*)nameC); Name name ((const char*)nameC);
Name ref ((const char*)refC); Name ref ((const char*)refC);
double value = ::getValue<double>(valueC); double value = ::getValue<double>(valueC);
rule = techno->addRule(name, value, ref); Name valueStr ((const char*)valueC);
rule = techno->addRule(name, value, valueStr, ref);
} else { // invalid case } else { // invalid case
throw DTRException("[ERROR] properties of 'rule' node must be ('name', 'value', 'ref') or ('name', 'layer', 'value', 'ref') or ('name', 'layer1', 'layer2', 'value', 'ref')."); throw DTRException("[ERROR] properties of 'rule' node must be ('name', 'value', 'ref') or ('name', 'layer', 'value', 'ref') or ('name', 'layer1', 'layer2', 'value', 'ref').");
return NULL; return NULL;
@ -168,7 +211,8 @@ Techno* Techno::readFromFile(const string filePath) {
Name layer2 ((const char*)layer2C); Name layer2 ((const char*)layer2C);
Name ref ((const char*)refC); Name ref ((const char*)refC);
double value = ::getValue<double>(valueC); double value = ::getValue<double>(valueC);
techno->addARule(name, value, ref, layer1, layer2); Name valueStr ((const char*)valueC);
techno->addARule(name, value, valueStr, ref, layer1, layer2);
} else { } else {
throw DTRException("[ERROR] 'arule' node must have 'name', 'layer1', 'layer2', 'value' and 'ref' properties."); throw DTRException("[ERROR] 'arule' node must have 'name', 'layer1', 'layer2', 'value' and 'ref' properties.");
return NULL; return NULL;

View File

@ -10,16 +10,19 @@
#ifndef __DTR_RULES_H__ #ifndef __DTR_RULES_H__
#define __DTR_RULES_H__ #define __DTR_RULES_H__
#include "vlsisapd/dtr/Name.h"
namespace DTR { namespace DTR {
class Rule { class Rule {
public: public:
Rule(Name name, double value, Name ref, Name layer1, Name layer2) Rule(Name name, double value, Name valueStr, Name ref, Name layer1, Name layer2)
: _name(name), _type(Name("")), _value(value), _ref(ref), _layer1(layer1), _layer2(layer2) {}; : _name(name), _type(Name("")), _value(value), _valueStr(valueStr), _ref(ref), _layer1(layer1), _layer2(layer2) {};
// accessors // accessors
inline Name getName(); inline Name getName();
inline Name getType(); inline Name getType();
inline double getValue(); inline double getValue();
inline std::string getValueAsString();
inline Name getRef(); inline Name getRef();
inline Name getLayer1(); inline Name getLayer1();
virtual inline Name getLayer2(); // add virtual so the Rule object is polymorphic virtual inline Name getLayer2(); // add virtual so the Rule object is polymorphic
@ -32,6 +35,7 @@ class Rule {
Name _name; Name _name;
Name _type; Name _type;
double _value; double _value;
Name _valueStr;
Name _ref; Name _ref;
Name _layer1; Name _layer1;
Name _layer2; Name _layer2;
@ -39,8 +43,8 @@ class Rule {
class ARule : public Rule { class ARule : public Rule {
public: public:
ARule(Name name, double value, Name ref, Name layer1, Name layer2) ARule(Name name, double value, Name valueStr, Name ref, Name layer1, Name layer2)
: Rule(name, value, ref, layer1, layer2) {}; : Rule(name, value, valueStr, ref, layer1, layer2) {};
}; };
inline Name Rule::getName() { return _name; }; inline Name Rule::getName() { return _name; };
@ -49,6 +53,7 @@ inline double Rule::getValue() { return _value; };
inline Name Rule::getRef() { return _ref; }; inline Name Rule::getRef() { return _ref; };
inline Name Rule::getLayer1() { return _layer1; }; inline Name Rule::getLayer1() { return _layer1; };
inline Name Rule::getLayer2() { return _layer2; }; inline Name Rule::getLayer2() { return _layer2; };
inline std::string Rule::getValueAsString() { return _valueStr.getString(); };
inline void Rule::setType(Name type) { _type = type; }; inline void Rule::setType(Name type) { _type = type; };
} // namespace DTR } // namespace DTR

View File

@ -28,13 +28,17 @@ class Techno {
inline Name getUnit(); inline Name getUnit();
inline std::vector<Rule*>& getRules(); inline std::vector<Rule*>& getRules();
Rule* addRule (Name name, double value, Name ref, Name layer1=Name(""), Name layer2=Name("")); Rule* addRule (Name name, double value, Name valueStr, 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 valueStr, Name ref, Name layer1 , Name layer2);
double getValue(Name name); double getValue(Name name);
double getValue(Name name, Name layer); double getValue(Name name, Name layer);
double getValue(Name name, Name layer1, Name layer2); double getValue(Name name, Name layer1, Name layer2);
std::string getValueAsString(Name name);
std::string getValueAsString(Name name, Name layer);
std::string getValueAsString(Name name, Name layer1, Name layer2);
bool writeToFile(std::string filePath); bool writeToFile(std::string filePath);
static Techno* readFromFile(const std::string filePath); static Techno* readFromFile(const std::string filePath);