簡體   English   中英

動態創建屬性后如何在owlready2中創建關系?

[英]How to create relations in owlready2 after the dynamic creation of the property?

使用 owlready2 我可以為我的本體創建屬性,如下所示:

with onto:
    class Drug(Thing):
        pass
    class Ingredient(Thing):
        pass

relation_name = "has_for_ingredient"

with onto: 
   NewProperty = types.new_class(relation_name, (ObjectProperty, FunctionalProperty))
   NewProperty.domain = domain
   NewProperty.range = range

我可以使用 list(onto.properties()) 查看它們。 現在我想使用下面的代碼創建關系(在我的實際代碼中這將動態完成):

my_drug = Drug("my_drug")
acetaminophen = Ingredient("acetaminophen")
my_drug.has_for_ingredient = [acetaminophen]

但我得到錯誤:

AttributeError: 'list' object has no attribute 'storid'

我還嘗試僅使用 ObjectProperty 而不使用 FunctionalProperty 創建屬性,但出現錯誤:

AttributeError: 'int' object has no attribute 'namespace'

使用文檔我無法弄清楚問題是什么......而且,使用 FunctionalProperty 的含義是什么?

with onto: 
   NewProperty = types.new_class(relation_name, (ObjectProperty, ))
   NewProperty.domain = [Drug]
   NewProperty.range = [Ingredient]

是通往 go 的路。鑒於:

with onto: 
   NewProperty = types.new_class(relation_name, (ObjectProperty))
   ...

拋出AttributeError: 'list' object has no attribute 'storid'

顯然需要一個元組來動態創建屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM