RDF.rb 是一个纯 Ruby 库,用于处理资源描述框架 (RDF) 数据。
This is a pure-Ruby library for working with [Resource Description Framework (RDF)][RDF] data.
RDF module, and does
not modify any of Ruby's core classes or standard library.{} and the optional keyword arguments may need to be specified using **{} if there are no keyword arguments.RDF.rb uses Net::HTTP for retrieving HTTP and HTTPS resources. If the
[RestClient][] gem is included, that will be used instead to retrieve remote
resources. Clients may also consider using [RestClient Components][] to enable
client-side caching of HTTP results using [Rack::Cache][] or other Rack
middleware.
See {RDF::Util::File} for configuring other mechanisms for retrieving resources.
RDF.rb uses a weak-reference cache for storing internalized versions of URIs and Nodes. This is particularly useful for Nodes as two nodes are equivalent only if they're the same node.
By default, each cache can grow to an unlimited size, but this can be configured using {RDF.config}, for general limits, along with URI- or Node-specific limits.
For example, to limit the size of the URI intern cache only:
RDF.config.uri_cache_size = 10_000The default for creating new caches without a specific initialization size can be set using:
RDF.config.cache_size = 100_000This version of RDF.rb is fully compatible with [RDF 1.1][], but it creates some marginal incompatibilities with [RDF 1.0][], as implemented in versions prior to the 1.1 release of RDF.rb:
Addressable gem. As URIs typically don't need to be parsed, this provides a substantial performance improvement when enumerating or querying graphs and repositories.rdf:List type. However, it will now recognize any subjects that are {RDF::Node} instances as being list elements, as long as they have both rdf:first and rdf:rest predicates.graph_name to a graph may only be done when the underlying storage model supports graph_names (the default {RDF::Repository} does). The notion of graph_name in RDF.rb is treated equivalently to Named Graphs within an RDF Dataset, and graphs on their own are not named.:validate option to false.graph_name of a graph within a Dataset or Repository may be either an {RDF::IRI} or {RDF::Node}. Implementations of repositories may restrict this to being only {RDF::IRI}.xsd:string datatype and language tagged literals are given the rdf:langString datatype. Creating a plain literal, without a datatype or language, will automatically provide the xsd:string datatype; similar for language tagged literals. Note that most serialization formats will remove this datatype. Code which depends on a literal having the xsd:string datatype being different from a plain literal (formally, without a datatype) may break. However note that the #has\_datatype? will continue to return false for plain or language-tagged literals.enumerable.query(query) to behave like query.execute(enumerable) and either return an enumerable or yield each solution.base_uri based on W3C recommendations:base_uri is set to the original URI if a status 303 is provided, otherwise any other redirect will set base_uri to the redirected location.base_uri is set to the content of the Location header if status is success.charset if provided, defaulting to UTF-8. Other access methods include last_modified and content_type,Notably, {RDF::Queryable#query} and {RDF::Query#execute} are now completely symmetric; this allows an implementation of {RDF::Queryable} to optimize queries using implementation-specific logic, allowing for substantial performance improvements when executing BGP queries.
direction property for directional language-tagged strings.text/plain (as an alias for application/n-triples) and text/x-nquads (as an alias for application/n-quads)When installed, RDF.rb includes a rdf shell script which acts as a wrapper to perform a number of different
operations on RDF files using available readers and writers.
count: Parse and RDF input and count the number of statements.predicates: Returns unique objects from parsed input.objects: Returns unique objects from parsed input.serialize: Parse an RDF input and re-serializing to [N-Triples][] or another available format using --output-format option.subjects: Returns unique subjects from parsed input.The serialize command can also be used to serialize as a vocabulary.
Different RDF gems will augment the rdf script with more capabilities, which may require specifying the appropriate --input-format option to revel.
require 'rdf'
include RDFrequire 'rdf/ntriples'
graph = RDF::Graph.new ]")Note that no prefixes are loaded automatically, however they can be provided as arguments:
graph.from_ttl("[ a rdf:Resource]", prefixes: {rdf: RDF.to_uri})require 'rdf/ntriples'
graph = RDF::Graph.load("https://ruby-rdf.github.io/rdf/etc/doap.nt")
query = RDF::Query.new({
person: {
RDF.type => FOAF.Person,
FOAF.name => :name,
FOAF.mbox => :email,
}
}, **{})
query.execute(graph) do |solution|
puts "name=#{solution.name} email=#{solution.email}"
endThe same query may also be run from the graph:
graph.query(query) do |solution|
puts "name=#{solution.name} email=#{solution.email}"
endIn general, querying from using the queryable instance allows a specific implementation of queryable to perform query optimizations specific to the datastore on which it is based.
A separate [SPARQL][SPARQL doc] gem builds on basic BGP support to provide full support for [SPARQL 1.1][] queries.
DC.title #=> RDF::URI("http://purl.org/dc/terms/title")
FOAF.knows #=> RDF::URI("http://xmlns.com/foaf/0.1/knows")
RDF.type #=> RDF::URI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")
RDFS.seeAlso #=> RDF::URI("http://www.w3.org/2000/01/rdf-schema#seeAlso")
RSS.title #=> RDF::URI("http://purl.org/rss/1.0/title")
OWL.sameAs #=> RDF::URI("http://www.w3.org/2002/07/owl#sameAs")
XSD.dateTime #=> RDF::URI("http://www.w3.org/2001/XMLSchema#dateTime")foaf = RDF::Vocabulary.new("http://xmlns.com/foaf/0.1/")
foaf.knows #=> RDF::URI("http://xmlns.com/foaf/0.1/knows")
foaf[:name] #=> RDF::URI("http://xmlns.com/foaf/0.1/name")
foaf['mbox'] #=> RDF::URI("http://xmlns.com/foaf/0.1/mbox")[RDF.rb][] includes provisional support for [RDF 1.2][] with an N-Triples/N-Quads syntax for tri
暂无开放 Issues,或尚未同步最近议题。