From 78c42f034e236189d599a82e027c7f6c2eaa1aaf Mon Sep 17 00:00:00 2001 From: Thomas Knoll Date: Thu, 11 Jan 2024 21:52:41 +0100 Subject: [PATCH] overwrite indices as vector --- exampleConfig.yaml | 48 ++++++++++++--------------------------------- main.cpp | 2 +- util/ConfigYaml.cpp | 9 +++++++-- util/ConfigYaml.h | 6 +++--- util/Grid.cpp | 39 ++++++++++++++++++------------------ 5 files changed, 44 insertions(+), 60 deletions(-) diff --git a/exampleConfig.yaml b/exampleConfig.yaml index dd51aa6..8c3042b 100644 --- a/exampleConfig.yaml +++ b/exampleConfig.yaml @@ -3,34 +3,15 @@ labels: - label: "AgentIsInGoal" text: "AgentIsInGoal" -constants: - - constant: "prop_slippery_turn" - type: "double" - value: "9/9" - overwrite: True - - constant: "prop_next_neighbour_turn" - type: "double" - value: "0/9" - overwrite: True - - constant: "prop_slippery_move_forward" - type: "double" - value: "3/4" - overwrite: True - - constant: "prop_direct_neighbour" - type: "double" - value: "1/4" - - constant: "prop_next_neighbour" - type: "double" - value: "1/8" - overwrite: True - - constant: "total_prop" - type: "double" - value: "4" - overwrite: True +# constants: +# - constant: "prop_slippery_turn" +# type: "double" +# value: "9/9" +# overwrite: True probabilities: - probability: "FaultProbability" - value: 0.1 + value: 0.2 - probability: "ProbForwardIntended" value: 0.1 - probability: "ProbTurnIntended" @@ -43,13 +24,10 @@ modules: guard: "AgentIsOnSlippery" update: "True" overwrite: True - -... - -const double prop_zero = 0/9; -const double prop_next_neighbour = 1/9; -const double prop_slippery_move_forward = 7/9; -const double prop_slippery_turn = 6/9; -const double prop_next_neighbour_turn = 1/9; -const double prop_direct_neighbour = 2/9; -const double total_prop = 9; \ No newline at end of file + index: 3 + - action: "[Agent_turn_right]" + guard: "AgentIsOnSlippery" + update: "True" + overwrite: True + index: [0,1] +... \ No newline at end of file diff --git a/main.cpp b/main.cpp index e41ac3a..2760259 100644 --- a/main.cpp +++ b/main.cpp @@ -166,7 +166,7 @@ int main(int argc, char* argv[]) { if(ok) { Grid grid(contentCells, backgroundCells, gridOptions, stateRewards, probIntended, faultyProbability); - grid.printToPrism(std::cout, configurations , gridOptions.getModelType()); + // grid.printToPrism(std::cout, configurations , gridOptions.getModelType()); std::stringstream ss; // grid.printToPrism(file, configurations ,prism::ModelType::MDP); grid.printToPrism(ss, configurations , gridOptions.getModelType()); diff --git a/util/ConfigYaml.cpp b/util/ConfigYaml.cpp index 7dc392c..57226d0 100644 --- a/util/ConfigYaml.cpp +++ b/util/ConfigYaml.cpp @@ -103,7 +103,12 @@ bool YAML::convert::decode(const YAML::Node& node, Command& rhs) { rhs.overwrite_ = node["overwrite"].as(); } if (node["index"]) { - rhs.index_ = node["index"].as(); + try { + rhs.index_ = node["index"].as>(); + } + catch(const std::exception& e) { + rhs.index_ = {node["index"].as()}; + } } return true; @@ -251,7 +256,7 @@ YamlConfigParseResult YamlConfigParser::parseConfiguration() { for (auto& constant : constants) { // std::cout << constant.constant_ << std::endl; configuration.push_back({constant.createExpression(), "const " + constant.type_ + " " + constant.constant_, ConfigType::Constant, constant.overwrite_}); - } + } } catch(const std::exception& e) { std::cout << "Exception '" << typeid(e).name() << "' caught:" << std::endl; diff --git a/util/ConfigYaml.h b/util/ConfigYaml.h index ccaa34c..bfdbc44 100644 --- a/util/ConfigYaml.h +++ b/util/ConfigYaml.h @@ -22,7 +22,7 @@ struct Configuration std::string module_ {}; std::string expression_{}; std::string identifier_{}; - int index_{}; + std::vector index_{0}; ConfigType type_ {ConfigType::Label}; bool overwrite_ {false}; @@ -33,7 +33,7 @@ struct Configuration , ConfigType type , bool overwrite = false , std::string module = "" - , int index = 0) : expression_(expression), identifier_(identifier), type_(type), overwrite_(overwrite), module_{module}, index_(index) {} + , std::vector index = {0}) : expression_(expression), identifier_(identifier), type_(type), overwrite_(overwrite), module_{module}, index_(index) {} ~Configuration() = default; Configuration(const Configuration&) = default; @@ -100,7 +100,7 @@ struct Command { std::string action_; std::string guard_; std::string update_; - int index_{0}; + std::vector index_{0}; bool overwrite_ {false}; std::string createExpression() const; diff --git a/util/Grid.cpp b/util/Grid.cpp index 294ddd1..ac22546 100644 --- a/util/Grid.cpp +++ b/util/Grid.cpp @@ -104,29 +104,30 @@ void Grid::applyOverwrites(std::string& str, std::vector& configu if (!config.overwrite_) { continue; } - size_t start_pos; - - if (config.type_ == ConfigType::Formula) { - start_pos = str.find("formula " + config.identifier_); - } else if (config.type_ == ConfigType::Label) { - start_pos = str.find("label " + config.identifier_); - } else if (config.type_ == ConfigType::Module) { - auto iter = boost::find_nth(str, config.identifier_, config.index_); + for (auto& index : config.index_) { + size_t start_pos; + std::string search; + + if (config.type_ == ConfigType::Formula) { + search = "formula " + config.identifier_; + } else if (config.type_ == ConfigType::Label) { + search = "label " + config.identifier_; + } else if (config.type_ == ConfigType::Module) { + search = config.identifier_; + } + else if (config.type_ == ConfigType::Constant) { + search = config.identifier_; + } + + auto iter = boost::find_nth(str, search, index); start_pos = std::distance(str.begin(), iter.begin()); - } - else if (config.type_ == ConfigType::Constant) { - start_pos = str.find(config.identifier_); + size_t end_pos = str.find(';', start_pos) + 1; - if (start_pos == std::string::npos) { - std::cout << "Couldn't find overwrite:" << config.expression_ << std::endl; + if (end_pos != std::string::npos && end_pos != 0) { + std::string expression = config.expression_; + str.replace(start_pos, end_pos - start_pos , expression); } } - - size_t end_pos = str.find(';', start_pos) + 1; - - std::string expression = config.expression_; - - str.replace(start_pos, end_pos - start_pos , expression); } } void Grid::printToPrism(std::ostream& os, std::vector& configuration ,const prism::ModelType& modelType) {