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
//! Ontology Pack Metadata and Core Data Structures
//!
//! Extension of GpackMetadata for ontology-specific packs. Ontology packs are
//! specialized template packs that focus on code generation from RDF/OWL ontologies.
//!
//! # Example
//!
//! ```ignore
//! use crate::ontology_pack::{OntologyPackMetadata, OntologyConfig, OntologyDefinition};
//!
//! let pack_metadata = OntologyPackMetadata {
//! base: Default::default(),
//! ontology: OntologyConfig {
//! ontologies: vec![
//! OntologyDefinition {
//! id: "schema.org".to_string(),
//! name: "Schema.org".to_string(),
//! version: "15.0".to_string(),
//! namespace: "https://schema.org/".to_string(),
//! file_path: "ontologies/schema-org.ttl".to_string(),
//! format: OntologyFormat::Turtle,
//! description: "Schema.org vocabulary".to_string(),
//! spec_url: Some("https://schema.org".to_string()),
//! classes_count: 792,
//! properties_count: 1453,
//! }
//! ],
//! ..Default::default()
//! },
//! };
//! ```
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
/// Ontology pack metadata - extends GpackMetadata with ontology-specific fields
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OntologyPackMetadata {
/// Base gpack metadata (id, name, version, description, etc.)
#[serde(flatten)]
pub base: crate::gpack::GpackMetadata,
/// Ontology-specific configuration
#[serde(default)]
pub ontology: OntologyConfig,
}
/// Ontology-specific configuration
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct OntologyConfig {
/// Ontologies included in this pack
#[serde(default)]
pub ontologies: Vec<OntologyDefinition>,
/// Default namespace for generated code
pub default_namespace: Option<String>,
/// Code generation targets supported by this pack
#[serde(default)]
pub targets: Vec<CodeGenTarget>,
/// Template organization (language-specific paths)
#[serde(default)]
pub template_paths: BTreeMap<String, String>,
/// Ontology-specific prefixes
#[serde(default)]
pub prefixes: BTreeMap<String, String>,
}
/// An ontology definition (SCHEMA.ORG, FOAF, etc.)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OntologyDefinition {
/// Ontology identifier (e.g., "schema.org", "foaf", "dublincore")
pub id: String,
/// Human-readable name
pub name: String,
/// Ontology version
pub version: String,
/// Namespace URI
pub namespace: String,
/// Path to ontology file within pack (TTL, RDF/XML, etc.)
pub file_path: String,
/// Format (turtle, rdfxml, ntriples, jsonld)
pub format: OntologyFormat,
/// Description
pub description: String,
/// Official specification URL
pub spec_url: Option<String>,
/// Classes count (populated after extraction)
#[serde(default)]
pub classes_count: usize,
/// Properties count (populated after extraction)
#[serde(default)]
pub properties_count: usize,
}
/// Ontology file format
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum OntologyFormat {
/// Turtle format (.ttl)
Turtle,
/// RDF/XML format (.rdf, .xml)
RdfXml,
/// N-Triples format (.nt)
NTriples,
/// JSON-LD format (.jsonld)
JsonLd,
}
impl OntologyFormat {
/// Get file extension for this format
pub fn extension(&self) -> &str {
match self {
OntologyFormat::Turtle => "ttl",
OntologyFormat::RdfXml => "rdf",
OntologyFormat::NTriples => "nt",
OntologyFormat::JsonLd => "jsonld",
}
}
}
/// Code generation target (language + features)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CodeGenTarget {
/// Target language (typescript, rust, python, etc.)
pub language: String,
/// Features enabled for this target
#[serde(default)]
pub features: Vec<String>, // ["zod", "utilities", "graphql", etc.]
/// Template path for this target (relative to pack root)
pub template_path: String,
/// Output file pattern (e.g., "{class_name}.ts", "types.ts")
pub output_pattern: String,
}
/// Extracted ontology schema (intermediate representation)
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct OntologySchema {
/// Schema namespace
pub namespace: String,
/// Classes extracted from ontology
#[serde(default)]
pub classes: Vec<OntologyClass>,
/// Properties extracted from ontology
#[serde(default)]
pub properties: Vec<OntologyProperty>,
/// Relationships between classes
#[serde(default)]
pub relationships: Vec<OntologyRelationship>,
/// Prefixes used in ontology
#[serde(default)]
pub prefixes: BTreeMap<String, String>,
}
/// Ontology class
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OntologyClass {
/// Class URI
pub uri: String,
/// Class name (extracted from URI)
pub name: String,
/// Label (rdfs:label)
pub label: Option<String>,
/// Comment (rdfs:comment)
pub comment: Option<String>,
/// Properties belonging to this class
#[serde(default)]
pub properties: Vec<String>, // URIs
/// Superclasses
#[serde(default)]
pub super_classes: Vec<String>,
/// Subclasses
#[serde(default)]
pub sub_classes: Vec<String>,
}
/// Ontology property
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OntologyProperty {
/// Property URI
pub uri: String,
/// Property name
pub name: String,
/// Label
pub label: Option<String>,
/// Comment
pub comment: Option<String>,
/// Domain (class this property applies to)
pub domain: String,
/// Range (value type or target class)
pub range: PropertyRange,
/// Cardinality constraints
pub cardinality: Option<Cardinality>,
/// Is this a functional property (single value)?
#[serde(default)]
pub is_functional: bool,
}
/// Property range (value type)
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum PropertyRange {
/// Primitive type (string, integer, boolean, etc.)
Datatype {
/// XSD datatype (xsd:string, xsd:integer, etc.)
datatype: String,
},
/// Reference to another class
Reference {
/// Class URI
class_uri: String,
},
/// Array of values
Array {
/// Item type
item_type: Box<PropertyRange>,
},
}
/// Cardinality constraint
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Cardinality {
/// Minimum occurrences (0 = optional, 1 = required)
pub min: Option<usize>,
/// Maximum occurrences (None = unbounded)
pub max: Option<usize>,
}
/// Relationship between classes
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OntologyRelationship {
/// Source class URI
pub from: String,
/// Target class URI
pub to: String,
/// Property URI that defines this relationship
pub via_property: String,
/// Relationship type
pub relationship_type: RelationshipType,
}
/// Type of relationship between classes
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum RelationshipType {
/// rdfs:subClassOf inheritance
Inheritance,
/// Composition (part-of relationship)
Composition,
/// General association
Association,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_ontology_format_extension() {
assert_eq!(OntologyFormat::Turtle.extension(), "ttl");
assert_eq!(OntologyFormat::RdfXml.extension(), "rdf");
assert_eq!(OntologyFormat::NTriples.extension(), "nt");
assert_eq!(OntologyFormat::JsonLd.extension(), "jsonld");
}
#[test]
fn test_ontology_class_serialization() {
let class = OntologyClass {
uri: "https://schema.org/Product".to_string(),
name: "Product".to_string(),
label: Some("Product".to_string()),
comment: Some("A manufactured product".to_string()),
properties: vec!["https://schema.org/name".to_string()],
super_classes: vec![],
sub_classes: vec![],
};
let json = serde_json::to_string(&class).unwrap();
let deserialized: OntologyClass = serde_json::from_str(&json).unwrap();
assert_eq!(deserialized.name, "Product");
assert_eq!(deserialized.properties.len(), 1);
}
}