oxirs-star 0.1.0

RDF-star and SPARQL-star grammar support for quoted triples
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
# OxiRS-Star Ecosystem Integration Guide

This document provides comprehensive guidance for integrating OxiRS-Star with the broader OxiRS ecosystem and external systems.

## Integration Overview

OxiRS-Star is designed to work seamlessly with:

- **oxirs-core**: Foundation RDF/SPARQL support
- **oxirs-arq**: Query optimization and execution
- **oxirs-vec**: Vector search and semantic similarity
- **oxirs-shacl**: Schema validation with RDF-star support
- **oxirs-fuseki**: HTTP server with RDF-star endpoints
- **oxirs-gql**: GraphQL to SPARQL-star translation

## Core Integration Examples

### Integration with oxirs-arq for Query Optimization

```rust
use oxirs_star::{StarStore, StarTriple, StarTerm};
use oxirs_arq::{QueryExecutor, SparqlStarTranslator};

// Create a store with RDF-star data
let mut store = StarStore::new();
let quoted_triple = StarTriple::new(
    StarTerm::iri("http://example.org/alice")?,
    StarTerm::iri("http://foaf.org/knows")?,
    StarTerm::iri("http://example.org/bob")?,
);

let confidence_triple = StarTriple::new(
    StarTerm::quoted_triple(quoted_triple),
    StarTerm::iri("http://example.org/confidence")?,
    StarTerm::literal("0.95")?,
);

store.insert(&confidence_triple)?;

// Execute SPARQL-star queries with ARQ optimization
let query = "SELECT ?stmt ?conf WHERE { ?stmt <http://example.org/confidence> ?conf }";
let mut executor = QueryExecutor::new();
let translator = SparqlStarTranslator::new();

let results = executor.execute_star_query(&store, query, &translator)?;
for result in results {
    println!("Statement: {}, Confidence: {}", result.get("stmt")?, result.get("conf")?);
}
```

### Integration with oxirs-vec for Semantic Search

```rust
use oxirs_star::{StarStore, StarTriple, StarTerm};
use oxirs_vec::{VectorStore, EmbeddingManager, EmbeddingStrategy};

// Combine RDF-star metadata with vector search
let mut star_store = StarStore::new();
let mut vector_store = VectorStore::new();
let embedding_manager = EmbeddingManager::new(EmbeddingStrategy::SentenceTransformers);

// Create RDF-star data with provenance
let base_triple = StarTriple::new(
    StarTerm::iri("http://example.org/paper1")?,
    StarTerm::iri("http://purl.org/dc/terms/title")?,
    StarTerm::literal("Machine Learning in RDF Processing")?,
);

let provenance_triple = StarTriple::new(
    StarTerm::quoted_triple(base_triple.clone()),
    StarTerm::iri("http://example.org/extractedFrom")?,
    StarTerm::iri("http://arxiv.org/abs/2023.12345")?,
);

// Store metadata
star_store.insert(&provenance_triple)?;

// Generate embeddings for content
let title_embedding = embedding_manager.embed_text("Machine Learning in RDF Processing")?;
vector_store.insert("paper1_title", title_embedding)?;

// Perform hybrid search: vector similarity + metadata filtering
let query_embedding = embedding_manager.embed_text("AI RDF graph processing")?;
let similar_items = vector_store.search(&query_embedding, 10, 0.7)?;

// Filter by provenance metadata
for item in similar_items {
    if let Some(provenance) = star_store.get_metadata(&item.id)? {
        if provenance.contains("arxiv.org") {
            println!("High-quality match: {} (similarity: {})", item.id, item.score);
        }
    }
}
```

### Integration with oxirs-shacl for Validation

```rust
use oxirs_star::{StarStore, StarTriple, StarTerm};
use oxirs_shacl::{ShaclValidator, ValidationReport};

// Define SHACL shapes for RDF-star validation
let shapes_graph = r#"
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix ex: <http://example.org/> .

ex:ConfidenceShape a sh:NodeShape ;
    sh:targetNode [ sh:path [ sh:inversePath ex:confidence ] ] ;
    sh:property [
        sh:path ex:confidence ;
        sh:datatype xsd:decimal ;
        sh:minInclusive 0.0 ;
        sh:maxInclusive 1.0 ;
    ] .
"#;

let mut star_store = StarStore::new();
let validator = ShaclValidator::new();

// Add RDF-star data
let confidence_triple = StarTriple::new(
    StarTerm::quoted_triple(StarTriple::new(
        StarTerm::iri("http://example.org/alice")?,
        StarTerm::iri("http://example.org/knows")?,
        StarTerm::iri("http://example.org/bob")?,
    )),
    StarTerm::iri("http://example.org/confidence")?,
    StarTerm::typed_literal("0.85", "http://www.w3.org/2001/XMLSchema#decimal")?,
);

star_store.insert(&confidence_triple)?;

// Validate RDF-star data against SHACL shapes
let report = validator.validate_star_data(&star_store, shapes_graph)?;
if report.conforms() {
    println!("RDF-star data is valid!");
} else {
    for violation in report.violations() {
        println!("Validation error: {}", violation.message());
    }
}
```

