From 00d464fe6f8cc7e8c13571363ff0f55e0787d270 Mon Sep 17 00:00:00 2001
From: Thomas Knoll <thomas.knoll@student.tugraz.at>
Date: Mon, 15 Jan 2024 22:42:10 +0100
Subject: [PATCH] basic overwrite guard and update

---
 main.cpp            |  4 ++--
 util/ConfigYaml.cpp | 36 +++++++++++++++++++++++++++++++-----
 util/ConfigYaml.h   |  8 +++++++-
 3 files changed, 40 insertions(+), 8 deletions(-)

diff --git a/main.cpp b/main.cpp
index 012f2e6..abc86a9 100644
--- a/main.cpp
+++ b/main.cpp
@@ -164,7 +164,7 @@ int main(int argc, char* argv[]) {
     }
     if(ok) {
       Grid grid(contentCells, backgroundCells, stateRewards, probIntended, faultyProbability);
-
+      
       //grid.printToPrism(std::cout, configurations);
       std::stringstream ss;
       grid.printToPrism(ss, configurations);
@@ -177,7 +177,7 @@ int main(int argc, char* argv[]) {
     std::cout << "got: \"" << std::string(e.first, e.last) << '"' << std::endl;
     std::cout << "Expectation failure: " << e.what() << " at '" << std::string(e.first,e.last) << "'\n";
   } catch(const std::exception& e) {
-    std::cerr << "Exception '" << typeid(e).name() << "' caught:" << std::endl;
+    std::cerr << "Exception '" << typeid(e).name() << "' caught:" << e.what() << std::endl;
     std::cerr << "\t" << e.what() << std::endl;
     std::exit(EXIT_FAILURE);
   }
diff --git a/util/ConfigYaml.cpp b/util/ConfigYaml.cpp
index ecb5337..1cfb6d2 100644
--- a/util/ConfigYaml.cpp
+++ b/util/ConfigYaml.cpp
@@ -47,10 +47,10 @@ std::string Formula::createExpression() const {
 
 std::string Command::createExpression() const {
     if (overwrite_) {
-        return action_  + "\t" + guard_ + "-> " + update_  + Configuration::overwrite_identifier_;
+        return action_  + "\t" + guard_ + " -> " + update_  + Configuration::overwrite_identifier_;
     }
     
-    return "\t" + action_  + "\t" + guard_ + "-> " + update_+ Configuration::configuration_identifier_;
+    return "\t" + action_  + "\t" + guard_ + " -> " + update_+ Configuration::configuration_identifier_;
 }
 
 std::string Constant::createExpression() const {
@@ -76,6 +76,13 @@ bool YAML::convert<Module>::decode(const YAML::Node& node, Module& rhs) {
     }
     rhs.commands_ = node["commands"].as<std::vector<Command>>();
     rhs.module_ = node["module"].as<std::string>();
+
+    if (node["module_text"]) {
+        rhs.module_text_ = node["module_text"].as<std::string>();
+    }
+    if (node["overwrite"]) {
+        rhs.overwrite_module = node["overwrite"].as<bool>();
+    }
     return true;
 }
 
@@ -96,8 +103,13 @@ bool YAML::convert<Command>::decode(const YAML::Node& node, Command& rhs) {
     }
 
     rhs.action_ = node["action"].as<std::string>();
-    rhs.guard_ = node["guard"].as<std::string>();
-    rhs.update_ = node["update"].as<std::string>();
+    if (node["guard"]) {
+        rhs.guard_ = node["guard"].as<std::string>();
+    }
+
+    if (node["update"]) {
+        rhs.update_ = node["update"].as<std::string>();
+    }
 
     if (node["overwrite"]) {
         rhs.overwrite_ = node["overwrite"].as<bool>();
@@ -249,8 +261,22 @@ YamlConfigParseResult YamlConfigParser::parseConfiguration() {
                 configuration.push_back({formula.createExpression(), formula.formula_ ,ConfigType::Formula, formula.overwrite_});
             }
             for (auto& module : modules) {
+                if (module.overwrite_module) {
+                    Configuration config = Configuration(module.module_text_, module.module_, ConfigType::Module, true, module.module_, {0}, "endmodule");
+                    configuration.push_back(config);
+                    continue;
+                }
                 for (auto& command : module.commands_) {
-                    configuration.push_back({command.createExpression(), command.action_, ConfigType::Module, command.overwrite_, module.module_, command.indexes_});
+                    Configuration config;
+                    if (!command.guard_.empty() && !command.action_.empty() && command.update_.empty()) {
+                        config = Configuration(" " + command.guard_, command.action_, ConfigType::Module, true, module.module_, command.indexes_, "->", false);
+                    } else if (!command.update_.empty() && !command.action_.empty() && command.guard_.empty()) {
+                        config = Configuration( " " + command.update_, command.action_, ConfigType::Module, true, module.module_,  command.indexes_, ";", false);
+                    } else {
+                        config = Configuration(command.createExpression(), command.action_, ConfigType::Module, command.overwrite_, module.module_, command.indexes_); 
+                    }
+
+                    configuration.push_back(config);
                 }
             }
             for (auto& constant : constants) {
diff --git a/util/ConfigYaml.h b/util/ConfigYaml.h
index 16b34b6..53eafea 100644
--- a/util/ConfigYaml.h
+++ b/util/ConfigYaml.h
@@ -22,10 +22,12 @@ struct Configuration
   std::string module_ {};
   std::string expression_{};
   std::string identifier_{};
+  std::string end_identifier_{};  
   std::vector<int> indexes_{0};
 
   ConfigType type_ {ConfigType::Label};
   bool overwrite_ {false};
+  bool include_identifier_for_overwrite_{true};
 
   Configuration() = default;
   Configuration(std::string expression
@@ -33,7 +35,9 @@ struct Configuration
                 , ConfigType type
                 , bool overwrite = false
                 , std::string module = ""
-                , std::vector<int> indexes = {0}) : expression_(expression), identifier_(identifier), type_(type), overwrite_(overwrite), module_{module}, indexes_(indexes) {}
+                , std::vector<int> indexes = {0}
+                , std::string end_identifier = {";"}
+                , bool include_identifier_for_overwrite = true) : expression_(expression), identifier_(identifier), type_(type), overwrite_(overwrite), module_{module}, indexes_(indexes), end_identifier_{end_identifier}, include_identifier_for_overwrite_{include_identifier_for_overwrite}  {}
   
   ~Configuration() = default;
   Configuration(const Configuration&) = default;
@@ -113,6 +117,8 @@ struct Module {
 
   std::vector<Command> commands_;
   std::string module_;
+  std::string module_text_;
+  bool overwrite_module{false};
 
   friend std::ostream& operator << (std::ostream& os, const Module& module);
 };