From 69abe4a8e3ecec44f44643fb811e5bf0bc3ae8f0 Mon Sep 17 00:00:00 2001 From: George Marchment <georgemarchment@yahoo.fr> Date: Tue, 22 Apr 2025 11:00:28 +0200 Subject: [PATCH] Renaming channels inside subworklfows -> to avoid issues when wanting to write the subworkflow in the main (in the case of multiple channels) --- src/code_.py | 8 ++++++-- src/subworkflow.py | 36 ++++++++++++++++++++++++++++++++---- src/workflow.py | 2 +- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/code_.py b/src/code_.py index 0213fc4..adb4c76 100644 --- a/src/code_.py +++ b/src/code_.py @@ -175,6 +175,7 @@ class Code: def rewrite_ternary_operation_to_normal_condition(self, code): pattern = r"(def)? *(\w+) *\= *([^?\n]+) *\? *([^:\n]+) *\: *([^\n]+)\n" to_replace = [] + checked = [] for match in re.finditer(pattern, code): def_variable = "" if(match.group(1)!=None): @@ -191,17 +192,20 @@ class Code: #Here we check that it's worked correctly -> that we have done a good parsing if(get_parenthese_count(condition)==0 and get_parenthese_count(exp1)==0 and get_parenthese_count(exp2)==0 and get_curly_count(condition)==0 and get_curly_count(exp1)==0 and get_curly_count(exp2)==0): to_replace.append((old, new)) + else: + checked.append(match.group(0)) for r in to_replace: old, new = r self.add_to_ternary_operation_dico(old, new) tmp = code - code = code.replace(old, new) + code = code.replace(old, new, 1) if(old!=new and tmp==code): raise Exception("This shouldn't happen -> the code wasn't replaced") #Check if there is still a ternary operation in this case we cannot analyse it #Cause it is a complexe/multiple ternanry operation for match in re.finditer(pattern, code): - raise BioFlowInsightError(f"Detected a multi ternary operation (a ternary operation in a ternary operation) in the file '{self.origin.get_file_address()}'. BioFlow-Insight does not support this, try defining it in a different way.", type="Multi ternary operation") + if(match.group(0) not in checked): + raise BioFlowInsightError(f"Detected a multi ternary operation (a ternary operation in a ternary operation) in the file '{self.origin.get_file_address()}'. BioFlow-Insight does not support this, try defining it in a different way.", type="Multi ternary operation") return code def rewrite_jump_dot(self, code): diff --git a/src/subworkflow.py b/src/subworkflow.py index 17a39e3..4749628 100644 --- a/src/subworkflow.py +++ b/src/subworkflow.py @@ -103,6 +103,22 @@ class Subworkflow(Main): return self.get_code_with_alias_and_id() def simplify_code(self): + name = self.get_alias() + + code = self.get_code() + #Renaming the channels defined inside the subworkflow + executors = {} + self.root.get_all_executors_in_subworkflow(calls=executors) + channels_to_rename = [] + for exe in executors: + if(exe.get_type()=="Operation"): + for g in exe.gives: + channels_to_rename.append(g.get_code()) + #channels_to_rename+=exe.gives + #for ch in channels_to_rename: + # code = replace_group1(code, fr"[^\w]({re.escape(ch.get_code())})[^\w]", f"{ch.get_code()}_{name}") + + code = super().simplify_code() #Putting the take, main and emit in that order, if needs be @@ -129,12 +145,12 @@ class Subworkflow(Main): start, _ = match.span(0) code_up_to_emit = code[:start] code_after_emit = code[start:] - name = self.get_alias() + for t in self.take: if(len(t.get_gives())!=1): raise Exception("This shoudn't happen") 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}") + code_up_to_emit = replace_group1(code_up_to_emit, fr"[^\w\.]({re.escape(ch.get_code())})[^\w]", f"{ch.get_code()}_{name}") @@ -145,7 +161,7 @@ class Subworkflow(Main): ch = e.gives[0] if(ch.get_type()=="Channel"): 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_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") elif(ch.get_type()=="Emitted"): @@ -157,7 +173,7 @@ class Subworkflow(Main): ch = e.origins[0] if(ch.get_type()=="Channel"): 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_after_emit = replace_group1(code_after_emit, fr"[^\w\.]({re.escape(ch.get_code())})[^\w]", f"{ch.get_code()}_{name}") 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") @@ -199,6 +215,18 @@ class Subworkflow(Main): # raise Exception("This shoudn't happen -> code hasn't been replaced") code = code_up_to_emit+'\n'+code_after_emit + + #Renaming the channels defined inside the subworkflow + for ch in channels_to_rename: + code = replace_group1(code, fr"[^\w\.]({re.escape(ch)})[^\w]", f"{ch}_{name}") + + to_replace = [] + for match in re.finditer(r"(\w+) *= *(\w+) *\n", code): + if(match.group(1)==match.group(2)): + to_replace.append(match.group(0)) + for r in to_replace: + code = code.replace(r, "", 1) + return code diff --git a/src/workflow.py b/src/workflow.py index 722b9ec..8bbd39a 100644 --- a/src/workflow.py +++ b/src/workflow.py @@ -1047,7 +1047,7 @@ George Marchment, Bryan Brancotte, Marie Schmit, Frédéric Lemoine, Sarah Cohen #And used as a parameter in a call in the same bloc #And that the channel is defined somewhere else in the code def check_that_a_channel_is_not_defined_used_and_redefined_used_in_another_block(self): - self.channel_that_is_defined_used_and_redefined_used_in_another_block = False + self.channel_that_is_defined_used_and_redefined_used_in_another_block = "" for main in [self.get_workflow_main()]+self.get_subworkflows_called(): problematic_channel = main.check_that_a_channel_is_not_defined_used_and_redefined_used_in_another_block() if(problematic_channel!=None): -- GitLab