## Advanced Integration Patterns

### Federation with Multiple RDF-star Endpoints

```rust
use oxirs_star::integration::{FederationManager, EndpointConfig};
use std::collections::HashMap;

// Configure multiple RDF-star endpoints
let endpoints = vec![
    EndpointConfig {
        name: "academic-papers".to_string(),
        url: "https://papers.example.org/sparql-star".to_string(),
        supported_formats: vec![StarFormat::TurtleStar, StarFormat::JsonLdStar],
        timeout: 30,
        ..Default::default()
    },
    EndpointConfig {
        name: "knowledge-base".to_string(),
        url: "https://kb.example.org/sparql-star".to_string(),
        supported_formats: vec![StarFormat::TrigStar, StarFormat::NQuadsStar],
        timeout: 15,
        ..Default::default()
    },
];

let federation_manager = FederationManager::new(endpoints);

// Execute federated SPARQL-star query
let federated_query = r#"
    PREFIX ex: <http://example.org/>
    
    SELECT ?paper ?confidence ?source WHERE {
        SERVICE <academic-papers> {
            << ?paper ex:hasAuthor ?author >> ex:confidence ?confidence .
        }
        SERVICE <knowledge-base> {
            << ?paper ex:hasAuthor ?author >> ex:source ?source .
        }
        FILTER(?confidence > 0.8)
    }
"#;

let results = federation_manager.execute_federated_query(federated_query).await?;
for result in results {
    println!("Paper: {}, Confidence: {}, Source: {}", 
             result.get("paper")?, result.get("confidence")?, result.get("source")?);
}
```

### Real-time Streaming Integration

```rust
use oxirs_star::{StarStore, StarTriple};
use oxirs_stream::{StreamProcessor, StarEventProcessor};
use tokio_stream::StreamExt;

// Create streaming processor for RDF-star events
let mut processor = StarEventProcessor::new();
let mut store = StarStore::new();

// Configure real-time processing of RDF-star streams
processor.on_triple_insert(|triple: StarTriple| async move {
    // Process new RDF-star triples in real-time
    if triple.has_quoted_subject() || triple.has_quoted_object() {
        // Handle metadata updates
        update_metadata_index(&triple).await?;
        
        // Trigger validation if needed
        if requires_validation(&triple) {
            validate_triple_async(&triple).await?;
        }
        
        // Update search indexes
        update_search_indexes(&triple).await?;
    }
    Ok(())
});

// Process streaming RDF-star data
let stream = create_rdf_star_stream().await?;
tokio::pin!(stream);

while let Some(event) = stream.next().await {
    processor.process(event).await?;
}
```

## Monitoring and Observability

### Performance Monitoring Integration

```rust
use oxirs_star::profiling::{StarProfiler, PerformanceCollector};
use oxirs_monitoring::{MetricsCollector, AlertManager};

// Set up comprehensive monitoring
let profiler = StarProfiler::new();
let metrics = MetricsCollector::new();
let alerts = AlertManager::new();

// Monitor RDF-star operations
profiler.start_monitoring();

// Set up alerts for performance issues
alerts.add_threshold("parsing_latency_ms", 1000.0);
alerts.add_threshold("query_execution_time_ms", 5000.0);
alerts.add_threshold("memory_usage_mb", 1024.0);

// Collect and export metrics
let report = profiler.generate_detailed_report();
metrics.export_prometheus_metrics(&report)?;

// Real-time dashboard integration
let dashboard_data = serde_json::to_string(&report)?;
send_to_dashboard("rdf-star-metrics", &dashboard_data).await?;
```

## Production Deployment Patterns

### Docker Integration

```dockerfile
# Dockerfile for RDF-star service
FROM rust:1.70 as builder

WORKDIR /app
COPY . .
RUN cargo build --release --package oxirs-star

FROM debian:bookworm-slim
COPY --from=builder /app/target/release/oxirs-star /usr/local/bin/
COPY config/production.toml /etc/oxirs/config.toml

EXPOSE 8080
CMD ["oxirs-star", "--config", "/etc/oxirs/config.toml"]
```

### Kubernetes Deployment

