尽管我没有广泛使用 owlready2 的推理器功能,但我相信这与使用 owlready2 的任何本体更新相同。
基本上在 owlready2 中,要分离不同的本体或同一本体的不同版本(可能使用不同的命名空间),您需要将它们放在不同的“世界”中。语法在此处描述。
这是一些基于文档示例的代码,可让您了解语法
from owlready2 import *
world = World()
onto = world.get_ontology("http://test.org/onto.owl")
with onto:
class Drug(Thing):
pass
class ActivePrinciple(Thing):
pass
class has_for_active_principle(Drug >> ActivePrinciple):
pass
class someActivePrinciple(ActivePrinciple):
pass
class MyDrug(Drug):
has_for_active_principle = [someActivePrinciple] #this one has some property restriction
# let's separate the worlds
world2 = World()
onto2 = world2.get_ontology("http://test.org/onto.owl")
with onto2:
class Drug(Thing):
pass
class ActivePrinciple(Thing):
pass
class has_for_active_principle(Drug >> ActivePrinciple):
pass
class someActivePrinciple(ActivePrinciple):
pass
class MyDrug(Thing): # not a subClass of Drug
pass # missing the has_for_active_principle restriction
# now we can save without mixing the ontologies
onto.save(file=r"c:\temp\owlready.rdf", format="rdfxml")
onto2.save(file=r"c:\temp\owlready2.rdf", format="rdfxml")
请注意,目前存在一个无法直接保存“世界”的错误,只能保存本体,但该错误已在开发版本中得到纠正。见owlready论坛相关讨论