Skip to main content

Crate rdfx

Crate rdfx 

Source
Expand description

The Resource Description Framework (RDF) is a very simple graph data model defined by the World Wide Web Consortium (W3C) to represent arbitrary pieces of information, primarily intended for the web. Nodes of the graph are called resources, and resources are connected together using relations, which are resources themselves.

This is a utility library providing common types, data-structures, traits, constants and macro definitions to deal with RDF data:

  • IRIs (through the iri-rs crate), blank node identifiers and literals to represent resources in their lexical form as terms;
  • Triples and quads;
  • Interpretations projecting resources from the lexical domain to the value domain;
  • Graphs and datasets representing collections of interpreted triples/quads.

§Getting started

Terms, triples and quads are built with the triple! and quad! macros, which accept the same lexical syntax as N-Triples/N-Quads:

use rdfx::{LocalTerm, triple};

// RDF 1.1: a language-tagged literal in object position.
let t = triple!(<"http://example.org/alice"> <"http://xmlns.com/foaf/0.1/name"> "Alice" @ "en");

// RDF 1.2: a triple term as the object.
let annotated = triple!(
    <"http://example.org/bob"> <"http://example.org/says">
    <<( <"http://example.org/s"> <"http://example.org/p"> <"http://example.org/o"> )>>
);
assert!(matches!(annotated.into_object(), LocalTerm::Triple(_)));

Storage is uniform over a single resource type: BTreeDataset<R>, HashDataset<R> and their indexed variants hold the same R: Resource in every position. LocalTerm is deliberately not a Resource — a literal cannot be a subject — so store interned handles from a vocabulary, or wrap the lexical term in a newtype opted in with impl_resource!:

use rdfx::{Quad, dataset::{BTreeDataset, TraversableDataset}};

let mut dataset: BTreeDataset<usize> = BTreeDataset::new();
dataset.insert(Quad(1, 0, 2, None));
dataset.insert(Quad(2, 0, 3, Some(9)));

assert_eq!(dataset.quads_count(), 2);

Blank node isomorphism lives in dataset::isomorphism: the plain entry points answer yes or no, while find_bijection_with returns the blank node mapping. The _with variants take an Interpretation, which is what makes blank nodes and triple-term interiors visible to the matcher; without one resources are opaque and equivalence degrades to set equality.

use rdfx::{Quad, dataset::{BTreeDataset, isomorphism::dataset_equivalent}};

let mut a: BTreeDataset<usize> = BTreeDataset::new();
a.insert(Quad(1, 0, 2, None));

let mut b: BTreeDataset<usize> = BTreeDataset::new();
b.insert(Quad(1, 0, 2, None));

assert!(dataset_equivalent(&a, &b));

§RDF 1.2

This crate targets RDF 1.2 Concepts alongside RDF 1.1 — RDF 1.1 graphs are a syntactic subset (they never construct the new variants):

use rdfx::{Direction, LiteralType, LocalTerm, Term, triple};

let t = triple!(<"http://example.org/doc"> <"http://example.org/title"> "hello" @ "ar" / "rtl");
let LocalTerm::Named(Term::Literal(literal)) = t.into_object() else { unreachable!() };

assert!(matches!(literal.into_type(), LiteralType::DirLangString { direction: Direction::Rtl, .. }));

§Feature flags

All features are off by default.

  • serdeSerialize / Deserialize for terms, literals, triples and quads.
  • meta — the meta module: locspan-tagged values and the Strip trait.
  • contextualDisplay through a vocabulary via the contextual crate.
  • uuid-generator, uuid-generator-v3, uuid-generator-v4, uuid-generator-v5 — UUID-based blank node generators.

Re-exports§

pub use dataset::Dataset;
pub use interpretation::Interpretation;
pub use interpretation::InterpretationMut;

Modules§

dataset
Dataset traits and implementations.
generator
interpretation
Resource interpretations.
iri
Compatibility namespace.
meta
Metadata-tagged terms and triples/quads, gated on the meta feature.
pattern
star
RDF 1.2 reification helpers — round-trippable mapping between triple-term-bearing graphs/datasets and their RDF 1.1-compatible reified form using the rdf:reifies annotation vocabulary.
utils
vocabulary
Lexical domain abstractions.

Macros§

