[−][src]Module rdftk_core::resource
Implementation of the Resource
pattern as a kind of statement builder. As a builder type the
interface is only additive, no update or remove methods exist.
Each Resource
comprises a common subject value and a set of predicates each of which may have
multiple associated values. It should therefore be noted that calling a predicate method will add a
new value, and not replace any existing value. New resource instances can be used directly as
the object of a predicate and so nested or related resources can be written inline. This is
particulary useful where the object is a blank node.
Additionally a Predicate
builder is provided where it is more readable to add only the values
individually rather than repeating the same predicate IRI as the Resource
interface requires. The
interface for Predicate
is intended to be a precise sub-set of the Resource
methods so that the
same look and feel is maintained.
Example
The following short example shows the use of Resource
, and a nested resource, to build a small
RDF model. Once a resource is created it can be converted into a vector of Statement
s for either
writing out or constructing a Graph
instance.
use rdftk_core::{Literal, Resource, Statement}; use rdftk_iri::IRI; use rdftk_names::{dc::elements as dc, foaf, rdf}; use std::str::FromStr; fn contact(name: &str) -> IRI { IRI::from_str(&format!( "http://www.w3.org/2000/10/swap/pim/contact#{}", name )) .unwrap() } let resource = Resource::named(IRI::from_str("http://en.wikipedia.org/wiki/Tony_Benn").unwrap().into()) .value_of(dc::publisher().clone(), Literal::new("Wikipedia")) .value_of(dc::title().clone(), Literal::new("Tony Benn")) .resource( dc::description().clone(), Resource::blank() .resource_named(rdf::a_type().clone(), foaf::person().clone()) .value_of(foaf::name().clone(), Literal::new("Tony Benn")) .to_owned(), ) .to_owned(); let sts: Vec<Statement> = resource.into(); assert_eq!(sts.len(), 5); for st in sts { println!("{}", st); }
The output from the example above will look like this, although blank node identifiers will likely be different.
<http://en.wikipedia.org/wiki/Tony_Benn> <http://purl.org/dc/elements/1.1/title> "Tony Benn" .
_:B1 <http://xmlns.com/foaf/0.1/name> "Tony Benn" .
_:B1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person> .
<http://en.wikipedia.org/wiki/Tony_Benn> <http://purl.org/dc/elements/1.1/description> _:B1 .
<http://en.wikipedia.org/wiki/Tony_Benn> <http://purl.org/dc/elements/1.1/publisher> "Wikipedia" .
Structs
Predicate | A predicate builder type, optionally used for multi-valued predicates. |
Resource | The main resource builder type. |