sbol2 1.1.0

Rust implementation of the SBOL 2.3.0 specification: typed data model, RDF I/O, and validation.
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
//! Field-metadata catalog for the SBOL 2 data model.
//!
//! [`FieldDescriptor`] describes one property: its predicate IRI, cardinality,
//! value kind, optional reference target, and validation rule. `class_spec`
//! maps every class IRI to its parents and directly-declared fields; the RDF
//! serializer and (later) validator share this single source of truth so the
//! schema cannot drift between subsystems.

use std::collections::BTreeMap;

use crate::Object;
use crate::vocab::*;

pub use sbol_core::schema::{
    Cardinality, ClassDescriptor, FieldDescriptor, ReferenceSpec, TargetClass, ValueKind,
};

mod properties;
use properties::*;

pub(crate) type PropertySpec = FieldDescriptor;
pub(crate) type ClassSpec = ClassDescriptor;

/// Looks up the [`ClassDescriptor`] for an SBOL 2 class by its RDF type IRI.
/// Returns `None` if the IRI is not a known SBOL 2, PROV, or OM class.
pub fn class_descriptor(class_iri: &str) -> Option<ClassDescriptor> {
    class_spec(class_iri)
}

/// Returns the field descriptors declared directly on the given class.
pub fn class_fields(class: crate::Sbol2Class) -> &'static [FieldDescriptor] {
    class_spec(class.iri())
        .map(|descriptor| descriptor.fields)
        .unwrap_or(&[])
}

