File size: 12,274 Bytes
9bd422a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/**
 * GraphProcessor - Bα»™ Xα»­ LΓ½ Đồ Thα»‹
 * Chuyển Δ‘α»•i dα»― liệu Δ‘α»“ thα»‹ tα»« ONNXParser sang Δ‘α»‹nh dαΊ‘ng Cytoscape.js
 * Requirements: 4.2, 4.3, 4.6, 15.5
 */

class GraphProcessor {
    /**
     * Xα»­ lΓ½ mΓ΄ hΓ¬nh Δ‘Γ£ phΓ’n tΓ­ch vΓ  tαΊ‘o cαΊ₯u trΓΊc dα»― liệu cho Cytoscape.js
     * @param {Object} parsedModel - Mô hình đã phÒn tích từ ONNXParser
     * @returns {{ elements: Array, stats: Object }}
     */
    processGraph(parsedModel) {
        if (!parsedModel || !parsedModel.graph) {
            return {
                elements: [],
                stats: {
                    nodeCount: 0,
                    edgeCount: 0,
                    initializerCount: 0,
                    inputCount: 0,
                    outputCount: 0
                }
            };
        }

        const { graph, inputs = [], outputs = [], initializers = [] } = parsedModel;
        const { nodes = [], edges = [], initializers: graphInitializers = [] } = graph;

        // TαΊ­p hợp tΓͺn Δ‘αΊ§u vΓ o vΓ  Δ‘αΊ§u ra để gΓ‘n class
        const inputNames = new Set((inputs || []).map(i => i.name));
        const outputNames = new Set((outputs || []).map(o => o.name));

        // XΓ’y dα»±ng map: tΓͺn tensor β†’ nodeId (để tΓ¬m nΓΊt Δ‘αΊ§u vΓ o/Δ‘αΊ§u ra)
        const outputTensorToNodeId = new Map();
        for (const node of nodes) {
            for (const out of (node.outputs || [])) {
                if (out) outputTensorToNodeId.set(out, node.id);
            }
        }

        const elements = [];

        // ── NΓΊt Δ‘αΊ§u vΓ o mΓ΄ hΓ¬nh (input-node) ──────────────────────────────────
        for (const input of inputs) {
            elements.push({
                data: {
                    id: `input_${input.name}`,
                    label: input.name,
                    name: input.name,
                    opType: 'Input',
                    shape: input.shape,
                    dataType: input.dataType,
                    description: input.description || '',
                    nodeType: 'input'
                },
                classes: 'input-node'
            });
        }

        // ── NΓΊt Δ‘αΊ§u ra mΓ΄ hΓ¬nh (output-node) ──────────────────────────────────
        for (const output of outputs) {
            elements.push({
                data: {
                    id: `output_${output.name}`,
                    label: output.name,
                    name: output.name,
                    opType: 'Output',
                    shape: output.shape,
                    dataType: output.dataType,
                    description: output.description || '',
                    nodeType: 'output'
                },
                classes: 'output-node'
            });
        }

        // ── NΓΊt bα»™ khởi tαΊ‘o (initializer-node) ────────────────────────────────
        const allInitializers = initializers.length > 0 ? initializers : graphInitializers;
        for (const init of allInitializers) {
            elements.push({
                data: {
                    id: `init_${init.name}`,
                    label: init.name,
                    name: init.name,
                    opType: 'Initializer',
                    shape: init.shape,
                    dataType: init.dataType,
                    size: init.size,
                    elementCount: init.elementCount,
                    nodeType: 'initializer'
                },
                classes: 'initializer-node'
            });
        }

        // ── Kiểm tra Δ‘α»“ thα»‹ lα»›n β†’ clustering ──────────────────────────────────
        const threshold = (typeof CONFIG !== 'undefined' && CONFIG.PERFORMANCE)
            ? CONFIG.PERFORMANCE.LAZY_LOAD_THRESHOLD
            : 1000;

        if (nodes.length > threshold) {
            return this._processLargeGraph(
                nodes, edges, inputs, outputs, allInitializers, elements, threshold
            );
        }

        // ── NΓΊt toΓ‘n tα»­ (op-node) ──────────────────────────────────────────────
        for (const node of nodes) {
            elements.push({
                data: {
                    id: node.id,
                    label: node.opType || node.name || node.id,
                    name: node.name,
                    opType: node.opType || '',
                    attributes: node.attributes || {},
                    inputs: node.inputs || [],
                    outputs: node.outputs || [],
                    domain: node.domain || '',
                    nodeType: 'op'
                },
                classes: 'op-node'
            });
        }

        // ── CαΊ‘nh giα»―a cΓ‘c nΓΊt toΓ‘n tα»­ ─────────────────────────────────────────
        let edgeIndex = 0;
        for (const edge of edges) {
            elements.push({
                data: {
                    id: `edge_${edgeIndex++}`,
                    source: edge.source,
                    target: edge.target,
                    label: edge.label || ''
                },
                classes: 'edge'
            });
        }

        // ── CαΊ‘nh tα»« input-node β†’ op-node ──────────────────────────────────────
        for (const node of nodes) {
            for (const inputTensor of (node.inputs || [])) {
                if (inputNames.has(inputTensor)) {
                    elements.push({
                        data: {
                            id: `edge_${edgeIndex++}`,
                            source: `input_${inputTensor}`,
                            target: node.id,
                            label: inputTensor
                        },
                        classes: 'edge'
                    });
                }
            }
        }

        // ── CαΊ‘nh tα»« op-node β†’ output-node ─────────────────────────────────────
        for (const node of nodes) {
            for (const outputTensor of (node.outputs || [])) {
                if (outputNames.has(outputTensor)) {
                    elements.push({
                        data: {
                            id: `edge_${edgeIndex++}`,
                            source: node.id,
                            target: `output_${outputTensor}`,
                            label: outputTensor
                        },
                        classes: 'edge'
                    });
                }
            }
        }

        const stats = {
            nodeCount: nodes.length,
            edgeCount: edges.length,
            initializerCount: allInitializers.length,
            inputCount: inputs.length,
            outputCount: outputs.length
        };

        return { elements, stats };
    }

