Browse Source

Added NumberParser

tempestpy_adaptions
Matthias Volk 7 years ago
parent
commit
f81b6d4917
  1. 7
      src/storm-dft/parser/DFTGalileoParser.cpp
  2. 8
      src/storm/parser/DirectEncodingParser.cpp
  3. 8
      src/storm/parser/ValueParser.cpp
  4. 24
      src/storm/parser/ValueParser.h

7
src/storm-dft/parser/DFTGalileoParser.cpp

@ -5,7 +5,6 @@
#include <regex>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string/replace.hpp>
#include "storm/exceptions/NotImplementedException.h"
#include "storm/exceptions/FileIoException.h"
@ -114,12 +113,12 @@ namespace storm {
} else if (type == "or") {
success = builder.addOrElement(name, childNames);
} else if (boost::starts_with(type, "vot")) {
unsigned threshold = boost::lexical_cast<unsigned>(type.substr(3));
unsigned threshold = NumberParser<unsigned>::parse(type.substr(3));
success = builder.addVotElement(name, threshold, childNames);
} else if (type.find("of") != std::string::npos) {
size_t pos = type.find("of");
unsigned threshold = boost::lexical_cast<unsigned>(type.substr(0, pos));
unsigned count = boost::lexical_cast<unsigned>(type.substr(pos + 2));
unsigned threshold = NumberParser<unsigned>::parse(type.substr(0, pos));
unsigned count = NumberParser<unsigned>::parse(type.substr(pos + 2));
STORM_LOG_THROW(count == childNames.size(), storm::exceptions::WrongFormatException, "Voting gate number " << count << " does not correspond to number of children " << childNames.size() << "in line " << lineNo << ".");
success = builder.addVotElement(name, threshold, childNames);
} else if (type == "pand") {

8
src/storm/parser/DirectEncodingParser.cpp

@ -79,7 +79,7 @@ namespace storm {
if(line == "@nr_states") {
STORM_LOG_THROW(nrStates == 0, storm::exceptions::WrongFormatException, "Number states declared twice");
std::getline(file, line);
nrStates = boost::lexical_cast<size_t>(line);
nrStates = NumberParser<size_t>::parse(line);
}
if(line == "@model") {
@ -132,7 +132,7 @@ namespace storm {
size_t parsedId;
size_t posId = line.find(" ");
if (posId != std::string::npos) {
parsedId = boost::lexical_cast<size_t>(line.substr(0, posId));
parsedId = NumberParser<size_t>::parse(line.substr(0, posId));
// Parse rewards and labels
line = line.substr(posId+1);
@ -167,7 +167,7 @@ namespace storm {
}
} else {
// Only state id given
parsedId = boost::lexical_cast<size_t>(line);
parsedId = NumberParser<size_t>::parse(line);
}
STORM_LOG_TRACE("New state " << state);
STORM_LOG_ASSERT(state == parsedId, "State ids do not correspond.");
@ -201,7 +201,7 @@ namespace storm {
// New transition
size_t posColon = line.find(":");
STORM_LOG_ASSERT(posColon != std::string::npos, "':' not found.");
size_t target = boost::lexical_cast<size_t>(line.substr(2, posColon-3));
size_t target = NumberParser<size_t>::parse(line.substr(2, posColon-3));
std::string valueStr = line.substr(posColon+2);
ValueType value = valueParser.parseValue(valueStr);
STORM_LOG_TRACE("Transition " << row << " -> " << target << ": " << value);

8
src/storm/parser/ValueParser.cpp

@ -1,7 +1,6 @@
#include "storm/parser/ValueParser.h"
#include "storm/exceptions/NotSupportedException.h"
#include "storm/exceptions/WrongFormatException.h"
namespace storm {
namespace parser {
@ -21,12 +20,7 @@ namespace storm {
template<>
double ValueParser<double>::parseValue(std::string const& value) const {
try {
return boost::lexical_cast<double>(value);
}
catch(boost::bad_lexical_cast &) {
STORM_LOG_THROW(false, storm::exceptions::WrongFormatException, "Could not parse value '" << value << "'.");
}
return NumberParser<double>::parse(value);
}
template<>

24
src/storm/parser/ValueParser.h

@ -4,6 +4,7 @@
#include "storm/storage/expressions/ExpressionManager.h"
#include "storm/parser/ExpressionParser.h"
#include "storm/storage/expressions/ExpressionEvaluator.h"
#include "storm/exceptions/WrongFormatException.h"
namespace storm {
namespace parser {
@ -39,14 +40,31 @@ namespace storm {
private:
std::shared_ptr<storm::expressions::ExpressionManager> manager;
storm::parser::ExpressionParser parser;
storm::expressions::ExpressionEvaluator<ValueType> evaluator;
std::unordered_map<std::string, storm::expressions::Expression> identifierMapping;
};
template<typename NumberType>
class NumberParser {
public:
/*!
* Parse number from string.
*
* @param value String containing the value.
*
* @return NumberType.
*/
static NumberType parse(std::string const& value) {
try {
return boost::lexical_cast<NumberType>(value);
}
catch(boost::bad_lexical_cast &) {
STORM_LOG_THROW(false, storm::exceptions::WrongFormatException, "Could not parse value '" << value << "' into " << typeid(NumberType).name() << ".");
}
}
};
} // namespace parser
} // namespace storm

Loading…
Cancel
Save