使用Owlready2,如何定义附加到本体的类(在这个基础iri上:http://example.com/ontology/item/1.1#)当一个个体被创建时,它会被定义在其他iri(喜欢:http://example.com/profile/item/resource/)?
所以如果我实例化一个定义的类Stuff继承owlready2.Thing:
from owlready2 import *
from rdflib import URIRef
onto = get_ontology('http://example.com/ontology/item/1.1#')
class Stuff(Thing):
namespace = onto
stuff = Stuff('http://example.com/profile/item/resource/8gdfb186-fc78-4b9e-95c4-545339d3ce1b')
然后我将本体保存到一个新文件中,它会有类似的东西:
<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xml:base="http://www.perfect-memory.com/ontology/stuff/1.1"
xmlns="http://www.perfect-memory.com/ontology/stuff/1.1#">
<owl:Ontology rdf:about="http://example.com/ontology/item/1.1"/>
<owl:Class rdf:about="http://example.com/ontology/item/1.1#Stuff">
<rdfs:subClassOf rdf:resource="http://www.w3.org/2002/07/owl#Thing"/>
</owl:Class>
<Stuff rdf:about="http://example.com/profile/item/resource/8gdfb186-fc78-4b9e-95c4-545339d3ce1b">
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
</Stuff>
</rdf:RDF>
我怎样才能做到这一点?
我在实例化的时候尝试过重载
__new__
方法,没有成功:
from owlready2 import *
from rdflib import URIRef
class Stuff(Thing):
namespace = onto
def __new__(Class, name=None, namespace=None, is_a=None, **kwargs):
if name and isinstance(name, URIRef):
splitted = str(name).rsplit('/', 1)
if len(splitted) == 2:
new_namespace = onto.get_namespace(f'{splitted[0]}/')
obj = Thing.__new__(
Class,
name=splitted[1],
namespace=new_namespace,
is_a=is_a,
**kwargs
)
obj.namespace = new_namespace
obj.namespace.ontology._base_iri = f'{splitted[0]}/'
obj.set_name(f'{splitted[1]}')
return obj
obj = Thing.__new__(
Class,
name=name,
namespace=namespace,
is_a=is_a,
**kwargs
)
return obj
新的个人 iri 变成:http://example.com/profile/item/resource/http://example.com/profile/item/resource/8gdfb186-fc78-4b9e-95c4-545339d3ce1b - 当然是这样不是我要找的。
有什么想法吗?
PS :由于 owlready2 库实际上正在查看所有父类以生成当前 owlready2 映射类的祖先,因此不可能对 mixins 进行一些继承。