SPARQL QUERY - UNEXPECTED RESULTS

Posted by chituan01 on
URL: http://owlready.306.s1.nabble.com/SPARQL-QUERY-UNEXPECTED-RESULTS-tp3235.html

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.