pythonowlontologyowlready

can not get my individuals from my ontology using search method in owlready python


I have an ontology : https://raw.githubusercontent.com/amiraelsayed/lumiere/master/lumiere3.owl

I want to get all lessons for specific class called CS-Java

I have tried to use owlready search method and add to it filtration with lesson name and object properties but always give 0 while it should retrieve about 19 item

This is my individual

<owl:NamedIndividual rdf:about="http://www.smacrs.com/lumiere.owl#CS-Java">
        <rdf:type rdf:resource="http://www.smacrs.com/lumiere.owl#Course"/>
        <contains rdf:resource="http://www.smacrs.com/lumiere.owl#Arrays"/>
        <contains rdf:resource="http://www.smacrs.com/lumiere.owl#Do_..._While"/>
        <contains rdf:resource="http://www.smacrs.com/lumiere.owl#Final"/>
        <contains rdf:resource="http://www.smacrs.com/lumiere.owl#For_Loops"/>
        <contains rdf:resource="http://www.smacrs.com/lumiere.owl#Getting_User_Input"/>
        <contains rdf:resource="http://www.smacrs.com/lumiere.owl#Hello_World_Program"/>
        <contains rdf:resource="http://www.smacrs.com/lumiere.owl#If_conditions"/>
        <contains rdf:resource="http://www.smacrs.com/lumiere.owl#Introduction_and_Installation"/>
        <contains rdf:resource="http://www.smacrs.com/lumiere.owl#Method_Parameters"/>
        <contains rdf:resource="http://www.smacrs.com/lumiere.owl#Methods"/>
        <contains rdf:resource="http://www.smacrs.com/lumiere.owl#Multi-Dimensional_Arrays"/>
        <contains rdf:resource="http://www.smacrs.com/lumiere.owl#Static"/>
        <contains rdf:resource="http://www.smacrs.com/lumiere.owl#String_Builder_and_String_Formatting"/>
        <contains rdf:resource="http://www.smacrs.com/lumiere.owl#Switch"/>
        <contains rdf:resource="http://www.smacrs.com/lumiere.owl#Using_Variables"/>
        <contains rdf:resource="http://www.smacrs.com/lumiere.owl#What_Java_Is_and_How_It_Works"/>
        <contains rdf:resource="http://www.smacrs.com/lumiere.owl#While_Loops"/>
        <contains rdf:resource="http://www.smacrs.com/lumiere.owl#Strings:_Working_With_Text"/>
        <Code rdf:datatype="http://www.w3.org/2001/XMLSchema#string">CS102</Code>
        <Description rdf:datatype="http://www.w3.org/2000/01/rdf-schema#Literal">This course of study builds on the skills gained by students in Java Fundamentals or Java Foundations to help advance Java programming skills. Students will design object-oriented applications with Java and will create Java programs using hands-on, engaging activities.</Description>
        <Name rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Introduction to Java Programming Language</Name>
    </owl:NamedIndividual>

this is the class and it is contain list of lesson individuals

onto.search(part_of="*") it brings all lessons in all courses and when I used onto.search(part_of="CS-Java") it return 0 while I need it to return only lessons in this course


Solution

  • I guess your search returns 0 result because the search method can only be used to perform Full Text Search on the content of the annotations.

    It seems that what you need is to perform a query against the knowledge contained in your ontology, so maybe using SPARQL could be an idea ?

    Owlready offers a way to perform SPARQL queries on an ontology, so you could express something like "Select all the things that CS-Java contains" :

    # convert the ontology to a RDFLib graph
    graph = onto.world.as_rdflib_graph()
    
    # perform the query
    result = list(graph.query_owlready("""
    prefix : <http://www.smacrs.com/lumiere.owl#>
    SELECT DISTINCT ?c WHERE {
      :CS-Java :contains ?c .
    }"""))
    

    The other way the go could simply be to read the content of the contains property on your CS-Java class :

    onto['CS-Java'].contains
    

    Both methods returns a list a Owlready2 objects (18 in your example case).