Browse Source

allowing constant definitions for gspns via cli

tempestpy_adaptions
TimQu 6 years ago
parent
commit
a44eed65e8
  1. 7
      src/storm-gspn-cli/storm-gspn.cpp
  2. 29
      src/storm-gspn/parser/GreatSpnEditorProjectParser.cpp
  3. 4
      src/storm-gspn/parser/GreatSpnEditorProjectParser.h
  4. 5
      src/storm-gspn/parser/GspnParser.cpp
  5. 2
      src/storm-gspn/parser/GspnParser.h
  6. 12
      src/storm-gspn/settings/modules/GSPNSettings.cpp
  7. 13
      src/storm-gspn/settings/modules/GSPNSettings.h

7
src/storm-gspn-cli/storm-gspn.cpp

@ -94,8 +94,13 @@ int main(const int argc, const char **argv) {
return -1; return -1;
} }
std::string constantDefinitionString = "";
if (gspnSettings.isConstantsSet()) {
constantDefinitionString = gspnSettings.getConstantDefinitionString();
}
auto parser = storm::parser::GspnParser(); auto parser = storm::parser::GspnParser();
auto gspn = parser.parse(gspnSettings.getGspnFilename());
auto gspn = parser.parse(gspnSettings.getGspnFilename(), constantDefinitionString);
std::string formulaString = ""; std::string formulaString = "";
if (storm::settings::getModule<storm::settings::modules::IOSettings>().isPropertySet()) { if (storm::settings::getModule<storm::settings::modules::IOSettings>().isPropertySet()) {

29
src/storm-gspn/parser/GreatSpnEditorProjectParser.cpp

@ -23,8 +23,15 @@ namespace {
namespace storm { namespace storm {
namespace parser { namespace parser {
GreatSpnEditorProjectParser::GreatSpnEditorProjectParser() : manager(std::make_shared<storm::expressions::ExpressionManager>()), expressionParser(*manager) {
// Intentionally left empty
GreatSpnEditorProjectParser::GreatSpnEditorProjectParser(std::string const& constantDefinitionString) : manager(std::make_shared<storm::expressions::ExpressionManager>()), expressionParser(*manager) {
std::vector<std::string> constDefs;
boost::split( constDefs, constantDefinitionString, boost::is_any_of(","));
for (auto const& pair : constDefs) {
std::vector<std::string> keyvaluepair;
boost::split( keyvaluepair, pair, boost::is_any_of("="));
STORM_LOG_THROW(keyvaluepair.size() == 2, storm::exceptions::WrongFormatException, "Expected a constant definition of the form N=42 but got " << pair);
constantDefinitions.emplace(keyvaluepair.at(0), keyvaluepair.at(1));
}
} }
storm::gspn::GSPN* GreatSpnEditorProjectParser::parse(xercesc::DOMElement const* elementRoot) { storm::gspn::GSPN* GreatSpnEditorProjectParser::parse(xercesc::DOMElement const* elementRoot) {
@ -168,6 +175,8 @@ namespace storm {
} else if (name.compare("consttype") == 0) { } else if (name.compare("consttype") == 0) {
if (storm::adapters::XMLtoString(attr->getNodeValue()).compare("REAL") == 0) { if (storm::adapters::XMLtoString(attr->getNodeValue()).compare("REAL") == 0) {
type = manager->getRationalType(); type = manager->getRationalType();
} else if (storm::adapters::XMLtoString(attr->getNodeValue()).compare("INTEGER") == 0) {
type = manager->getIntegerType();
} else { } else {
STORM_PRINT_AND_LOG("Unknown consttype: " << storm::adapters::XMLtoString(attr->getNodeValue()) << std::endl); STORM_PRINT_AND_LOG("Unknown consttype: " << storm::adapters::XMLtoString(attr->getNodeValue()) << std::endl);
} }
@ -182,10 +191,20 @@ namespace storm {
} }
} }
STORM_LOG_THROW(constantDefinitions.count(identifier) == 0, storm::exceptions::NotSupportedException, "Multiple definitions of constant '" << identifier << "' were found.");
storm::expressions::Expression valueExpression; storm::expressions::Expression valueExpression;
if (valueStr == "") {
auto constDef = constantDefinitions.find(identifier);
STORM_LOG_THROW(constDef != constantDefinitions.end(), storm::exceptions::NotSupportedException, "Constant '" << identifier << "' has no value defined.");
valueStr = constDef->second;
}
if (type.isRationalType()) { if (type.isRationalType()) {
expressionParser.setAcceptDoubleLiterals(true); expressionParser.setAcceptDoubleLiterals(true);
valueExpression = manager->rational(expressionParser.parseFromString(valueStr).evaluateAsRational()); valueExpression = manager->rational(expressionParser.parseFromString(valueStr).evaluateAsRational());
} else if (type.isIntegerType()) {
expressionParser.setAcceptDoubleLiterals(false);
valueExpression = manager->integer(expressionParser.parseFromString(valueStr).evaluateAsInt());
} else { } else {
STORM_LOG_THROW(false, storm::exceptions::NotSupportedException, "Unknown type of constant" << type << "."); STORM_LOG_THROW(false, storm::exceptions::NotSupportedException, "Unknown type of constant" << type << ".");
} }
@ -257,7 +276,8 @@ namespace storm {
if (name.compare("name") == 0) { if (name.compare("name") == 0) {
placeName = storm::adapters::XMLtoString(attr->getNodeValue()); placeName = storm::adapters::XMLtoString(attr->getNodeValue());
} else if (name.compare("marking") == 0) { } else if (name.compare("marking") == 0) {
initialTokens = std::stoull(storm::adapters::XMLtoString(attr->getNodeValue()));
expressionParser.setAcceptDoubleLiterals(false);
initialTokens = expressionParser.parseFromString(storm::adapters::XMLtoString(attr->getNodeValue())).evaluateAsInt();
} else if (ignorePlaceAttribute(name)) { } else if (ignorePlaceAttribute(name)) {
// ignore node // ignore node
} else { } else {
@ -388,7 +408,8 @@ namespace storm {
} else if (name.compare("kind") == 0) { } else if (name.compare("kind") == 0) {
kind = storm::adapters::XMLtoString(attr->getNodeValue()); kind = storm::adapters::XMLtoString(attr->getNodeValue());
} else if (name.compare("mult") == 0) { } else if (name.compare("mult") == 0) {
mult = std::stoull(storm::adapters::XMLtoString(attr->getNodeValue()));
expressionParser.setAcceptDoubleLiterals(false);
mult = expressionParser.parseFromString(storm::adapters::XMLtoString(attr->getNodeValue())).evaluateAsInt();
} else if (ignoreArcAttribute(name)) { } else if (ignoreArcAttribute(name)) {
// ignore node // ignore node
} else { } else {

4
src/storm-gspn/parser/GreatSpnEditorProjectParser.h

@ -20,7 +20,7 @@ namespace storm {
public: public:
GreatSpnEditorProjectParser();
GreatSpnEditorProjectParser(std::string const& constantDefinitionString);
/*! /*!
* Parses the given file into the GSPN. * Parses the given file into the GSPN.
@ -46,7 +46,7 @@ namespace storm {
storm::gspn::GspnBuilder builder; storm::gspn::GspnBuilder builder;
std::shared_ptr<storm::expressions::ExpressionManager> manager; std::shared_ptr<storm::expressions::ExpressionManager> manager;
storm::parser::ExpressionParser expressionParser; storm::parser::ExpressionParser expressionParser;
std::unordered_map<std::string, std::string> constantDefinitions;
}; };
} }
} }

5
src/storm-gspn/parser/GspnParser.cpp

@ -12,7 +12,7 @@
namespace storm { namespace storm {
namespace parser { namespace parser {
storm::gspn::GSPN* GspnParser::parse(std::string const& filename) {
storm::gspn::GSPN* GspnParser::parse(std::string const& filename, std::string const& constantDefinitions) {
#ifdef STORM_HAVE_XERCES #ifdef STORM_HAVE_XERCES
// initialize xercesc // initialize xercesc
try { try {
@ -62,10 +62,11 @@ namespace storm {
xercesc::DOMElement* elementRoot = parser->getDocument()->getDocumentElement(); xercesc::DOMElement* elementRoot = parser->getDocument()->getDocumentElement();
if (storm::adapters::XMLtoString(elementRoot->getTagName()) == "pnml") { if (storm::adapters::XMLtoString(elementRoot->getTagName()) == "pnml") {
STORM_LOG_WARN_COND(constantDefinitions == "", "Constant definitions for pnml files are currently not supported.");
PnmlParser p; PnmlParser p;
return p.parse(elementRoot); return p.parse(elementRoot);
} else if (storm::adapters::XMLtoString(elementRoot->getTagName()) == "project") { } else if (storm::adapters::XMLtoString(elementRoot->getTagName()) == "project") {
GreatSpnEditorProjectParser p;
GreatSpnEditorProjectParser p(constantDefinitions);
return p.parse(elementRoot); return p.parse(elementRoot);
} else { } else {
// If the top-level node is not a "pnml" or "" node, then throw an exception. // If the top-level node is not a "pnml" or "" node, then throw an exception.

2
src/storm-gspn/parser/GspnParser.h

@ -4,7 +4,7 @@ namespace storm {
namespace parser { namespace parser {
class GspnParser { class GspnParser {
public: public:
static storm::gspn::GSPN* parse(std::string const& filename);
static storm::gspn::GSPN* parse(std::string const& filename, std::string const& constantDefinitions = "");
}; };
} }
} }

12
src/storm-gspn/settings/modules/GSPNSettings.cpp

@ -19,12 +19,16 @@ namespace storm {
const std::string GSPNSettings::capacitiesFileOptionName = "capacitiesfile"; const std::string GSPNSettings::capacitiesFileOptionName = "capacitiesfile";
const std::string GSPNSettings::capacitiesFileOptionShortName = "capacities"; const std::string GSPNSettings::capacitiesFileOptionShortName = "capacities";
const std::string GSPNSettings::capacityOptionName = "capacity"; const std::string GSPNSettings::capacityOptionName = "capacity";
const std::string GSPNSettings::constantsOptionName = "constants";
const std::string GSPNSettings::constantsOptionShortName = "const";
GSPNSettings::GSPNSettings() : ModuleSettings(moduleName) { GSPNSettings::GSPNSettings() : ModuleSettings(moduleName) {
this->addOption(storm::settings::OptionBuilder(moduleName, gspnFileOptionName, false, "Parses the GSPN.").setShortName(gspnFileOptionShortName).addArgument(storm::settings::ArgumentBuilder::createStringArgument("filename", "path to file").addValidatorString(ArgumentValidatorFactory::createExistingFileValidator()).build()).build()); this->addOption(storm::settings::OptionBuilder(moduleName, gspnFileOptionName, false, "Parses the GSPN.").setShortName(gspnFileOptionShortName).addArgument(storm::settings::ArgumentBuilder::createStringArgument("filename", "path to file").addValidatorString(ArgumentValidatorFactory::createExistingFileValidator()).build()).build());
this->addOption(storm::settings::OptionBuilder(moduleName, capacitiesFileOptionName, false, "Capacaties as invariants for places.").setShortName(capacitiesFileOptionShortName).addArgument(storm::settings::ArgumentBuilder::createStringArgument("filename", "path to file").addValidatorString(ArgumentValidatorFactory::createExistingFileValidator()).build()).build()); this->addOption(storm::settings::OptionBuilder(moduleName, capacitiesFileOptionName, false, "Capacaties as invariants for places.").setShortName(capacitiesFileOptionShortName).addArgument(storm::settings::ArgumentBuilder::createStringArgument("filename", "path to file").addValidatorString(ArgumentValidatorFactory::createExistingFileValidator()).build()).build());
this->addOption(storm::settings::OptionBuilder(moduleName, capacityOptionName, false, "Global capacity as invariants for all places.").addArgument(storm::settings::ArgumentBuilder::createUnsignedIntegerArgument("value", "capacity").addValidatorUnsignedInteger(ArgumentValidatorFactory::createUnsignedGreaterValidator(0)).build()).build()); this->addOption(storm::settings::OptionBuilder(moduleName, capacityOptionName, false, "Global capacity as invariants for all places.").addArgument(storm::settings::ArgumentBuilder::createUnsignedIntegerArgument("value", "capacity").addValidatorUnsignedInteger(ArgumentValidatorFactory::createUnsignedGreaterValidator(0)).build()).build());
this->addOption(storm::settings::OptionBuilder(moduleName, constantsOptionName, false, "Specifies the constant replacements to use.").setShortName(constantsOptionShortName).addArgument(storm::settings::ArgumentBuilder::createStringArgument("values", "A comma separated list of constants and their value, e.g. a=1,b=2,c=3.").setDefaultValueString("").build()).build());
} }
bool GSPNSettings::isGspnFileSet() const { bool GSPNSettings::isGspnFileSet() const {
@ -51,6 +55,14 @@ namespace storm {
return this->getOption(capacityOptionName).getArgumentByName("value").getValueAsUnsignedInteger(); return this->getOption(capacityOptionName).getArgumentByName("value").getValueAsUnsignedInteger();
} }
bool GSPNSettings::isConstantsSet() const {
return this->getOption(constantsOptionName).getHasOptionBeenSet();
}
std::string GSPNSettings::getConstantDefinitionString() const {
return this->getOption(constantsOptionName).getArgumentByName("values").getValueAsString();
}
void GSPNSettings::finalize() { void GSPNSettings::finalize() {
} }

13
src/storm-gspn/settings/modules/GSPNSettings.h

@ -44,6 +44,16 @@ namespace storm {
*/ */
uint64_t getCapacity() const; uint64_t getCapacity() const;
/*!
* Retrieves whether the constants ption was set.
*/
bool isConstantsSet() const;
/*!
* Retrieves the string that defines the constants of a gspn
*/
std::string getConstantDefinitionString() const;
bool check() const override; bool check() const override;
void finalize() override; void finalize() override;
@ -56,7 +66,8 @@ namespace storm {
static const std::string capacitiesFileOptionName; static const std::string capacitiesFileOptionName;
static const std::string capacitiesFileOptionShortName; static const std::string capacitiesFileOptionShortName;
static const std::string capacityOptionName; static const std::string capacityOptionName;
static const std::string constantsOptionName;
static const std::string constantsOptionShortName;
}; };
} }
} }

Loading…
Cancel
Save