From 71909cd6b50d37ad5a56f71a534c34875ec9ba1c Mon Sep 17 00:00:00 2001
From: Franck Dary <franck.dary@lis-lab.fr>
Date: Mon, 17 Feb 2020 11:03:01 +0100
Subject: [PATCH] Checking for cycles in action attach

---
 reading_machine/src/Action.cpp | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/reading_machine/src/Action.cpp b/reading_machine/src/Action.cpp
index b24d547..94bfee7 100644
--- a/reading_machine/src/Action.cpp
+++ b/reading_machine/src/Action.cpp
@@ -358,7 +358,30 @@ Action Action::attach(Object governorObject, int governorIndex, Object dependent
       govLineIndex = config.getStack(governorIndex);
     }
 
-    return addHypothesisRelative(Config::headColName, dependentObject, dependentIndex, std::to_string(govLineIndex)).appliable(config, action);
+    int depLineIndex = 0;
+    if (dependentObject == Object::Buffer)
+    {
+      depLineIndex = config.getWordIndex() + dependentIndex;
+      if (!config.has(0, depLineIndex, 0))
+        return false;
+    }
+    else
+    {
+      if (!config.hasStack(dependentIndex))
+        return false;
+      depLineIndex = config.getStack(dependentIndex);
+    }
+
+    // Check for cycles
+    while (govLineIndex != depLineIndex)
+    {
+      try
+      {
+        govLineIndex = std::stoi(config.getLastNotEmptyHypConst(Config::headColName, govLineIndex));
+      } catch(std::exception &) {return true;}
+    }
+
+    return false;
   };
 
   return {Type::Write, apply, undo, appliable}; 
-- 
GitLab