Creating Custom Inference Rules using the SPIN Vocabulary and Virtuoso 8.0


One of Virtuoso 8.0’s exciting new features is the addition of Custom Inference Rules capability to the existing Built-In Inference Rules functionality.

As you may remember, built-in inference rules are canned rules provided in every Virtuoso instance. These rules are based on specific Relationship Type and Entity Class definitions provided by the RDF Schema (RDFS) and Web Ontology Language (OWL) ontologies.

With the addition of Custom Inference, Virtuoso 8.0 enables users and developers to:

  • Build any kind of inference rule, based on specific needs at hand
  • Create alternatives to Virtuoso’s built-in inference rules
  • Use a combination of built-in and custom inference rules to build powerful rule sets that serve specific needs

Virtuoso 8.0 provides two ways to create Custom Inference Rules:

  • Use of SPIN Vocabulary Terms — this enables creation of custom inference rules using a de facto open standard
  • Use of Virtuoso’s Macro Language — this enables creation of custom inference rules using SQL

The remainder of this post will be focused on Custom Inference Rules using the SPIN Vocabulary. Custom Inference Rules using Virtuoso’s Macro Language will be covered in a future post.

Creating a Custom Inference Rule using SPIN Vocabulary Terms

The SPIN Vocabulary is put into use through just a few steps.

  1. Describe your Rule, and save it to a document location that is accessible through the http: or file: URI scheme
  2. Load that Rule Description document into Virtuoso using SPARQL LOAD or the TTLP() Stored Procedure
  3. Run the Macro Generator

Rule Description

I’ve used SPIN Vocabulary terms to describe a Custom Inference Rule, and saved it to an RDF document in a Folder in my Public Briefcase. Here are various document type renditions of that document:

In addition to the above, you can also view rule descriptions using tools such as our RDF Language Editor (OSDE) and/or Structured Data Sniffer (OSDS):

Here is a screenshot depicting the effect of using our Structured Data Sniffer atop our RDF Editor.

Data Extracted from RDF Editor’s HTML-based Structured Data Island

Here is a screenshot depicting effect of using our Structured Data Sniffer to view the JSON-LD rendition of the rule description document.

Structured Data Sniffer visualization of RDF Statements created using JSON-LD notation

Macro Generation

This boils down to execution of a few SQL commands.

-- Step #0 
-- Cleanup
SPARQL CLEAR GRAPH <urn:spin:rule:geospatial:lib4> ;
SPARQL DROP SPIN LIBRARY <urn:spin:rule:geospatial:lib4> ;
-- Step #1 
-- Load Rules Description from RDF Document (the data source)
DEFINE get:soft "no-sponge"
INTO <urn:spin:rule:geospatial:lib4> ;
-- Step #2 
-- Remove relations scoped to the source document,
-- leaving only RDF Language statements that describe
-- the Class (with which inference rules are being
-- associated) and the SPIN-based Rule instances.
WHERE { GRAPH <urn:spin:rule:geospatial:lib4>
{ <> ?p ?o }

WHERE { GRAPH <urn:spin:rule:geospatial:lib4>
{ <> ?p ?o }
WHERE { GRAPH <urn:spin:rule:geospatial:lib4> { <> ?p ?o } };
-- Step #3 
-- Generate Virtuoso Macros and add to Macro Library
-- using Macro Generator
EXEC ('SPARQL ' || SPARQL_SPIN_GRAPH_TO_DEFSPIN('urn:spin:rule:geospatial:lib4'));

Sample SPARQL Query, with Custom Inference Rules Context Enabled

DEFINE get:soft "soft"
DEFINE input:macro-lib <urn:spin:rule:geospatial:lib4>
SELECT ?s as ?webid 
xsd:string(?station) as ?stationLabel
?s a sioc:Item .
?s <#hasLatitude> ?latitude .
?s <#hasLongitude> ?longitude .
?s <> ?station

You can see the results on a Live SPARQL Query Results Page at:

Alternatively, you can load the SQL script at <> into your own Virtuoso 8.0 instance, en route to the same result.


AI has always been one of the fundamental goals targeted by the notion of a Semantic Web of Linked Data.

In that pursuit, Virtuoso 8.0 brings a powerful fusion of Relational Database Management (RDBMS) Technology and AI-focused features that deliver on the promises of Prolog and Datalog from the past.

In other words, Virtuoso 8.0 enables you to fully explore the power of relational database technology by leveraging the nature (semantics) of entity relationship types.

As an added bonus, you can bring this powerful functionality to bear on existing ODBC- (and ODBC-to-JDBC Bridge)-accessible SQL RDBMS-oriented data sources via Virtuoso’s Data Virtualization functionality (a/k/a its Virtual Database Layer).

Further, because of Virtuoso’s support for SPASQL (SPARQL-in-SQL), you can use your own favorite ODBC, JDBC OLE DB, or ADO.NET client tool to access all of this functionality — and more.