rust-kgdb 0.3.2 → 0.3.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.
Files changed (2) hide show
  1. package/README.md +117 -1
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -14,7 +14,8 @@
14
14
  | Feature | Description |
15
15
  |---------|-------------|
16
16
  | **GraphDB** | Core RDF/SPARQL database with 100% W3C compliance |
17
- | **GraphFrames** | Spark-compatible graph analytics (PageRank, triangles, motifs) |
17
+ | **GraphFrames** | Spark-compatible graph analytics (PageRank, triangles, components) |
18
+ | **Motif Finding** | Graph pattern DSL for structural queries (fraud rings, recommendations) |
18
19
  | **EmbeddingService** | Vector similarity search, text search, multi-provider embeddings |
19
20
  | **Embedding Triggers** | Automatic embedding generation on INSERT/UPDATE/DELETE |
20
21
  | **Embedding Providers** | OpenAI, Voyage, Cohere, Anthropic, Mistral, Jina, Ollama, HF-TEI |
@@ -140,6 +141,121 @@ console.log('Star graph:', star.vertexCount(), 'vertices,', star.edgeCount(), 'e
140
141
  console.log('Cycle graph:', cycle.vertexCount(), 'vertices,', cycle.edgeCount(), 'edges')
141
142
  ```
142
143
 
144
+ ### 2b. Motif Pattern Matching (Graph Pattern DSL)
145
+
146
+ Motifs are recurring structural patterns in graphs. rust-kgdb supports a powerful DSL for finding motifs:
147
+
148
+ ```javascript
149
+ const { GraphFrame, completeGraph, chainGraph, cycleGraph, friendsGraph } = require('rust-kgdb')
150
+
151
+ // === Basic Motif Syntax ===
152
+ // (a)-[]->(b) Single edge from a to b
153
+ // (a)-[e]->(b) Named edge 'e' from a to b
154
+ // (a)-[]->(b); (b)-[]->(c) Two-hop path (chain pattern)
155
+ // !(a)-[]->(b) Negation (edge does NOT exist)
156
+
157
+ // === Find Single Edges ===
158
+ const chain = chainGraph(5) // v0 -> v1 -> v2 -> v3 -> v4
159
+ const edges = JSON.parse(chain.find("(a)-[]->(b)"))
160
+ console.log('All edges:', edges.length) // 4
161
+
162
+ // === Two-Hop Paths (Friend-of-Friend Pattern) ===
163
+ const twoHop = JSON.parse(chain.find("(a)-[]->(b); (b)-[]->(c)"))
164
+ console.log('Two-hop paths:', twoHop.length) // 3
165
+ // v0->v1->v2, v1->v2->v3, v2->v3->v4
166
+
167
+ // === Three-Hop Paths ===
168
+ const threeHop = JSON.parse(chain.find("(a)-[]->(b); (b)-[]->(c); (c)-[]->(d)"))
169
+ console.log('Three-hop paths:', threeHop.length) // 2
170
+
171
+ // === Triangle Pattern (Cycle of Length 3) ===
172
+ const k4 = completeGraph(4) // K4 has triangles
173
+ const triangles = JSON.parse(k4.find("(a)-[]->(b); (b)-[]->(c); (c)-[]->(a)"))
174
+ // Filter to avoid counting same triangle multiple times
175
+ const uniqueTriangles = triangles.filter(t => t.a < t.b && t.b < t.c)
176
+ console.log('Triangles in K4:', uniqueTriangles.length) // 4
177
+
178
+ // === Star Pattern (Hub with Multiple Spokes) ===
179
+ const social = new GraphFrame(
180
+ JSON.stringify([
181
+ {id: "influencer"},
182
+ {id: "follower1"}, {id: "follower2"}, {id: "follower3"}
183
+ ]),
184
+ JSON.stringify([
185
+ {src: "influencer", dst: "follower1"},
186
+ {src: "influencer", dst: "follower2"},
187
+ {src: "influencer", dst: "follower3"}
188
+ ])
189
+ )
190
+ // Find hub pattern: someone with 2+ outgoing edges
191
+ const hubPattern = JSON.parse(social.find("(hub)-[]->(f1); (hub)-[]->(f2)"))
192
+ console.log('Hub patterns (2+ followers):', hubPattern.length)
193
+
194
+ // === Reciprocal Relationship (Mutual Friends) ===
195
+ const mutual = new GraphFrame(
196
+ JSON.stringify([{id: "alice"}, {id: "bob"}, {id: "carol"}]),
197
+ JSON.stringify([
198
+ {src: "alice", dst: "bob"},
199
+ {src: "bob", dst: "alice"}, // Reciprocal
200
+ {src: "bob", dst: "carol"} // One-way
201
+ ])
202
+ )
203
+ const reciprocal = JSON.parse(mutual.find("(a)-[]->(b); (b)-[]->(a)"))
204
+ console.log('Mutual relationships:', reciprocal.length) // 2 (alice<->bob counted twice)
205
+
206
+ // === Diamond Pattern (Common in Fraud Detection) ===
207
+ // A -> B, A -> C, B -> D, C -> D (convergence point D)
208
+ const diamond = new GraphFrame(
209
+ JSON.stringify([{id: "A"}, {id: "B"}, {id: "C"}, {id: "D"}]),
210
+ JSON.stringify([
211
+ {src: "A", dst: "B"},
212
+ {src: "A", dst: "C"},
213
+ {src: "B", dst: "D"},
214
+ {src: "C", dst: "D"}
215
+ ])
216
+ )
217
+ const diamondPattern = JSON.parse(diamond.find(
218
+ "(a)-[]->(b); (a)-[]->(c); (b)-[]->(d); (c)-[]->(d)"
219
+ ))
220
+ console.log('Diamond patterns:', diamondPattern.length) // 1
221
+
222
+ // === Use Case: Fraud Ring Detection ===
223
+ // Find circular money transfers: A -> B -> C -> A
224
+ const transactions = new GraphFrame(
225
+ JSON.stringify([
226
+ {id: "acc001"}, {id: "acc002"}, {id: "acc003"}, {id: "acc004"}
227
+ ]),
228
+ JSON.stringify([
229
+ {src: "acc001", dst: "acc002", amount: 10000},
230
+ {src: "acc002", dst: "acc003", amount: 9900},
231
+ {src: "acc003", dst: "acc001", amount: 9800}, // Suspicious cycle!
232
+ {src: "acc003", dst: "acc004", amount: 5000} // Normal transfer
233
+ ])
234
+ )
235
+ const cycles = JSON.parse(transactions.find(
236
+ "(a)-[]->(b); (b)-[]->(c); (c)-[]->(a)"
237
+ ))
238
+ console.log('Circular transfer patterns:', cycles.length) // Found fraud ring!
239
+
240
+ // === Use Case: Recommendation (Friends-of-Friends not yet connected) ===
241
+ const network = friendsGraph()
242
+ const fofPattern = JSON.parse(network.find("(a)-[]->(b); (b)-[]->(c)"))
243
+ // Filter: a != c and no direct edge a->c (potential recommendation)
244
+ console.log('Friend-of-friend patterns for recommendations:', fofPattern.length)
245
+ ```
246
+
247
+ ### Motif Pattern Reference
248
+
249
+ | Pattern | DSL Syntax | Description |
250
+ |---------|------------|-------------|
251
+ | **Edge** | `(a)-[]->(b)` | Single directed edge |
252
+ | **Named Edge** | `(a)-[e]->(b)` | Edge with binding name |
253
+ | **Two-hop** | `(a)-[]->(b); (b)-[]->(c)` | Path of length 2 |
254
+ | **Triangle** | `(a)-[]->(b); (b)-[]->(c); (c)-[]->(a)` | 3-cycle |
255
+ | **Star** | `(h)-[]->(a); (h)-[]->(b); (h)-[]->(c)` | Hub pattern |
256
+ | **Diamond** | `(a)-[]->(b); (a)-[]->(c); (b)-[]->(d); (c)-[]->(d)` | Convergence |
257
+ | **Negation** | `!(a)-[]->(b)` | Edge must NOT exist |
258
+
143
259
  ### 3. EmbeddingService (Vector Similarity & Text Search)
144
260
 
145
261
  ```javascript
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rust-kgdb",
3
- "version": "0.3.2",
3
+ "version": "0.3.3",
4
4
  "description": "High-performance RDF/SPARQL database with GraphFrames analytics, vector embeddings, Datalog reasoning, and Pregel BSP processing",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",