oxirag 0.1.1

A four-layer RAG engine with SMT-based logic verification and knowledge graph support
Documentation
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//! Relationship extraction implementations.

use async_trait::async_trait;
use std::collections::HashMap;

use crate::error::GraphError;
use crate::layer4_graph::traits::RelationshipExtractor;
use crate::layer4_graph::types::{GraphEntity, GraphRelationship, RelationshipType};

/// A mock relationship extractor that returns predefined relationships for testing.
#[derive(Debug, Default)]
pub struct MockRelationshipExtractor {
    relationships: Vec<GraphRelationship>,
}

impl MockRelationshipExtractor {
    /// Create a new mock extractor with no predefined relationships.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a mock extractor with predefined relationships.
    #[must_use]
    pub fn with_relationships(relationships: Vec<GraphRelationship>) -> Self {
        Self { relationships }
    }

    /// Add a predefined relationship.
    pub fn add_relationship(&mut self, relationship: GraphRelationship) {
        self.relationships.push(relationship);
    }
}

#[async_trait]
impl RelationshipExtractor for MockRelationshipExtractor {
    async fn extract_relationships(
        &self,
        _text: &str,
        _entities: &[GraphEntity],
    ) -> Result<Vec<GraphRelationship>, GraphError> {
        Ok(self.relationships.clone())
    }

    fn supported_relationship_types(&self) -> Vec<RelationshipType> {
        vec![
            RelationshipType::IsA,
            RelationshipType::PartOf,
            RelationshipType::RelatedTo,
            RelationshipType::Uses,
            RelationshipType::CreatedBy,
        ]
    }
}

/// A pattern-based relationship extractor using keyword patterns.
#[derive(Debug)]
#[allow(clippy::struct_field_names)]
pub struct PatternRelationshipExtractor {
    /// Patterns that indicate `IS_A` relationships.
    is_a_patterns: Vec<String>,
    /// Patterns that indicate `PART_OF` relationships.
    part_of_patterns: Vec<String>,
    /// Patterns that indicate `USES` relationships.
    uses_patterns: Vec<String>,
    /// Patterns that indicate `CREATED_BY` relationships.
    created_by_patterns: Vec<String>,
    /// Patterns that indicate `LOCATED_IN` relationships.
    located_in_patterns: Vec<String>,
    /// Patterns that indicate `WORKS_FOR` relationships.
    works_for_patterns: Vec<String>,
    /// Patterns that indicate `DEPENDS_ON` relationships.
    depends_on_patterns: Vec<String>,
}

impl Default for PatternRelationshipExtractor {
    fn default() -> Self {
        Self::new()
    }
}

impl PatternRelationshipExtractor {
    /// Create a new pattern-based relationship extractor with default patterns.
    #[must_use]
    pub fn new() -> Self {
        Self {
            is_a_patterns: vec![
                "is a".to_string(),
                "is an".to_string(),
                "are".to_string(),
                "was a".to_string(),
                "were".to_string(),
                "type of".to_string(),
                "kind of".to_string(),
                "instance of".to_string(),
            ],
            part_of_patterns: vec![
                "part of".to_string(),
                "belongs to".to_string(),
                "included in".to_string(),
                "component of".to_string(),
                "member of".to_string(),
                "within".to_string(),
            ],
            uses_patterns: vec![
                "uses".to_string(),
                "utilizes".to_string(),
                "employs".to_string(),
                "relies on".to_string(),
                "depends on".to_string(),
                "built with".to_string(),
                "implemented with".to_string(),
                "written in".to_string(),
            ],
            created_by_patterns: vec![
                "created by".to_string(),
                "developed by".to_string(),
                "built by".to_string(),
                "made by".to_string(),
                "authored by".to_string(),
                "designed by".to_string(),
                "invented by".to_string(),
            ],
            located_in_patterns: vec![
                "located in".to_string(),
                "based in".to_string(),
                "in".to_string(),
                "at".to_string(),
                "from".to_string(),
                "headquartered in".to_string(),
            ],
            works_for_patterns: vec![
                "works for".to_string(),
                "employed by".to_string(),
                "works at".to_string(),
                "founder of".to_string(),
                "CEO of".to_string(),
                "leads".to_string(),
            ],
            depends_on_patterns: vec![
                "depends on".to_string(),
                "requires".to_string(),
                "needs".to_string(),
                "built on".to_string(),
                "based on".to_string(),
            ],
        }
    }

