• Topic
  • Discussion
  • ODS.VirtSPARQLReasoningTutorial(Last) -- DAVWikiAdmin? , 2017-06-29 07:34:31 Edit WebDAV System Administrator 2017-06-29 07:34:31

    Tutorial Demonstrating Reasoning via SPARQL

    Why?

    Reasoning is a topic of discussion that suffers (like most things related to the Semantic Web vision) unduely due to poor narratives and simple examples.

    What?

    This tutorial demonstrates "on the fly" reasoning (i.e., no persistence of inferred query results) via the use of inference rules or SPARQL 1.1 property path functionality.

    How?

    To make this example as simple as possible, we are going to use data that represents the relationships among key members of the British royal family. Basically we will use DBpedia identifiers (HTTP URIs) to denote:

    • Each family member;
    • Predicates/Properties that express entity relationship semantics between family members.
    Family Members: Predicates/Properties:

    The predicates used in this exercise have been sourced from the relationship vocabulary at <http://purl.org/vocab/relationship/>.

    In this guide we make specific use of the following:

    Step 1: Raw Data

    Using Turtle [2] notation, we can write an RDF-based Linked Data graph to express how the royals are related:


    ## Turtle based Data Representation Start ##
    
    @prefix rel: <http://purl.org/vocab/relationship/> .
    
    <http://dbpedia.org/resource/Prince_William_of_Wales>
        rel:siblingOf   <http://dbpedia.org/resource/Prince_Harry_of_Wales> .
    <http://dbpedia.org/resource/Elizabeth_Bowes-Lyon>
        rel:ancestorOf   <http://dbpedia.org/resource/Elizabeth_II_of_the_United_Kingdom> .
    <http://dbpedia.org/resource/Elizabeth_II_of_the_United_Kingdom>
        rel:ancestorOf   <http://dbpedia.org/resource/Charles%2C_Prince_of_Wales> .
    <http://dbpedia.org/resource/Charles%2C_Prince_of_Wales>
        rel:ancestorOf   <http://dbpedia.org/resource/Prince_William_of_Wales> .
    
    ## End ##
    

    Step 2: Data Loading

    Now that we have raw data (in the form of a Turtle-based entity relationship graph) in place, we can proceed to load this data into a SPARQL 1.1 compliant Virtuoso DBMS. Virtuoso enables us load data using any of the following methods:

    • SPARQL 1.1 LOAD? -- whereby we load the data into Virtuoso from a local or remote file comprised of the Turtle raw data above
    • SPARQL 1.1 INSERT? -- whereby we load the data declaratively, using SPARQL query patterns
    • SPASQL -- whereby we load the data using Virtuoso's SQL/SPARQL hybrid language, which provides an intuitive transition from SQL to SPARQL, for those familiar with SQL data manipulation language constructs

    Step 2.1: SPARQL 1.1


    ## Create Instance Data for Relationship Ontology
    PREFIX rel: <http://purl.org/vocab/relationship/>
    
    INSERT
      {
        GRAPH <urn:owl:inference:tests> 
          {
            <http://dbpedia.org/resource/Prince_William_of_Wales>
                rel:siblingOf   <http://dbpedia.org/resource/Prince_Harry_of_Wales> .
            <http://dbpedia.org/resource/Elizabeth_Bowes-Lyon>
                rel:ancestorOf   <http://dbpedia.org/resource/Elizabeth_II_of_the_United_Kingdom> .
            <http://dbpedia.org/resource/Elizabeth_II_of_the_United_Kingdom>
                rel:ancestorOf   <http://dbpedia.org/resource/Charles%2C_Prince_of_Wales> .
            <http://dbpedia.org/resource/Charles%2C_Prince_of_Wales>
                rel:ancestorOf   <http://dbpedia.org/resource/Prince_William_of_Wales> .
          }
      }
    

    Step 2.2: SPASQL


    SPARQL
    ## Create Instance Data for Relationship Ontology
    PREFIX rel: <http://purl.org/vocab/relationship/>
    
    INSERT into GRAPH <urn:owl:inference:tests>
      {
        <http://dbpedia.org/resource/Prince_William_of_Wales>
           rel:siblingOf   <http://dbpedia.org/resource/Prince_Harry_of_Wales> .
        <http://dbpedia.org/resource/Elizabeth_Bowes-Lyon>
           rel:ancestorOf   <http://dbpedia.org/resource/Elizabeth_II_of_the_United_Kingdom> .
        <http://dbpedia.org/resource/Elizabeth_II_of_the_United_Kingdom>
           rel:ancestorOf   <http://dbpedia.org/resource/Charles%2C_Prince_of_Wales> .
        <http://dbpedia.org/resource/Charles%2C_Prince_of_Wales>
           rel:ancestorOf   <http://dbpedia.org/resource/Prince_William_of_Wales> .
      }
    

    Step 3: Verify Data

    To verify that your data has been created, execute the following basic SPARQL query:


    ## Verify Data
    
    SELECT *
    FROM <urn:owl:inference:tests>
    WHERE
      {
        ?s ?p ?o .
      }
    

    Step 4: Setting Up Inference Rules

    We are using terms from the relationship vocabulary to drive this exercise, so we need to make Virtuoso aware of this through the use of an inference rule declaration that binds said rule to the relationship vocabulary. To complete this task, you need to execute the following three commands via one of Virtuoso's SQL interfaces (e.g., command-line, ODBC, JDBC, ADO.NET, or XMLA):


    SPARQL CLEAR GRAPH  <http://vocab.org/relationship/>;
    
    SPARQL LOAD <http://vocab.org/relationship/rel-vocab-20100607.rdf>
        INTO <http://vocab.org/relationship/>;
    
    rdfs_rule_set ('urn:owl:inference:rules:tests', 'http://vocab.org/relationship/') ;
    

    Step 5: Verify Rule's existence


    SELECT *
    FROM sys_rdf_schema
    

    Step 6: SPARQL Inference Queries

    Who are the descendants of the entity denoted by the DBpedia Identifier <http://dbpedia.org/resource/Elizabeth_Bowes-Lyon>? (SPARQL pragma)

    In this case we will use a Virtuoso SPARQL pragma to conditionally invoke Virtuoso's built-in reasoner against the rule created earlier:


    DEFINE input:inference 'urn:owl:inference:rules:tests'
    PREFIX rel: <http://purl.org/vocab/relationship/>
    
    SELECT *
    FROM <urn:owl:inference:tests>
    WHERE
      {
        <http://dbpedia.org/resource/Elizabeth_Bowes-Lyon>
           rel:ancestorOf   ?o
      }
    

    Who are the descendants of the entity denoted by the DBpedia Identifier <http://dbpedia.org/resource/Elizabeth_Bowes-Lyon>? (SPARQL 1.1 Property Paths)

    In this case, we will use SPARQL 1.1 Property Paths to achieve the same goal via the "+" unary operator applied to the rel:ancestorOf predicate in the SPARQL query pattern:


    PREFIX rel: <http://purl.org/vocab/relationship/>
    
    SELECT *
    FROM <urn:owl:inference:tests>
    WHERE
      {
         <http://dbpedia.org/resource/Elizabeth_Bowes-Lyon>  
             rel:ancestorOf+  ?o  .
    
      }
    

    Who are the descendants of the entity denoted by the DBpedia Identifier <http://dbpedia.org/resource/Elizabeth_Bowes-Lyon>? (incomplete result)

    In this case, neither the use of a SPARQL inference rules pragma nor a SPARQL 1.1 property paths are put to use, so you end up with in incomplete result (or solution):


    PREFIX rel: <http://purl.org/vocab/relationship/>
    
    SELECT *
    FROM <urn:owl:inference:tests>
    WHERE
      {
        <http://dbpedia.org/resource/Elizabeth_Bowes-Lyon>   
             rel:ancestorOf   ?o  . 
      }
    

    Who are the siblings of the entity denoted by the DBpedia Identifier <http://dbpedia.org/resource/Prince_Harry_of_Wales>?

    This collection of queries leverages with the semantics of the rel:siblingOf predicate. This particular predicate's semantics imply that the subject and object positions in triples have no effect on the query result. Thus, the position of the DBpedia Identifier <http://dbpedia.org/resource/Prince_Harry_of_Wales> in the SPARQL triple pattern has no effect on the eventual query result, when reasoning is in use.

    Using the Virtuoso SPARQL inference rules pragma (siblings)

    Using the Virtuoso inference rule SPARQL pragma approach the query would be as follows:


    DEFINE input:inference 'urn:owl:inference:rules:tests'
    PREFIX rel: <http://purl.org/vocab/relationship/>
    
    SELECT *
    FROM <urn:owl:inference:tests>
    WHERE
      {
        <http://dbpedia.org/resource/Prince_Harry_of_Wales>   
             rel:siblingOf   ?o  . 
      }
    

    Using SPARQL 1.1 property paths (siblings)

    Using SPARQL 1.1 property paths to get the same effect via combined use of the following operators with the rel:siblingOf query pattern predicate: "/" (path sequence operator), "^" inverse operator, a one or more ("+"). The resulting SPARQL query takes the following form:


    PREFIX rel: <http://purl.org/vocab/relationship/>
    
    SELECT *
    FROM <urn:owl:inference:tests>
    WHERE
      {
        <http://dbpedia.org/resource/Prince_Harry_of_Wales>  
             (rel:siblingOf+|^rel:siblingOf)   ?o  .
      }
    
    Using no inference rules pragmas or SPARQL 1.1 property paths (siblings)

    Executing the SPARQL query without inference rules pragmas or SPARQL 1.1 property paths results in an empty results set. The query in question would take the form below. Of course, you can simply comment out the Virtuoso SPARQL pragma declaration too.


    PREFIX rel: <http://purl.org/vocab/relationship/>
    
    SELECT *
    FROM <urn:owl:inference:tests>
    WHERE
      {
        <http://dbpedia.org/resource/Prince_Harry_of_Wales>   
             rel:siblingOf   ?o  . 
      }
    

    The entity denoted by the DBpedia Identifier <http://dbpedia.org/resource/Elizabeth_II_of_the_United_Kingdom> is the descendant of whom?

    This collection of queries leverage the inverseOf semantics that underly the rel:descendantOf predicate. Basically, this is about the opposite (inverse) implications of an ancestor and a descendant. The object of an ancestor triple is the subject of a descendant triple which implies that my raw data doesn't need to explicitly include any rel:descendantOf triples.

    Using the Virtuoso SPARQL inference rules pragma (descendant of whom)

    Using the Virtuoso SPARQL inference rules pragma your SPARQL query would be as follows:


    DEFINE input:inference 'urn:owl:inference:rules:tests'
    PREFIX rel: <http://purl.org/vocab/relationship/>
    
    SELECT *
    FROM <urn:owl:inference:tests>
    WHERE
      {
        <http://dbpedia.org/resource/Elizabeth_II_of_the_United_Kingdom>  
             rel:descendantOf  ?o  . 
      }
    
    Using SPARQL 1.1 property paths (descendant of whom)

    Using SPARQL 1.1 property paths to achieve the same result via use of the alternative paths operator ("|") combined with the inverse ("^") operator leads to the following query:


    PREFIX rel: <http://purl.org/vocab/relationship/>
    
    SELECT *
    FROM <urn:owl:inference:tests>
    WHERE
      {
        <http://dbpedia.org/resource/Elizabeth_II_of_the_United_Kingdom>  
             rel:descendantOf|^rel:ancestorOf   ?o  . 
      }
    

    Using no inference rules pragmas or SPARQL 1.1 property paths (descendant of whom)

    Executing the SPARQL query without a Virtuoso inference rule pragma or SPARQL 1.1 query paths alternative, you will get an empty result for the following:


    PREFIX rel: <http://purl.org/vocab/relationship/>
    
    SELECT *
    FROM <urn:owl:inference:tests>
    WHERE
      {
        <http://dbpedia.org/resource/Elizabeth_II_of_the_United_Kingdom>  
             rel:descendantOf  ?o  . 
      }
    

    Related

    1. SPARQL Named Graphs with SPARQL 1.1 Property Paths and Reasoning?
    2. W3C Turtle Spec
    3. SPARQL 1.1 Property Paths
    4. Simple SPARQL based Data Integration that leverages inference or SPARQL 1.1 Property Paths
    5. Virtuoso SPARQL 1.1 Features Examples Collection?