Skip to content
Snippets Groups Projects
Commit 9c09bc6c authored by Franck Dary's avatar Franck Dary
Browse files

Defined the Strategy class

parent d6ac94e6
No related branches found
No related tags found
No related merge requests found
......@@ -77,7 +77,7 @@ bool isEmpty(const boost::flyweight<T> & s)
return isEmpty(s.get());
}
bool doIfNameMatch(const std::regex & reg, const std::string & name, const std::function<void(const std::smatch &)> & f);
bool doIfNameMatch(const std::regex & reg, std::string_view name, const std::function<void(const std::smatch &)> & f);
};
......
......@@ -111,10 +111,12 @@ std::string util::int2HumanStr(int number)
return result;
}
bool util::doIfNameMatch(const std::regex & reg, const std::string & name, const std::function<void(const std::smatch &)> & f)
bool util::doIfNameMatch(const std::regex & reg, std::string_view name, const std::function<void(const std::smatch &)> & f)
{
std::smatch sm;
std::regex_match(name, sm, reg);
std::string sname(name);
std::regex_match(sname, sm, reg);
if (sm.empty())
return false;
......
......@@ -3,14 +3,15 @@
#include <memory>
#include "Classifier.hpp"
#include "Strategy.hpp"
class ReadingMachine
{
private :
std::string name;
std::function<std::pair<std::string, int>(const Config & config)> strategy;
std::unique_ptr<Classifier> classifier;
std::unique_ptr<Strategy> strategy;
public :
......
......@@ -3,4 +3,23 @@
#include "Config.hpp"
class Strategy
{
private :
enum Type
{
Incremental,
Sequential
};
Type type;
std::map<std::pair<std::string, std::string>, std::string> edges;
public :
Strategy(const std::vector<std::string_view> & lines);
std::pair<std::string, int> getMovement(const Config & c, const std::string & transition);
};
#endif
#include "ReadingMachine.hpp"
#include "util.hpp"
#include "Strategy.hpp"
ReadingMachine::ReadingMachine(const std::string & filename)
{
std::regex nameRegex("Name : (.+)[\n]");
std::regex strategyRegex("Strategy : (.+)[\n]");
std::regex classifierRegex("Classifier : (.+) (.+) (.+)[\n]");
std::FILE * file = std::fopen(filename.c_str(), "r");
char buffer[1024];
std::string fileContent;
while (!std::feof(file))
{
if (buffer != std::fgets(buffer, 1024, file))
break;
try
{
if (util::doIfNameMatch(nameRegex, buffer, [this](auto sm){name = sm[1];}))
continue;
if (util::doIfNameMatch(strategyRegex, buffer, [this](auto sm){}))
continue;
if (util::doIfNameMatch(classifierRegex, buffer, [this](auto sm){classifier.reset(new Classifier(sm[1], sm[2], sm[3]));}))
continue;
} catch(std::exception & e) {util::myThrow(fmt::format("during reading of '{}' : {}", filename, e.what()));}
fileContent += buffer;
}
std::fclose(file);
auto lines = util::split(fileContent, '\n');
try
{
unsigned int curLine = 0;
if (!util::doIfNameMatch(std::regex("Name : (.+)"), lines[curLine++], [this](auto sm){name = sm[1];}))
util::myThrow("No name specified");
while (util::doIfNameMatch(std::regex("Classifier : (.+) (.+) (.+)"), lines[curLine++], [this](auto sm){classifier.reset(new Classifier(sm[1], sm[2], sm[3]));}));
if (!classifier.get())
util::myThrow("No Classifier specified");
std::vector<std::string_view> restOfFile(lines.begin()+curLine-1, lines.end());
strategy.reset(new Strategy(restOfFile));
} catch(std::exception & e) {util::myThrow(fmt::format("during reading of '{}' : {}", filename, e.what()));}
}
#include "Strategy.hpp"
#include "util.hpp"
Strategy::Strategy(const std::vector<std::string_view> & lines)
{
if (!util::doIfNameMatch(std::regex("Strategy : ((incremental)|(sequential))"), lines[0], [this](auto sm)
{type = sm[1] == "sequential" ? Type::Sequential : Type::Incremental;}))
util::myThrow(fmt::format("Invalid strategy identifier '{}'", lines[0]));
for (unsigned int i = 1; i < lines.size(); i++)
{
auto splited = util::split(lines[i], ' ');
if (splited.size() == 2)
edges[std::pair<std::string,std::string>(splited[0], "")] = splited[1];
else if (splited.size() == 3)
edges[std::pair<std::string,std::string>(splited[0], splited[1])] = splited[2];
else
util::myThrow(fmt::format("Invalid strategy line '{}'", lines[i]));
}
}
std::pair<std::string, int> Strategy::getMovement(const Config & c, const std::string & transition)
{
auto foundSpecific = edges.find(std::make_pair(c.getState(), transition));
auto foundGeneric = edges.find(std::make_pair(c.getState(), ""));
if (foundSpecific != edges.end())
return {foundSpecific->second, 1};
if (foundGeneric != edges.end())
return {foundGeneric->second, 1};
util::myThrow(fmt::format("no suitable movement found for current state '{}' and transition '{}'", c.getState(), transition));
return {"", 0};
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment