-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorith.py
More file actions
50 lines (41 loc) · 1.82 KB
/
algorith.py
File metadata and controls
50 lines (41 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class ChatbotFlowNavigator:
def __init__(self, nodes, edges):
# Crear un índice de nodos para acceso O(1)
self.nodes_index = {node['id']: node for node in nodes}
# Crear un índice de edges por nodo fuente para acceso O(1)
self.edges_by_source = {}
for edge in edges:
source = edge['source']
if source not in self.edges_by_source:
self.edges_by_source[source] = []
self.edges_by_source[source].append(edge)
def find_next_node(self, current_node_id, criteria):
"""
Encuentra el siguiente nodo basado en criterios específicos
Complejidad: O(k) donde k es el número de edges del nodo actual
"""
# Obtener todos los edges que salen del nodo actual
outgoing_edges = self.edges_by_source.get(current_node_id, [])
if not outgoing_edges:
return None
# Aplicar criterios de búsqueda
for edge in outgoing_edges:
if self.matches_criteria(edge, criteria):
target_node_id = edge['target']
return self.nodes_index.get(target_node_id)
return None
def matches_criteria(self, edge, criteria):
"""
Evalúa si un edge cumple con los criterios dados
"""
# Ejemplo de criterios basados en tu estructura
if criteria.get('button_type'):
if criteria['button_type'] in edge.get('sourceHandle', ''):
return True
if criteria.get('edge_type'):
if edge.get('type') == criteria['edge_type']:
return True
if criteria.get('target_contains'):
if criteria['target_contains'] in edge.get('target', ''):
return True
return False