generalized_quad
Creates a generalized RDF quad (GeneralizedQuad<LocalTerm>).
generalized_quad_pattern
Creates a generalized RDF quad pattern.
generalized_quads
Creates an array of generalized quads.
generalized_triple
Creates a generalized RDF triple (GeneralizedTriple<LocalTerm>).
generalized_triples
Creates an array of generalized triples.
impl_resource
Implements Resource and the four position traits for a type, granting it eligibility in every position of a triple/quad.
iri
quad
Creates a strict RDF 1.1 / 1.2 quad (Quad<Id, IriBuf, LocalTerm, Id>).
quad_pattern
Creates a strict quad pattern.
quads
Creates an array of strict RDF 1.1 / 1.2 quads.
triple
Creates a strict RDF 1.1 / 1.2 triple (Triple<Id, IriBuf, LocalTerm>).
triple_pattern
Creates a strict triple pattern Triple<ResourceOrVar<Id, _>, ResourceOrVar<IriBuf, _>, ResourceOrVar<LocalTerm, _>>.
triples
Creates an array of strict RDF 1.1 / 1.2 triples.

Structs§

BlankId
Blank node identifier.
BlankIdBuf
Owned blank node identifier.
CowLiteral
RDF Literal reference.
Datatype
Validated typed-literal datatype IRI.
DatatypeRef
Borrowed counterpart of Datatype.
GeneralizedQuad
Generalized RDF quad — no position bounds on its components.
GeneralizedTriple
Generalized RDF triple — no position bounds on its components.
InvalidBlankId
Invalid blank node identifier.
InvalidDirection
Returned by Direction::parse for an unknown direction string.
Iri
Literal
RDF Literal.
LiteralRef
RDF Literal reference.
Quad
RDF quad.
RdfDisplayed
Value ready to be formatted as an RDF syntax element.
Triple
RDF triple.

Enums§

CowId
CowLiteralType
Owned or referenced RDF literal type.
CowLocalTerm
CowTerm
Direction
Base direction of a directional language-tagged string.
GeneralizationError
Reasons a GeneralizedTriple or GeneralizedQuad cannot be converted to a strict Triple / Quad.
GeneralizedLocalTerm
Fully-generalized local term — like LocalTerm but its triple-term bodies may themselves carry literal subjects, blank predicates, and nested triple terms in any position.
Id
IdRef
LiteralType
RDF literal type.
LiteralTypeRef
RDF literal type reference.
LocalTerm
Lexical representation of an RDF resource.
LocalTermRef
Lexical RDF term reference.
QuadExportFailed
Error returned when calling try_extract_from_vocabulary on a Quad.
ReservedDatatypeError
Error returned when attempting to construct a Datatype from one of the RDF-reserved language-string datatype IRIs.
Term
Lexical representation of an RDF resource.
TermRef
Lexical RDF term reference.

Constants§