    // ─── Private Helpers ────────────────────────────────────────────────────────

    /**
     * Xα»­ lΓ½ Δ‘α»“ thα»‹ lα»›n bαΊ±ng cΓ‘ch nhΓ³m cΓ‘c nΓΊt theo opType (clustering)
     * @private
     */
    _processLargeGraph(nodes, edges, inputs, outputs, initializers, baseElements, threshold) {
        // NhΓ³m nΓΊt theo opType
        const clusters = new Map();
        for (const node of nodes) {
            const opType = node.opType || 'Unknown';
            if (!clusters.has(opType)) {
                clusters.set(opType, []);
            }
            clusters.get(opType).push(node);
        }

        const elements = [...baseElements];
        let edgeIndex = 0;

        // TαΊ‘o nΓΊt cluster cho mα»—i opType
        for (const [opType, clusterNodes] of clusters) {
            const clusterId = `cluster_${opType}`;
            elements.push({
                data: {
                    id: clusterId,
                    label: `${opType} (${clusterNodes.length})`,
                    name: opType,
                    opType: opType,
                    nodeCount: clusterNodes.length,
                    nodeIds: clusterNodes.map(n => n.id),
                    nodeType: 'cluster',
                    isCluster: true
                },
                classes: 'op-node cluster-node'
            });
        }

        // XΓ’y dα»±ng map: nodeId β†’ clusterId
        const nodeToCluster = new Map();
        for (const [opType, clusterNodes] of clusters) {
            const clusterId = `cluster_${opType}`;
            for (const node of clusterNodes) {
                nodeToCluster.set(node.id, clusterId);
            }
        }

        // TαΊ‘o cαΊ‘nh giα»―a cΓ‘c cluster (trΓ‘nh trΓΉng lαΊ·p)
        const clusterEdgeSet = new Set();
        for (const edge of edges) {
            const srcCluster = nodeToCluster.get(edge.source);
            const tgtCluster = nodeToCluster.get(edge.target);
            if (srcCluster && tgtCluster && srcCluster !== tgtCluster) {
                const key = `${srcCluster}β†’${tgtCluster}`;
                if (!clusterEdgeSet.has(key)) {
                    clusterEdgeSet.add(key);
                    elements.push({
                        data: {
                            id: `edge_${edgeIndex++}`,
                            source: srcCluster,
                            target: tgtCluster,
                            label: ''
                        },
                        classes: 'edge'
                    });
                }
            }
        }

        // CαΊ‘nh tα»« input-node β†’ cluster
        const inputNames = new Set((inputs || []).map(i => i.name));
        const outputNames = new Set((outputs || []).map(o => o.name));
        const inputClusterEdgeSet = new Set();

        for (const node of nodes) {
            const clusterId = nodeToCluster.get(node.id);
            for (const inputTensor of (node.inputs || [])) {
                if (inputNames.has(inputTensor)) {
                    const key = `input_${inputTensor}β†’${clusterId}`;
                    if (!inputClusterEdgeSet.has(key)) {
                        inputClusterEdgeSet.add(key);
                        elements.push({
                            data: {
                                id: `edge_${edgeIndex++}`,
                                source: `input_${inputTensor}`,
                                target: clusterId,
                                label: inputTensor
                            },
                            classes: 'edge'
                        });
                    }
                }
            }
            for (const outputTensor of (node.outputs || [])) {
                if (outputNames.has(outputTensor)) {
                    const key = `${clusterId}β†’output_${outputTensor}`;
                    if (!inputClusterEdgeSet.has(key)) {
                        inputClusterEdgeSet.add(key);
                        elements.push({
                            data: {
                                id: `edge_${edgeIndex++}`,
                                source: clusterId,
                                target: `output_${outputTensor}`,
                                label: outputTensor
                            },
                            classes: 'edge'
                        });
                    }
                }
            }
        }

        const stats = {
            nodeCount: nodes.length,
            edgeCount: edges.length,
            initializerCount: initializers.length,
            inputCount: inputs.length,
            outputCount: outputs.length,
            clusterCount: clusters.size,
            isClustered: true
        };

        return { elements, stats };
    }
}

// Export as global for browser usage
window.GraphProcessor = GraphProcessor;