From 60e8b2f580e50dbb27e7a06ae9267b89e6f97555 Mon Sep 17 00:00:00 2001
From: Franck Dary <franck.dary@lis-lab.fr>
Date: Thu, 19 Dec 2019 21:01:49 +0100
Subject: [PATCH] Added classes Action and Transition

---
 reading_machine/include/Action.hpp     | 43 +++++++++++++++
 reading_machine/include/Config.hpp     |  2 +
 reading_machine/include/Transition.hpp | 12 +++++
 reading_machine/src/Action.cpp         | 75 ++++++++++++++++++++++++++
 reading_machine/src/Config.cpp         | 20 ++++++-
 reading_machine/src/Transition.cpp     |  3 ++
 6 files changed, 153 insertions(+), 2 deletions(-)
 create mode 100644 reading_machine/include/Action.hpp
 create mode 100644 reading_machine/include/Transition.hpp
 create mode 100644 reading_machine/src/Action.cpp
 create mode 100644 reading_machine/src/Transition.cpp

diff --git a/reading_machine/include/Action.hpp b/reading_machine/include/Action.hpp
new file mode 100644
index 0000000..fa3ea7a
--- /dev/null
+++ b/reading_machine/include/Action.hpp
@@ -0,0 +1,43 @@
+#ifndef ACTION__H
+#define ACTION__H
+
+#include <functional>
+#include <string>
+#include <vector>
+#include "Config.hpp"
+
+class Action
+{
+  public :
+
+  enum Type
+  {
+    Push,
+    Pop,
+    Write,
+    MoveWord,
+    MoveChar,
+    AddLines,
+    Check
+  };
+
+  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);
+
+  public :
+
+  static Action addLinesIfNeeded(int nbLines);
+  static Action moveWordIndex(int movement);
+  static Action moveCharacterIndex(int movement);
+};
+
+#endif
diff --git a/reading_machine/include/Config.hpp b/reading_machine/include/Config.hpp
index c046f1f..ac71993 100644
--- a/reading_machine/include/Config.hpp
+++ b/reading_machine/include/Config.hpp
@@ -72,6 +72,8 @@ class Config
   const String & getConst(const std::string & colName, int lineIndex, int hypothesisIndex) const;
   String & getLastNotEmpty(const std::string & colName, int lineIndex);
   const String & getLastNotEmptyConst(const std::string & colName, int lineIndex) const;
+  String & getFirstEmpty(int colIndex, int lineIndex);
+  String & getFirstEmpty(const std::string & colName, int lineIndex);
   bool hasCharacter(int letterIndex) const;
   util::utf8char getLetter(int letterIndex) const;
   void addToHistory(const std::string & transition);
diff --git a/reading_machine/include/Transition.hpp b/reading_machine/include/Transition.hpp
new file mode 100644
index 0000000..b55dac0
--- /dev/null
+++ b/reading_machine/include/Transition.hpp
@@ -0,0 +1,12 @@
+#ifndef TRANSITION__H
+#define TRANSITION__H
+
+#include <vector>
+
+class Transition
+{
+  private :
+
+};
+
+#endif
diff --git a/reading_machine/src/Action.cpp b/reading_machine/src/Action.cpp
new file mode 100644
index 0000000..54fb1e6
--- /dev/null
+++ b/reading_machine/src/Action.cpp
@@ -0,0 +1,75 @@
+#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)
+{
+  this->type = type;
+  this->apply = apply;
+  this->undo = undo;
+  this->appliable = appliable;
+}
+
+Action Action::addLinesIfNeeded(int nbLines)
+{
+  auto apply = [nbLines](Config & config, Action &)
+  {
+    config.addLines(1);
+  };
+
+  auto undo = [](Config &, Action &)
+  {
+  };
+
+  auto appliable = [](Config &, Action &)
+  {
+    return true;
+  };
+
+  return {Type::AddLines, apply, undo, appliable};
+}
+
+Action Action::moveWordIndex(int movement)
+{
+  auto apply = [movement](Config & config, Action &)
+  {
+    config.moveWordIndex(movement);
+  };
+
+  auto undo = [movement](Config & config, Action &)
+  {
+    config.moveWordIndex(movement);
+  };
+
+  auto appliable = [movement](Config & config, Action &)
+  {
+    bool possible = config.moveWordIndex(movement);
+    if (possible)
+      moveWordIndex(-movement);
+    return possible;
+  };
+
+  return {Type::MoveWord, apply, undo, appliable};
+}
+
+Action Action::moveCharacterIndex(int movement)
+{
+  auto apply = [movement](Config & config, Action &)
+  {
+    config.moveCharacterIndex(movement);
+  };
+
+  auto undo = [movement](Config & config, Action &)
+  {
+    config.moveCharacterIndex(movement);
+  };
+
+  auto appliable = [movement](Config & config, Action &)
+  {
+    bool possible = config.moveCharacterIndex(movement);
+    if (possible)
+      moveCharacterIndex(-movement);
+    return possible;
+  };
+
+  return {Type::MoveChar, apply, undo, appliable};
+}
+
diff --git a/reading_machine/src/Config.cpp b/reading_machine/src/Config.cpp
index 9cde239..f89578b 100644
--- a/reading_machine/src/Config.cpp
+++ b/reading_machine/src/Config.cpp
@@ -166,6 +166,22 @@ Config::String & Config::getLastNotEmpty(int colIndex, int lineIndex)
   return lines[baseIndex];
 }
 
+Config::String & Config::getFirstEmpty(int colIndex, int lineIndex)
+{
+  int baseIndex = getIndexOfLine(lineIndex-getFirstLineIndex()) + getIndexOfCol(colIndex);
+
+  for (int i = 0; i <= nbHypothesesMax; ++i)
+    if (util::isEmpty(lines[baseIndex+i]))
+      return lines[baseIndex+i];
+
+  return lines[baseIndex+nbHypothesesMax]; 
+}
+
+Config::String & Config::getFirstEmpty(const std::string & colName, int lineIndex)
+{
+  return getFirstEmpty(getColIndex(colName), lineIndex);
+}
+
 const Config::String & Config::getLastNotEmptyConst(int colIndex, int lineIndex) const
 {
   int baseIndex = getIndexOfLine(lineIndex-getFirstLineIndex()) + getIndexOfCol(colIndex);
@@ -241,11 +257,11 @@ bool Config::isToken(std::size_t lineIndex) const
 bool Config::moveWordIndex(int relativeMovement)
 {
   int nbMovements = 0;
+  int oldVal = wordIndex;
   while (nbMovements != relativeMovement)
   {
     do
     {
-      int oldVal = wordIndex;
       relativeMovement > 0 ? wordIndex++ : wordIndex--;
       if (!has(0,wordIndex,0))
       {
@@ -262,9 +278,9 @@ bool Config::moveWordIndex(int relativeMovement)
 
 bool Config::moveCharacterIndex(int relativeMovement)
 {
+  int oldVal = characterIndex;
   for (int i = 0; i < relativeMovement; i++)
   {
-    int oldVal = characterIndex;
     relativeMovement > 0 ? characterIndex++ : characterIndex--;
     if (!hasCharacter(characterIndex))
     {
diff --git a/reading_machine/src/Transition.cpp b/reading_machine/src/Transition.cpp
new file mode 100644
index 0000000..0819cf0
--- /dev/null
+++ b/reading_machine/src/Transition.cpp
@@ -0,0 +1,3 @@
+#include "Transition.hpp"
+
+
-- 
GitLab