oxirouter 0.1.0

Autonomous Semantic Federation Engine for the Edge - Learned source selection for SPARQL federated queries with context-awareness
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//! Integration tests for OxiRouter

use oxirouter::core::source::SourceCapabilities;
use oxirouter::prelude::*;

#[test]
fn test_basic_routing() {
    let mut router = Router::new();

    // Add some sources
    router.add_source(
        DataSource::new("dbpedia", "https://dbpedia.org/sparql")
            .with_capabilities(SourceCapabilities::full())
            .with_vocabulary("http://dbpedia.org/ontology/")
            .with_region("EU"),
    );

    router.add_source(
        DataSource::new("wikidata", "https://query.wikidata.org/sparql")
            .with_capabilities(SourceCapabilities::full())
            .with_vocabulary("http://www.wikidata.org/")
            .with_region("EU"),
    );

    // Create and route a query
    let query = Query::parse(
        r"
        PREFIX dbo: <http://dbpedia.org/ontology/>
        SELECT ?name WHERE {
            ?person a dbo:Person .
            ?person dbo:name ?name .
        }
        LIMIT 100
        ",
    )
    .unwrap();

    let ranking = router.route(&query).unwrap();

    assert!(!ranking.is_empty());
    assert!(ranking.best().is_some());
}

#[test]
fn test_source_management() {
    let mut router = Router::new();

    // Add source
    router.add_source(DataSource::new("test", "http://example.com/sparql"));
    assert_eq!(router.source_count(), 1);

    // Get source
    let source = router.get_source("test");
    assert!(source.is_some());
    assert_eq!(source.unwrap().endpoint, "http://example.com/sparql");

    // Remove source
    let removed = router.remove_source("test");
    assert!(removed.is_some());
    assert_eq!(router.source_count(), 0);
}

#[test]
fn test_query_parsing() {
    // SELECT query
    let select = Query::parse("SELECT ?s WHERE { ?s ?p ?o }").unwrap();
    assert_eq!(select.query_type, oxirouter::core::query::QueryType::Select);

    // CONSTRUCT query
    let construct = Query::parse("CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o }").unwrap();
    assert_eq!(
        construct.query_type,
        oxirouter::core::query::QueryType::Construct
    );

    // ASK query
    let ask = Query::parse("ASK { ?s ?p ?o }").unwrap();
    assert_eq!(ask.query_type, oxirouter::core::query::QueryType::Ask);

    // DESCRIBE query
    let describe = Query::parse("DESCRIBE <http://example.org/resource>").unwrap();
    assert_eq!(
        describe.query_type,
        oxirouter::core::query::QueryType::Describe
    );
}

#[test]
fn test_query_complexity() {
    let simple = Query::parse("SELECT ?s WHERE { ?s ?p ?o }").unwrap();

    let complex = Query::parse(
        r"
        SELECT ?s (COUNT(?o) AS ?count) WHERE {
            { ?s ?p ?o } UNION { ?s ?p2 ?o2 }
            OPTIONAL { ?s ?p3 ?o3 }
            FILTER(?count > 5)
        }
        GROUP BY ?s
        ",
    )
    .unwrap();

    assert!(complex.complexity > simple.complexity);
    assert!(complex.has_union);
    assert!(complex.has_optional);
    assert!(complex.has_filter);
    assert!(complex.has_aggregation);
}

#[test]
fn test_source_stats_update() {
    let mut router = Router::new();
    router.add_source(DataSource::new("test", "http://example.com/sparql"));

    // Update stats
    router.update_source_stats("test", 100, true, 50).unwrap();
    router.update_source_stats("test", 200, true, 30).unwrap();
    router.update_source_stats("test", 150, false, 0).unwrap();

    let source = router.get_source("test").unwrap();
    assert_eq!(source.stats.total_queries, 3);
    assert_eq!(source.stats.successful_queries, 2);
    assert!(source.stats.avg_latency_ms > 0.0);
}

#[test]
fn test_source_availability() {
    let mut router = Router::new();
    router.add_source(DataSource::new("test", "http://example.com/sparql"));

    // Initially available
    assert!(router.get_source("test").unwrap().available);

    // Mark unavailable
    router.mark_unavailable("test").unwrap();
    assert!(!router.get_source("test").unwrap().available);

    // Mark available again
    router.mark_available("test").unwrap();
    assert!(router.get_source("test").unwrap().available);
}

