Accessing ontology class from other method in class

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

Accessing ontology class from other method in class

Hanif
Dear ALL, could anyone help me please.

I created an ontology:

class behaviour:

    onto = get_ontology(
        "C:/Users/bhuiyanh/OneDrive - Queensland University of Technology/Ontology- Protege/av_b.owl").load()

    def __init__(self):
       
        ## Creating Classes and Subclasses

        with self.onto:
            class driving_behaviour(Thing):
                pass

             ........................
             ........................

        with self.onto:
            class timestamp(Thing):
                pass

self.onto.save(file="C:/Users/bhuiyanh/OneDrive - Queensland University of Technology/Ontology- Protege/av_b.owl")

    def create_individual(self):

        #onto = get_ontology("C:/Users/bhuiyanh/OneDrive - Queensland University of Technology/Ontology- Protege/av_b.owl").load()

      timing = self.onto.timestamp("time_1")


**** I want to access timestamp class (behaviour class) from create_individual_method to create an individual, but I am getting error.

Traceback (most recent call last):
  File "C:\Users\bhuiyanh\PycharmProjects\practise02\av_behaviour.py", line 220, in create_individual
    timing = self.onto.timestamp("time_1")
IndexError: list assignment index out of range


Anyone could help me please
Reply | Threaded
Open this post in threaded view
|

Re: Accessing ontology class from other method in class

Jiba
Administrator
Hi,

The error you get (IndexError) is surprising, because there is not indexing on the line mentioned (timing = self.onto.timestamp("time_1")).

I tried to run your code as below, and it runs without trouble. Could you be more precise or send me a full example to reproduce the problem?

Jiba


from owlready2 import *

class behaviour:
    onto = get_ontology("http://test.org/av_b.owl")

    def __init__(self):
        with self.onto:
            class driving_behaviour(Thing):
                pass

        with self.onto:
            class timestamp(Thing):
                pass
             
    def create_individual(self):
      timing = self.onto.timestamp("time_1")
      print(timing)
     
behaviour().create_individual()

Reply | Threaded
Open this post in threaded view
|

Re: Accessing ontology class from other method in class

Hanif
Thank Jiba for your reply,

Actually what I am trying to do: I got an error again,

main.py----------File

time_list = ['time_1', 'time_2']
vehicle_list= ['vehicle-1', 'vehicle-2']

a = behaviour()

a.create_individual(time_list, vehicle_list)   // calling method in behaviour class of av_behaviour file

av_behaviour.py ----------File

class behaviour:

    onto = get_ontology(
        "C:/Users/bhuiyanh/OneDrive - Queensland University of Technology/Ontology- Protege/av_b.owl").load()

    def __init__(self):
       
        ## Creating Classes and Subclasses

        with self.onto:
            class driving_behaviour(Thing):
                pass

             ........................
             ........................

        with self.onto:
            class timestamp(Thing):
                pass

self.onto.save(file="C:/Users/bhuiyanh/OneDrive - Queensland University of Technology/Ontology- Protege/av_b.owl")

     def create_individual(self, time_list1 = [], vehicle_list1 = []):

        #timing = self.onto.timstamp("time_1")

        time_individual = []
        vehicle_individual = []

        self.time_list1 = time_list1
        self.vehicle_list = vehicle_list1

        for i in range(len(time_list1)):
            time_individual[i] = self.onto.timestamp(time_list1[i])

        for i in range(len(time_list1)):
            vehicle_individual[i] = self.onto.timestamp(vehicle_list1[i])


****** This is my code ......I ran it and the error comes,

Traceback (most recent call last):
  File "C:/Users/bhuiyanh/PycharmProjects/practise02/main.py", line 55, in <module>
    a.create_individual(time_list,vehicle_list)
  File "C:\Users\bhuiyanh\PycharmProjects\practise02\av_behaviour.py", line 221, in create_individual
    time_individual[i] = self.onto.timestamp(time_list1[i])
IndexError: list assignment index out of range

I can't understand why this is.


******Even what you wrote in the previous reply I tried to follow your procedure for one individual, it runs without error but no effect in the ontology. time_1 is not added as an individual.

when only this:

    def create_individual(self):

        timing = self.onto.timstamp("time_1")

Reply | Threaded
Open this post in threaded view
|

Re: Accessing ontology class from other method in class

Jiba
Administrator
Hi,

I think the "IndexError: list assignment index out of range" is actually unrelated to Owlready: the problem occurs here:

    time_individual[i] = self.onto.timestamp(time_list1[i])

The list time_individual is empty, thus you cannot assign element i (where i = 0).

You should use time_individual.append() to add at the end of the list.

Jiba