rust-kgdb 0.4.1 → 0.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +735 -1912
- package/examples/business-assertions.test.ts +1196 -0
- package/examples/core-concepts-demo.ts +502 -0
- package/examples/datalog-example.ts +478 -0
- package/examples/embeddings-example.ts +376 -0
- package/examples/fraud-detection-agent.js +346 -0
- package/examples/graphframes-example.ts +367 -0
- package/examples/hypermind-fraud-underwriter.ts +669 -0
- package/examples/pregel-example.ts +399 -0
- package/examples/underwriting-agent.js +379 -0
- package/package.json +3 -2
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GraphFrames Example for rust-kgdb TypeScript SDK
|
|
3
|
+
*
|
|
4
|
+
* Demonstrates graph analytics capabilities including:
|
|
5
|
+
* - Creating graphs from vertices and edges
|
|
6
|
+
* - Running PageRank algorithm
|
|
7
|
+
* - Finding connected components
|
|
8
|
+
* - Computing shortest paths
|
|
9
|
+
* - Triangle counting
|
|
10
|
+
* - Label propagation for community detection
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { GraphFrame } from 'rust-kgdb';
|
|
14
|
+
|
|
15
|
+
// =============================================================================
|
|
16
|
+
// Example 1: Basic GraphFrame Creation
|
|
17
|
+
// =============================================================================
|
|
18
|
+
|
|
19
|
+
async function basicGraphFrameExample() {
|
|
20
|
+
console.log('=== Basic GraphFrame Example ===\n');
|
|
21
|
+
|
|
22
|
+
// Create a simple social network graph
|
|
23
|
+
const vertices = JSON.stringify([
|
|
24
|
+
{ id: 'alice', name: 'Alice', age: 34 },
|
|
25
|
+
{ id: 'bob', name: 'Bob', age: 36 },
|
|
26
|
+
{ id: 'charlie', name: 'Charlie', age: 30 },
|
|
27
|
+
{ id: 'david', name: 'David', age: 29 },
|
|
28
|
+
{ id: 'esther', name: 'Esther', age: 32 },
|
|
29
|
+
{ id: 'fanny', name: 'Fanny', age: 36 }
|
|
30
|
+
]);
|
|
31
|
+
|
|
32
|
+
const edges = JSON.stringify([
|
|
33
|
+
{ src: 'alice', dst: 'bob', relationship: 'friend' },
|
|
34
|
+
{ src: 'bob', dst: 'charlie', relationship: 'follow' },
|
|
35
|
+
{ src: 'charlie', dst: 'bob', relationship: 'follow' },
|
|
36
|
+
{ src: 'fanny', dst: 'charlie', relationship: 'follow' },
|
|
37
|
+
{ src: 'esther', dst: 'fanny', relationship: 'follow' },
|
|
38
|
+
{ src: 'esther', dst: 'david', relationship: 'friend' },
|
|
39
|
+
{ src: 'david', dst: 'alice', relationship: 'friend' }
|
|
40
|
+
]);
|
|
41
|
+
|
|
42
|
+
// Create GraphFrame
|
|
43
|
+
const graph = new GraphFrame(vertices, edges);
|
|
44
|
+
|
|
45
|
+
// Get basic statistics
|
|
46
|
+
const vertexCount = graph.vertexCount();
|
|
47
|
+
const edgeCount = graph.edgeCount();
|
|
48
|
+
|
|
49
|
+
console.log(`Graph created with ${vertexCount} vertices and ${edgeCount} edges`);
|
|
50
|
+
console.log();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// =============================================================================
|
|
54
|
+
// Example 2: PageRank Algorithm
|
|
55
|
+
// =============================================================================
|
|
56
|
+
|
|
57
|
+
async function pageRankExample() {
|
|
58
|
+
console.log('=== PageRank Example ===\n');
|
|
59
|
+
|
|
60
|
+
// Create a citation network
|
|
61
|
+
const vertices = JSON.stringify([
|
|
62
|
+
{ id: 'paper1', title: 'Machine Learning Fundamentals' },
|
|
63
|
+
{ id: 'paper2', title: 'Deep Learning Advances' },
|
|
64
|
+
{ id: 'paper3', title: 'Neural Networks Review' },
|
|
65
|
+
{ id: 'paper4', title: 'AI Applications' },
|
|
66
|
+
{ id: 'paper5', title: 'Computer Vision Survey' }
|
|
67
|
+
]);
|
|
68
|
+
|
|
69
|
+
const edges = JSON.stringify([
|
|
70
|
+
{ src: 'paper2', dst: 'paper1' }, // paper2 cites paper1
|
|
71
|
+
{ src: 'paper3', dst: 'paper1' }, // paper3 cites paper1
|
|
72
|
+
{ src: 'paper3', dst: 'paper2' }, // paper3 cites paper2
|
|
73
|
+
{ src: 'paper4', dst: 'paper1' }, // paper4 cites paper1
|
|
74
|
+
{ src: 'paper4', dst: 'paper2' }, // paper4 cites paper2
|
|
75
|
+
{ src: 'paper4', dst: 'paper3' }, // paper4 cites paper3
|
|
76
|
+
{ src: 'paper5', dst: 'paper3' }, // paper5 cites paper3
|
|
77
|
+
{ src: 'paper5', dst: 'paper4' } // paper5 cites paper4
|
|
78
|
+
]);
|
|
79
|
+
|
|
80
|
+
const graph = new GraphFrame(vertices, edges);
|
|
81
|
+
|
|
82
|
+
// Run PageRank with damping factor 0.85 and 20 iterations
|
|
83
|
+
const resetProbability = 0.15;
|
|
84
|
+
const maxIterations = 20;
|
|
85
|
+
const ranks = graph.pageRank(resetProbability, maxIterations);
|
|
86
|
+
|
|
87
|
+
// Parse and display results
|
|
88
|
+
const rankResults = JSON.parse(ranks);
|
|
89
|
+
console.log('PageRank Results (sorted by rank):');
|
|
90
|
+
|
|
91
|
+
const sortedRanks = Object.entries(rankResults)
|
|
92
|
+
.sort(([, a], [, b]) => (b as number) - (a as number));
|
|
93
|
+
|
|
94
|
+
for (const [paperId, rank] of sortedRanks) {
|
|
95
|
+
console.log(` ${paperId}: ${(rank as number).toFixed(4)}`);
|
|
96
|
+
}
|
|
97
|
+
console.log();
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// =============================================================================
|
|
101
|
+
// Example 3: Connected Components
|
|
102
|
+
// =============================================================================
|
|
103
|
+
|
|
104
|
+
async function connectedComponentsExample() {
|
|
105
|
+
console.log('=== Connected Components Example ===\n');
|
|
106
|
+
|
|
107
|
+
// Create a graph with multiple disconnected components
|
|
108
|
+
const vertices = JSON.stringify([
|
|
109
|
+
// Component 1: Social group A
|
|
110
|
+
{ id: 'a1' }, { id: 'a2' }, { id: 'a3' },
|
|
111
|
+
// Component 2: Social group B
|
|
112
|
+
{ id: 'b1' }, { id: 'b2' }, { id: 'b3' }, { id: 'b4' },
|
|
113
|
+
// Component 3: Isolated node
|
|
114
|
+
{ id: 'c1' }
|
|
115
|
+
]);
|
|
116
|
+
|
|
117
|
+
const edges = JSON.stringify([
|
|
118
|
+
// Component 1 edges
|
|
119
|
+
{ src: 'a1', dst: 'a2' },
|
|
120
|
+
{ src: 'a2', dst: 'a3' },
|
|
121
|
+
{ src: 'a3', dst: 'a1' },
|
|
122
|
+
// Component 2 edges
|
|
123
|
+
{ src: 'b1', dst: 'b2' },
|
|
124
|
+
{ src: 'b2', dst: 'b3' },
|
|
125
|
+
{ src: 'b3', dst: 'b4' },
|
|
126
|
+
{ src: 'b4', dst: 'b1' }
|
|
127
|
+
// c1 has no edges - isolated component
|
|
128
|
+
]);
|
|
129
|
+
|
|
130
|
+
const graph = new GraphFrame(vertices, edges);
|
|
131
|
+
|
|
132
|
+
// Find connected components
|
|
133
|
+
const components = graph.connectedComponents();
|
|
134
|
+
const componentResults = JSON.parse(components);
|
|
135
|
+
|
|
136
|
+
console.log('Connected Components:');
|
|
137
|
+
|
|
138
|
+
// Group vertices by component
|
|
139
|
+
const componentGroups: Record<string, string[]> = {};
|
|
140
|
+
for (const [vertex, component] of Object.entries(componentResults)) {
|
|
141
|
+
const compId = String(component);
|
|
142
|
+
if (!componentGroups[compId]) {
|
|
143
|
+
componentGroups[compId] = [];
|
|
144
|
+
}
|
|
145
|
+
componentGroups[compId].push(vertex);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
let compNum = 1;
|
|
149
|
+
for (const [compId, members] of Object.entries(componentGroups)) {
|
|
150
|
+
console.log(` Component ${compNum}: [${members.join(', ')}]`);
|
|
151
|
+
compNum++;
|
|
152
|
+
}
|
|
153
|
+
console.log(`\nTotal components found: ${Object.keys(componentGroups).length}`);
|
|
154
|
+
console.log();
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// =============================================================================
|
|
158
|
+
// Example 4: Shortest Paths
|
|
159
|
+
// =============================================================================
|
|
160
|
+
|
|
161
|
+
async function shortestPathsExample() {
|
|
162
|
+
console.log('=== Shortest Paths Example ===\n');
|
|
163
|
+
|
|
164
|
+
// Create a transportation network
|
|
165
|
+
const vertices = JSON.stringify([
|
|
166
|
+
{ id: 'NYC', name: 'New York' },
|
|
167
|
+
{ id: 'BOS', name: 'Boston' },
|
|
168
|
+
{ id: 'CHI', name: 'Chicago' },
|
|
169
|
+
{ id: 'MIA', name: 'Miami' },
|
|
170
|
+
{ id: 'LAX', name: 'Los Angeles' },
|
|
171
|
+
{ id: 'SEA', name: 'Seattle' }
|
|
172
|
+
]);
|
|
173
|
+
|
|
174
|
+
const edges = JSON.stringify([
|
|
175
|
+
{ src: 'NYC', dst: 'BOS' },
|
|
176
|
+
{ src: 'NYC', dst: 'CHI' },
|
|
177
|
+
{ src: 'NYC', dst: 'MIA' },
|
|
178
|
+
{ src: 'BOS', dst: 'CHI' },
|
|
179
|
+
{ src: 'CHI', dst: 'LAX' },
|
|
180
|
+
{ src: 'CHI', dst: 'SEA' },
|
|
181
|
+
{ src: 'LAX', dst: 'SEA' },
|
|
182
|
+
{ src: 'MIA', dst: 'LAX' }
|
|
183
|
+
]);
|
|
184
|
+
|
|
185
|
+
const graph = new GraphFrame(vertices, edges);
|
|
186
|
+
|
|
187
|
+
// Find shortest paths from NYC and LAX as landmarks
|
|
188
|
+
const landmarks = ['NYC', 'LAX'];
|
|
189
|
+
const paths = graph.shortestPaths(landmarks);
|
|
190
|
+
const pathResults = JSON.parse(paths);
|
|
191
|
+
|
|
192
|
+
console.log('Shortest Paths to Landmarks:');
|
|
193
|
+
for (const [vertex, distances] of Object.entries(pathResults)) {
|
|
194
|
+
const distObj = distances as Record<string, number>;
|
|
195
|
+
const distStr = Object.entries(distObj)
|
|
196
|
+
.map(([landmark, dist]) => `${landmark}=${dist}`)
|
|
197
|
+
.join(', ');
|
|
198
|
+
console.log(` ${vertex}: {${distStr}}`);
|
|
199
|
+
}
|
|
200
|
+
console.log();
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// =============================================================================
|
|
204
|
+
// Example 5: Triangle Counting
|
|
205
|
+
// =============================================================================
|
|
206
|
+
|
|
207
|
+
async function triangleCountExample() {
|
|
208
|
+
console.log('=== Triangle Counting Example ===\n');
|
|
209
|
+
|
|
210
|
+
// Create a graph with known triangles
|
|
211
|
+
const vertices = JSON.stringify([
|
|
212
|
+
{ id: 'v0' }, { id: 'v1' }, { id: 'v2' },
|
|
213
|
+
{ id: 'v3' }, { id: 'v4' }
|
|
214
|
+
]);
|
|
215
|
+
|
|
216
|
+
// This creates a K4 (complete graph on 4 vertices) plus one extra vertex
|
|
217
|
+
// K4 has C(4,3) = 4 triangles
|
|
218
|
+
const edges = JSON.stringify([
|
|
219
|
+
// K4 edges (v0, v1, v2, v3)
|
|
220
|
+
{ src: 'v0', dst: 'v1' },
|
|
221
|
+
{ src: 'v0', dst: 'v2' },
|
|
222
|
+
{ src: 'v0', dst: 'v3' },
|
|
223
|
+
{ src: 'v1', dst: 'v2' },
|
|
224
|
+
{ src: 'v1', dst: 'v3' },
|
|
225
|
+
{ src: 'v2', dst: 'v3' },
|
|
226
|
+
// Extra edge to v4 (no triangles with v4)
|
|
227
|
+
{ src: 'v3', dst: 'v4' }
|
|
228
|
+
]);
|
|
229
|
+
|
|
230
|
+
const graph = new GraphFrame(vertices, edges);
|
|
231
|
+
|
|
232
|
+
// Count triangles
|
|
233
|
+
const triangleCount = graph.triangleCount();
|
|
234
|
+
|
|
235
|
+
console.log(`Number of triangles in graph: ${triangleCount}`);
|
|
236
|
+
console.log('(Expected: 4 triangles from the K4 subgraph)');
|
|
237
|
+
console.log();
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// =============================================================================
|
|
241
|
+
// Example 6: Label Propagation (Community Detection)
|
|
242
|
+
// =============================================================================
|
|
243
|
+
|
|
244
|
+
async function labelPropagationExample() {
|
|
245
|
+
console.log('=== Label Propagation Example ===\n');
|
|
246
|
+
|
|
247
|
+
// Create a graph with clear community structure
|
|
248
|
+
const vertices = JSON.stringify([
|
|
249
|
+
// Community 1: Tech enthusiasts
|
|
250
|
+
{ id: 'tech1' }, { id: 'tech2' }, { id: 'tech3' }, { id: 'tech4' },
|
|
251
|
+
// Community 2: Sports fans
|
|
252
|
+
{ id: 'sport1' }, { id: 'sport2' }, { id: 'sport3' },
|
|
253
|
+
// Bridge node
|
|
254
|
+
{ id: 'bridge' }
|
|
255
|
+
]);
|
|
256
|
+
|
|
257
|
+
const edges = JSON.stringify([
|
|
258
|
+
// Dense connections in tech community
|
|
259
|
+
{ src: 'tech1', dst: 'tech2' },
|
|
260
|
+
{ src: 'tech1', dst: 'tech3' },
|
|
261
|
+
{ src: 'tech2', dst: 'tech3' },
|
|
262
|
+
{ src: 'tech2', dst: 'tech4' },
|
|
263
|
+
{ src: 'tech3', dst: 'tech4' },
|
|
264
|
+
// Dense connections in sports community
|
|
265
|
+
{ src: 'sport1', dst: 'sport2' },
|
|
266
|
+
{ src: 'sport1', dst: 'sport3' },
|
|
267
|
+
{ src: 'sport2', dst: 'sport3' },
|
|
268
|
+
// Bridge connections (sparse)
|
|
269
|
+
{ src: 'tech4', dst: 'bridge' },
|
|
270
|
+
{ src: 'bridge', dst: 'sport1' }
|
|
271
|
+
]);
|
|
272
|
+
|
|
273
|
+
const graph = new GraphFrame(vertices, edges);
|
|
274
|
+
|
|
275
|
+
// Run label propagation for 10 iterations
|
|
276
|
+
const maxIterations = 10;
|
|
277
|
+
const labels = graph.labelPropagation(maxIterations);
|
|
278
|
+
const labelResults = JSON.parse(labels);
|
|
279
|
+
|
|
280
|
+
console.log('Community Detection Results:');
|
|
281
|
+
|
|
282
|
+
// Group by label
|
|
283
|
+
const communities: Record<string, string[]> = {};
|
|
284
|
+
for (const [vertex, label] of Object.entries(labelResults)) {
|
|
285
|
+
const labelId = String(label);
|
|
286
|
+
if (!communities[labelId]) {
|
|
287
|
+
communities[labelId] = [];
|
|
288
|
+
}
|
|
289
|
+
communities[labelId].push(vertex);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
let commNum = 1;
|
|
293
|
+
for (const [, members] of Object.entries(communities)) {
|
|
294
|
+
console.log(` Community ${commNum}: [${members.join(', ')}]`);
|
|
295
|
+
commNum++;
|
|
296
|
+
}
|
|
297
|
+
console.log();
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// =============================================================================
|
|
301
|
+
// Example 7: Motif Finding
|
|
302
|
+
// =============================================================================
|
|
303
|
+
|
|
304
|
+
async function motifFindingExample() {
|
|
305
|
+
console.log('=== Motif Finding Example ===\n');
|
|
306
|
+
|
|
307
|
+
// Create a social network for motif finding
|
|
308
|
+
const vertices = JSON.stringify([
|
|
309
|
+
{ id: 'a' }, { id: 'b' }, { id: 'c' }, { id: 'd' }, { id: 'e' }
|
|
310
|
+
]);
|
|
311
|
+
|
|
312
|
+
const edges = JSON.stringify([
|
|
313
|
+
{ src: 'a', dst: 'b' },
|
|
314
|
+
{ src: 'b', dst: 'c' },
|
|
315
|
+
{ src: 'c', dst: 'a' }, // Triangle: a -> b -> c -> a
|
|
316
|
+
{ src: 'c', dst: 'd' },
|
|
317
|
+
{ src: 'd', dst: 'e' },
|
|
318
|
+
{ src: 'e', dst: 'c' } // Triangle: c -> d -> e -> c
|
|
319
|
+
]);
|
|
320
|
+
|
|
321
|
+
const graph = new GraphFrame(vertices, edges);
|
|
322
|
+
|
|
323
|
+
// Find simple edge pattern
|
|
324
|
+
const edgePattern = '(x)-[e]->(y)';
|
|
325
|
+
const edgeResults = graph.find(edgePattern);
|
|
326
|
+
console.log(`Pattern "${edgePattern}": Found ${JSON.parse(edgeResults).length} matches`);
|
|
327
|
+
|
|
328
|
+
// Find 2-hop paths
|
|
329
|
+
const pathPattern = '(x)-[e1]->(y); (y)-[e2]->(z)';
|
|
330
|
+
const pathResults = graph.find(pathPattern);
|
|
331
|
+
console.log(`Pattern "${pathPattern}": Found ${JSON.parse(pathResults).length} matches`);
|
|
332
|
+
|
|
333
|
+
// Find triangles
|
|
334
|
+
const trianglePattern = '(a)-[]->(b); (b)-[]->(c); (c)-[]->(a)';
|
|
335
|
+
const triangleResults = graph.find(trianglePattern);
|
|
336
|
+
console.log(`Pattern "${trianglePattern}": Found ${JSON.parse(triangleResults).length} matches`);
|
|
337
|
+
console.log();
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// =============================================================================
|
|
341
|
+
// Run All Examples
|
|
342
|
+
// =============================================================================
|
|
343
|
+
|
|
344
|
+
async function main() {
|
|
345
|
+
console.log('========================================');
|
|
346
|
+
console.log(' GraphFrames SDK Examples');
|
|
347
|
+
console.log('========================================\n');
|
|
348
|
+
|
|
349
|
+
try {
|
|
350
|
+
await basicGraphFrameExample();
|
|
351
|
+
await pageRankExample();
|
|
352
|
+
await connectedComponentsExample();
|
|
353
|
+
await shortestPathsExample();
|
|
354
|
+
await triangleCountExample();
|
|
355
|
+
await labelPropagationExample();
|
|
356
|
+
await motifFindingExample();
|
|
357
|
+
|
|
358
|
+
console.log('========================================');
|
|
359
|
+
console.log(' All examples completed successfully!');
|
|
360
|
+
console.log('========================================');
|
|
361
|
+
} catch (error) {
|
|
362
|
+
console.error('Error running examples:', error);
|
|
363
|
+
process.exit(1);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
main();
|