From c0481ab72d7a28992adb96cf968f93c3aae07d00 Mon Sep 17 00:00:00 2001 From: Matthias Volk Date: Tue, 13 Feb 2018 18:02:02 +0100 Subject: [PATCH] Moved ValueParser to separate file --- src/storm/parser/DirectEncodingParser.cpp | 30 +------------ src/storm/parser/DirectEncodingParser.h | 41 +----------------- src/storm/parser/ValueParser.cpp | 39 +++++++++++++++++ src/storm/parser/ValueParser.h | 53 +++++++++++++++++++++++ 4 files changed, 94 insertions(+), 69 deletions(-) create mode 100644 src/storm/parser/ValueParser.cpp create mode 100644 src/storm/parser/ValueParser.h diff --git a/src/storm/parser/DirectEncodingParser.cpp b/src/storm/parser/DirectEncodingParser.cpp index 45768f908..68530a5a7 100644 --- a/src/storm/parser/DirectEncodingParser.cpp +++ b/src/storm/parser/DirectEncodingParser.cpp @@ -25,32 +25,6 @@ namespace storm { namespace parser { - template - void ValueParser::addParameter(std::string const& parameter) { - STORM_LOG_THROW(false, storm::exceptions::NotSupportedException, "Parameters are not supported in this build."); - } - - template<> - void ValueParser::addParameter(std::string const& parameter) { - //STORM_LOG_THROW((std::is_same::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::parseValue(std::string const& value) const { - return boost::lexical_cast(value); - } - - template<> - storm::RationalFunction ValueParser::parseValue(std::string const& value) const { - storm::RationalFunction rationalFunction = evaluator.asRational(parser.parseFromString(value)); - STORM_LOG_TRACE("Parsed expression: " << rationalFunction); - return rationalFunction; - } - template std::shared_ptr> DirectEncodingParser::parseModel(std::string const& filename) { @@ -253,9 +227,7 @@ namespace storm { // Template instantiations. template class DirectEncodingParser; - -#ifdef STORM_HAVE_CARL template class DirectEncodingParser; -#endif + } // namespace parser } // namespace storm diff --git a/src/storm/parser/DirectEncodingParser.h b/src/storm/parser/DirectEncodingParser.h index db1bac12e..456445e1c 100644 --- a/src/storm/parser/DirectEncodingParser.h +++ b/src/storm/parser/DirectEncodingParser.h @@ -1,53 +1,14 @@ #ifndef STORM_PARSER_DIRECTENCODINGPARSER_H_ #define STORM_PARSER_DIRECTENCODINGPARSER_H_ +#include "storm/parser/ValueParser.h" #include "storm/models/sparse/Model.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" namespace storm { namespace parser { - /*! - * Parser for values according to their ValueType. - */ - template - 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 manager; - - storm::parser::ExpressionParser parser; - - storm::expressions::ExpressionEvaluator evaluator; - - std::unordered_map identifierMapping; - }; - /*! * Parser for models in the DRN format with explicit encoding. */ diff --git a/src/storm/parser/ValueParser.cpp b/src/storm/parser/ValueParser.cpp new file mode 100644 index 000000000..90ccd4aff --- /dev/null +++ b/src/storm/parser/ValueParser.cpp @@ -0,0 +1,39 @@ +#include "storm/parser/ValueParser.h" + +#include "storm/exceptions/NotSupportedException.h" + +namespace storm { + namespace parser { + + template + void ValueParser::addParameter(std::string const& parameter) { + STORM_LOG_THROW(false, storm::exceptions::NotSupportedException, "Parameters are not supported in this build."); + } + + template<> + void ValueParser::addParameter(std::string const& parameter) { + //STORM_LOG_THROW((std::is_same::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::parseValue(std::string const& value) const { + return boost::lexical_cast(value); + } + + template<> + storm::RationalFunction ValueParser::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; + template class ValueParser; + + } // namespace parser +} // namespace storm diff --git a/src/storm/parser/ValueParser.h b/src/storm/parser/ValueParser.h new file mode 100644 index 000000000..ab2e741b6 --- /dev/null +++ b/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 + 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 manager; + + storm::parser::ExpressionParser parser; + + storm::expressions::ExpressionEvaluator evaluator; + + std::unordered_map identifierMapping; + }; + + } // namespace parser +} // namespace storm + +#endif /* STORM_PARSER_VALUEPARSER_H_ */