```yaml
# kubernetes/rdf-star-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: oxirs-star
spec:
  replicas: 3
  selector:
    matchLabels:
      app: oxirs-star
  template:
    metadata:
      labels:
        app: oxirs-star
    spec:
      containers:
      - name: oxirs-star
        image: oxirs/star:latest
        ports:
        - containerPort: 8080
        env:
        - name: RUST_LOG
          value: "info"
        - name: OXIRS_CONFIG
          value: "/etc/oxirs/config.toml"
        resources:
          requests:
            memory: "512Mi"
            cpu: "500m"
          limits:
            memory: "2Gi"
            cpu: "2000m"
        livenessProbe:
          httpGet:
            path: /health
            port: 8080
          initialDelaySeconds: 30
          periodSeconds: 10
        readinessProbe:
          httpGet:
            path: /ready
            port: 8080
          initialDelaySeconds: 5
          periodSeconds: 5
---
apiVersion: v1
kind: Service
metadata:
  name: oxirs-star-service
spec:
  selector:
    app: oxirs-star
  ports:
  - protocol: TCP
    port: 80
    targetPort: 8080
  type: LoadBalancer
```

## CLI Integration Examples

### Batch Processing

```bash
# Convert between RDF-star formats
oxirs-star convert \
    --input data.ttls \
    --output data.nts \
    --from turtle-star \
    --to ntriples-star \
    --validate

# Validate RDF-star data
oxirs-star validate \
    --data data.ttls \
    --shapes shapes.ttl \
    --format turtle-star \
    --report validation-report.json

# Performance profiling
oxirs-star profile \
    --operation parsing \
    --data large-dataset.ttls \
    --iterations 100 \
    --output profile-report.json
```

### Integration with CI/CD Pipelines

```yaml
# .github/workflows/rdf-star-validation.yml
name: RDF-star Validation
on: [push, pull_request]

jobs:
  validate:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    
    - name: Install OxiRS-Star
      run: |
        cargo install oxirs-star
        
    - name: Validate RDF-star Data
      run: |
        oxirs-star validate \
          --data data/**/*.ttls \
          --recursive \
          --fail-on-error \
          --report validation-report.json
          
    - name: Upload Validation Report
      uses: actions/upload-artifact@v3
      with:
        name: validation-report
        path: validation-report.json
```

## Best Practices for Production

### Configuration Management

```toml
# production.toml
[star]
# Performance optimization
enable_indexing = true
index_quoted_triples = true
cache_size = 100000
parallel_parsing = true
memory_limit_mb = 2048

# Security settings
[security]
enable_authentication = true
rate_limiting = true
max_query_complexity = 1000
sandbox_sparql = true

# Monitoring
[monitoring]
enable_metrics = true
metrics_port = 9090
health_check_port = 8081
log_level = "info"

# Integration endpoints
[[endpoints]]
name = "primary-store"
url = "https://store.example.org/sparql-star"
timeout = 30
pool_size = 10
```

### Error Handling and Recovery

```rust
use oxirs_star::{StarStore, StarError, StarErrorKind};
use anyhow::{Context, Result};

// Robust error handling for production
async fn robust_rdf_star_processing(data: &str) -> Result<()> {
    let mut store = StarStore::new();
    
    match store.parse_and_insert(data).await {
        Ok(_) => {
            info!("Successfully processed RDF-star data");
            Ok(())
        },
        Err(StarError { kind: StarErrorKind::ParseError, .. }) => {
            warn!("Parse error encountered, attempting recovery");
            // Attempt graceful recovery
            let cleaned_data = sanitize_rdf_star_input(data)?;
            store.parse_and_insert(&cleaned_data).await
                .context("Recovery attempt failed")
        },
        Err(StarError { kind: StarErrorKind::ValidationError, .. }) => {
            error!("Validation failed, data rejected");
            Err(anyhow::anyhow!("Data validation failed"))
        },
        Err(e) => {
            error!("Unexpected error: {}", e);
            Err(e.into())
        }
    }
}
```

## Migration and Upgrade Paths

### From Standard RDF to RDF-star

```rust
use oxirs_star::migration::{RdfToStarMigrator, MigrationStrategy};

// Migrate existing RDF data to RDF-star format
let migrator = RdfToStarMigrator::new();
let strategy = MigrationStrategy::ConservativeReification;

let rdf_data = load_existing_rdf_data()?;
let star_data = migrator.migrate(rdf_data, strategy)?;

// Validate migration results
let validation_report = migrator.validate_migration(&star_data)?;
if validation_report.success_rate() > 0.95 {
    commit_migration(&star_data)?;
} else {
    rollback_migration()?;
}
```

This comprehensive ecosystem integration guide provides production-ready patterns and examples for deploying OxiRS-Star in enterprise environments with full observability, monitoring, and integration capabilities.