如何使用 owlready2 向现有类添加新实例

问题描述 投票:0回答:0

我一直在尝试制作一种将新实例添加到现有类的方法,但我在网上找到的所有内容都涉及在添加实例之前动态创建新类。我该怎么做?

我一直在尝试像下面的代码这样的事情,我希望从超类 Thing() 创建一个我认为不会不正确的实例,然后将它添加到预期的类中:

def add_instance(self, instance_iri: str, class_iri: str): #获取类对象 class_obj = self._ontology.search(iri = class_iri)[0]

    # Adds the instance to the ontology
    instance = Thing()
    instance.iri = instance_iri

    # Adds the instance to the class
    instance.is_a.append(class_obj)

    # Saves the modified ontology
    self._ontology.save("test_onto.owl")
python ontology owlready
© www.soinside.com 2019 - 2024. All rights reserved.