#[test]
fn test_vocabulary_matching() {
    let mut router = Router::new();

    router.add_source(
        DataSource::new("schema", "http://schema.example.com/sparql")
            .with_vocabulary("http://schema.org/"),
    );

    router.add_source(
        DataSource::new("foaf", "http://foaf.example.com/sparql")
            .with_vocabulary("http://xmlns.com/foaf/0.1/"),
    );

    // Query with schema.org vocabulary
    let query = Query::parse(
        r"
        PREFIX schema: <http://schema.org/>
        SELECT ?name WHERE { ?s schema:name ?name }
        ",
    )
    .unwrap();

    let ranking = router.route(&query).unwrap();

    // Source with matching vocabulary should rank higher
    assert!(!ranking.is_empty());
}

#[test]
fn test_capability_requirements() {
    let mut router = Router::new();

    router.add_source(
        DataSource::new("basic", "http://basic.example.com/sparql")
            .with_capabilities(SourceCapabilities::basic()),
    );

    router.add_source(
        DataSource::new("full", "http://full.example.com/sparql")
            .with_capabilities(SourceCapabilities::full()),
    );

    // Query requiring SPARQL 1.1
    let query = Query::parse(
        r"
        SELECT ?s (COUNT(?o) AS ?count) WHERE {
            ?s ?p ?o
        }
        GROUP BY ?s
        ",
    )
    .unwrap();

    assert!(query.requires_sparql_1_1());

    let ranking = router.route(&query).unwrap();

    // Only full-featured source should be selected
    assert!(!ranking.is_empty());
    for selection in ranking.sources.iter() {
        if selection.source_id == "basic" {
            // Basic source should not be in ranking or should have very low confidence
        }
    }
}

#[test]
fn test_no_sources_error() {
    let router = Router::new();
    let query = Query::parse("SELECT ?s WHERE { ?s ?p ?o }").unwrap();

    let result = router.route(&query);
    assert!(result.is_err());
    assert!(matches!(
        result.unwrap_err(),
        OxiRouterError::NoSources { .. }
    ));
}

#[test]
fn test_invalid_query_error() {
    let result = Query::parse("NOT A VALID QUERY");
    assert!(result.is_err());
}

#[test]
fn test_router_config() {
    let config = oxirouter::core::router::RouterConfig {
        max_sources: 3,
        min_confidence: 0.5,
        use_ml: false,
        use_context: false,
        timeout_us: 50_000,
        history_weight: 0.4,
        vocab_weight: 0.3,
        geo_weight: 0.3,
        circuit_breaker: oxirouter::CircuitBreakerConfig::default(),
        max_response_bytes: 64 * 1024 * 1024,
        #[cfg(feature = "cache")]
        cache_enabled: true,
        #[cfg(feature = "cache")]
        cache_ttl_ms: 300_000,
        #[cfg(feature = "cache")]
        cache_max_entries: 1000,
    };

    let router = Router::with_config(config, oxirouter::context::DefaultContextProvider);

    // Verify config is applied
    assert_eq!(router.config().max_sources, 3);
    assert!(!router.config().use_ml);
}

#[test]
fn test_federation_executor() {
    use oxirouter::federation::{ExecutionConfig, Executor};

    let config = ExecutionConfig {
        timeout_ms: 5000,
        max_retries: 1,
        parallel: true,
        max_concurrency: 2,
        ..Default::default()
    };

    let executor = Executor::with_config(config);
    assert_eq!(executor.config().timeout_ms, 5000);
}

#[test]
fn test_federation_aggregator() {
    use oxirouter::federation::aggregator::{AggregationConfig, AggregationStrategy};
    use oxirouter::federation::{Aggregator, QueryResult};

    let config = AggregationConfig {
        strategy: AggregationStrategy::Largest,
        ..Default::default()
    };

    let aggregator = Aggregator::with_config(config);

    let results = vec![
        QueryResult::success("src1", vec![], 10, 100),
        QueryResult::success("src2", vec![], 50, 200),
    ];

    let aggregated = aggregator.aggregate(&results).unwrap();
    assert_eq!(aggregated.row_count, 50);
}

#[test]
fn test_context_combined() {
    use oxirouter::context::CombinedContext;

    let ctx = CombinedContext::new();
    assert!(ctx.quality_score() >= 0.0);
}

#[cfg(feature = "geo")]
#[test]
fn test_geo_context() {
    use oxirouter::GeoContext;

    let geo = GeoContext::from_coords(-122.4194, 37.7749)
        .with_country("US")
        .with_region("California");

    assert_eq!(geo.country_code, Some("US".to_string()));
    assert!(!geo.is_eu_region());

    // Test distance calculation
    let distance = geo.distance_to(-122.0, 37.0).unwrap();
    assert!(distance > 0.0 && distance < 100.0); // Within 100km
}

