Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 813
fstab Avatar asked Mar 06 '14 15:03

fstab


People also ask

What is subject predicate object in RDF?

An RDF statement expresses a relationship between two resources. The subject and the object represent the two resources being related; the predicate represents the nature of their relationship. The relationship is phrased in a directional way (from subject to object) and is called in RDF a property.

What is RDF give an example of Turtle format?

The Resource Description Framework ( RDF ) is a general-purpose language for representing information in the Web. This document defines a textual syntax for RDF called Turtle that allows an RDF graph to be completely written in a compact and natural text form, with abbreviations for common usage patterns and datatypes.

What is RDF triples explain it with suitable example?

Some simple facts indicate a relationship between two things. Such a fact may be represented as an RDF triple in which the predicate names the relationship, and the subject and object denote the two things. A familiar representation of such a fact might be as a row in a table in a relational database.

What is turtle TTL?

TTL (pronounced 'turtle') stands for “Terse RDF Triple Language” and is a file format used to express RDF data—a common alternative to N-Triples, JSON-LD, and XML in the RDF space. The format . ttl is a W3C standard that is described as a “general-purpose language for representing information in the web”.


2 Answers

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 .
}
like image 198
RobV Avatar answered Sep 28 '22 08:09

RobV


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

p value a &sqsubseteq; 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 &sqsubseteq; 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

like image 29
Joshua Taylor Avatar answered Sep 28 '22 08:09

Joshua Taylor