SPARQL QUERY - UNEXPECTED RESULTS

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

SPARQL QUERY - UNEXPECTED RESULTS

chituan01
Hi everyone, hi Jiba,

I'm using SPARQL in Owlready2 and I've got unexpected results. The result I've got from my query is different from the built-in function in Owlready2. Below is my code in Google Colab:
- - - - - - - - - - - - - - -
!git clone https://bitbucket.org/jibalamy/owlready2/src/master/ owlready2
import sys
sys.path.append('/content/owlready2')

from owlready2 import *
onto = get_ontology("id.rdf")

with onto:
  class Individuals(Thing):
    pass
  class relationship2(ObjectProperty,SymmetricProperty):
    domain    = [Individuals]
    range     = [Individuals]

name1 = "Individual 1"
indi1 = Individuals(name1.replace(" ",""))
indi1.label.append(name1)

name2 = "Individual 2"
indi2 = Individuals(name2.replace(" ",""))
indi2.label.append(name2)

name3 = "Individual 3"
indi3 = Individuals(name3.replace(" ",""))
indi3.label.append(name3)

indi1.relationship2.append(indi2)
indi1.relationship2.append(indi3)
indi2.relationship2.append(indi3)

sync_reasoner()

search_term = "Individual 2"
pre_query = """?x rdf:type owl:NamedIndividual. ?x rdfs:label "{keyword}". ?x <id.rdf#relationship2> ?y. ?y rdfs:label ?ylabel.""".format(keyword=str(search_term))
query = """SELECT DISTINCT (STR(?ylabel) AS ?label)"""+ "{" + pre_query + "}"
result = list(default_world.sparql(str(query)))
print(result)
- - - - - - - - - - - - - - -

- Results from the query above: [['Individual 3']]
- Expected results: [['Individual 1'], ['Individual 3']]

- Comment 1: I have tried list(indi2.relationship2) and also list(onto.Individual2.relationship2). They gave me exactly what I expected: [id.Individual1, id.Individual3].
- Comment 2: I have tried my query in Protege and got my expected results too.
- Comment 3: Additionally, I used sync_reasoner(infer_property_values = True) but it seems useless.

So I don't really know why my query didn't work in Colab. Any comments are welcome.
Thank you.
Reply | Threaded
Open this post in threaded view
|

Re: SPARQL QUERY - UNEXPECTED RESULTS

Jiba
Administrator
Hi,

The SPARQL engine does not takes into account inverse properties, and symmetric properties are considered as a special type of inverse property.

The reasoner does not infer inverse relations, as they are considered as too trivial (and they can be voluminous).

The best solution is probably to modify the query in order to take into account the symmetric nature of the property:

pre_query = """?x rdf:type owl:NamedIndividual. ?x rdfs:label "{keyword}". { ?x <id.rdf#relationship2> ?y } UNION { ?y <id.rdf#relationship2> ?x } .  ?y rdfs:label ?ylabel.""".format(keyword=str(search_term))

Jiba
Reply | Threaded
Open this post in threaded view
|

Re: SPARQL QUERY - UNEXPECTED RESULTS

chituan01
Hi Jiba,

Thank you for your response. I noted this. Since I had implemented the same query successfully in Protege, I think this mechanism only applies to SPARQL engine in Owlready2, right?

Anyway, thank you for your help. It works ;)

Reply | Threaded
Open this post in threaded view
|

Re: SPARQL QUERY - UNEXPECTED RESULTS

Jiba
Administrator
Hi,

Regarding SPARQL, I think most engine does not consider inverse relation (but RDFlib seems to be an exception).

What is specific to Owlready, is that inverse relation are not inferred from the reasoner.

Jiba