Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [rdf4j-dev] sparql aggregate query not working

Hi Boris,

the OPTIONAL expressions in you query are composed in a way that you may end up with a Cartesian product of solutions for the variable ?node2

e.g. in case that the value bound to ?node is not related with a rdfs:subClassOf to something that is of type Restriction or if it is, then it may not have a ow:someValuesFrom relation.
What happens next is that, when such partial solution is found, the third OPTIONAL would not bind anything to ?node2 and as a consequence, the last OPTIONAL will start to provide bindings to ?node2 from all the statements with the rdfs:label predicate ... which is probably not exactly what you are aiming for...

Can suggest you to use nested optionals like below, if such pattern matches your intentions:
{code}
SELECT ?node ?nodeLabel ?superclass ?superclassLabel (group_concat(DISTINCT ?node2) as ?node2s) (group_concat(DISTINCT ?node2Label) as ?node2Labels) where {
    ?node rdf:type owl:Class .
    ?node rdfs:subClassOf ?superclass .
    OPTIONAL {
        ?node rdfs:subClassOf ?restriction .
        ?restriction a owl:Restriction .
        ?restriction owl:someValuesFrom ?node2 .
        OPTIONAL { ?node2 rdfs:label ?node2Label }
    }
    ?node rdfs:label ?nodeLabel .
    ?superclass rdfs:label ?superclassLabel .
}
group by ?node ?nodeLabel ?superclass ?superclassLabel ?node2 ?node2Label
LIMIT 10
{code}

HTH
Damyan

On 08/12/2017 17:04, Boris Metodiev wrote:
Hello all, I was wondering if anyone could help me with this query: 

I'm trying to retrieve certain elements from an rdf ontology using rdflib, which uses SPARQL 1.1. The ontology is of the human body, and all the parts have their own class. Here is an example:

<owl:Class rdf:about="http://human.owl#NCI_C12909"><rdfs:label rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Hematopoietic_System</rdfs:label><rdfs:subClassOf rdf:resource="http://human.owl#NCI_C41166"/>rdfs:subClassOfowl:Restriction<owl:onProperty rdf:resource="http://human.owl#UNDEFINED_part_of"/><owl:someValuesFrom rdf:resource="http://human.owl#NCI_C41165"/></owl:Restriction></rdfs:subClassOf><oboInOwl:hasRelatedSynonym rdf:resource="http://human.owl#genid7506"/><oboInOwl:hasRelatedSynonym rdf:resource="http://human.owl#genid7507"/><oboInOwl:hasRelatedSynonym rdf:resource="http://human.owl#genid7508"/><oboInOwl:hasRelatedSynonym rdf:resource="http://human.owl#genid7509"/><oboInOwl:hasRelatedSynonym rdf:resource="http://human.owl#genid7510"/><oboInOwl:hasRelatedSynonym rdf:resource="http://human.owl#genid7511"/><oboInOwl:hasRelatedSynonym rdf:resource="http://human.owl#genid7513"/></owl:Class>

As you can see, some of the classes have properties with multiple values, such as hasRelatedSynonym, and SomeValuesFrom. I'm trying to run a query that returns the class, the class label, the super class, the superclass label, and any other hasRelatedSynonym values and SomeValuesFrom values. I would like to use group_concat for the properties with the multiple values, and have tried the following query:

querytrial4=graph.query("""SELECT ?node ?nodeLabel ?superclass ?superclassLabel (group_concat(DISTINCT ?node2) as ?node2s) (group_concat(DISTINCT ?node2Label) as ?node2Labels) where {
?node rdf:type owl:Class .
?node rdfs:subClassOf ?superclass .
OPTIONAL { ?node rdfs:subClassOf ?restriction }
OPTIONAL { ?restriction a owl:Restriction }
OPTIONAL { ?restriction owl:someValuesFrom ?node2 }
?node rdfs:label ?nodeLabel .
?superclass rdfs:label ?superclassLabel .
OPTIONAL { ?node2 rdfs:label ?node2Label }
}
group by ?node ?nodeLabel ?superclass ?superclassLabel ?node2 ?node2Label
LIMIT 10""")

The query seems to execute, but when I try to iterate over its rows and print them, Jupyter Notebook goes on thinking forever and doesn't return anything. Could it be that I've made a mistake and have created some kind of recursive infinite loop? Any help would be much appreciated.



_______________________________________________
rdf4j-dev mailing list
rdf4j-dev@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/rdf4j-dev



Back to the top