-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm.js
More file actions
67 lines (67 loc) · 2.57 KB
/
algorithm.js
File metadata and controls
67 lines (67 loc) · 2.57 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChatbotFlowNavigator = void 0;
var ChatbotFlowNavigator = /** @class */ (function () {
function ChatbotFlowNavigator(nodes, edges) {
// Crear un índice de nodos para acceso O(1)
this.nodesIndex = new Map();
for (var _i = 0, nodes_1 = nodes; _i < nodes_1.length; _i++) {
var node = nodes_1[_i];
this.nodesIndex.set(node.id, node);
}
// Crear un índice de edges por nodo fuente para acceso O(1)
this.edgesBySource = new Map();
for (var _a = 0, edges_1 = edges; _a < edges_1.length; _a++) {
var edge = edges_1[_a];
var source = edge.source;
if (!this.edgesBySource.has(source)) {
this.edgesBySource.set(source, []);
}
this.edgesBySource.get(source).push(edge);
}
}
ChatbotFlowNavigator.prototype.findNextNode = function (currentNodeId, 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
var outgoingEdges = this.edgesBySource.get(currentNodeId) || [];
if (outgoingEdges.length === 0) {
return null;
}
// Aplicar criterios de búsqueda
for (var _i = 0, outgoingEdges_1 = outgoingEdges; _i < outgoingEdges_1.length; _i++) {
var edge = outgoingEdges_1[_i];
if (this.matchesCriteria(edge, criteria)) {
var targetNodeId = edge.target;
return this.nodesIndex.get(targetNodeId) || null;
}
}
return null;
};
ChatbotFlowNavigator.prototype.matchesCriteria = function (edge, criteria) {
/**
* Evalúa si un edge cumple con los criterios dados
*/
// Ejemplo de criterios basados en tu estructura
if (criteria.button_type) {
if (edge.sourceHandle && edge.sourceHandle.includes(criteria.button_type)) {
return true;
}
}
if (criteria.edge_type) {
if (edge.type === criteria.edge_type) {
return true;
}
}
if (criteria.target_contains) {
if (edge.target && edge.target.includes(criteria.target_contains)) {
return true;
}
}
return false;
};
return ChatbotFlowNavigator;
}());
exports.ChatbotFlowNavigator = ChatbotFlowNavigator;