0

The following python code is part of a larger piece (everything else is working fine):

import rdflib

g1 = rdflib.Graph()
g1.parse("existing_graph.nt", format="nt")

q = "select ?ent_a ?ent_b where { ?ent_a <http://www.example.org/rel> ?c . " \
    "?ent_b <http://www.example.org/rel> ?c. }"
res = g1.query(q)

I wish to get in my results only cases where ent_a differs from ent_b and can't find the relevant documentation.

GalB1t
  • 265
  • 1
  • 3
  • 13
  • What exactly are you trying to extract? Why don't you just call up one triple such as `?s ?p ?o`? Your query makes no sense in its current form. Do you wish to get two copies of the same type? – Artemis Jun 01 '15 at 14:34
  • @Artemis - I wish to get to items ("a", "b") that relates to anothe item ("c") in the same way, e.g. - two cities that are next to the same river, but I want "a" to be different from "b". – GalB1t Jun 01 '15 at 14:40

3 Answers3

2

You will get every differerent ?a ?b pair with FILTER !=, but also ?b ?a (the pair reversed)

If ?a and ?b are URIs, then this pattern may help:

select *
where {
    ?a a ?s.
    ?b a ?s.
filter (str(?a) > str(?b))
} 
Jörn Hees
  • 3,338
  • 22
  • 44
AndyS
  • 16,345
  • 17
  • 21
  • They are indeed and it worked perfectly, do you know any place where I can find nice elementary filtering tricks as this? I need some of those desperately,, – GalB1t Jun 01 '15 at 16:15
  • @GalB1t [SPARQL 1.1 Query Language](http://www.w3.org/TR/sparql11-query/) lists all the filter functions and has lots of examples. Have a look at section 17. – Joshua Taylor Jun 01 '15 at 16:40
  • @JoshuaTaylor - it lacks the correct python syntax which drives me nuts :( (I'm using rdflib) – GalB1t Jun 01 '15 at 16:47
  • @GalB1t I'm not sure what you mean; isn't the problem with *SPARQL*? – Joshua Taylor Jun 01 '15 at 16:48
  • I use rdflib to run the SPARQL queries, it wasn't my choice. – GalB1t Jun 01 '15 at 16:49
1

The easiest way to just say that two things are not the same is to use != sign.

select distinct *
where {
    ?a a ?s.
    ?b a ?s.
filter (?a!=?b)
} 

However, this query is very strange because by just writing:

select distinct *
where {
    ?a ?p ?s.
} 

You are able to extract every distinct ?a that has a ?p relation with ?s. Thus, depending on your use, you have already generated your result set.

If you need to dig deeper, as per your comment:

I have an ontology where objects of type "teams" have a "locatedIn" relationship with their "hometown", and I wish to find all of the possible local derbies.

You need to add more restrictions by adding another triple that relates to the first tripe. For example, in dbpedia, the following query will give you all the teams and their grounds:

select distinct *
where{
    ?o a dbpedia-owl:SportsTeam.
    ?o dbpedia-owl:ground ?ground.
}
Artemis
  • 3,271
  • 2
  • 20
  • 33
  • @Arthemis - this is good yet not fully working as it still returns "a s b" and "b s a" tuples, any idea how to prevent it? BTW this syntax is not compatible with python's rdflib and I needed to remove the * – GalB1t Jun 01 '15 at 15:33
  • I added some explanation to why I think your query doesn't make sense. do you want to tell me what use case you have for these pairs? Why don't you just get the distinct triples and just create a distinct pair of arrays in Python? – Artemis Jun 01 '15 at 15:49
  • I have an ontology where objects of type "teams" have a "locatedIn" relationship with their "hometown", and I wish to find all of the possible local derbies. – GalB1t Jun 01 '15 at 15:53
0

Why do you run slow SPARQL queries when you loaded the whole graph into memory already? You can loop over tripples in any way you want and compare for equalness etc. http://rdflib.readthedocs.org/en/latest/intro_to_graphs.html#basic-triple-matching

Terran
  • 1,091
  • 18
  • 29