From af7088dfb654e7144904206c9001241474cff3b3 Mon Sep 17 00:00:00 2001
From: George Marchment <georgemarchment@yahoo.fr>
Date: Mon, 31 Mar 2025 11:55:18 +0200
Subject: [PATCH] Added thr renaming of the emits of subworkflows inside and
 outiside (outside its at the emiited obkject level) -> it was creating issues
 when "breaking subworkflows" + remmoved the remove GGs method -> need to do
 it in a smarter way

---
 src/emitted.py     |  6 +++++
 src/subworkflow.py | 58 +++++++++++++++++++++++++++++++++++++---------
 src/workflow.py    |  2 +-
 3 files changed, 54 insertions(+), 12 deletions(-)

diff --git a/src/emitted.py b/src/emitted.py
index 42e5475..ba60636 100644
--- a/src/emitted.py
+++ b/src/emitted.py
@@ -25,6 +25,12 @@ class Emitted(Channel):
         old_name = thing_which_emits.get_alias()
         new_call_name = thing_which_emits.get_alias_and_id()
         code = re.sub(fr'{re.escape(old_name)} *\.', f'{new_call_name}.', code)
+        #In the cas of a subworklfow -> we replace the last word of the emit by a rewritten version containing the suborklow's alias (to avoid duplicates)
+        thing_which_emits = self.emitted_by.get_first_element_called()
+        if(thing_which_emits.get_type()=="Subworkflow"):
+            if(code.count('.')==2):
+                last_word = code.split(".")[-1]
+                code = code.replace(last_word, f"{last_word}_{thing_which_emits.get_alias()}")
         return code
 
     def get_emitted_by(self):
diff --git a/src/subworkflow.py b/src/subworkflow.py
index d9c8ff0..7fc58c9 100644
--- a/src/subworkflow.py
+++ b/src/subworkflow.py
@@ -116,20 +116,56 @@ class Subworkflow(Main):
             ch = t.get_gives()[0]
             code_up_to_emit = replace_group1(code_up_to_emit, fr"[^\w]({re.escape(ch.get_code())})[^\w]", f"{ch.get_code()}_{name}")
 
-        #Renaming the takes in the emits -> if the takes are given as emits
+
+
+        #Renaming the emits so that the name of the subworkflow appears in the param name
+        #The renaming of the emits outside the subworkflow is done at the emit level (when we simplify the emit)
         for e in self.emit:
-            channels_take = []
-            for t in self.take:
-                channels_take.append(t.get_gives()[0])
-            re_write_channel = False
-            for o in e.origins:
-                if(o in channels_take):
-                    re_write_channel = True
-            if(re_write_channel):
+            if(len(e.gives)==1):
+                ch = e.gives[0]
+                temp = code_after_emit
+                code_after_emit = replace_group1(code_after_emit, fr"[^\w]({re.escape(ch.get_code())})[^\w]", f"{ch.get_code()}_{name}")
+                if(temp==code_after_emit):
+                    raise Exception("This shoudn't happen -> code hasn't been replaced")
+            else:
                 ch = e.origins[0]
+                temp = code_after_emit
                 code_after_emit = replace_group1(code_after_emit, fr"[^\w]({re.escape(ch.get_code())})[^\w]", f"{ch.get_code()}_{name}")
-
-        code = code_up_to_emit+code_after_emit
+                code_up_to_emit+=f"\n{ch.get_code()}_{name} = {ch.get_code()}"
+                if(temp==code_after_emit):
+                    raise Exception("This shoudn't happen -> code hasn't been replaced")
+
+
+        #In the emits replacing the 'ch = ....' by 'ch' and adding 'ch = ....' at the end of the body
+        to_replace = []
+        for match in re.finditer(r"(\w+) *= *.+", code_after_emit):
+            old, new = match.group(0), match.group(1)
+            to_replace.append((old, new))
+        for r in to_replace:
+            old, new = r
+            code_up_to_emit+=f"\n{old}"
+            temp = code_after_emit
+            code_after_emit = code_after_emit.replace(old, new)
+            if(temp==code_after_emit):
+                raise Exception("This shoudn't happen -> code hasn't been replaced")
+
+        ##Renaming the takes in the emits -> if the takes are given as emits
+        #for e in self.emit:
+        #    channels_take = []
+        #    for t in self.take:
+        #        channels_take.append(t.get_gives()[0])
+        #    re_write_channel = False
+        #    for o in e.origins:
+        #        if(o in channels_take):
+        #            re_write_channel = True
+        #    if(re_write_channel):
+        #        ch = e.origins[0]
+        #        temp = code_after_emit
+        #        code_after_emit = replace_group1(code_after_emit, fr"[^\w]({re.escape(ch.get_code())})[^\w]", f"{ch.get_code()}_{name}")
+        #        if(temp==code_after_emit):
+        #            raise Exception("This shoudn't happen -> code hasn't been replaced")
+
+        code = code_up_to_emit+'\n'+code_after_emit
         return code
 
 
diff --git a/src/workflow.py b/src/workflow.py
index b7497c7..1277eda 100644
--- a/src/workflow.py
+++ b/src/workflow.py
@@ -1221,7 +1221,7 @@ George Marchment, Bryan Brancotte, Marie Schmit, Frédéric Lemoine, Sarah Cohen
             code = code.replace("$OR$", "||")
             code = self.put_modified_operations_back(code, map_element_dico)
             code = remove_extra_jumps(format_with_tabs(code))
-            code = self.remove_GG_from_code(code)
+            #code = self.remove_GG_from_code(code)
             f = open(self.get_output_dir()/ "debug" / "rewritten.nf", "w")
             f.write(code)
             f.close()
-- 
GitLab