From a74b125288684b25ebb993968893c0d98deb475a Mon Sep 17 00:00:00 2001 From: Franck Dary <franck.dary@lis-lab.fr> Date: Sun, 12 Jan 2020 20:09:48 +0100 Subject: [PATCH] TransitionSet can now give vector of appliable transitions --- reading_machine/include/Action.hpp | 11 ++++++--- reading_machine/include/Config.hpp | 2 ++ reading_machine/include/Transition.hpp | 3 +++ reading_machine/include/TransitionSet.hpp | 1 + reading_machine/src/Action.cpp | 22 ++++++----------- reading_machine/src/Config.cpp | 30 +++++++++++++++++------ reading_machine/src/Transition.cpp | 20 +++++++++++++++ reading_machine/src/TransitionSet.cpp | 18 ++++++++++++++ 8 files changed, 82 insertions(+), 25 deletions(-) diff --git a/reading_machine/include/Action.hpp b/reading_machine/include/Action.hpp index e6b58dc..3fd0aef 100644 --- a/reading_machine/include/Action.hpp +++ b/reading_machine/include/Action.hpp @@ -30,14 +30,17 @@ class Action private : Type type; - std::function<void(Config & config, Action & action)> apply; - std::function<void(Config & config, Action & action)> undo; - std::function<bool(Config & config, Action & action)> appliable; std::vector<std::string> data; public : - Action(Type type, std::function<void(Config & config, Action & action)> apply, std::function<void(Config & config, Action & action)> undo, std::function<bool(Config & config, Action & action)> appliable); + std::function<void(Config & config, Action & action)> apply; + std::function<void(Config & config, Action & action)> undo; + std::function<bool(const Config & config, const Action & action)> appliable; + + private : + + Action(Type type, std::function<void(Config & config, Action & action)> apply, std::function<void(Config & config, Action & action)> undo, std::function<bool(const Config & config, const Action & action)> appliable); public : diff --git a/reading_machine/include/Config.hpp b/reading_machine/include/Config.hpp index 697418c..6738810 100644 --- a/reading_machine/include/Config.hpp +++ b/reading_machine/include/Config.hpp @@ -83,7 +83,9 @@ class Config bool isEmptyNode(std::size_t lineIndex) const; bool isToken(std::size_t lineIndex) const; bool moveWordIndex(int relativeMovement); + bool canMoveWordIndex(int relativeMovement) const; bool moveCharacterIndex(int relativeMovement); + bool canMoveCharacterIndex(int relativeMovement) const; bool rawInputOnlySeparatorsLeft() const; std::size_t getWordIndex() const; std::size_t getCharacterIndex() const; diff --git a/reading_machine/include/Transition.hpp b/reading_machine/include/Transition.hpp index a250fb2..ecd90df 100644 --- a/reading_machine/include/Transition.hpp +++ b/reading_machine/include/Transition.hpp @@ -20,6 +20,9 @@ class Transition public : Transition(const std::string & name); + void apply(Config & config); + bool appliable(const Config & config) const; + int getCost(const Config & config) const; }; #endif diff --git a/reading_machine/include/TransitionSet.hpp b/reading_machine/include/TransitionSet.hpp index b1ffd84..04c1e90 100644 --- a/reading_machine/include/TransitionSet.hpp +++ b/reading_machine/include/TransitionSet.hpp @@ -17,6 +17,7 @@ class TransitionSet public : TransitionSet(const std::string & filename); + std::vector<std::pair<Transition &, int>> getAppliableTransitionsCosts(const Config & c); }; #endif diff --git a/reading_machine/src/Action.cpp b/reading_machine/src/Action.cpp index a470bab..6de5017 100644 --- a/reading_machine/src/Action.cpp +++ b/reading_machine/src/Action.cpp @@ -1,6 +1,6 @@ #include "Action.hpp" -Action::Action(Action::Type type, std::function<void(Config & config, Action & action)> apply, std::function<void(Config & config, Action & action)> undo, std::function<bool(Config & config, Action & action)> appliable) +Action::Action(Action::Type type, std::function<void(Config & config, Action & action)> apply, std::function<void(Config & config, Action & action)> undo, std::function<bool(const Config & config, const Action & action)> appliable) { this->type = type; this->apply = apply; @@ -19,7 +19,7 @@ Action Action::addLinesIfNeeded(int nbLines) { }; - auto appliable = [](Config &, Action &) + auto appliable = [](const Config &, const Action &) { return true; }; @@ -39,12 +39,9 @@ Action Action::moveWordIndex(int movement) config.moveWordIndex(movement); }; - auto appliable = [movement](Config & config, Action &) + auto appliable = [movement](const Config & config, const Action &) { - bool possible = config.moveWordIndex(movement); - if (possible) - moveWordIndex(-movement); - return possible; + return config.canMoveWordIndex(movement); }; return {Type::MoveWord, apply, undo, appliable}; @@ -62,12 +59,9 @@ Action Action::moveCharacterIndex(int movement) config.moveCharacterIndex(movement); }; - auto appliable = [movement](Config & config, Action &) + auto appliable = [movement](const Config & config, const Action &) { - bool possible = config.moveCharacterIndex(movement); - if (possible) - moveCharacterIndex(-movement); - return possible; + return config.canMoveCharacterIndex(movement); }; return {Type::MoveChar, apply, undo, appliable}; @@ -85,7 +79,7 @@ Action Action::addHypothesis(const std::string & colName, std::size_t lineIndex, config.getLastNotEmpty(colName, lineIndex) = ""; }; - auto appliable = [colName, lineIndex](Config & config, Action &) + auto appliable = [colName, lineIndex](const Config & config, const Action &) { return config.has(colName, lineIndex, 0); }; @@ -117,7 +111,7 @@ Action Action::addHypothesisRelative(const std::string & colName, Object object, return addHypothesis(colName, lineIndex, "").undo(config, a); }; - auto appliable = [colName, object, relativeIndex](Config & config, Action & a) + auto appliable = [colName, object, relativeIndex](const Config & config, const Action & a) { int lineIndex = 0; if (object == Object::Buffer) diff --git a/reading_machine/src/Config.cpp b/reading_machine/src/Config.cpp index a8d8134..96500a7 100644 --- a/reading_machine/src/Config.cpp +++ b/reading_machine/src/Config.cpp @@ -275,22 +275,38 @@ bool Config::moveWordIndex(int relativeMovement) return true; } -bool Config::moveCharacterIndex(int relativeMovement) +bool Config::canMoveWordIndex(int relativeMovement) const { - int oldVal = characterIndex; - for (int i = 0; i < relativeMovement; i++) + int nbMovements = 0; + int oldVal = wordIndex; + while (nbMovements != relativeMovement) { - relativeMovement > 0 ? characterIndex++ : characterIndex--; - if (!hasCharacter(characterIndex)) + do { - characterIndex = oldVal; - return false; + relativeMovement > 0 ? oldVal++ : oldVal--; + if (!has(0,oldVal,0)) + return false; } + while (!isToken(oldVal)); + nbMovements += relativeMovement > 0 ? 1 : -1; } return true; } +bool Config::moveCharacterIndex(int relativeMovement) +{ + int oldVal = characterIndex; + characterIndex = std::max(0, (int)std::min(characterIndex+relativeMovement, util::getSize(rawInput))); + return (int)characterIndex == oldVal + relativeMovement; +} + +bool Config::canMoveCharacterIndex(int relativeMovement) const +{ + int target = std::max(0, (int)std::min(characterIndex+relativeMovement, util::getSize(rawInput))); + return target == (int)characterIndex + relativeMovement; +} + bool Config::rawInputOnlySeparatorsLeft() const { for (unsigned int i = characterIndex; i < rawInput.size(); i++) diff --git a/reading_machine/src/Transition.cpp b/reading_machine/src/Transition.cpp index b0ced36..d8b9c8a 100644 --- a/reading_machine/src/Transition.cpp +++ b/reading_machine/src/Transition.cpp @@ -19,6 +19,26 @@ Transition::Transition(const std::string & name) } +void Transition::apply(Config & config) +{ + for (Action & action : sequence) + action.apply(config, action); +} + +bool Transition::appliable(const Config & config) const +{ + for (const Action & action : sequence) + if (!action.appliable(config, action)) + return false; + + return true; +} + +int Transition::getCost(const Config & config) const +{ + return cost(config); +} + void Transition::initWrite(std::string colName, std::string object, std::string index, std::string value) { auto objectValue = Action::str2object(object); diff --git a/reading_machine/src/TransitionSet.cpp b/reading_machine/src/TransitionSet.cpp index 1bf42b1..467ad42 100644 --- a/reading_machine/src/TransitionSet.cpp +++ b/reading_machine/src/TransitionSet.cpp @@ -23,3 +23,21 @@ TransitionSet::TransitionSet(const std::string & filename) std::fclose(file); } +std::vector<std::pair<Transition &, int>> TransitionSet::getAppliableTransitionsCosts(const Config & c) +{ + using Pair = std::pair<Transition &, int>; + std::vector<Pair> appliableTransitions; + + for (auto & transition : transitions) + if (transition.appliable(c)) + appliableTransitions.emplace_back(transition, transition.getCost(c)); + + std::sort(appliableTransitions.begin(), appliableTransitions.end(), + [](const Pair & a, const Pair & b) + { + return a.second < b.second; + }); + + return appliableTransitions; +} + -- GitLab