Creating Custom Inference Rules using the SPIN Vocabulary and Virtuoso 8.0

Kingsley Uyi Idehen
OpenLink Virtuoso Weblog
5 min readMay 16, 2017
Source: https://media.licdn.com/mpr/mpr/AAEAAQAAAAAAAAXHAAAAJDY4ZWFjMzQzLTEyM2QtNGJkZS1hZWI4LTg0Zjg2ZmM4ZDcwNA.jpg

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.

Courtesy of Virtuoso’s support for both SQL and SPARQL as Data Manipulation Languages, the process of building and deploying Custom Inference Rules boils down to the following steps, conceptually:

  1. Create a SPARQL CONSTRUCT Query that represents the Rule that you have in mind
  2. Describe your SPARQL-based Rule using a collection of RDF statements leveraging terms from the SPIN vocabulary
  3. Load Rules into a Virtuoso Named Graph
  4. Use the following dynamic SQL Stored Procedure invocation pattern to generate an Inference Rule stored in the Virtuoso Macro Library:
    EXEC (‘SPARQL ‘|| SPARQL_SPIN_GRAPH_TO_DEFSPIN(‘{named-graph-holding-rules}’))
-- ExampleEXEC ('SPARQL ' || SPARQL_SPIN_GRAPH_TO_DEFSPIN('urn:spin:rule:geospatial:lib4'));

5. Test your rule .

Working Example — 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)
SPARQL
DEFINE get:soft "no-sponge"
LOAD <http://kingsley.idehen.net/DAV/home/kidehen/Public/SPIN%20Rules/geospatial-rules.ttl>
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.
SPARQL
DELETE
WHERE { GRAPH <urn:spin:rule:geospatial:lib4>
{ <http://kingsley.idehen.net/DAV/home/kidehen/Public/SPIN%20Rules/geospatial-rules.ttl> ?p ?o }
};

SPARQL
DELETE
WHERE { GRAPH <urn:spin:rule:geospatial:lib4>
{ <http://kingsley.idehen.net/about/id/http/kingsley.idehen.net/DAV/home/kidehen/Public/SPIN%20Rules/geospatial-rules.ttl> ?p ?o }
};
SPARQL
DELETE
WHERE { GRAPH <urn:spin:rule:geospatial:lib4> { <http://kingsley.idehen.net/DAV/home/kidehen/Public/SPIN%20Rules/geospatial-rules.ttl#this> ?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
?latitude
?longitude
FROM <http://bostonopendata-boston.opendata.arcgis.com/datasets/465e00f9632145a1ad645a27d27069b4_2.csv>
WHERE {
?s a sioc:Item .
?s <#hasLatitude> ?latitude .
?s <#hasLongitude> ?longitude .
?s <http://bostonopendata-boston.opendata.arcgis.com/datasets/465e00f9632145a1ad645a27d27069b4_2.csv#Station_Name> ?station
}

Results on a Live SPARQL Query Results Page with Custom Reasoning & Inference enabled.

Results on a Live SPARQL Query Results Page with Custom Reasoning & Inference disabled.

Alternatively, you can load the SQL script at <https://github.com/OpenLinkSoftware/SPASQL-Utility-Showcase-Queries/blob/master/british-royal-family-custom-rules.sql> into your own Virtuoso 8.0 instance, en route to the same result.

Conclusion

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.

SPARQL and SQL Samples

Related Posts

--

--

Kingsley Uyi Idehen
OpenLink Virtuoso Weblog

CEO, OpenLink Software —High-Performance Data Centric Technology Providers.