RDF::N3 reader/writer and reasoner

Notation-3 reader/writer for RDF.rb .

Gem Version Build Status Coverage Status Gitter chat

Description

RDF::N3 is an Notation-3 parser and reasoner for Ruby using the RDF.rb library suite.

Reader inspired from TimBL predictiveParser and Python librdf implementation.

Uses CG Specification

This version tracks the W3C N3 Community Group Specification which has incompatibilities with the Team Submission version. Notably:

  • The @keywords declaration is removed, and most form of @ keywords (e.g., @is, @has, @true) are no longer supported.
  • Terminals adhere closely to their counterparts in Turtle.
  • The modifier <- is introduced as a synonym for is ... of.
  • The SPARQL BASE and PREFIX declarations are supported.
  • Implicit universal variables are defined at the top-level, rather than in the parent formula of the one in which they are defined.
  • Support for explicit existential and universal variables (@forAll and @forSome) has been removed. Quick variables are the standard for universal quantification and blank nodes for existential, but scoping rules are different: Quickvars have top-level scope, and blank nodes formula scope.

This brings N3 closer to compatibility with Turtle.

Features

RDF::N3 parses Notation-3, Turtle and N-Triples into statements or quads. It also performs reasoning and serializes to N3.

Install with gem install rdf-n3

Implementation Report

Limitations

  • Support for Variables in Formulae. Existential variables are quantified to RDF::Node instances, Universals to RDF::Query::Variable, with the URI of the variable target used as the variable name.

Usage

Instantiate a reader from a local file:

RDF::N3::Reader.open("etc/foaf.n3") do |reader|
   reader.each_statement do |statement|
     puts statement.inspect
   end
end

Define @base and @prefix definitions, and use for serialization using :base_uri an :prefixes options

Write a graph to a file:

RDF::N3::Writer.open("etc/test.n3") do |writer|
   writer << graph
end

Reasoning

Experimental N3 reasoning is supported. Instantiate a reasoner from a dataset:

RDF::N3::Reasoner.new do |reasoner|
  RDF::N3::Reader.open("etc/foaf.n3") {|reader| reasoner << reader}

   reader.each_statement do |statement|
     puts statement.inspect
   end
end

Reasoning is performed by turning a repository containing formula and predicate operators into an executable set of operators (similar to the executable SPARQL Algebra). Reasoning adds statements to the base dataset, marked with :inferred (e.g. statement.inferred?). Predicate operators are defined from the following vocabularies.

When dispatching built-in operators, precedence is given to operators whos operands are fully evaluated, followed by those having only variable output operands, followed by those having the fewest operands. Operators are evaluated until either no solutions are derived, or all operators have been completed.

Reasoning is discussed in the Design Issues document.

RDF List vocabulary http://www.w3.org/2000/10/swap/list#

RDF Log vocabulary http://www.w3.org/2000/10/swap/log#

RDF Math vocabulary http://www.w3.org/2000/10/swap/math#

RDF String vocabulary http://www.w3.org/2000/10/swap/str#

RDF Time vocabulary http://www.w3.org/2000/10/swap/time#

Parser features

Chaining with iriPropertyList

Adds a proposed syntactic extension for subject embedding similar to a blankNodePropertyList. An iriPropertyList begins with [ id _id_, instead of a simple [. This sets id as the subject to be used for the following propertyList. This provides a mechanisms similar to JSON-LD Embedding.

@prefix dc: <http://purl.org/dc/terms/>.
@prefix : <http://example.org/nd#>.

:SummerReadingList a :OrderedListOfBooks ;
  :toRead (
    [id :mobyDick dc:title "Moby Dick"; :setting :WhaleIntestines ]
    [
      id :jaws
      dc:title "Jaws";
      :setting :Beach
    ]
  ).

Note that the id used in the iriPropertyList is not delimited by a ;

Formulae / Quoted Graphs

N3 Formulae are introduced with the { statement-list } syntax. A given formula is assigned an RDF::Node instance, which is also used as the graph_name for RDF::Statement instances provided to RDF::N3::Reader#each_statement. For example, the following N3 generates the associated statements:

@prefix x: <http://example.org/x-ns/#> .
@prefix log: <http://www.w3.org/2000/10/swap/log#> .
@prefix dc: <http://purl.org/dc/elements/1.1/#> .

{ [ x:firstname  "Ora" ] dc:wrote [ dc:title  "Moby Dick" ] } a log:falsehood .

when turned into an RDF Repository results in the following quads

_:form <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/10/swap/log#falsehood> .
_:moby <http://purl.org/dc/elements/1.1/#title> "Moby Dick" _:form .
_:ora <http://purl.org/dc/elements/1.1/#wrote> _:moby _:form .
_:ora <http://example.org/x-ns/#firstname> "Ora" _:form .

Reasoning uses a Notation3 Algebra, similar to SPARQL S-Expressions. This implementation considers formulae to be patterns, which may be asserted on statements made in the default graph, possibly loaded from a separate file. The logical relationships are reduced to algebraic operators.

Variables

The latest version of N3 supports only quickVars (e.g., ?x). THe former explicit @forAll and @forSome of been removed. Existential variables are replaced with an allocated RDF::Node instance.

Note that the behavior of both existential and universal variables is not entirely in keeping with the Team Submission, and neither work quite like SPARQL variables. When used in the antecedent part of an implication, universal variables should behave much like SPARQL variables. This area is subject to a fair amount of change.

  • Variables, themselves, cannot be part of a solution, which limits the ability to generate updated rules for reasoning.
  • Both Existentials and Universals are treated as simple variables, and there is really no preference given based on the order in which they are introduced.

Query

Formulae are typically used to query the knowledge-base, which is set from the base-formula/default graph. A formula is composed of both constant statements, and variable statements. When running as a query, such as for the antecedent formula in log:implies, statements including either explicit variables or blank nodes are treated as query patterns and are used to query the knowledge base to create a solution set, which is used either prove the formula correct, or create bindings passed to the consequent formula.

Blank nodes associated with rdf:List statements used as part of a built-in are made non-distinguished existential variables, and patters containing these variables become optional. If they are not bound as part of the query, the implicitly are bound as the original blank nodes defined within the formula, which allows for both constant list arguments, list arguments that contain variables, or arguments which are variables expanding to lists.

Dependencies

Documentation

Full documentation available on RubyDoc.info

Principle Classes

Additional vocabularies

Change Log

See Release Notes on GitHub

Resources

Author

Contributing

This repository uses Git Flow to mange development and release activity. All submissions must be on a feature branch based on the develop branch to ease staging and integration.

  • Do your best to adhere to the existing coding conventions and idioms.
  • Don't use hard tabs, and don't leave trailing whitespace on any line.
  • Do document every method you add using YARD annotations. Read the tutorial or just look at the existing code for examples.
  • Don't touch the .gemspec, VERSION or AUTHORS files. If you need to change them, do so on your private branch only.
  • Do feel free to add yourself to the CREDITS file and the corresponding list in the the README. Alphabetical order applies.
  • Do note that in order for us to merge any non-trivial changes (as a rule of thumb, additions larger than about 15 lines of code), we need an explicit public domain dedication on record from you, which you will be asked to agree to on the first commit to a repo within the organization. Note that the agreement applies to all repos in the Ruby RDF organization.

License

This is free and unencumbered public domain software. For more information, see https://unlicense.org/ or the accompanying UNLICENSE file.

Feedback