Hello, I need help of semantic guru's :). I want to create query which I plan to use as some kind of inferencing. The situation is following. We have class MainClass which has x subClasses: SubClassOne, SubClassTwo, SubClassThree... The subclasses consist of individuals. I need to match( insert new triple ) the individuals inside each subclasses if some triples fulfil my conditions. I will try to explain it in some pseudo code.

INSERT { ?a <my:newtriple> ?b} 
?a rdf:type <my:MainClass> .
?b rdf:type <my:MainClass> .
?a rdf:type ?aClass . 
?b rdf:type ?bClass .
?aClass hasTheSameClassAs ?bClass . ( if ?aClass is SubClassOne and ?bClass is SubClassOne the condition is fulfil ) .
?a my:predicate ?aPredicate .
?b my:predicate ?bPredicate .
?aPredicate == ?bPredicate .

The first problem with the query is condition: ?aClass hasTheSameClassAs ?bClass . The problem is that ?a is rdf:type MainClass and at the same time it is for example rdf:type SubClassOne. It means for every individual there will be at least one match which is not desirable. I want to match only individuals within subClasses.

The second problem is performance. I'm aware of high complexity of the query, but I hope there is better way of matching individuals using SPARQL syntax. Thank you for any answer. Marek

asked 18 Jan '12, 15:27

Marek%20Surek's gravatar image

Marek Surek
accept rate: 0%

edited 18 Jan '12, 15:29

Details matter but fro what I read into the question ...

The whole thing is symmetric in ?a and ?b so for every:

INSERT { ?a <my:newtriple> ?b}

there will be

INSERT { ?b <my:newtriple> ?a}

Do you want that?

To match only on subclasses:

A simple approach is to eliminate the match of ?aClass with the top of the hierarchy with

FILTER ( ?aClass != :MainClass )
FILTER ( ?bClass != :MainClass )

Then you can find the cases where there is a class in common with

 FILTER(?aClass = ?bClass)

But this is the same in effect as:

?a rdf:type ?class . 
?b rdf:type ?class .

Putting it together:

  # Note this yields both <a> <b> and <b> <a>
  # You may want that - you may not.
  ?a rdf:type <my:MainClass> .
  ?b rdf:type <my:MainClass> .
  # Assume they are URIs : get the pairs once only one way round.
  FILTER( str(?a) < str(?b) )

  ?a rdf:type ?class .
  FILTER(?class != :MainClass)

  # And on to the predicate part.
  . . . 

Efficiency: using ?class and not ?aClass and ?bClass should help. One less cross-product.

permanent link

answered 19 Jan '12, 05:05

AndyS's gravatar image

AndyS ♦
accept rate: 33%

edited 20 Jan '12, 15:35

Signified's gravatar image

Signified ♦

Thank you for your answear. Symetricity is desirable because the my:newtriple is my own version of sameAs (sameAsCandidate) so it has to be symetrical. After all I had to change my reasoning strategy because the real ontology consists of more subclasses (SubClassOne rdfs:subClassOf MainClass1 rdfs:subClassOf MainClass2 rdfs:subClassOf MainClass3) and it is not possible to FILTER results because FILTER is performed after WHERE clause is done. And it would be total performance killer.

(20 Jan '12, 14:25) Marek Surek Marek%20Surek's gravatar image

@Andy, Speaking of symmetry, I edited some out of your answer. :)

(20 Jan '12, 15:34) Signified ♦ Signified's gravatar image
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:


question asked: 18 Jan '12, 15:27

question was seen: 1,757 times

last updated: 02 Nov '12, 08:46