Arrange classes and subclasses

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

Arrange classes and subclasses

LouisS
Hello,

From an unknown ontology or any ontology, I try to read and make a hierarchy with for each class, find and structure its subclasses.  

Being a beginner on ontology notions, I have read up on how to apply the different methods but I only managed to get the desired results on 2 branches as follows:
[{'Class': 'test1', 'Subclass': [{'Class': 'result_test1'}, {'Class': 'result2_test1'}, {'Class': 'result3_test1'}, ]}, {'Class': 'test2', 'Subclass': [{'Class': 'result_result_test2', 'Subclass': [result2_result_test2, result_test2,result3_result_test2]}]}

I would like to be able to get the classes and subclasses for N-dimensional array in the same way, I thought about creating a recursive function but I get stuck on the results.

I made the following code without recursion:

listEmpty = []
listThing = list(Thing.subclasses())

  for i in list(listThing ):
        listTemp = []
        varList = list(i.subclasses())
        if varList:
            listTemp2 = []
            for o in list(varList):
                varList2 = list(o.subclasses())
                if varList2:
                    listTemp2.append({"Class": o.name, "Subclass": varList2})
                else:
                    listTemp2.append({"Class": o.name})
            listTemp.append({"Class": i.name, "Subclass": listTemp2})
        else:
            listTemp.append({"Class": i.name})
        listEmpty.append((listTemp[0]))


If you have any ideas on how I could find the solution and solve this problem in this way or by another method I am open.

Thank you very much in advance
Reply | Threaded
Open this post in threaded view
|

Re: Arrange classes and subclasses

Jiba
Administrator
Hi,

If you want to deal with hierarchical structures of any depth, the easiest way is to use a recursive function. How to use recursive functions in Python is beyond the topic of this forum, but I think you could find answer in general Python programming websites and forums.

Jiba