RDFS_CLASS
The rdfs:Class IRI, http://www.w3.org/2000/01/rdf-schema#Class.
RDFS_COMMENT
The rdfs:comment IRI, http://www.w3.org/2000/01/rdf-schema#comment.
RDFS_CONTAINER
The rdfs:Container IRI, http://www.w3.org/2000/01/rdf-schema#Container.
RDFS_CONTAINER_MEMBERSHIP_PROPERTY
The rdfs:ContainerMembershipProperty IRI, http://www.w3.org/2000/01/rdf-schema#ContainerMembershipProperty.
RDFS_DATATYPE
The rdfs:Datatype IRI, http://www.w3.org/2000/01/rdf-schema#Datatype.
RDFS_DOMAIN
The rdfs:domain IRI, http://www.w3.org/2000/01/rdf-schema#domain.
RDFS_IS_DEFINED_BY
The rdfs:isDefinedBy IRI, http://www.w3.org/2000/01/rdf-schema#isDefinedBy.
RDFS_LABEL
The rdfs:label IRI, http://www.w3.org/2000/01/rdf-schema#label.
RDFS_LITERAL
The rdfs:Literal IRI, http://www.w3.org/2000/01/rdf-schema#Literal.
RDFS_MEMBER
The rdfs:member IRI, http://www.w3.org/2000/01/rdf-schema#member.
RDFS_RANGE
The rdfs:range IRI, http://www.w3.org/2000/01/rdf-schema#range.
RDFS_RESOURCE
The rdfs:Resource IRI, http://www.w3.org/2000/01/rdf-schema#Resource.
RDFS_SEE_ALSO
The rdfs:seeAlso IRI, http://www.w3.org/2000/01/rdf-schema#seeAlso.
RDFS_SUB_CLASS_OF
The rdfs:subClassOf IRI, http://www.w3.org/2000/01/rdf-schema#subClassOf.
RDFS_SUB_PROPERTY_OF
The rdfs:subPropertyOf IRI, http://www.w3.org/2000/01/rdf-schema#subPropertyOf.
RDF_ALT
The rdf:Alt IRI, http://www.w3.org/1999/02/22-rdf-syntax-ns#Alt.
RDF_BAG
The rdf:Bag IRI, http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag.
RDF_DIR_LANG_STRING
rdf:dirLangString — RDF 1.2 directional language-tagged literal datatype.
RDF_FIRST
The rdf:first IRI, http://www.w3.org/1999/02/22-rdf-syntax-ns#first.
RDF_HTML
The rdf:HTML IRI, http://www.w3.org/1999/02/22-rdf-syntax-ns#HTML.
RDF_JSON
The rdf:JSON IRI, http://www.w3.org/1999/02/22-rdf-syntax-ns#JSON.
RDF_LANG_STRING
The rdf:langString IRI, http://www.w3.org/1999/02/22-rdf-syntax-ns#langString.
RDF_LIST
The rdf:List IRI, http://www.w3.org/1999/02/22-rdf-syntax-ns#List.
RDF_NIL
The rdf:nil IRI, http://www.w3.org/1999/02/22-rdf-syntax-ns#nil.
RDF_OBJECT
The rdf:object IRI, http://www.w3.org/1999/02/22-rdf-syntax-ns#object.
RDF_PREDICATE
The rdf:predicate IRI, http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate.
RDF_PROPERTY
The rdf:Property IRI, http://www.w3.org/1999/02/22-rdf-syntax-ns#Property.
RDF_REIFIES
rdf:reifies — RDF 1.2 annotation predicate linking a reifier to its reified triple term.
RDF_REST
The rdf:rest IRI, http://www.w3.org/1999/02/22-rdf-syntax-ns#rest.
RDF_SEQ
The rdf:Seq IRI, http://www.w3.org/1999/02/22-rdf-syntax-ns#Seq.
RDF_STATEMENT
The rdf:Statement IRI, http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement.
RDF_SUBJECT
The rdf:subject IRI, http://www.w3.org/1999/02/22-rdf-syntax-ns#subject.
RDF_TRIPLE_TERM
rdf:TripleTerm — RDF 1.2 class of reified triple terms.
RDF_TYPE
The rdf:type IRI, http://www.w3.org/1999/02/22-rdf-syntax-ns#type.
RDF_VALUE
The rdf:value IRI, http://www.w3.org/1999/02/22-rdf-syntax-ns#value.
RDF_XML_LITERAL
The rdf:XMLLiteral IRI, http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral.
XSD_STRING

Traits§

FromBlankId
Types that can be constructed from a blank node identifier.
FromIri
Types that can be constructed from an iri.
FromLiteral
Types that can be constructed from a literal.
Generator
Subject identifier generator.
IsGraph
Types that may appear in the graph-name position of an RDF quad.
IsObject
Types that may appear in the object position of an RDF triple/quad.
IsPredicate
Types that may appear in the predicate position of an RDF triple/quad.
IsSubject
Types that may appear in the subject position of an RDF triple/quad.
LocalGenerator
MaybeBlankId
Types that may represent a blank node identifier.
MaybeIri
Types that may represent an iri.
MaybeLiteral
Types that may represent a literal value.
RdfDisplay
Display method for RDF syntax elements.
RdfDisplayWithContext
Resource
Marker trait for opaque resource handles (e.g. interpretation indices).
TryAsBlankId
Types that may have a blank node identifier representation that can be borrowed.
TryAsIri
Types that may have an iri representation that can be borrowed.
TryAsLiteral
Types that may have a literal representation that can be borrowed.
TryExportQuad
Type that can turn a Quad<S, P, O, G> into a Quad.
TryIntoBlankId
Types that can be turned into a blank node identifier.
TryIntoIri
Types that can be turned into an iri.
TryIntoLiteral
Types that can be turned into a literal.

Type Aliases§

CowDatatype
Owned-or-borrowed Datatype.
FullyGeneralizedQuad
Fully-generalized quad — body components are GeneralizedLocalTerm.
FullyGeneralizedTriple
Fully-generalized triple — body components are GeneralizedLocalTerm.
IriBuf
LexicalQuad
Lexical RDF quad.
LexicalQuadRef
Lexical RDF quad reference.
LexicalTriple
Lexical RDF triple.
LexicalTripleRef
Lexical RDF triple reference.
TripleTerm
RDF 1.2 triple term — a triple appearing as a term inside object position.
TripleTermRef
Reference counterpart of TripleTerm.