How Do I use Transitivity via Inference Rules and Transitivity for lists?
Two methods can be used for typical recursions, transitivity on inference and plain transitive patterns (or subqueries).
The advantage of inference is that queries are short and one inference rule set may be maintained for numerous queries.
If queries are about trees of classes or properties, or about equivalences of nodes, consider using inference rule sets.
Transitive patterns are inconvenient and may easily result in queries that runs too long or hard to debug, but they're unavoidable in traversing social networks or plain querying of RDF lists.
So consider a rule set, a handful of nodes with classes from the rule set and a couple of RDF Lisp-style lists defined on demo.openlinksw.com:
SQL> SPARQL CLEAR GRAPH <http://example.com/2/owl>; callret-0 VARCHAR _______________________________________________________________________________ Clear <http://example.com/2/owl> -- done 1 Rows. -- 0 msec. SQL> TTLP (' @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix owl: <http://www.w3.org/2002/07/owl#> . @prefix e: <http://example.com/e/> . e:c1 rdfs:subClassOf e:c1or2 . e:c2 rdfs:subClassOf e:c1or2 . e:c1-10 rdfs:subClassOf e:c1 . e:c1-20 rdfs:subClassOf e:c1 . e:c2-30 rdfs:subClassOf e:c2 . e:c2-40 rdfs:subClassOf e:c2 . ', 'http://example.com/2/owl', 'http://example.com/2/owl' ); Done. -- 0 msec.
You can also use the SPARUL equivalent variant:
SPARQL PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX e: <http://example.com/e/> INSERT IN GRAPH <http://example.com/2/owl> { e:c1 rdfs:subClassOf e:c1or2 . e:c2 rdfs:subClassOf e:c1or2 . e:c1-10 rdfs:subClassOf e:c1 . e:c1-20 rdfs:subClassOf e:c1 . e:c2-30 rdfs:subClassOf e:c2 . e:c2-40 rdfs:subClassOf e:c2 . } ;
Define the inference rule:
SQL> rdfs_rule_set ('http://example.com/2/owl', 'http://example.com/2/owl'); Done. -- 0 msec. SQL> SPARQL CLEAR GRAPH <http://example.com/2/data> ; callret-0 VARCHAR _______________________________________________________________________________ Clear <http://example.com/2/data> -- done 1 Rows. -- 0 msec. SQL> TTLP (' @prefix e: <http://example.com/e/> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix owl: <http://www.w3.org/2002/07/owl#> . e:s1 a e:c1 ; e:p1 "Value of p1 for s1" . e:s2 a e:c2 ; e:p1 "Value of p1 for s2" . e:s1-10 a e:c1-10 ; e:p1 "Value of p1 for s1-10" . e:s1-20 a e:c1-20 ; e:p1 "Value of p1 for s1-20" . e:s2-30 a e:c2-30 ; e:p1 "Value of p1 for s2-30" . e:s2-40 a e:c2-40 ; e:p1 "Value of p1 for s2-40" . e:lists rdf:_1 ( e:list1-item1 e:list1-item2 e:list1-item3 ) ; rdf:_2 ( [ e:p2 "Value of p2 of item1 of list2" ; e:p3 "Value of p3 of item1 of list2" ] [ e:p2 "Value of p2 of item2 of list2" ; e:p3 "Value of p3 of item2 of list2" ] [ e:p2 "Value of p2 of item3 of list2" ; e:p3 "Value of p3 of item3 of list2" ] ) . ', 'http://example.com/2/data', 'http://example.com/2/data' ); Done. -- 0 msec.
You can also use the SPARUL equivalent variant:
SPARQL PREFIX e: <http://example.com/e/> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX owl: <http://www.w3.org/2002/07/owl#> INSERT IN GRAPH <http://example.com/2/data> { e:s1 a e:c1 ; e:p1 "Value of p1 for s1" . e:s2 a e:c2 ; e:p1 "Value of p1 for s2" . e:s1-10 a e:c1-10 ; e:p1 "Value of p1 for s1-10" . e:s1-20 a e:c1-20 ; e:p1 "Value of p1 for s1-20" . e:s2-30 a e:c2-30 ; e:p1 "Value of p1 for s2-30" . e:s2-40 a e:c2-40 ; e:p1 "Value of p1 for s2-40" . e:lists rdf:_1 ( e:list1-item1 e:list1-item2 e:list1-item3 ) ; rdf:_2 ( [ e:p2 "Value of p2 of item1 of list2" ; e:p3 "Value of p3 of item1 of list2" ] [ e:p2 "Value of p2 of item2 of list2" ; e:p3 "Value of p3 of item2 of list2" ] [ e:p2 "Value of p2 of item3 of list2" ; e:p3 "Value of p3 of item3 of list2" ] ) };
SPARQL DESCRIBE works fine with inference, deriving additional type information:
DEFINE input:inference <http://example.com/2/owl> DESCRIBE <http://example.com/e/s1> FROM <http://example.com/2/data> fmtaggret- LONG VARCHAR _______________________________________________________________________________ @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix ns1: <http://example.com/e/> . ns1:s1 rdf:type ns1:c1or2 , ns1:c1 ; ns1:p1 "Value of p1 for s1" . 1 Rows. -- 0 msec.
Example links against Virtuoso Demo Server SPARQL Endpoint with SPARQl Protocol URLs:
DEFINE input:inference <http://example.com/2/owl> DESCRIBE <http://example.com/e/s2> FROM <http://example.com/2/data> fmtaggret- LONG VARCHAR _______________________________________________________________________________ @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix ns1: <http://example.com/e/> . ns1:s2 rdf:type ns1:c1or2 , ns1:c2 ; ns1:p1 "Value of p1 for s2" . 1 Rows. -- 0 msec.
Example links against Virtuoso Demo Server SPARQL Endpoint with SPARQl Protocol URLs:
Querying is simple as well:
SQL>SPARQL DEFINE input:inference <http://example.com/2/owl> PREFIX e:<http://example.com/e/> SELECT * FROM <http://example.com/2/data> WHERE { ?s a e:c1or2 ; e:p1 ?o } s o VARCHAR VARCHAR ___________________________ http://example.com/e/s1 Value of p1 for s1 http://example.com/e/s1-10 Value of p1 for s1-10 http://example.com/e/s1-20 Value of p1 for s1-20 http://example.com/e/s2-30 Value of p1 for s2-30 http://example.com/e/s2-40 Value of p1 for s2-40 http://example.com/e/s2 Value of p1 for s2 6 Rows. -- 0 msec.
Example links against Virtuoso Demo Server SPARQL Endpoint with SPARQl Protocol URLs:
SQL>SPARQL DEFINE input:inference <http://example.com/2/owl> PREFIX e:<http://example.com/e/> SELECT * FROM <http://example.com/2/data> WHERE { ?s a e:c1 ; e:p1 ?o } s o VARCHAR VARCHAR ___________________________ http://example.com/e/s1 Value of p1 for s1 http://example.com/e/s1-10 Value of p1 for s1-10 http://example.com/e/s1-20 Value of p1 for s1-20 3 Rows. -- 0 msec.
Example links against Virtuoso Demo Server SPARQL Endpoint with SPARQl Protocol URLs:
However you should care about duplicates if both types and properties are queried: the join will result in all combinations of types and property values.
SQL>SPARQL DEFINE input:inference <http://example.com/2/owl> PREFIX e:<http://example.com/e/> SELECT * FROM <http://example.com/2/data> WHERE { ?s a ?t ; e:p1 ?o } s t o VARCHAR VARCHAR VARCHAR ___________________________ http://example.com/e/s1 http://example.com/e/c1 Value of p1 for s1 http://example.com/e/s1 http://example.com/e/c1or2 Value of p1 for s1 http://example.com/e/s1-10 http://example.com/e/c1-10 Value of p1 for s1-10 http://example.com/e/s1-10 http://example.com/e/c1 Value of p1 for s1-10 http://example.com/e/s1-10 http://example.com/e/c1or2 Value of p1 for s1-10 http://example.com/e/s1-20 http://example.com/e/c1-20 Value of p1 for s1-20 http://example.com/e/s1-20 http://example.com/e/c1 Value of p1 for s1-20 http://example.com/e/s1-20 http://example.com/e/c1or2 Value of p1 for s1-20 http://example.com/e/s2-30 http://example.com/e/c2-30 Value of p1 for s2-30 http://example.com/e/s2-30 http://example.com/e/c2 Value of p1 for s2-30 http://example.com/e/s2-30 http://example.com/e/c1or2 Value of p1 for s2-30 http://example.com/e/s2-40 http://example.com/e/c2-40 Value of p1 for s2-40 http://example.com/e/s2-40 http://example.com/e/c2 Value of p1 for s2-40 http://example.com/e/s2-40 http://example.com/e/c1or2 Value of p1 for s2-40 http://example.com/e/s2 http://example.com/e/c2 Value of p1 for s2 http://example.com/e/s2 http://example.com/e/c1or2 Value of p1 for s2 16 Rows. -- 0 msec.
Example links against Virtuoso Demo Server SPARQL Endpoint with SPARQl Protocol URLs:
Transitive queries are convenient as SPARQL 1.1 "predicate+" equivalent. The equivalent of "predicate*" requires the use of a union:
SQL>SPARQL PREFIX e:<http://example.com/e/> SELECT ?item FROM <http://example.com/2/data> WHERE { { ?lists rdf:_1 ?node } UNION { ?lists rdf:_1 ?l . ?l rdf:rest ?node option (transitive) . } ?node rdf:first ?item } item VARCHAR _______________________________________________________________________________ http://example.com/e/list1-item1 http://example.com/e/list1-item2 http://example.com/e/list1-item3 3 Rows. -- 0 msec.
Example links against Virtuoso Demo Server SPARQL Endpoint with SPARQl Protocol URLs:
SQL> SPARQL PREFIX e:<http://example.com/e/> SELECT ?p ?o FROM <http://example.com/2/data> WHERE { { ?lists rdf:_2 ?node } UNION { ?lists rdf:_2 ?l . ?l rdf:rest ?node option (transitive) . } ?node rdf:first ?item . ?item ?p ?o } p o VARCHAR VARCHAR ________________________ http://example.com/e/p2 Value of p2 of item1 of list2 http://example.com/e/p3 Value of p3 of item1 of list2 http://example.com/e/p2 Value of p2 of item2 of list2 http://example.com/e/p3 Value of p3 of item2 of list2 http://example.com/e/p2 Value of p2 of item3 of list2 http://example.com/e/p3 Value of p3 of item3 of list2 6 Rows. -- 0 msec.
Example links against Virtuoso Demo Server SPARQL Endpoint with SPARQl Protocol URLs:
Note that the result set can be in order of items in the list, but it don't have to. If the order should be preserved, then fix the direction of transitive scan, get step number as a variable, order by that variable.
SQL> SPARQL PREFIX e:<http://example.com/e/> SELECT ?p ?o bif:coalesce(?step_no, 0) FROM <http://example.com/2/data> WHERE { { ?lists rdf:_2 ?node } UNION { ?lists rdf:_2 ?l . ?l rdf:rest ?node OPTION (transitive, t_direction 1, t_step("step_no") as ?step_no) . } ?node rdf:first ?item . ?item ?p ?o } ORDER BY ASC (?step_no) p o callret-2 VARCHAR VARCHAR VARCHAR ________________________ http://example.com/e/p2 Value of p2 of item1 of list2 0 http://example.com/e/p3 Value of p3 of item1 of list2 0 http://example.com/e/p2 Value of p2 of item2 of list2 1 http://example.com/e/p3 Value of p3 of item2 of list2 1 http://example.com/e/p2 Value of p2 of item3 of list2 2 http://example.com/e/p3 Value of p3 of item3 of list2 2 6 Rows. -- 7 msec.
Example links against Virtuoso Demo Server SPARQL Endpoint with SPARQl Protocol URLs:
Related
- Virtuoso Tips and Tricks Collection
- Virtuoso Documentation: Subclasses and Subproperties
- Transitive SPARQL query options and Exploit Inference Rules Examples