diff --git a/reading_machine/include/Transition.hpp b/reading_machine/include/Transition.hpp index b3b5f0984a07151cb7882975cbc476246c8c0079..a250fb26deb1a5d8f4f5e6207641231c8c6adcf7 100644 --- a/reading_machine/include/Transition.hpp +++ b/reading_machine/include/Transition.hpp @@ -3,6 +3,7 @@ #include <vector> #include "Action.hpp" +#include "Config.hpp" class Transition { @@ -10,6 +11,11 @@ class Transition std::string name; std::vector<Action> sequence; + std::function<int(const Config & config)> cost; + + private : + + void initWrite(std::string colName, std::string object, std::string index, std::string value); public : diff --git a/reading_machine/src/Transition.cpp b/reading_machine/src/Transition.cpp index be2cda7aac226a65f873ad89a8e0df6187fde201..e6466051aad7873241b9db69fa1572086aa5c8b7 100644 --- a/reading_machine/src/Transition.cpp +++ b/reading_machine/src/Transition.cpp @@ -7,21 +7,49 @@ Transition::Transition(const std::string & name) std::regex writeRegex("WRITE ([bs])\\.(.+) (.+) (.+)"); - std::smatch sm; + auto doIfNameMatch = [name](auto & reg, auto init) + { + std::smatch sm; + std::regex_match(name, sm, reg); + if (sm.empty()) + return false; + + init(sm); + + return true; + }; try { - std::regex_match(name, sm, writeRegex); - if (!sm.empty()) - { - sequence.emplace_back(Action::addHypothesisRelative(sm[3], Action::str2object(sm[1]), std::stoi(sm[2]), sm[4])); + if (doIfNameMatch(writeRegex, [this](auto sm){initWrite(sm[3], sm[1], sm[2], sm[4]);})) return; - } - throw std::exception(); + throw std::invalid_argument("no match"); + + } catch (std::exception & e) {util::myThrow(fmt::format("Invalid name '{}' ({})", name, e.what()));} + +} + +void Transition::initWrite(std::string colName, std::string object, std::string index, std::string value) +{ + auto objectValue = Action::str2object(object); + int indexValue = std::stoi(index); + + sequence.emplace_back(Action::addHypothesisRelative(colName, objectValue, indexValue, value)); + + cost = [colName, objectValue, indexValue, value](const Config & config) + { + int lineIndex = 0; + if (objectValue == Action::Object::Buffer) + lineIndex = config.getWordIndex() + indexValue; + else + lineIndex = config.getStack(indexValue); - } catch (std::exception &) {util::myThrow(fmt::format("Invalid name '{}'", name));} + if (config.getConst(colName, lineIndex, 0) == value) + return 0; + return 1; + }; }