oxigraph-ruby
Ruby bindings for Oxigraph, a SPARQL database and RDF toolkit. The gem is named oxigraph and wraps the Oxigraph Rust library in a native extension.
This is an unofficial binding and is not affiliated with the Oxigraph project.
Install
Install from RubyGems with gem install oxigraph, or add it to your Gemfile:
gem "oxigraph", "~> 0.1.0"Precompiled gems are available for Ruby 3.4 and 4.0 on Linux (glibc and musl), macOS, and Windows, on both x86-64 and ARM64. Installing one does not need Rust. Building the source gem needs a Rust toolchain at version 1.87 or newer.
Stores and queries
A store holds an RDF dataset in memory:
require "oxigraph"
store = Oxigraph::Store.new
store.size # => 0
store.empty? # => trueLoading RDF
Store#load reads RDF from a string, from an IO, or from a file:
store.load("<http://example.com/a> <http://example.com/p> <http://example.com/b> .", format: :n_triples)
File.open("more.ttl") { |io| store.load(io, format: :turtle) }
store.load(path: "vocabulary.ttl")
store.load(path: "ontology.owl", format: :rdf_xml, base_iri: "http://example.com/")- The formats are
:turtle,:rdf_xml,:n_triples,:n_quads,:trig,:n3, and:json_ld. -
format:is required with a string or anIO. Withpath:it defaults to the format the file extension names, and an extension that names none raisesArgumentError. - Data goes to the default graph unless the format carries graph names.
- An
IOis read in chunks, so a large file is never held in memory as one string. Any object with anIO-styleread(length)works. - A load is all or nothing. Malformed input raises
Oxigraph::ParseErrorand leaves the store unchanged. - Blank nodes are renamed on the way in, so a label from the input is not the identifier in the store.
load returns an Oxigraph::LoadSummary:
summary = store.load("@prefix ex: <http://example.com/> . ex:a ex:p ex:b .", format: :turtle)
summary.prefixes # => {"ex" => "http://example.com/"}
summary.base_iri # => nilprefixes holds what the input declared, in no particular order (sort it if the order matters), and base_iri is the base IRI in effect at its end. Turtle, TriG, and N3 report their prefixes. RDF/XML reports none: its xmlns declarations do not appear.
Oxigraph::ParseError has line and column, counted from one:
begin
store.load("<http://example.com/a> oops", format: :turtle)
rescue Oxigraph::ParseError => error
error.message # => "Parser error at line 1 between columns 24 and 28: oops is not a valid predicate"
error.line # => 1
error.column # => 24
endBoth are nil when the parser reports no position, which is the case for RDF/XML.
Querying
Store#query runs a SPARQL query, given as a string or as a parsed Oxigraph::Query:
store = Oxigraph::Store.new
store.load(<<~TURTLE, format: :turtle)
@prefix ex: <http://example.com/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
ex:a rdfs:subClassOf ex:b ; rdfs:label "chat"@fr .
ex:b rdfs:subClassOf ex:c .
TURTLE
query = Oxigraph::Query.parse(
"SELECT ?x WHERE { ex:a rdfs:subClassOf+ ?x } ORDER BY ?x",
prefixes: {"ex" => "http://example.com/", "rdfs" => "http://www.w3.org/2000/01/rdf-schema#"}
)
solutions = store.query(query)
solutions.variables # => [:x]
solutions.each do |solution|
solution[:x] # => #<Oxigraph::NamedNode <http://example.com/b>>, then <http://example.com/c>
endWhat query returns depends on the form of the query:
| Form | Result |
|---|---|
SELECT |
Oxigraph::QuerySolutions, an Enumerable of Oxigraph::QuerySolution
|
CONSTRUCT, DESCRIBE
|
Oxigraph::QueryTriples, an Enumerable of Oxigraph::Triple
|
ASK |
true or false
|
- A solution is read with
solution[:x],solution["x"], orsolution[0]. An unbound variable reads asnil, and a variable the query does not select raisesIndexError.to_hgives every binding. - Results stream from the store one at a time and can be read once. Reading them again raises
Oxigraph::Error; run the query again instead. - Results come from a snapshot taken when the query ran, so a later
loaddoes not change them. - Row order is only defined by
ORDER BY. - Blank node identifiers differ from one process to the next, so neither they nor an order based on them is stable.
-
prefixes:andbase_iri:declare prefixes and a base IRI without adding lines to the query text, which keeps the positions in error messages true to it.
What a query cannot do
A query reads what the store holds and nothing else:
-
It never writes. An update passed to
queryorQuery.parseraisesOxigraph::QuerySyntaxError. -
It never opens a network connection. A query that contains
SERVICE,SERVICE SILENTincluded, raisesOxigraph::UnsupportedErrorbefore it runs.Query#service?reports the same thing without running it. -
FROMandFROM NAMEDchoose among the graphs in the store and never fetch one. A graph the store does not hold matches nothing, so the query returns no rows rather than failing.Query#dataset?tells a caller that a query has such a clause, so it can refuse the query.
Oxigraph::Query.parse("SELECT * FROM <http://example.com/g> WHERE { ?s ?p ?o }").dataset? # => true
Oxigraph::Query.parse("ASK { ?s ?p ?o }").form # => :askOxigraph::Update.parse recognizes a SPARQL update and cannot run it. It tells a valid update apart from text that is not SPARQL at all:
Oxigraph::Update.parse("DROP ALL").to_s # => "DROP ALL ;"
Oxigraph::Update.parse("nonsense") # raises Oxigraph::QuerySyntaxErrorA query or update that does not parse raises Oxigraph::QuerySyntaxError. Its message is the parser's own, and its wording may change. Its line and column say where the parser stopped, counted from one; both are nil for the few errors that have no position.
The parser reports a prefix that nothing declared as an ordinary syntax error. It stops after the name, sometimes on a later line, and its message does not always mention the prefix:
Oxigraph::Query.parse("SELECT * WHERE { ex:a ?p ?o }")
# raises Oxigraph::QuerySyntaxError: error at 1:26: expected OPTIONALA query that fails while it runs raises Oxigraph::QueryEvaluationError.
Terms
RDF terms are value objects:
node = Oxigraph::NamedNode.new("http://example.com/a")
node.value # => "http://example.com/a"
node.to_s # => "<http://example.com/a>"
Oxigraph::BlankNode.new("b0").to_s # => "_:b0"
Oxigraph::BlankNode.new.value # => a generated identifier
literal = Oxigraph::Literal.new("chat", language: "fr")
literal.value # => "chat"
literal.language # => "fr"
literal.datatype # => #<Oxigraph::NamedNode <http://www.w3.org/1999/02/22-rdf-syntax-ns#langString>>
integer = Oxigraph::NamedNode.new("http://www.w3.org/2001/XMLSchema#integer")
Oxigraph::Literal.new("1", datatype: integer).to_s # => "\"1\"^^<http://www.w3.org/2001/XMLSchema#integer>"
triple = Oxigraph::Triple.new(node, Oxigraph::NamedNode.new("http://example.com/p"), literal)
triple.subject # => #<Oxigraph::NamedNode <http://example.com/a>>
triple.to_s # => "<http://example.com/a> <http://example.com/p> \"chat\"@fr"- Terms are frozen. They compare by value with
==andeql?, so they work asHashkeys. -
to_sis the N-Triples form. - An invalid IRI, blank node identifier, or language tag raises
ArgumentError. - Literals compare by lexical form and datatype:
"1"and"01"asxsd:integerare different terms. - A language tag is lowercased.
Scope and limitations
- A store is read through SPARQL queries. There is no way yet to add or remove single triples, match a triple pattern directly, run an update, or write RDF back out.
- A query reads the default graph unless it uses
GRAPH,FROM, orFROM NAMED. A store has named graphs only when it loaded a format that names them. - JSON-LD that refers to a remote context fails to load, because the gem never fetches anything.
- Stores live in memory only. The gem is built without RocksDB, so there is no on-disk store.
- The gem is built without an HTTP client, so it never opens a network connection.
- Only Ruby 3.4 and 4.0 are tested.
Contributing
See CONTRIBUTING.md.
License
MIT. See LICENSE.