pub(crate) fn class_spec(iri: &str) -> Option<ClassSpec> {
    Some(match iri {
        SBOL2_IDENTIFIED_CLASS => ClassSpec {
            parents: &[],
            fields: IDENTIFIED_PROPS,
        },
        SBOL2_TOP_LEVEL_CLASS => ClassSpec {
            parents: &[SBOL2_IDENTIFIED_CLASS],
            fields: TOP_LEVEL_PROPS,
        },
        SBOL2_MEASURED_CLASS => ClassSpec {
            parents: &[SBOL2_IDENTIFIED_CLASS],
            fields: MEASURED_PROPS,
        },
        SBOL2_COMPONENT_INSTANCE_CLASS => ClassSpec {
            parents: &[SBOL2_MEASURED_CLASS],
            fields: COMPONENT_INSTANCE_PROPS,
        },
        SBOL2_LOCATION_CLASS => ClassSpec {
            parents: &[SBOL2_IDENTIFIED_CLASS],
            fields: LOCATION_PROPS,
        },
        SBOL2_SEQUENCE_CLASS => ClassSpec {
            parents: &[SBOL2_TOP_LEVEL_CLASS],
            fields: SEQUENCE_PROPS,
        },
        SBOL2_COMPONENT_DEFINITION_CLASS => ClassSpec {
            parents: &[SBOL2_TOP_LEVEL_CLASS],
            fields: COMPONENT_DEFINITION_PROPS,
        },
        SBOL2_MODULE_DEFINITION_CLASS => ClassSpec {
            parents: &[SBOL2_TOP_LEVEL_CLASS],
            fields: MODULE_DEFINITION_PROPS,
        },
        SBOL2_MODEL_CLASS => ClassSpec {
            parents: &[SBOL2_TOP_LEVEL_CLASS],
            fields: MODEL_PROPS,
        },
        SBOL2_COLLECTION_CLASS => ClassSpec {
            parents: &[SBOL2_TOP_LEVEL_CLASS],
            fields: COLLECTION_PROPS,
        },
        SBOL2_COMBINATORIAL_DERIVATION_CLASS => ClassSpec {
            parents: &[SBOL2_TOP_LEVEL_CLASS],
            fields: COMBINATORIAL_DERIVATION_PROPS,
        },
        SBOL2_IMPLEMENTATION_CLASS => ClassSpec {
            parents: &[SBOL2_TOP_LEVEL_CLASS],
            fields: IMPLEMENTATION_PROPS,
        },
        SBOL2_ATTACHMENT_CLASS => ClassSpec {
            parents: &[SBOL2_TOP_LEVEL_CLASS],
            fields: ATTACHMENT_PROPS,
        },
        SBOL2_EXPERIMENTAL_DATA_CLASS => ClassSpec {
            parents: &[SBOL2_TOP_LEVEL_CLASS],
            fields: EXPERIMENTAL_DATA_PROPS,
        },
        SBOL2_EXPERIMENT_CLASS => ClassSpec {
            parents: &[SBOL2_TOP_LEVEL_CLASS],
            fields: EXPERIMENT_PROPS,
        },
        SBOL2_GENERIC_TOP_LEVEL_CLASS => ClassSpec {
            parents: &[SBOL2_TOP_LEVEL_CLASS],
            fields: GENERIC_TOP_LEVEL_PROPS,
        },
        SBOL2_COMPONENT_CLASS => ClassSpec {
            parents: &[SBOL2_COMPONENT_INSTANCE_CLASS],
            fields: COMPONENT_PROPS,
        },
        SBOL2_FUNCTIONAL_COMPONENT_CLASS => ClassSpec {
            parents: &[SBOL2_COMPONENT_INSTANCE_CLASS],
            fields: FUNCTIONAL_COMPONENT_PROPS,
        },
        SBOL2_MODULE_CLASS => ClassSpec {
            parents: &[SBOL2_MEASURED_CLASS],
            fields: MODULE_PROPS,
        },
        SBOL2_MAPS_TO_CLASS => ClassSpec {
            parents: &[SBOL2_IDENTIFIED_CLASS],
            fields: MAPS_TO_PROPS,
        },
        SBOL2_SEQUENCE_ANNOTATION_CLASS => ClassSpec {
            parents: &[SBOL2_IDENTIFIED_CLASS],
            fields: SEQUENCE_ANNOTATION_PROPS,
        },
        SBOL2_SEQUENCE_CONSTRAINT_CLASS => ClassSpec {
            parents: &[SBOL2_IDENTIFIED_CLASS],
            fields: SEQUENCE_CONSTRAINT_PROPS,
        },
        SBOL2_VARIABLE_COMPONENT_CLASS => ClassSpec {
            parents: &[SBOL2_IDENTIFIED_CLASS],
            fields: VARIABLE_COMPONENT_PROPS,
        },
        SBOL2_INTERACTION_CLASS => ClassSpec {
            parents: &[SBOL2_MEASURED_CLASS],
            fields: INTERACTION_PROPS,
        },
        SBOL2_PARTICIPATION_CLASS => ClassSpec {
            parents: &[SBOL2_MEASURED_CLASS],
            fields: PARTICIPATION_PROPS,
        },
        SBOL2_RANGE_CLASS => ClassSpec {
            parents: &[SBOL2_LOCATION_CLASS],
            fields: RANGE_PROPS,
        },
        SBOL2_CUT_CLASS => ClassSpec {
            parents: &[SBOL2_LOCATION_CLASS],
            fields: CUT_PROPS,
        },
        SBOL2_GENERIC_LOCATION_CLASS => ClassSpec {
            parents: &[SBOL2_LOCATION_CLASS],
            fields: GENERIC_LOCATION_PROPS,
        },
        PROV_ACTIVITY => ClassSpec {
            parents: &[SBOL2_TOP_LEVEL_CLASS],
            fields: PROV_ACTIVITY_PROPS,
        },
        PROV_AGENT_CLASS => ClassSpec {
            parents: &[SBOL2_TOP_LEVEL_CLASS],
            fields: PROV_AGENT_PROPS,
        },
        PROV_PLAN => ClassSpec {
            parents: &[SBOL2_TOP_LEVEL_CLASS],
            fields: PROV_PLAN_PROPS,
        },
        PROV_ASSOCIATION => ClassSpec {
            parents: &[SBOL2_IDENTIFIED_CLASS],
            fields: PROV_ASSOCIATION_PROPS,
        },
        PROV_USAGE => ClassSpec {
            parents: &[SBOL2_IDENTIFIED_CLASS],
            fields: PROV_USAGE_PROPS,
        },
        OM_MEASURE => ClassSpec {
            parents: &[SBOL2_IDENTIFIED_CLASS],
            fields: OM_MEASURE_PROPS,
        },
        OM_UNIT => ClassSpec {
            parents: &[SBOL2_TOP_LEVEL_CLASS],
            fields: OM_UNIT_PROPS,
        },
        OM_SINGULAR_UNIT => ClassSpec {
            parents: &[OM_UNIT],
            fields: OM_SINGULAR_UNIT_PROPS,
        },
        OM_COMPOUND_UNIT => ClassSpec {
            parents: &[OM_UNIT],
            fields: OM_COMPOUND_UNIT_PROPS,
        },
        OM_UNIT_MULTIPLICATION => ClassSpec {
            parents: &[OM_COMPOUND_UNIT],
            fields: OM_UNIT_MULTIPLICATION_PROPS,
        },
        OM_UNIT_DIVISION => ClassSpec {
            parents: &[OM_COMPOUND_UNIT],
            fields: OM_UNIT_DIVISION_PROPS,
        },
        OM_UNIT_EXPONENTIATION => ClassSpec {
            parents: &[OM_COMPOUND_UNIT],
            fields: OM_UNIT_EXPONENTIATION_PROPS,
        },
        OM_PREFIXED_UNIT => ClassSpec {
            parents: &[OM_UNIT],
            fields: OM_PREFIXED_UNIT_PROPS,
        },
        OM_PREFIX => ClassSpec {
            parents: &[SBOL2_TOP_LEVEL_CLASS],
            fields: OM_PREFIX_PROPS,
        },
        OM_SI_PREFIX => ClassSpec {
            parents: &[OM_PREFIX],
            fields: OM_SI_PREFIX_PROPS,
        },
        OM_BINARY_PREFIX => ClassSpec {
            parents: &[OM_PREFIX],
            fields: OM_BINARY_PREFIX_PROPS,
        },
        _ => return None,
    })
}

