From 17590518dedd7f78667476f0537e71440b7b4340 Mon Sep 17 00:00:00 2001 From: Rasmus Jakobsson Date: Mon, 21 Oct 2024 10:02:38 +0200 Subject: [PATCH] Fixed such that the parser doesnt get stuck when models has loops --- bpmnconstraints/parser/bpmn_parser.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/bpmnconstraints/parser/bpmn_parser.py b/bpmnconstraints/parser/bpmn_parser.py index b6b6982..971e7ca 100644 --- a/bpmnconstraints/parser/bpmn_parser.py +++ b/bpmnconstraints/parser/bpmn_parser.py @@ -55,6 +55,7 @@ def run(self): self.__flatten_model() else: self.model = XmlModel(self.bpmn_model) + self.__parse() self.__mark_gateway_elements() if self.transitivity: @@ -115,22 +116,34 @@ def __get_parsed_cfo_by_bpmn_element(self, elem): if parsed_cfo.get("id") == elem_id: return parsed_cfo - def __find_transitive_closure(self, cfo, transitivity): + def __find_transitive_closure(self, cfo, transitivity, visited): + # Check if the current node has already been visited to prevent a loop + if cfo.get("id") in visited: + print(f"Already visited {cfo.get('id')}, skipping to avoid a loop.") + return + + # Mark the current node as visited + visited.add(cfo.get("id")) + if cfo: for successor in cfo.get("successor"): successor_id = successor.get("id") + successor = self.__get_cfo_by_id(successor_id) if successor: if "is in gateway" not in successor: transitivity.append(successor) for successor in cfo.get("successor"): successor_cfo = self.__get_cfo_by_id(successor.get("id")) - self.__find_transitive_closure(successor_cfo, transitivity) + self.__find_transitive_closure(successor_cfo, transitivity, visited) def __add_transitivity(self): for cfo in self.sequence: transitivity = [] - self.__find_transitive_closure(cfo, transitivity) + + visited = set() # Initialize the visited set for cycle detection + self.__find_transitive_closure(cfo, transitivity, visited) + if transitivity: cfo.update({"transitivity": transitivity})