in turtle or RDF can I add a predicate/object on all subjects that match a criteria?
Asked Answered
F

2

5

I am doing some experiments with importing triples formulated in the turtle language within the openrdf-workbench webapp in Tomcat, which has incorporated a SPARQL endpoint.

I wonder if with turtle, or, generally, in RDF / RDFS is it possible to add a certain predicate/object declaration on all (implicit) subjects conditionally to the existence of another predicate/object.

For example, if I have the following triples defined:

foo:a foo:b foo:c
foo:d foo:b foo:c
foo:e foo:b foo:c
foo:f foo:b foo:c

I would like to automatically add the following predicate/subject to all subjects that match predicate=foo:b and object=foo:c:

(implicit subject) foo:g foo:h

in order to automatically produce the following triples:

foo:a foo:g foo:h
foo:d foo:g foo:h
foo:e foo:g foo:h
foo:f foo:g foo:h

Is this possible?

Alternatively: is there any way to define some triples in order to enable SPARQL to find foo:a/d/e/f when queried for subjects that have foo:g foo:h as predicate/object?

Flanch answered 6/3, 2014 at 15:33 Comment(4)
In OWL, you'd do this with an axiom: (b value c) subClassOf (g value h). This says that if something has c as a value for property b, then it also has h as a value for property g.Stamey
@JoshuaTaylor Thank you very much for the answer. Do you know how or if it's possible to express the axiom via RDF triples?Flanch
Yup, I've added an answer showing results in Protege, and with a serialization of the ontology.Stamey
Duplicated at answers.semanticweb.com/questions/26848/… .Joleenjolene
D
8

Part 1 - Creating additional information

The first part of your question can be solved in one of two ways:

  1. Using Inference
  2. Using SPARQL Update

Inferencing

Inference is a technique whereby you define rules that infer additional triple based on your existing triples. You typically either use a pre-defined set of rules or use your own custom rules. I think Sesame only supports pre-defined rule sets out of the box so you may want to take a look at OWLIM which is an alternative back end that can be used with Sesame and has much more customisable rules AFAIK.

Inferencing can typically be applied in two ways, one where you only store the rules and you compute the additional information every time a rule fires and another where you pre-compute all the additional information and add it to your database. Which you will want to use depends on how you intend to use your system and there are performance trade offs involved. I'm not going into detail because that's really a whole other question - see Forward vs Backward Chaining for some discussion

SPARQL Update

Alternatively if your rules are relatively simple and you are OK with pre-computing the extra information and adding it to your database you can write SPARQL Updates to do this e.g.

PREFIX foo: <http://example.org/foo#>
INSERT
{
  ?x foo:g foo:h .
}
WHERE
{
  ?x foo:b foo:c .
}

Part 2 - Querying the Data

I am guessing you are fairly new to SPARQL because from what you've described this sounds trivial to me.

If I wanted to find all subjects which had the predicate foo:g and the object foo:h I would simply write the following:

PREFIX foo: <http://example.org/foo#>
SELECT ?x
WHERE
{
  ?x foo:g foo:h .
}
Dermato answered 6/3, 2014 at 17:44 Comment(1)
Thank you very much for your answer. Is it possible to use OWL to define such inference? I would be very grateful if you can provide an example. Moreover, is SPARQL able to infer on OWL declarations?Flanch
S
6

You can do this type of inference using OWL with an axiom of the form

p value a ⊑ q value b

which says that if something has a as a value for property p, then it also has b as a value for property q. As an example, here's an ontology with four individuals (a, b, c, d), two object properties (p, q), and the axiom (p value c ⊑ q value d).

@prefix :      <http://example.org/add-predicate-object#> .
@prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> .
@prefix owl:   <http://www.w3.org/2002/07/owl#> .
@prefix xsd:   <http://www.w3.org/2001/XMLSchema#> .
@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

<http://example.org/add-predicate-object> a owl:Ontology .

:p a owl:ObjectProperty .
:q a owl:ObjectProperty .

[ a owl:Restriction ;
  owl:onProperty :p ;
  owl:hasValue   :c ;
  rdfs:subClassOf [ a owl:Restriction ;
                    owl:onProperty :q ;
                    owl:hasValue   :d ] . ] .

:a a owl:Thing, owl:NamedIndividual ; :p :c .
:b a owl:Thing, owl:NamedIndividual ; :p :c .
:c a owl:Thing, owl:NamedIndividual .
:d a owl:Thing, owl:NamedIndividual .

In Protégé, the axiom looks like this:

enter image description here

You can enable a reasoner and query for instances of q value d and see:

enter image description here

or you can browse to individuals and see the results:

enter image description here

Stamey answered 6/3, 2014 at 18:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.