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
//! OxiRS Bridge: RDF*/SHACL/GraphQL → TensorLogic Integration
//!
//! **Version**: 0.1.0 | **Status**: Production Ready
//!
//! This crate provides comprehensive bidirectional integration between RDF knowledge graphs
//! and TensorLogic's tensor-based logical reasoning system. It enables semantic web data
//! to be compiled into tensor operations while preserving provenance and validation semantics.
//!
//! # Overview
//!
//! The bridge connects five key components:
//!
//! 1. **Schema Import**: RDF/OWL schemas → TensorLogic [`SymbolTable`](tensorlogic_adapters::SymbolTable)
//! 2. **Constraint Compilation**: SHACL shapes → TensorLogic [`TLExpr`](tensorlogic_ir::TLExpr) rules
//! 3. **Provenance Tracking**: RDF entities ↔ tensor indices with RDF* metadata
//! 4. **Validation**: SHACL-compliant validation reports from tensor outputs
//! 5. **GraphQL Integration**: GraphQL schemas → TensorLogic domains/predicates
//!
//! # Quick Start
//!
//! ```
//! use tensorlogic_oxirs_bridge::SchemaAnalyzer;
//! use anyhow::Result;
//!
//! fn main() -> Result<()> {
//! // Define an RDF schema
//! let turtle = r#"
//! @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
//! @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
//! @prefix ex: <http://example.org/> .
//!
//! ex:Person a rdfs:Class ;
//! rdfs:label "Person" .
//!
//! ex:knows a rdf:Property ;
//! rdfs:domain ex:Person ;
//! rdfs:range ex:Person .
//! "#;
//!
//! // Parse and analyze the schema
//! let mut analyzer = SchemaAnalyzer::new();
//! analyzer.load_turtle(turtle)?;
//! analyzer.analyze()?;
//!
//! // Convert to TensorLogic symbol table
//! let symbol_table = analyzer.to_symbol_table()?;
//! println!("Converted {} classes and {} properties",
//! symbol_table.domains.len(),
//! symbol_table.predicates.len());
//! Ok(())
//! }
//! ```
//!
//! # Key Features
//!
//! ## RDF/OWL Schema Support
//!
//! - **RDFS**: Classes, properties, subclass/subproperty hierarchies
//! - **OWL**: Class expressions, property characteristics, restrictions
//! - **RDFS Inference**: Automatic entailment and materialization
//! - **Formats**: Turtle, N-Triples, JSON-LD
//!
//! ### SHACL Validation
//! - **Constraint Types**: 15+ SHACL constraint types (minCount, pattern, datatype, etc.)
//! - **Logical Operators**: sh:and, sh:or, sh:not, sh:xone
//! - **Validation Reports**: Full SHACL-compliant reports with severity levels
//! - **Export**: Turtle and JSON export formats
//!
//! ### SPARQL 1.1 Query Support
//! - **Query Types**: SELECT, ASK, DESCRIBE, CONSTRUCT
//! - **Graph Patterns**: OPTIONAL (left-outer join), UNION (disjunction)
//! - **Filters**: Comparison operators, BOUND, isIRI, isLiteral, regex
//! - **Solution Modifiers**: DISTINCT, LIMIT, OFFSET, ORDER BY
//! - **Compilation**: Full compilation to TensorLogic expressions
//!
//! ## Performance Features
//!
//! - **Triple Indexing**: O(1) lookups by subject/predicate/object
//! - **Caching**: In-memory and file-based caching (10-50x speedup)
//! - **Metadata**: Multilingual label preservation and quality checking
//!
//! ## Provenance & Tracking
//!
//! - **RDF* Support**: Statement-level metadata with quoted triples
//! - **Bidirectional Mapping**: Entities ↔ tensor indices
//! - **Confidence Tracking**: Confidence scores for inferred statements
//! - **Rule Attribution**: Track which rules produced which results
//!
//! # Architecture
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────┐
//! │ Input Formats │
//! │ Turtle │ N-Triples │ JSON-LD │ GraphQL │ SHACL │
//! └────────────┬────────────────────────────────────────────────┘
//! │
//! ▼
//! ┌─────────────────────────────────────────────────────────────┐
//! │ SchemaAnalyzer │
//! │ • Parse RDF triples │
//! │ • Extract classes & properties │
//! │ • Build indexes (optional) │
//! │ • Preserve metadata (optional) │
//! └────────────┬────────────────────────────────────────────────┘
//! │
//! ▼
//! ┌─────────────────────────────────────────────────────────────┐
//! │ SymbolTable │
//! │ • Domains (from classes) │
//! │ • Predicates (from properties) │
//! │ • Axis metadata │
//! └────────────┬────────────────────────────────────────────────┘
//! │
//! ▼
//! ┌─────────────────────────────────────────────────────────────┐
//! │ TensorLogic Compiler │
//! │ • TLExpr (logical expressions) │
//! │ • Tensor operations │
//! │ • Provenance tracking │
//! └─────────────────────────────────────────────────────────────┘
//! ```
//!
//! # Examples
//!
//! ## With Performance Features
//!
//! ```
//! use tensorlogic_oxirs_bridge::SchemaAnalyzer;
//! use anyhow::Result;
//!
//! fn main() -> Result<()> {
//! // Enable indexing and metadata preservation
//! let mut analyzer = SchemaAnalyzer::new()
//! .with_indexing()
//! .with_metadata();
//!
//! analyzer.load_turtle(r#"
//! @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
//! @prefix ex: <http://example.org/> .
//!
//! ex:Person rdfs:label "Person"@en, "Personne"@fr .
//! "#)?;
//!
//! // Fast indexed lookup
//! if let Some(index) = analyzer.index() {
//! let triples = index.find_by_subject("http://example.org/Person");
//! println!("Found {} triples", triples.len());
//! }
//!
//! // Multilingual metadata
//! if let Some(metadata) = analyzer.metadata() {
//! if let Some(meta) = metadata.get("http://example.org/Person") {
//! println!("EN: {}", meta.get_label(Some("en")).unwrap_or("N/A"));
//! println!("FR: {}", meta.get_label(Some("fr")).unwrap_or("N/A"));
//! }
//! }
//! Ok(())
//! }
//! ```
//!
//! ## SHACL Validation
//!
//! ```
//! use tensorlogic_oxirs_bridge::{ShaclConverter, SchemaAnalyzer};
//! use tensorlogic_adapters::SymbolTable;
//! use anyhow::Result;
//!
//! fn main() -> Result<()> {
//! let shacl = r#"
//! @prefix sh: <http://www.w3.org/ns/shacl#> .
//! @prefix ex: <http://example.org/> .
//! @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
//!
//! ex:PersonShape a sh:NodeShape ;
//! sh:targetClass ex:Person ;
//! sh:property [
//! sh:path ex:name ;
//! sh:minCount 1 ;
//! sh:datatype xsd:string ;
//! ] .
//! "#;
//!
//! let symbol_table = SymbolTable::new();
//! let mut converter = ShaclConverter::new(symbol_table);
//! converter.parse_shapes(shacl)?;
//!
//! println!("Parsed SHACL shapes successfully");
//! Ok(())
//! }
//! ```
//!
//! ## Caching for Performance
//!
//! ```
//! use tensorlogic_oxirs_bridge::{SchemaCache, SchemaAnalyzer};
//! use anyhow::Result;
//!
//! fn main() -> Result<()> {
//! let mut cache = SchemaCache::new();
//! let turtle = "@prefix ex: <http://example.org/> .";
//!
//! // First access - cache miss
//! let table = if let Some(cached) = cache.get_symbol_table(turtle) {
//! cached
//! } else {
//! let mut analyzer = SchemaAnalyzer::new();
//! analyzer.load_turtle(turtle)?;
//! analyzer.analyze()?;
//! let table = analyzer.to_symbol_table()?;
//! cache.put_symbol_table(turtle, table.clone());
//! table
//! };
//!
//! // Second access - cache hit (20-50x faster!)
//! assert!(cache.get_symbol_table(turtle).is_some());
//! Ok(())
//! }
//! ```
//!
//! # Module Organization
//!
//! - [`schema`] - Core RDF schema parsing and analysis
//! - [`cache`](schema::cache) - Caching for performance
//! - [`index`](schema::index) - Triple indexing for fast lookups
//! - [`metadata`](schema::metadata) - Multilingual metadata management
//! - [`owl`](schema::owl) - OWL ontology support
//! - [`inference`](schema::inference) - RDFS inference engine
//! - [`shacl`] - SHACL constraint compilation and validation
//! - [`rdfstar`] - RDF* provenance tracking
//! - [`graphql`] - GraphQL schema integration
//! - [`sparql`] - SPARQL 1.1 query compilation (SELECT/ASK/DESCRIBE/CONSTRUCT + OPTIONAL/UNION)
//!
//! # See Also
//!
//! - [Examples directory](https://github.com/cool-japan/tensorlogic/tree/main/crates/tensorlogic-oxirs-bridge/examples) - Comprehensive examples
//! - [TensorLogic project](https://github.com/cool-japan/tensorlogic) - Main project repository
//! - [OxiRS](https://github.com/cool-japan/oxirs) - Full RDF/SPARQL support
//!
//! # Implementation Note
//!
//! This is a **lightweight implementation** using only `oxrdf` and `oxttl` for RDF parsing.
//! For full SPARQL query execution, federation, and advanced RDF features, use the `oxirs-core` crate.
pub use BlankNodeManager;
pub use compile_rules;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ProvenanceTracker;
pub use QuadStore;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use TurtleExporter;
pub use ;