Browse Source

Use ValueParser in GalileoParser

tempestpy_adaptions
Matthias Volk 7 years ago
parent
commit
821300e777
  1. 45
      src/storm-dft/parser/DFTGalileoParser.cpp
  2. 22
      src/storm-dft/parser/DFTGalileoParser.h

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

@ -2,12 +2,14 @@
#include <iostream>
#include <fstream>
#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"
#include "storm/exceptions/NotSupportedException.h"
#include "storm/parser/ValueParser.h"
#include "storm/utility/macros.h"
#include "storm/utility/file.h"
@ -52,6 +54,8 @@ namespace storm {
storm::utility::openFile(filename, file);
std::string line;
ValueParser<ValueType> valueParser;
while (std::getline(file, line)) {
bool success = true;
STORM_LOG_TRACE("Parsing: " << line);
@ -69,16 +73,9 @@ namespace storm {
toplevelId = stripQuotsFromName(line.substr(toplevelToken.size() + 1));
}
else if (boost::starts_with(line, parametricToken)) {
#ifdef STORM_HAVE_CARL
STORM_LOG_THROW((std::is_same<ValueType, storm::RationalFunction>::value), storm::exceptions::NotSupportedException, "Parameters only allowed when using rational functions.");
std::string parameter = stripQuotsFromName(line.substr(parametricToken.size() + 1));
storm::expressions::Variable var = manager->declareRationalVariable(parameter);
identifierMapping.emplace(var.getName(), var);
parser.setIdentifierMapping(identifierMapping);
STORM_LOG_TRACE("Added parameter: " << var.getName());
#else
STORM_LOG_THROW(false, storm::exceptions::NotSupportedException, "Parameters are not supported in this build.");
#endif
valueParser.addParameter(parameter);
} else {
std::vector<std::string> tokens;
boost::split(tokens, line, boost::is_any_of(" "));
@ -119,11 +116,11 @@ namespace storm {
} else if (tokens[1] == "fdep") {
success = builder.addDepElement(name, childNames, storm::utility::one<ValueType>());
} else if (boost::starts_with(tokens[1], "pdep=")) {
ValueType probability = parseRationalExpression(tokens[1].substr(5));
ValueType probability = valueParser.parseValue(tokens[1].substr(5));
success = builder.addDepElement(name, childNames, probability);
} else if (boost::starts_with(tokens[1], "lambda=")) {
ValueType failureRate = parseRationalExpression(tokens[1].substr(7));
ValueType dormancyFactor = parseRationalExpression(tokens[2].substr(5));
ValueType failureRate = valueParser.parseValue(tokens[1].substr(7));
ValueType dormancyFactor = valueParser.parseValue(tokens[2].substr(5));
success = builder.addBasicElement(name, failureRate, dormancyFactor, false); // TODO set transient BEs
} else {
STORM_LOG_THROW(false, storm::exceptions::NotSupportedException, "Type name: " << tokens[1] << " not recognized.");
@ -138,33 +135,9 @@ namespace storm {
storm::utility::closeFile(file);
}
template<typename ValueType>
ValueType DFTGalileoParser<ValueType>::parseRationalExpression(std::string const& expr) {
STORM_LOG_ASSERT(false, "Specialized method should be called.");
return 0;
}
template<>
double DFTGalileoParser<double>::parseRationalExpression(std::string const& expr) {
return boost::lexical_cast<double>(expr);
}
// Explicitly instantiate the class.
template class DFTGalileoParser<double>;
#ifdef STORM_HAVE_CARL
template<>
storm::RationalFunction DFTGalileoParser<storm::RationalFunction>::parseRationalExpression(std::string const& expr) {
STORM_LOG_TRACE("Translating expression: " << expr);
storm::expressions::Expression expression = parser.parseFromString(expr);
STORM_LOG_TRACE("Expression: " << expression);
storm::RationalFunction rationalFunction = evaluator.asRational(expression);
STORM_LOG_TRACE("Parsed expression: " << rationalFunction);
return rationalFunction;
}
template class DFTGalileoParser<RationalFunction>;
#endif
}
}

22
src/storm-dft/parser/DFTGalileoParser.h

@ -15,18 +15,15 @@ namespace storm {
template<typename ValueType>
class DFTGalileoParser {
storm::storage::DFTBuilder<ValueType> builder;
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;
public:
DFTGalileoParser(bool defaultInclusive = true, bool binaryDependencies = true) : builder(defaultInclusive, binaryDependencies), manager(new storm::expressions::ExpressionManager()), parser(*manager), evaluator(*manager) {
/*!
* Constructor.
*
* @param defaultInclusive Flag indicating if priority gates are inclusive by default.
* @param binaryDependencies Flag indicating if dependencies should be converted to binary dependencies.
*/
DFTGalileoParser(bool defaultInclusive = true, bool binaryDependencies = true) : builder(defaultInclusive, binaryDependencies) {
}
storm::storage::DFT<ValueType> parseDFT(std::string const& filename);
@ -37,8 +34,7 @@ namespace storm {
std::string stripQuotsFromName(std::string const& name);
std::string parseNodeIdentifier(std::string const& name);
ValueType parseRationalExpression(std::string const& expr);
storm::storage::DFTBuilder<ValueType> builder;
bool defaultInclusive;
};
}

Loading…
Cancel
Save