/// The canonical XSD datatype IRI for a literal-valued [`ValueKind`], or
/// `None` for the reference kinds (`Uri`, `Url`).
pub(crate) fn xsd_datatype(kind: ValueKind) -> Option<&'static str> {
    match kind {
        ValueKind::String => Some(XSD_STRING),
        ValueKind::Integer => Some(XSD_INTEGER),
        ValueKind::Long => Some(XSD_LONG),
        ValueKind::Float => Some(XSD_DECIMAL),
        ValueKind::DateTime => Some(XSD_DATE_TIME),
        ValueKind::Uri | ValueKind::Url => None,
        _ => None,
    }
}

/// The spec-defined XSD datatype for a recognized literal-valued predicate.
///
/// SBOL 2 files vary in how they type literals. A Turtle file may write
/// `sbol:start 10` (an `xsd:integer`) while an RDF/XML file writes
/// `<sbol:start>10</sbol:start>` (an `xsd:string`) for the same property. The
/// serializer emits, and the reader normalizes to, the datatype the SBOL 2
/// data model assigns each property, so the typed surface and its RDF form
/// agree. Predicates outside the recognized field set (extension triples)
/// keep their literals verbatim.
pub(crate) fn literal_datatype(predicate: &str) -> Option<&'static str> {
    static CACHE: std::sync::OnceLock<BTreeMap<&'static str, &'static str>> =
        std::sync::OnceLock::new();
    CACHE
        .get_or_init(|| {
            let mut map = BTreeMap::new();
            for class_iri in ALL_CLASS_IRIS {
                if let Some(spec) = class_spec(class_iri) {
                    for field in spec.fields {
                        if let Some(datatype) = xsd_datatype(field.value_kind) {
                            map.insert(field.predicate, datatype);
                        }
                    }
                }
            }
            map
        })
        .get(predicate)
        .copied()
}

const ALL_CLASS_IRIS: &[&str] = &[
    SBOL2_IDENTIFIED_CLASS,
    SBOL2_TOP_LEVEL_CLASS,
    SBOL2_MEASURED_CLASS,
    SBOL2_COMPONENT_INSTANCE_CLASS,
    SBOL2_LOCATION_CLASS,
    SBOL2_SEQUENCE_CLASS,
    SBOL2_COMPONENT_DEFINITION_CLASS,
    SBOL2_MODULE_DEFINITION_CLASS,
    SBOL2_MODEL_CLASS,
    SBOL2_COLLECTION_CLASS,
    SBOL2_COMBINATORIAL_DERIVATION_CLASS,
    SBOL2_IMPLEMENTATION_CLASS,
    SBOL2_ATTACHMENT_CLASS,
    SBOL2_EXPERIMENTAL_DATA_CLASS,
    SBOL2_EXPERIMENT_CLASS,
    SBOL2_GENERIC_TOP_LEVEL_CLASS,
    SBOL2_COMPONENT_CLASS,
    SBOL2_FUNCTIONAL_COMPONENT_CLASS,
    SBOL2_MODULE_CLASS,
    SBOL2_MAPS_TO_CLASS,
    SBOL2_SEQUENCE_ANNOTATION_CLASS,
    SBOL2_SEQUENCE_CONSTRAINT_CLASS,
    SBOL2_VARIABLE_COMPONENT_CLASS,
    SBOL2_INTERACTION_CLASS,
    SBOL2_PARTICIPATION_CLASS,
    SBOL2_RANGE_CLASS,
    SBOL2_CUT_CLASS,
    SBOL2_GENERIC_LOCATION_CLASS,
    PROV_ACTIVITY,
    PROV_AGENT_CLASS,
    PROV_PLAN,
    PROV_ASSOCIATION,
    PROV_USAGE,
    OM_MEASURE,
    OM_UNIT,
    OM_SINGULAR_UNIT,
    OM_COMPOUND_UNIT,
    OM_UNIT_MULTIPLICATION,
    OM_UNIT_DIVISION,
    OM_UNIT_EXPONENTIATION,
    OM_PREFIXED_UNIT,
    OM_PREFIX,
    OM_SI_PREFIX,
    OM_BINARY_PREFIX,
];

