Browse Source

Refactored some classes into templates

Former-commit-id: b495cf93d4
main
Mavo 10 years ago
parent
commit
4343b5b980
  1. 3
      src/builder/ExplicitDFTModelBuilder.cpp
  2. 21
      src/parser/DFTGalileoParser.cpp
  3. 10
      src/parser/DFTGalileoParser.h
  4. 4
      src/storage/dft/DFT.h
  5. 33
      src/storage/dft/DFTBuilder.cpp
  6. 7
      src/storage/dft/DFTBuilder.h
  7. 4
      src/storage/dft/DFTState.cpp
  8. 3
      src/storage/dft/DFTState.h
  9. 2
      src/storm-dyftee.cpp

3
src/builder/ExplicitDFTModelBuilder.cpp

@ -165,8 +165,7 @@ namespace storm {
} }
// Explicitly instantiate the class. // Explicitly instantiate the class.
template
class ExplicitDFTModelBuilder<double, storm::models::sparse::StandardRewardModel<double>, uint32_t>;
template class ExplicitDFTModelBuilder<double, storm::models::sparse::StandardRewardModel<double>, uint32_t>;
#ifdef STORM_HAVE_CARL #ifdef STORM_HAVE_CARL
template class ExplicitDFTModelBuilder<double, storm::models::sparse::StandardRewardModel<storm::Interval>, uint32_t>; template class ExplicitDFTModelBuilder<double, storm::models::sparse::StandardRewardModel<storm::Interval>, uint32_t>;

21
src/parser/DFTGalileoParser.cpp