#[cfg(feature = "ml")]
#[test]
fn test_feature_extraction() {
    use oxirouter::ml::FeatureVector;

    let query = Query::parse("SELECT ?s WHERE { ?s ?p ?o }").unwrap();
    let features = FeatureVector::from_query(&query).unwrap();

    assert!(!features.is_empty());
    assert!(features.get("query_type_select").is_some());
}

#[cfg(feature = "ml")]
#[test]
fn test_naive_bayes() {
    use oxirouter::ml::{FeatureVector, Model, NaiveBayesClassifier};

    let mut nb = NaiveBayesClassifier::new(10);
    let sources = vec!["src1".to_string(), "src2".to_string()];
    let source_refs: Vec<&String> = sources.iter().collect();
    nb.initialize_sources(&source_refs);

    let mut features = FeatureVector::new();
    for i in 0..10 {
        features.add(format!("f{}", i), 0.5);
    }

    let predictions = nb.predict(&features, &source_refs).unwrap();
    assert_eq!(predictions.len(), 2);
}

#[cfg(feature = "rl")]
#[test]
fn test_feedback_and_reward() {
    use oxirouter::rl::Feedback;

    let success = Feedback::success("src1", 123, 100, 50);
    let reward = success.to_reward();
    assert!(reward.value() > 0.5);

    let failure = Feedback::failure("src1", 123, "Error");
    let reward = failure.to_reward();
    assert!(reward.value() < 0.5);
}

#[cfg(feature = "rl")]
#[test]
fn test_policy_update() {
    use oxirouter::rl::{Policy, Reward};

    let mut policy = Policy::ucb();
    policy.initialize_source("src1");

    let initial_q = policy.get_q_value("src1");
    policy.update("src1", Reward::new(0.9));

    assert!(policy.get_q_value("src1") > initial_q);
}

#[test]
fn test_query_log_routing() {
    use oxirouter::QueryLog;

    let mut log = QueryLog::new();

    // Record some routing decisions and outcomes
    log.record_routing(1, "dbpedia", 0.9, false, None);
    log.record_outcome(1, "dbpedia", true, 150, 100, 0.9);

    log.record_routing(2, "wikidata", 0.7, false, None);
    log.record_outcome(2, "wikidata", false, 5000, 0, 0.0);

    assert_eq!(log.len(), 2);
    assert_eq!(log.best_source(), Some("dbpedia"));

    let ranked = log.ranked_sources();
    assert_eq!(ranked[0].0, "dbpedia");
}

#[test]
fn test_route_and_log() {
    let mut router = Router::new();

    router.add_source(
        DataSource::new("dbpedia", "https://dbpedia.org/sparql")
            .with_vocabulary("http://dbpedia.org/ontology/"),
    );

    let query = Query::parse(
        r"PREFIX dbo: <http://dbpedia.org/ontology/>
           SELECT ?s WHERE { ?s a dbo:Person }",
    )
    .unwrap();

    let ranking = router.route_and_log(&query).unwrap();
    assert!(!ranking.is_empty());

    // Query log should have an entry
    assert!(!router.query_log().is_empty());

    // Record outcome
    let query_id = query.predicate_hash();
    router
        .learn_from_outcome(query_id, "dbpedia", true, 200, 50)
        .unwrap();

    // Source stats should be updated
    let source = router.get_source("dbpedia").unwrap();
    assert_eq!(source.stats.total_queries, 1);

    // Query log should have outcome recorded
    let stats = router.query_log().source_stats("dbpedia").unwrap();
    assert_eq!(stats.outcomes_received, 1);
}

#[cfg(feature = "rl")]
#[test]
fn test_route_with_rl_policy() {
    let mut router = Router::new();

    router.add_source(
        DataSource::new("good", "https://good.example.com/sparql")
            .with_vocabulary("http://schema.org/"),
    );
    router.add_source(
        DataSource::new("bad", "https://bad.example.com/sparql")
            .with_vocabulary("http://schema.org/"),
    );

    // Enable RL
    router.enable_rl();

    let query =
        Query::parse("PREFIX s: <http://schema.org/> SELECT ?x WHERE { ?x a s:Person }").unwrap();

    let query_id = query.predicate_hash();

    // Simulate multiple rounds — "good" always succeeds, "bad" always fails
    for _ in 0..5 {
        let _ranking = router.route_and_log(&query).unwrap();

        router
            .learn_from_outcome(query_id, "good", true, 100, 50)
            .unwrap();
        router
            .learn_from_outcome(query_id, "bad", false, 5000, 0)
            .unwrap();
    }

    // After learning, "good" should have higher score in the log
    let log = router.query_log();
    let good_score = log.routing_score("good").unwrap_or(0.5);
    let bad_score = log.routing_score("bad").unwrap_or(0.5);
    assert!(good_score > bad_score, "good={good_score} bad={bad_score}");
}