    /// Add custom `IS_A` patterns.
    pub fn add_is_a_patterns(&mut self, patterns: impl IntoIterator<Item = impl Into<String>>) {
        for p in patterns {
            self.is_a_patterns.push(p.into());
        }
    }

    /// Add custom USES patterns.
    pub fn add_uses_patterns(&mut self, patterns: impl IntoIterator<Item = impl Into<String>>) {
        for p in patterns {
            self.uses_patterns.push(p.into());
        }
    }

    /// Find the relationship type and direction between two entities based on text patterns.
    fn find_relationship_pattern(
        &self,
        text: &str,
        source: &GraphEntity,
        target: &GraphEntity,
    ) -> Option<(RelationshipType, bool)> {
        let lower_text = text.to_lowercase();
        let source_lower = source.name.to_lowercase();
        let target_lower = target.name.to_lowercase();

        // Try to find a sentence or clause containing both entities
        let sentences: Vec<&str> = text.split(['.', '!', '?', ';', ',']).collect();

        for sentence in sentences {
            let sentence_lower = sentence.to_lowercase();

            // Check if both entities are mentioned in this sentence
            let source_pos = sentence_lower.find(&source_lower);
            let target_pos = sentence_lower.find(&target_lower);

            if let (Some(sp), Some(tp)) = (source_pos, target_pos) {
                // Get the text between the two entities
                let (start, end, forward) = if sp < tp {
                    (sp + source_lower.len(), tp, true)
                } else {
                    (tp + target_lower.len(), sp, false)
                };

                if start < end {
                    let between = &sentence_lower[start..end];

                    // Check each pattern type
                    for pattern in &self.is_a_patterns {
                        if between.contains(pattern) {
                            return Some((RelationshipType::IsA, forward));
                        }
                    }

                    for pattern in &self.part_of_patterns {
                        if between.contains(pattern) {
                            return Some((RelationshipType::PartOf, forward));
                        }
                    }

                    for pattern in &self.uses_patterns {
                        if between.contains(pattern) {
                            return Some((RelationshipType::Uses, forward));
                        }
                    }

                    for pattern in &self.created_by_patterns {
                        if between.contains(pattern) {
                            return Some((RelationshipType::CreatedBy, !forward));
                        }
                    }

                    for pattern in &self.located_in_patterns {
                        if between.contains(pattern) {
                            return Some((RelationshipType::LocatedIn, forward));
                        }
                    }

                    for pattern in &self.works_for_patterns {
                        if between.contains(pattern) {
                            return Some((RelationshipType::WorksFor, forward));
                        }
                    }

                    for pattern in &self.depends_on_patterns {
                        if between.contains(pattern) {
                            return Some((RelationshipType::DependsOn, forward));
                        }
                    }
                }

                // If entities are in the same sentence but no clear pattern, mark as related
                if lower_text.contains(&source_lower) && lower_text.contains(&target_lower) {
                    return Some((RelationshipType::RelatedTo, true));
                }
            }
        }

        None
    }
}

