Example of using RDFS Inference

  • Load a simple RDF Graph
  • Query the RDF Graph to show type information
  • Expand the RDF Graph using an RDFS/OWL Ontology
  • Run the same query to see inferred types

In [1]:
import rdflib
import RDFClosure

Load a simple RDF Graph

  • A NIDM Summary-level dataset descriptor of the OpenfMRI database

In [2]:
g = rdflib.Graph()

g.parse('data/nidm-desc-sum-database-level.ttl', format='turtle')
print g.serialize(format='turtle')


@prefix : <http://openfmri.s3.amazonaws.com/nidm.ttl#> .
@prefix dcat: <http://www.w3.org/ns/dcat#> .
@prefix dct: <http://purl.org/dc/terms/> .
@prefix dctypes: <http://purl.org/dc/dcmitype/> .
@prefix nidm: <http://www.incf.org/ns/nidash/nidm#> .
@prefix pav: <http://purl.org/pav/> .
@prefix prov: <http://www.w3.org/ns/prov#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix schemaorg: <http://schema.org/> .
@prefix void: <http://www.w3.org/TR/void/> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

:openfmri a dctypes:Dataset,
        nidm:Database,
        prov:Collection ;
    dct:description """OpenfMRI.org is a project dedicated to the free and
                       open sharing of functional magnetic resonance imaging
                       (fMRI) datasets, including raw data."""@en ;
    dct:hasPart :ds000001,
        :ds000005,
        :ds000113 ;
    dct:license <http://www.opendatacommons.org/licenses/pddl/1.0/> ;
    dct:publisher <https://openfmri.org> ;
    dct:title "OpenfMRI"@en ;
    schemaorg:logo <https://openfmri.org/sites/all/themes/openfmri/logo.png> ;
    dcat:accessURL <https://openfmri.org> ;
    dcat:theme <http://dbpedia.org/page/Neuroimaging> .


Query the RDF Graph to show type information

  • Query the OpenfMRI Resource and show its types

In [3]:
query = """CONSTRUCT { :openfmri a ?type . }
           WHERE { :openfmri a ?type . }
        """
result = g.query(query)
print result.serialize(format='turtle')


@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

<http://openfmri.s3.amazonaws.com/nidm.ttl#openfmri> a <http://purl.org/dc/dcmitype/Dataset>,
        <http://www.incf.org/ns/nidash/nidm#Database>,
        <http://www.w3.org/ns/prov#Collection> .


Expand the RDF Graph using an RDFS/OWL Ontology

  • load the prov ontology
  • create an rdfs deductive reasoner
  • expand the openfmri graph using rdfs semantics

In [4]:
g.parse(location='http://www.w3.org/ns/prov.ttl', format='turtle')
rdfs = RDFClosure.DeductiveClosure(RDFClosure.RDFS_Semantics)
rdfs.expand(g)

Run the same query to see inferred types


In [5]:
result = g.query(query)
print result.serialize(format='turtle')


@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

<http://openfmri.s3.amazonaws.com/nidm.ttl#openfmri> a <http://purl.org/dc/dcmitype/Dataset>,
        <http://www.incf.org/ns/nidash/nidm#Database>,
        rdfs:Resource,
        <http://www.w3.org/ns/prov#Collection>,
        <http://www.w3.org/ns/prov#Entity> .


Summary

We loaded an rdf graph describing the OpenfMRI database and then expanded the set of triples in the graph using RDFS reasoning over the PROV Ontology.

The :openfmri resource was appropriately given the following additional types: