From a0abc48e340e2b37338442d841b8a3ee5a58deb4 Mon Sep 17 00:00:00 2001
From: George Marchment <georgemarchment@yahoo.fr>
Date: Mon, 17 Mar 2025 10:03:19 +0100
Subject: [PATCH] Added when replacing by new parameters -> doing the longest
 ones first to avoid problems with replacing by wrong thing

---
 src/workflow.py | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/src/workflow.py b/src/workflow.py
index 16135f8..49da439 100644
--- a/src/workflow.py
+++ b/src/workflow.py
@@ -541,6 +541,7 @@ George Marchment, Bryan Brancotte, Marie Schmit, Frédéric Lemoine, Sarah Cohen
         #Replace old analysis with new analysis (simplified code)
         self.__init__(str(temp_file), display_info = False, duplicate=True, processes_2_remove=processes_2_remove)
         self.initialise()
+        os.remove(temp_file)
         self.graph.initialise(processes_2_remove = self.processes_2_remove)
         if(not self.graph.check_if_process_dependendy_is_equivalent_to_other_without_subworkflows(temp_process_dependency_graph)):
             if(render_graphs==True):
@@ -1028,14 +1029,23 @@ George Marchment, Bryan Brancotte, Marie Schmit, Frédéric Lemoine, Sarah Cohen
                     
                     #Replace names inside subworkflow
                     subworkflow_code = f"workflow {name} {{\n{take}\nmain:\n{body}\n{emit}\n}}"
+
+                    #We do this so that the longest thing are rewritten first in the code -> to avoid problems
+                    takes_param_2_length = {}
+                    takes_param_2_new_param_names = {}
                     for i in range(len(new_param_names)):
-                        if(takes_param[i].get_code() != new_param_names[i]):
+                        takes_param_2_new_param_names[takes_param[i].get_code()] = new_param_names[i]
+                        takes_param_2_length[takes_param[i].get_code()] = len(takes_param[i].get_code())
+                    sorted_takes_param_2_length = {k: v for k, v in sorted(takes_param_2_length.items(), key=lambda item: item[1], reverse=True)}
+                    for take_param in sorted_takes_param_2_length:
+                        new_param = takes_param_2_new_param_names[take_param]
+                        if(take_param != new_param):
                             #pattern = fr"[\=\,\(] *({re.escape(takes_param[i].get_code())})[\s\,\)\.]"
-                            pattern = fr"({re.escape(takes_param[i].get_code())})[\s\,\)\.]"
+                            pattern = fr"({re.escape(take_param)})[\s\,\)\.]"
                             temp = subworkflow_code
-                            subworkflow_code = replace_group1(subworkflow_code, pattern, new_param_names[i])
+                            subworkflow_code = replace_group1(subworkflow_code, pattern, new_param)
                             if(temp==subworkflow_code):
-                                print(takes_param[i].get_code(), new_param_names[i])
+                                print(take_param, new_param)
                                 print(pattern)
                                 print(f'"{subworkflow_code}"')
                                 raise Exception("Something went wrong -> cause the paramter wasn't updated")
-- 
GitLab