Browse Source

Moved ValueParser to separate file

tempestpy_adaptions
Matthias Volk 7 years ago
parent
commit
c0481ab72d
  1. 30
      src/storm/parser/DirectEncodingParser.cpp
  2. 41
      src/storm/parser/DirectEncodingParser.h
  3. 39
      src/storm/parser/ValueParser.cpp
  4. 53
      src/storm/parser/ValueParser.h

30
src/storm/parser/DirectEncodingParser.cpp

@ -25,32 +25,6 @@
namespace storm { namespace storm {
namespace parser { namespace parser {
template<typename ValueType>
void ValueParser<ValueType>::addParameter(std::string const& parameter) {
STORM_LOG_THROW(false, storm::exceptions::NotSupportedException, "Parameters are not supported in this build.");
}
template<>
void ValueParser<storm::RationalFunction>::addParameter(std::string const& parameter) {
//STORM_LOG_THROW((std::is_same<ValueType, storm::RationalFunction>::value), storm::exceptions::NotSupportedException, "Parameters only allowed when using rational functions.");
storm::expressions::Variable var = manager->declareRationalVariable(parameter);
identifierMapping.emplace(var.getName(), var);
parser.setIdentifierMapping(identifierMapping);
STORM_LOG_TRACE("Added parameter: " << var.getName());
}
template<>
double ValueParser<double>::parseValue(std::string const& value) const {
return boost::lexical_cast<double>(value);
}
template<>
storm::RationalFunction ValueParser<storm::RationalFunction>::parseValue(std::string const& value) const {
storm::RationalFunction rationalFunction = evaluator.asRational(parser.parseFromString(value));
STORM_LOG_TRACE("Parsed expression: " << rationalFunction);
return rationalFunction;
}
template<typename ValueType, typename RewardModelType> template<typename ValueType, typename RewardModelType>
std::shared_ptr<storm::models::sparse::Model<ValueType, RewardModelType>> DirectEncodingParser<ValueType, RewardModelType>::parseModel(std::string const& filename) { std::shared_ptr<storm::models::sparse::Model<ValueType, RewardModelType>> DirectEncodingParser<ValueType, RewardModelType>::parseModel(std::string const& filename) {
@ -253,9 +227,7 @@ namespace storm {
// Template instantiations. // Template instantiations.
template class DirectEncodingParser<double>; template class DirectEncodingParser<double>;
#ifdef STORM_HAVE_CARL
template class DirectEncodingParser<storm::RationalFunction>; template class DirectEncodingParser<storm::RationalFunction>;
#endif
} // namespace parser } // namespace parser
} // namespace storm } // namespace storm

41
src/storm/parser/DirectEncodingParser.h

@ -1,53 +1,14 @@
#ifndef STORM_PARSER_DIRECTENCODINGPARSER_H_ #ifndef STORM_PARSER_DIRECTENCODINGPARSER_H_
#define STORM_PARSER_DIRECTENCODINGPARSER_H_ #define STORM_PARSER_DIRECTENCODINGPARSER_H_
#include "storm/parser/ValueParser.h"
#include "storm/models/sparse/Model.h" #include "storm/models/sparse/Model.h"
#include "storm/models/sparse/StandardRewardModel.h" #include "storm/models/sparse/StandardRewardModel.h"
#include "storm/storage/expressions/ExpressionManager.h"
#include "storm/parser/ExpressionParser.h"
#include "storm/storage/expressions/ExpressionEvaluator.h"
#include "storm/storage/sparse/ModelComponents.h" #include "storm/storage/sparse/ModelComponents.h"
namespace storm { namespace storm {
namespace parser { namespace parser {
/*!
* Parser for values according to their ValueType.
*/
template<typename ValueType>
class ValueParser {
public:
ValueParser() : manager(new storm::expressions::ExpressionManager()), parser(*manager), evaluator(*manager) {
}
/*!
* Parse ValueType from string.
*
* @param value String containing the value.
*
* @return ValueType
*/
ValueType parseValue(std::string const& value) const;
/*!
* Add declaration of parameter.
*
* @param parameter New parameter.
*/
void addParameter(std::string const& parameter);
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;
};
/*! /*!
* Parser for models in the DRN format with explicit encoding. * Parser for models in the DRN format with explicit encoding.
*/ */

39
src/storm/parser/ValueParser.cpp

@ -0,0 +1,39 @@
#include "storm/parser/ValueParser.h"
#include "storm/exceptions/NotSupportedException.h"
namespace storm {
namespace parser {
template<typename ValueType>
void ValueParser<ValueType>::addParameter(std::string const& parameter) {
STORM_LOG_THROW(false, storm::exceptions::NotSupportedException, "Parameters are not supported in this build.");
}
template<>
void ValueParser<storm::RationalFunction>::addParameter(std::string const& parameter) {
//STORM_LOG_THROW((std::is_same<ValueType, storm::RationalFunction>::value), storm::exceptions::NotSupportedException, "Parameters only allowed when using rational functions.");
storm::expressions::Variable var = manager->declareRationalVariable(parameter);
identifierMapping.emplace(var.getName(), var);
parser.setIdentifierMapping(identifierMapping);
STORM_LOG_TRACE("Added parameter: " << var.getName());
}
template<>
double ValueParser<double>::parseValue(std::string const& value) const {
return boost::lexical_cast<double>(value);
}
template<>
storm::RationalFunction ValueParser<storm::RationalFunction>::parseValue(std::string const& value) const {
storm::RationalFunction rationalFunction = evaluator.asRational(parser.parseFromString(value));
STORM_LOG_TRACE("Parsed expression: " << rationalFunction);
return rationalFunction;
}
// Template instantiations.
template class ValueParser<double>;
template class ValueParser<storm::RationalFunction>;
} // namespace parser
} // namespace storm

53
src/storm/parser/ValueParser.h

@ -0,0 +1,53 @@
#ifndef STORM_PARSER_VALUEPARSER_H_
#define STORM_PARSER_VALUEPARSER_H_
#include "storm/storage/expressions/ExpressionManager.h"
#include "storm/parser/ExpressionParser.h"
#include "storm/storage/expressions/ExpressionEvaluator.h"
namespace storm {
namespace parser {
/*!
* Parser for values according to their ValueType.
*/
template<typename ValueType>
class ValueParser {
public:
/*!
* Constructor.
*/
ValueParser() : manager(new storm::expressions::ExpressionManager()), parser(*manager), evaluator(*manager) {
}
/*!
* Parse ValueType from string.
*
* @param value String containing the value.
*
* @return ValueType
*/
ValueType parseValue(std::string const& value) const;
/*!
* Add declaration of parameter.
*
* @param parameter New parameter.
*/
void addParameter(std::string const& parameter);
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;
};
} // namespace parser
} // namespace storm
#endif /* STORM_PARSER_VALUEPARSER_H_ */
Loading…
Cancel
Save