Browse Source

changed to yaml config

pull/3/head
Thomas Knoll 1 year ago
parent
commit
42d0f4d6d9
  1. 4
      CMakeLists.txt
  2. 22
      main.cpp
  3. 1
      util/CMakeLists.txt
  4. 88
      util/ConfigGrammar.h
  5. 150
      util/ConfigYaml.cpp
  6. 115
      util/ConfigYaml.h
  7. 2
      util/Grid.h
  8. 2
      util/PrismModulesPrinter.h

4
CMakeLists.txt

@ -14,9 +14,11 @@ project(
VERSION 0.1
LANGUAGES CXX)
find_package(yaml-cpp)
add_executable(main
${SRCS}
main.cpp
)
target_link_libraries(main pthread)
target_link_libraries(main pthread yaml-cpp)

22
main.cpp

@ -1,13 +1,14 @@
#include "util/OptionParser.h"
#include "util/MinigridGrammar.h"
#include "util/Grid.h"
#include "util/ConfigGrammar.h"
#include "util/ConfigYaml.h"
#include <iostream>
#include <fstream>
#include <filesystem>
#include <sstream>
std::vector<std::string> parseCommaSeparatedString(std::string const& str) {
std::vector<std::string> result;
std::stringstream stream(str);
@ -20,6 +21,7 @@ std::vector<std::string> parseCommaSeparatedString(std::string const& str) {
return result;
}
struct printer {
typedef boost::spirit::utf8_string string;
@ -104,7 +106,7 @@ int main(int argc, char* argv[]) {
std::fstream file {outputFilename->value(0), file.trunc | file.out};
std::fstream infile {inputFilename->value(0), infile.in};
std::string line, content, background, rewards, config;
std::string line, content, background, rewards;
std::cout << "\n";
bool parsingBackground = false;
bool parsingStateRewards = false;
@ -129,15 +131,6 @@ int main(int argc, char* argv[]) {
}
std::cout << "\n";
if (configFilename->is_set()) {
std::fstream configFile {configFilename->value(0), configFile.in};
while (std::getline(configFile, line) && !line.empty()) {
std::cout << "Configuration:\t" << line << "\n";
config += line + "\n";
}
}
pos_iterator_t contentFirst(content.begin());
pos_iterator_t contentIter = contentFirst;
pos_iterator_t contentLast(content.end());
@ -146,10 +139,6 @@ int main(int argc, char* argv[]) {
pos_iterator_t backgroundIter = backgroundFirst;
pos_iterator_t backgroundLast(background.end());
MinigridParser<pos_iterator_t> backgroundParser(backgroundFirst);
pos_iterator_t configFirst(config.begin());
pos_iterator_t configIter = configFirst;
pos_iterator_t configLast(config.end());
ConfigParser<pos_iterator_t> configParser(configFirst);
cells contentCells;
cells backgroundCells;
@ -161,7 +150,8 @@ int main(int argc, char* argv[]) {
ok &= phrase_parse(backgroundIter, backgroundLast, backgroundParser, qi::space, backgroundCells);
// TODO }
if (configFilename->is_set()) {
ok &= phrase_parse(configIter, configLast, configParser, qi::space, configurations);
YamlConfigParser parser(configFilename->value(0));
configurations = parser.parseConfiguration();
}
for (auto& config : configurations) {

1
util/CMakeLists.txt

@ -5,4 +5,5 @@ list(APPEND SRCS
${CMAKE_CURRENT_LIST_DIR}/PrismModulesPrinter.cpp
${CMAKE_CURRENT_LIST_DIR}/popl.hpp
${CMAKE_CURRENT_LIST_DIR}/OptionParser.cpp
${CMAKE_CURRENT_LIST_DIR}/ConfigYaml.cpp
)

88
util/ConfigGrammar.h

@ -1,88 +0,0 @@
#pragma once
#include "cell.h"
#include <vector>
#include <boost/tokenizer.hpp>
#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/variant/recursive_wrapper.hpp>
#include <boost/spirit/include/support_line_pos_iterator.hpp>
namespace qi = boost::spirit::qi;
namespace phoenix = boost::phoenix;
typedef std::vector<std::string> expressions;
enum class ConfigType : char {
Label = 'L',
Formula = 'F',
};
struct Configuration
{
expressions expressions_;
std::string derivation_;
ConfigType type_ {ConfigType::Label};
Configuration() = default;
Configuration(expressions expressions, std::string derivation, ConfigType type) : expressions_(expressions), derivation_(derivation), type_(type) {}
~Configuration() = default;
Configuration(const Configuration&) = default;
friend std::ostream& operator << (std::ostream& os, const Configuration& config) {
os << "Configuration with Type: " << static_cast<char>(config.type_) << std::endl;
for (auto& expression : config.expressions_) {
os << "\tExpression=" << expression << std::endl;
}
return os << "\tDerviation=" << config.derivation_;
}
};
BOOST_FUSION_ADAPT_STRUCT(
Configuration,
(ConfigType, type_)
(expressions, expressions_)
(std::string, derivation_)
)
template <typename It>
struct ConfigParser : qi::grammar<It, std::vector<Configuration>>
{
ConfigParser(It first) : ConfigParser::base_type(config_)
{
using namespace qi;
//F:(AgentCannotMoveSouth & AgentCannotMoveNorth) | (AgentCannotMoveEast & AgentCannotMoveWest) ;AgentCannotTurn
configType_.add
("L", ConfigType::Label)
("F", ConfigType::Formula);
expression_ = -qi::char_('!') > + char_("a-zA-Z_0-9");
expressions_ = (expression_ % ',');
row_ = (configType_ > ':' > expressions_ > ';' > expression_);
// row_ = (expressions_ > ';' > expression_);
config_ = (row_ % "\n");
BOOST_SPIRIT_DEBUG_NODE(configType_);
BOOST_SPIRIT_DEBUG_NODE(expression_);
BOOST_SPIRIT_DEBUG_NODE(expressions_);
BOOST_SPIRIT_DEBUG_NODE(config_);
}
private:
qi::symbols<char, ConfigType> configType_;
qi::rule<It, expressions()> expressions_;
qi::rule<It, std::string()> expression_;
qi::rule<It, Configuration()> row_;
qi::rule<It, std::vector<Configuration>> config_;
};

150
util/ConfigYaml.cpp

@ -0,0 +1,150 @@
#include "ConfigYaml.h"
#include <iostream>
std::ostream& operator <<(std::ostream &os, const Label& label) {
os << "\"" << label.label_ << "\"" << "=" << label.text_;
return os;
}
std::ostream& operator << (std::ostream &os, const Formula& formula) {
os << formula.formula_ << "=" << formula.content_;
return os;
}
std::ostream& operator << (std::ostream& os, const Action& action) {
os << action.action_;
return os;
}
std::ostream& operator << (std::ostream& os, const Module& module) {
os << "Module: " << module.module_ << std::endl;
for (auto& action : module.actions_) {
os << action << std::endl;
}
return os;
}
YAML::Node YAML::convert<Module>::encode(const Module& rhs) {
YAML::Node node;
node.push_back(rhs.module_);
node.push_back(rhs.actions_);
return node;
}
bool YAML::convert<Module>::decode(const YAML::Node& node, Module& rhs) {
if (!node.Type() == NodeType::Map) {
return false;
}
rhs.actions_ = node["actions"].as<std::vector<Action>>();
rhs.module_ = node["module"].as<std::string>();
return true;
}
YAML::Node YAML::convert<Action>::encode(const Action& rhs) {
YAML::Node node;
node.push_back(rhs.action_);
node.push_back(rhs.guard_);
node.push_back(rhs.overwrite_);
node.push_back(rhs.update_);
return node;
}
bool YAML::convert<Action>::decode(const YAML::Node& node, Action& rhs) {
if (!node.Type() == NodeType::Map) {
return false;
}
rhs.action_ = node["action"].as<std::string>();
rhs.guard_ = node["guard"].as<std::string>();
rhs.update_ = node["update"].as<std::string>();
if (node["overwrite"]) {
rhs.overwrite_ = node["overwrite"].as<bool>();
}
return true;
}
YAML::Node YAML::convert<Label>::encode(const Label& rhs) {
YAML::Node node;
node.push_back(rhs.label_);
node.push_back(rhs.text_);
return node;
}
bool YAML::convert<Label>::decode(const YAML::Node& node, Label& rhs) {
if (!node.Type() == NodeType::Map) {
return false;
}
rhs.label_ = node["label"].as<std::string>();
rhs.text_ = node["text"].as<std::string>();
if (node["overwrite"]) {
rhs.overwrite_ = node["overwrite"].as<bool>();
}
return true;
}
YAML::Node YAML::convert<Formula>::encode(const Formula& rhs) {
YAML::Node node;
node.push_back(rhs.content_);
node.push_back(rhs.formula_);
node.push_back(rhs.overwrite_);
return node;
}
bool YAML::convert<Formula>::decode(const YAML::Node& node, Formula& rhs) {
if (!node.Type() == NodeType::Map) {
return false;
}
rhs.formula_ = node["formula"].as<std::string>();
rhs.content_ = node["content"].as<std::string>();
if(node["overwrite"]) {
rhs.overwrite_ = node["overwrite"].as<bool>();
}
return true;
}
std::vector<Configuration> YamlConfigParser::parseConfiguration() {
std::vector<Configuration> configuration;
try {
YAML::Node config = YAML::LoadFile(file_);
const std::vector<Label> labels = config["labels"].as<std::vector<Label>>();
const std::vector<Formula> formulas = config["formulas"].as<std::vector<Formula>>();
const std::vector<Module> modules = config["modules"].as<std::vector<Module>>();
for (auto& label : labels) {
configuration.push_back({label.text_, label.label_, ConfigType::Label, label.overwrite_});
}
for (auto& formula : formulas) {
configuration.push_back({formula.content_, formula.formula_ , ConfigType::Formula, formula.overwrite_});
}
for (auto& module : modules) {
std::cout << module << std::endl;
}
}
catch(const std::exception& e) {
std::cout << "Exception '" << typeid(e).name() << "' caught:" << std::endl;
std::cout << "\t" << e.what() << std::endl;
std::cout << "while parsing configuration " << file_ << std::endl;
}
return configuration;
}

115
util/ConfigYaml.h

@ -0,0 +1,115 @@
#pragma once
#include <vector>
#include <ostream>
#include "yaml-cpp/yaml.h"
typedef std::string expressions;
enum class ConfigType : char {
Label = 'L',
Formula = 'F',
Module = 'M'
};
struct Configuration
{
expressions expressions_;
std::string derivation_;
ConfigType type_ {ConfigType::Label};
bool overwrite_;
Configuration() = default;
Configuration(std::string expression, std::string derivation, ConfigType type, bool overwrite = false) : expressions_(expression), derivation_(derivation), type_(type), overwrite_(overwrite) {}
~Configuration() = default;
Configuration(const Configuration&) = default;
friend std::ostream& operator << (std::ostream& os, const Configuration& config) {
os << "Configuration with Type: " << static_cast<char>(config.type_) << std::endl;
os << "\tExpression=" << config.expressions_ << std::endl;
return os << "\tDerviation=" << config.derivation_;
}
};
struct Label {
private:
public:
std::string text_;
std::string label_;
bool overwrite_;
friend std::ostream& operator <<(std::ostream &os, const Label& label);
};
struct Formula {
private:
public:
std::string formula_;
std::string content_;
bool overwrite_;
friend std::ostream& operator << (std::ostream &os, const Formula& formula);
};
struct Action {
public:
std::string action_;
std::string guard_;
std::string update_;
bool overwrite_;
friend std::ostream& operator << (std::ostream& os, const Action& action);
};
struct Module {
public:
std::vector<Action> actions_;
std::string module_;
friend std::ostream& operator << (std::ostream& os, const Module& module);
};
template<>
struct YAML::convert<Module> {
static YAML::Node encode(const Module& rhs);
static bool decode(const YAML::Node& node, Module& rhs);
};
template<>
struct YAML::convert<Action> {
static YAML::Node encode(const Action& rhs);
static bool decode(const YAML::Node& node, Action& rhs);
};
template<>
struct YAML::convert<Label> {
static YAML::Node encode(const Label& rhs);
static bool decode(const YAML::Node& node, Label& rhs);
};
template<>
struct YAML::convert<Formula> {
static YAML::Node encode(const Formula& rhs);
static bool decode(const YAML::Node& node, Formula& rhs);
};
struct YamlConfigParser {
public:
YamlConfigParser(std::string file) : file_(file) {}
YamlConfigParser(const YamlConfigParser&) = delete;
~YamlConfigParser() = default;
std::vector<Configuration> parseConfiguration();
private:
std::string file_;
};

2
util/Grid.h

@ -7,7 +7,7 @@
#include "MinigridGrammar.h"
#include "PrismModulesPrinter.h"
#include "ConfigGrammar.h"
#include "ConfigYaml.h"
struct GridOptions {
std::vector<AgentName> agentsToBeConsidered;

2
util/PrismModulesPrinter.h

@ -3,8 +3,8 @@
#include <iostream>
#include <functional>
#include "MinigridGrammar.h"
#include "ConfigGrammar.h"
#include "PrismPrinter.h"
#include "ConfigYaml.h"
namespace prism {
class PrismModulesPrinter {

Loading…
Cancel
Save