【问题标题】:How to use .instances() method to iterate through all Instances of owl:Thing in owlready2?如何使用 .instances() 方法遍历 owl:Thing 中的所有 owlready2 实例?
【发布时间】:2021-11-29 00:52:50
【问题描述】:

我可以通过使用以下代码来遍历 owlready2 中类的所有实例,该代码运行良好:

>>> for i in Drug.instances(): print(i)

但我需要遍历本体的所有实例(通过 owl:Thing)。我尝试了以下方法,但它不起作用:

>>> for i in Thing.instances(): print(i)
>>> for i in onto.Thing.instances(): print(i)
>>> for i in Thing.subclasses.instances(): print(i)

【问题讨论】:

  • 为了简化问题的回答,您可以更准确地了解“但它不起作用”的含义。

标签: python ontology semantic-web owlready


【解决方案1】:

我认为你只会引用你的本体而不是你的类。

onto = get_ontology("test.owl")
for i in onto.instances():
    print(i)

您正在做的是获取类并迭代特定于类的实例。

【讨论】:

    【解决方案2】:

    这对我有用:

    from owlready2 import *
    
    onto = get_ontology("http://test.org/onto.owl")
    
    with onto:
    
        class Person(Thing):
            pass
        
        class Building(Thing):
            pass
        
    Person("Anna")
    Person("Bella")
    Person("Cercei")
    Person("Dora")
    
    Building("Eiffel_Tower")
    
    print(list(Thing.instances()))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-25
      • 2011-06-06
      • 1970-01-01
      • 1970-01-01
      • 2018-08-17
      • 2010-11-17
      • 1970-01-01
      相关资源
      最近更新 更多