I have created an ontology which has Axioms i.e. classes, properties, and Swrl rules incoporated using Protege editor. When I run the reasoner Pallet (Incremental), it infer the implicit facts based on the SWRL rules defined in my ontology. For example considering the below examplary hierarchy of my ontology, I have added a SWRL rules which says that: If car1 (an Individual of Class Car) hasSpeed 0.0, It infers Car1 into the subclass (Stopped). This logic is working fine in the Protege.
Car (SuperClass):
CarModel (Subclass of Car)
CarStatus(Subclass of Car):
Stopped (SubClass of CarStatus)
Driving (Subclass of CarStatus)
Now I want to make a Python based user interface for my ontology application with few widgets to manipulate the ontology and show reasoning results. I have imported the owl file of my ontology in python and using OwlReady2 package and reasoner(Hermit/Pallet). But the issue is I donot know how to perform above such type of reasoning in python. Like I tried to create method to infer individuals in the deepest subclass but not able to infer them, instead showing individuals in repeated manner or haphhazard manner but not in inferred classes.So how to work with reasoner in python to provide reasoning results just like Protege does. Is it required to write methods for what I wish inferred results I wish to see?. Here is the python code of the method to classify individuals.
I have tried 2 methods in function, one to classify individuals and other to display results which is called in one of the pushButton of my application.
def classify_individuals(self):
# Classify individuals into subclasses based on SWRL rules
self.classified_individuals = {}
for cls in self.ontology.classes():
for individual in cls.instances():
for subclass in individual.is_a:
if subclass not in self.classified_individuals:
self.classified_individuals[subclass] = []
self.classified_individuals[subclass].append(individual)
def display_results(self):
# Display the results in the QListWidget
self.run_reasoner()
self.classify_individuals()
self.listWidget.clear()
for subclass, individuals in self.classified_individuals.items():
subclass_item = QListWidgetItem(f"Subclass: {subclass.name}")
self.listWidget.addItem(subclass_item)
for individual in individuals:
individual_item = QListWidgetItem(f"\tIndividual: {individual.name}")
self.listWidget.addItem(individual_item)