#[async_trait]
impl RelationshipExtractor for PatternRelationshipExtractor {
    async fn extract_relationships(
        &self,
        text: &str,
        entities: &[GraphEntity],
    ) -> Result<Vec<GraphRelationship>, GraphError> {
        let mut relationships = Vec::new();
        let mut seen: HashMap<(String, String), bool> = HashMap::new();

        // Check all pairs of entities
        for (i, source) in entities.iter().enumerate() {
            for target in entities.iter().skip(i + 1) {
                // Skip self-relationships
                if source.id == target.id {
                    continue;
                }

                // Check if we've already processed this pair
                let pair_key = if source.id < target.id {
                    (source.id.clone(), target.id.clone())
                } else {
                    (target.id.clone(), source.id.clone())
                };

                if seen.contains_key(&pair_key) {
                    continue;
                }
                seen.insert(pair_key, true);

                // Try to find a relationship pattern
                if let Some((rel_type, forward)) =
                    self.find_relationship_pattern(text, source, target)
                {
                    let (src_id, tgt_id) = if forward {
                        (source.id.clone(), target.id.clone())
                    } else {
                        (target.id.clone(), source.id.clone())
                    };

                    relationships.push(
                        GraphRelationship::new(src_id, tgt_id, rel_type).with_confidence(0.6), // Pattern-based has moderate confidence
                    );
                }
            }
        }

        Ok(relationships)
    }

    fn supported_relationship_types(&self) -> Vec<RelationshipType> {
        vec![
            RelationshipType::IsA,
            RelationshipType::PartOf,
            RelationshipType::Uses,
            RelationshipType::CreatedBy,
            RelationshipType::LocatedIn,
            RelationshipType::WorksFor,
            RelationshipType::DependsOn,
            RelationshipType::RelatedTo,
        ]
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::layer4_graph::types::EntityType;

    #[tokio::test]
    async fn test_mock_relationship_extractor() {
        let relationships = vec![GraphRelationship::new(
            "rust",
            "llvm",
            RelationshipType::Uses,
        )];
        let extractor = MockRelationshipExtractor::with_relationships(relationships);

        let entities = vec![
            GraphEntity::new("Rust", EntityType::Technology).with_id("rust"),
            GraphEntity::new("LLVM", EntityType::Technology).with_id("llvm"),
        ];

        let result = extractor
            .extract_relationships("any text", &entities)
            .await
            .unwrap();
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].relationship_type, RelationshipType::Uses);
    }

    #[tokio::test]
    async fn test_pattern_extractor_is_a() {
        let extractor = PatternRelationshipExtractor::new();
        let text = "Rust is a systems programming language.";

        let entities = vec![
            GraphEntity::new("Rust", EntityType::Technology).with_id("rust"),
            GraphEntity::new("systems programming language", EntityType::Concept)
                .with_id("systems_lang"),
        ];

        let result = extractor
            .extract_relationships(text, &entities)
            .await
            .unwrap();
        assert!(!result.is_empty());
    }

    #[tokio::test]
    async fn test_pattern_extractor_uses() {
        let extractor = PatternRelationshipExtractor::new();
        let text = "Rust uses LLVM for code generation.";

        let entities = vec![
            GraphEntity::new("Rust", EntityType::Technology).with_id("rust"),
            GraphEntity::new("LLVM", EntityType::Technology).with_id("llvm"),
        ];

        let result = extractor
            .extract_relationships(text, &entities)
            .await
            .unwrap();
        assert!(!result.is_empty());

        // Should find USES relationship
        let uses_rel = result
            .iter()
            .find(|r| r.relationship_type == RelationshipType::Uses);
        assert!(uses_rel.is_some());
    }

    #[tokio::test]
    async fn test_pattern_extractor_created_by() {
        let extractor = PatternRelationshipExtractor::new();
        let text = "Rust was created by Mozilla.";

        let entities = vec![
            GraphEntity::new("Rust", EntityType::Technology).with_id("rust"),
            GraphEntity::new("Mozilla", EntityType::Organization).with_id("mozilla"),
        ];

        let result = extractor
            .extract_relationships(text, &entities)
            .await
            .unwrap();
        assert!(!result.is_empty());
    }

    #[test]
    fn test_supported_relationship_types() {
        let extractor = PatternRelationshipExtractor::new();
        let types = extractor.supported_relationship_types();

        assert!(types.contains(&RelationshipType::IsA));
        assert!(types.contains(&RelationshipType::Uses));
        assert!(types.contains(&RelationshipType::CreatedBy));
    }
}