/// Collects every field descriptor (inherited plus own) an object's RDF types
/// resolve to, keyed by predicate IRI.
pub fn property_specs_for(object: &Object) -> BTreeMap<&'static str, FieldDescriptor> {
    let mut specs = BTreeMap::new();
    for rdf_type in object.rdf_types() {
        collect_class_properties(rdf_type.as_str(), &mut specs);
    }
    specs
}

pub(crate) fn collect_class_properties(
    iri: &str,
    specs: &mut BTreeMap<&'static str, PropertySpec>,
) {
    let Some(class) = class_spec(iri) else {
        return;
    };
    for parent in class.parents {
        collect_class_properties(parent, specs);
    }
    for property in class.fields {
        specs.insert(property.predicate, *property);
    }
}

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

    #[test]
    fn every_class_has_a_spec() {
        for class in [
            Sbol2Class::Identified,
            Sbol2Class::TopLevel,
            Sbol2Class::Measured,
            Sbol2Class::ComponentInstance,
            Sbol2Class::Location,
            Sbol2Class::Sequence,
            Sbol2Class::ComponentDefinition,
            Sbol2Class::ModuleDefinition,
            Sbol2Class::Model,
            Sbol2Class::Collection,
            Sbol2Class::CombinatorialDerivation,
            Sbol2Class::Implementation,
            Sbol2Class::Attachment,
            Sbol2Class::ExperimentalData,
            Sbol2Class::Experiment,
            Sbol2Class::GenericTopLevel,
            Sbol2Class::Component,
            Sbol2Class::FunctionalComponent,
            Sbol2Class::Module,
            Sbol2Class::MapsTo,
            Sbol2Class::SequenceAnnotation,
            Sbol2Class::SequenceConstraint,
            Sbol2Class::VariableComponent,
            Sbol2Class::Interaction,
            Sbol2Class::Participation,
            Sbol2Class::Range,
            Sbol2Class::Cut,
            Sbol2Class::GenericLocation,
            Sbol2Class::ProvActivity,
            Sbol2Class::ProvAgent,
            Sbol2Class::ProvPlan,
            Sbol2Class::ProvAssociation,
            Sbol2Class::ProvUsage,
            Sbol2Class::OmMeasure,
            Sbol2Class::OmUnit,
            Sbol2Class::OmSingularUnit,
            Sbol2Class::OmCompoundUnit,
            Sbol2Class::OmUnitMultiplication,
            Sbol2Class::OmUnitDivision,
            Sbol2Class::OmUnitExponentiation,
            Sbol2Class::OmPrefixedUnit,
            Sbol2Class::OmPrefix,
            Sbol2Class::OmSiPrefix,
            Sbol2Class::OmBinaryPrefix,
        ] {
            assert!(
                class_spec(class.iri()).is_some(),
                "missing class_spec for {}",
                class.local_name()
            );
        }
    }

    #[test]
    fn component_inherits_component_instance_and_measured_fields() {
        let mut specs = BTreeMap::new();
        collect_class_properties(Sbol2Class::Component.iri(), &mut specs);
        assert!(specs.contains_key(SBOL2_DEFINITION));
        assert!(specs.contains_key(SBOL2_MEASURE));
        assert!(specs.contains_key(SBOL2_DISPLAY_ID));
        assert!(specs.contains_key(SBOL2_ROLE_INTEGRATION));
    }
}