dartfx-rdf provides bidirectional, type-safe mapping between Pydantic v2 data models and RDF graphs backed by rdflib.
flowchart LR
Pydantic[Pydantic Models] <-->|RdfBaseModel & RdfProperty| Graph[rdflib.Graph]
Graph <--> Serializations[Turtle / JSON-LD / RDF-XML / NTriples]
Base class inheriting from pydantic.BaseModel.
rdf_type: ClassVar specifying the RDF type(s) for instances. Accepts a scalar (str | URIRef), sequence (list | tuple), orsetof URIs (e.g.[SCHEMA.PropertyValue, CDI.InstanceVariable]).rdf_preserve_unmodeled_types: ClassVar boolean (defaultTrue). Preserves incoming unmodeledrdf:typestatements from existing graphs on round-trip. Set toFalsefor strict canonical exports.rdf_namespace: Namespace prepended to relative identifiers.rdf_id_field: Model field used as subject identifier (defaults to"id").rdf_prefixes: Namespace prefix bindings for clean serialization._extra_rdf_types: Private attribute (PrivateAttr(default_factory=set)) holding extra graph types discovered during deserialization.rdf_uri_generator: Strategy for minting RDF subject URIs. Defaults toDefaultUriGenerator().
Metadata descriptor used inside Annotated[T, RdfProperty(...)].
predicate: Target predicate URI.datatype: Optional XSD datatype override.language: Optional language tag for literals.serializer: Optional custom function(PythonValue) -> RDFNode.parser: Optional custom function(RDFNode) -> PythonValue.
- Serialization:
_serialise_into_graphemits triples for all URIs in_extract_rdf_types(self). Ifrdf_preserve_unmodeled_typesisTrue, it also emits all URIs in_extra_rdf_types. - Subject Inference:
_infer_subjectcomputes set intersection across all types declared inrdf_typeto locate candidate resources in multi-resource graphs. - Polymorphic Dispatch: When a nested field accepts a union of models (
Union[ModelA, ModelB]),from_rdf_graphcalls_resolve_model_for_node, which scores each candidate model against the node'srdf:typetriples in the graph to instantiate the most specific matching class.
LangString: Frozen model representing a literal value with an optional language tag.LangStringList: Subclass oflist[LangString]with language inspection helpers (has_language,get_by_language,languages,count_by_lang) and deduplicating mutation methods.LocalizedStr: Annotated type alias with a BeforeValidator coercing strings, dicts, and LangString lists into canonicalLangStringList.
DefaultUriGenerator: Standard resolution (id field → namespace/base_uri → UUID fallback).TemplateUriGenerator: URI formatting using a string template with model fields.HashUriGenerator: Deterministic cryptographic hash of model fields.CompositeUriGenerator: Priority-ordered chain of generators.PrefixedUriGenerator: Fixed prefix concatenated with a field.