I ran into an issue that requires me to find all subjects, for which only triples with specified predicates, but no other kinds of triples exist. Or, thinking the other way round, I want to exclude all subjects having triples with predicates different than the ones specified. Generally, those subjects have the specified specified predicates too, but do have different predicates as well.

As a minimal example:

s:tom p:hates h:football .
s:tom p:loves h:basketball .

s:frank p:hates h:basketball .
s:frank p:loves h:football .
s:frank p:practices h:football .

s:jon p:hates h:pingpong .
s:jon p:hates h:baseball .
s:jon p:loves h:football .
s:jon p:practices h:pingpong .

For example, I would like to query for everyone who does nothing else than "hate" or "love" any kind of hobby. So, in this case, I would want s:tom as a result.

I have been trying to get this to work with my limited SPARQL knowledge by playing around with Subqueries, MINUS, FILTERS and just can't get it to work. Using FILTER I managed to exclude triples with the specified predicates and therefore got the subjects having more predicates than the specified ones. I thought that it should maybe possible to use this in a Subquery to get what I need, but I could neither get that to work, nor do I know whether it makes sense or if there are better alternatives. Another similar issue on here ( http://answers.semanticweb.com/questions/628/limiting-results-in-sparql-to-subjects-with-only-one-predicate-of-a-certain-type ) couldn't help me out either.

As a note: I am using Jena Arq 2.7 with the advanced parser for SPARQL 1.1 functionality.

Thanks in advance, any help would be appreciated!

asked 13 Nov '12, 20:33

knut_'s gravatar image

accept rate: 0%

edited 13 Nov '12, 20:43

Not sure about performance on large datasets, but you could try something like:

select distinct ?s { 
    ?s ?p ?x filter not exists {
        ?s ?q ?y filter ( ?q not in (p:loves, p:hates) )

answered 14 Nov '12, 02:59

AB's gravatar image

accept rate: 35%


Nice use of the not in filter operator! The following will work in a very recent sparql engine. Introduces unions which might perform better than filters. select distinct ?s { ?s ?p ?x . filter not exists { VALUES ?q {(p:loves) (p:hates)} ?s ?q ?y } }

(14 Nov '12, 03:33) Jerven Jerven's gravatar image

@Jerven, you need another negation ... you need to say that ?q is not in that set. (But thanks for pointing me to new use of VALUES.)

(14 Nov '12, 10:58) Signified ♦ Signified's gravatar image

@Signified, thanks for letting me know. I had misread the question. Then the answer by @AB is definitely the correct one. Introducing a MINUS around the ?s ?q ?y will be unlikely to improve the performance.

(14 Nov '12, 11:04) Jerven Jerven'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



Asked: 13 Nov '12, 20:33

Seen: 684 times

Last updated: 14 Nov '12, 11:21