@ -10,7 +10,9 @@
namespace storm { namespace storm {
namespace parser { namespace parser {
storm::storage::DFT DFTGalileoParser::parseDFT(const std::string& filename) {
template<typename ValueType>
storm::storage::DFT DFTGalileoParser<ValueType>::parseDFT(const std::string& filename) {
if(readFile(filename)) { if(readFile(filename)) {
storm::storage::DFT dft = mBuilder.build(); storm::storage::DFT dft = mBuilder.build();
STORM_LOG_DEBUG("Elements:" << std::endl << dft.getElementsString()); STORM_LOG_DEBUG("Elements:" << std::endl << dft.getElementsString());
@ -20,8 +22,9 @@ namespace storm {
throw storm::exceptions::FileIoException(); throw storm::exceptions::FileIoException();
} }
} }
std::string stripQuotsFromName(std::string const& name) {
template<typename ValueType>
std::string DFTGalileoParser<ValueType>::stripQuotsFromName(std::string const& name) {
size_t firstQuots = name.find("\""); size_t firstQuots = name.find("\"");
size_t secondQuots = name.find("\"", firstQuots+1); size_t secondQuots = name.find("\"", firstQuots+1);
@ -33,8 +36,9 @@ namespace storm {
return name.substr(firstQuots+1,secondQuots-1); return name.substr(firstQuots+1,secondQuots-1);
} }
} }
bool DFTGalileoParser::readFile(const std::string& filename) {
template<typename ValueType>
bool DFTGalileoParser<ValueType>::readFile(const std::string& filename) {
// constants // constants
std::string topleveltoken = "toplevel"; std::string topleveltoken = "toplevel";
std::string toplevelId; std::string toplevelId;
@ -108,6 +112,13 @@ namespace storm {
file.close(); file.close();
return generalSuccess; return generalSuccess;
} }
// Explicitly instantiate the class.
template class DFTGalileoParser<double>;
#ifdef STORM_HAVE_CARL
template class DFTGalileoParser<RationalFunction>;
#endif
} }
} }

10
src/parser/DFTGalileoParser.h

@ -8,16 +8,18 @@
namespace storm { namespace storm {
namespace parser { namespace parser {
template<typename ValueType>
class DFTGalileoParser { class DFTGalileoParser {
storm::storage::DFTBuilder mBuilder;
storm::storage::DFTBuilder<ValueType> mBuilder;
public: public:
storm::storage::DFT parseDFT(std::string const& filename); storm::storage::DFT parseDFT(std::string const& filename);
private: private:
bool readFile(std::string const& filename); bool readFile(std::string const& filename);
};
std::string stripQuotsFromName(std::string const& name);
};
} }
} }

4
src/storage/dft/DFT.h

@ -131,12 +131,14 @@ namespace storm {
assert(index < nrElements()); assert(index < nrElements());
return mElements[index]; return mElements[index];
} }
// TODO Matthias: template
std::shared_ptr<DFTBE<double>> getBasicElement(size_t index) const { std::shared_ptr<DFTBE<double>> getBasicElement(size_t index) const {
assert(mElements[index]->isBasicElement()); assert(mElements[index]->isBasicElement());
return std::static_pointer_cast<DFTBE<double>>(mElements[index]); return std::static_pointer_cast<DFTBE<double>>(mElements[index]);
} }
// TODO Matthias: template
std::vector<std::shared_ptr<DFTBE<double>>> getBasicElements() const { std::vector<std::shared_ptr<DFTBE<double>>> getBasicElements() const {
std::vector<std::shared_ptr<DFTBE<double>>> elements; std::vector<std::shared_ptr<DFTBE<double>>> elements;
for (std::shared_ptr<storm::storage::DFTElement> elem : mElements) { for (std::shared_ptr<storm::storage::DFTElement> elem : mElements) {

33
src/storage/dft/DFTBuilder.cpp

@ -12,8 +12,9 @@
namespace storm { namespace storm {
namespace storage { namespace storage {
DFT DFTBuilder::build() {
template<typename ValueType>
DFT DFTBuilder<ValueType>::build() {
for(auto& elem : mChildNames) { for(auto& elem : mChildNames) {
for(auto const& child : elem.second) { for(auto const& child : elem.second) {
std::shared_ptr<DFTGate> gate = std::static_pointer_cast<DFTGate>(elem.first); std::shared_ptr<DFTGate> gate = std::static_pointer_cast<DFTGate>(elem.first);
@ -36,8 +37,9 @@ namespace storm {
} }
return DFT(elems, mElements[topLevelIdentifier]); return DFT(elems, mElements[topLevelIdentifier]);
} }
unsigned DFTBuilder::computeRank(std::shared_ptr<DFTElement> const& elem) {
template<typename ValueType>
unsigned DFTBuilder<ValueType>::computeRank(std::shared_ptr<DFTElement> const& elem) {
if(elem->rank() == -1) { if(elem->rank() == -1) {
if(elem->nrChildren() == 0) { if(elem->nrChildren() == 0) {
elem->setRank(0); elem->setRank(0);
@ -58,8 +60,9 @@ namespace storm {
return elem->rank(); return elem->rank();
} }
bool DFTBuilder::addStandardGate(std::string const& name, std::vector<std::string> const& children, DFTElementTypes tp) {
template<typename ValueType>
bool DFTBuilder<ValueType>::addStandardGate(std::string const& name, std::vector<std::string> const& children, DFTElementTypes tp) {
assert(children.size() > 0); assert(children.size() > 0);
if(mElements.count(name) != 0) { if(mElements.count(name) != 0) {
// Element with that name already exists. // Element with that name already exists.
@ -97,9 +100,9 @@ namespace storm {
mChildNames[element] = children; mChildNames[element] = children;
return true; return true;
} }
void DFTBuilder::topoVisit(std::shared_ptr<DFTElement> const& n, std::map<std::shared_ptr<DFTElement>, topoSortColour>& visited, std::vector<std::shared_ptr<DFTElement>>& L) {
template<typename ValueType>
void DFTBuilder<ValueType>::topoVisit(std::shared_ptr<DFTElement> const& n, std::map<std::shared_ptr<DFTElement>, topoSortColour>& visited, std::vector<std::shared_ptr<DFTElement>>& L) {
if(visited[n] == topoSortColour::GREY) { if(visited[n] == topoSortColour::GREY) {
throw storm::exceptions::WrongFormatException("DFT is cyclic"); throw storm::exceptions::WrongFormatException("DFT is cyclic");
} else if(visited[n] == topoSortColour::WHITE) { } else if(visited[n] == topoSortColour::WHITE) {
@ -114,7 +117,8 @@ namespace storm {
} }
} }
std::vector<std::shared_ptr<DFTElement>> DFTBuilder::topoSort() {
template<typename ValueType>
std::vector<std::shared_ptr<DFTElement>> DFTBuilder<ValueType>::topoSort() {
std::map<std::shared_ptr<DFTElement>, topoSortColour> visited; std::map<std::shared_ptr<DFTElement>, topoSortColour> visited;
for(auto const& e : mElements) { for(auto const& e : mElements) {
visited.insert(std::make_pair(e.second, topoSortColour::WHITE)); visited.insert(std::make_pair(e.second, topoSortColour::WHITE));
@ -126,7 +130,14 @@ namespace storm {
} }
//std::reverse(L.begin(), L.end()); //std::reverse(L.begin(), L.end());
return L; return L;
}
}
// Explicitly instantiate the class.
template class DFTBuilder<double>;
#ifdef STORM_HAVE_CARL
template class DFTBuilder<RationalFunction>;
#endif
} }
} }

7
src/storage/dft/DFTBuilder.h

@ -10,7 +10,8 @@
namespace storm { namespace storm {
namespace storage { namespace storage {
class DFT; class DFT;
template<typename ValueType>
class DFTBuilder { class DFTBuilder {
std::size_t mNextId = 0; std::size_t mNextId = 0;
@ -69,7 +70,7 @@ namespace storm {
return true; return true;
} }
bool addBasicElement(std::string const& name, double failureRate, double dormancyFactor) {
bool addBasicElement(std::string const& name, ValueType failureRate, ValueType dormancyFactor) {
if(failureRate <= 0.0) { if(failureRate <= 0.0) {
std::cerr << "Failure rate must be positive." << std::endl; std::cerr << "Failure rate must be positive." << std::endl;
return false; return false;
@ -80,7 +81,7 @@ namespace storm {
return false; return false;
} }
mElements[name] = std::make_shared<DFTBE<double>>(mNextId++, name, failureRate, dormancyFactor);
mElements[name] = std::make_shared<DFTBE<ValueType>>(mNextId++, name, failureRate, dormancyFactor);
return true; return true;
} }

4
src/storage/dft/DFTState.cpp

@ -65,8 +65,8 @@ namespace storm {
} }
} }
std::pair<std::shared_ptr<DFTBE<double>>, bool> DFTState::letNextBEFail(size_t index)
// TODO Matthias: template
std::pair<std::shared_ptr<DFTBE<double>>, bool> DFTState::letNextBEFail(size_t index)
{ {
assert(index < mIsCurrentlyFailableBE.size()); assert(index < mIsCurrentlyFailableBE.size());
STORM_LOG_TRACE("currently failable: " << getCurrentlyFailableString()); STORM_LOG_TRACE("currently failable: " << getCurrentlyFailableString());

3
src/storage/dft/DFTState.h

@ -105,7 +105,8 @@ namespace storm {
size_t nrFailableBEs() const { size_t nrFailableBEs() const {
return mIsCurrentlyFailableBE.size(); return mIsCurrentlyFailableBE.size();
} }
// TODO Matthias: template
std::pair<std::shared_ptr<DFTBE<double>>, bool> letNextBEFail(size_t smallestIndex = 0); std::pair<std::shared_ptr<DFTBE<double>>, bool> letNextBEFail(size_t smallestIndex = 0);
std::string getCurrentlyFailableString() { std::string getCurrentlyFailableString() {

2
src/storm-dyftee.cpp

@ -19,7 +19,7 @@ int main(int argc, char** argv) {
logger.getAppender("mainConsoleAppender")->setThreshold(level); logger.getAppender("mainConsoleAppender")->setThreshold(level);
std::cout << "Parsing DFT file..." << std::endl; std::cout << "Parsing DFT file..." << std::endl;
storm::parser::DFTGalileoParser parser;
storm::parser::DFTGalileoParser<double> parser;
storm::storage::DFT dft = parser.parseDFT(argv[1]); storm::storage::DFT dft = parser.parseDFT(argv[1]);
std::cout << "Built data structure" << std::endl; std::cout << "Built data structure" << std::endl;

Loading…
Cancel
Save