我在 desctop Protege 中创建了本体。它由两个类组成 - test1 和 test2。然后我为他们声明了标签。第一个没有数据类型的标签,第二个 - 文字。OWL 文件内容如下。差异是字符串 'rdf:datatype="&rdfs;Literal"' 的存在尝试通过 owlready2 获取标签时,出现错误“ValueError:无法读取数据类型 305 的文字!” 用于文字类型标签。
这是 OWL 文件的内容:
<?xml version="1.0"?>
<!DOCTYPE rdf:RDF [
<!ENTITY owl "http://www.w3.org/2002/07/owl#" >
<!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >
<!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" >
<!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
]>
<rdf:RDF xmlns="http://www.semanticweb.org/jdrey/ontologies/2020/11/test#"
xml:base="http://www.semanticweb.org/jdrey/ontologies/2020/11/test"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
<owl:Ontology rdf:about="http://www.semanticweb.org/jdrey/ontologies/2020/11/test"/>
<!--
///////////////////////////////////////////////////////////////////////////////////////
//
// Classes
//
///////////////////////////////////////////////////////////////////////////////////////
-->
<!-- http://www.semanticweb.org/jdrey/ontologies/2020/11/test:test1 -->
<owl:Class rdf:about="http://www.semanticweb.org/jdrey/ontologies/2020/11/test:test1">
<rdfs:label>label_test1</rdfs:label>
</owl:Class>
<!-- http://www.semanticweb.org/jdrey/ontologies/2020/11/test:test2 -->
<owl:Class rdf:about="http://www.semanticweb.org/jdrey/ontologies/2020/11/test:test2">
<rdfs:label rdf:datatype="&rdfs;Literal">label_test2</rdfs:label>
</owl:Class>
</rdf:RDF>
这是打印子类标签的代码:
from owlready2 import *
owlready2.JAVA_EXE = r'c:\Users\JDrey\AppData\Local\JetBrains\PyCharm Community Edition with Anaconda plugin 2020.1.2\jbr\bin\java.exe'
my_world = World()
onto = my_world.get_ontology(r'd:\temp\OWL\test_owl.owl').load()
for elem in list(Thing.subclasses(world = my_world)):
print(elem.label)
这是带有错误消息的输出:
['label_test1']
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-6-5e6c14d62ea5> in <module>
5 onto = my_world.get_ontology(r'd:\temp\OWL\test_owl.owl').load()
6 for elem in list(Thing.subclasses(world = my_world)):
----> 7 print(elem.label)
D:\anaconda\envs\new_env\lib\site-packages\owlready2\entity.py in __getattr__(Class, attr)
561 values = Class.__dict__.get(attr)
562 if values is None:
--> 563 values = Prop._get_values_for_class(Class)
564 type.__setattr__(Class, attr, values)
565 return values
D:\anaconda\envs\new_env\lib\site-packages\owlready2\prop.py in _get_values_for_individual(Prop, entity)
309 def _get_values_for_individual(Prop, entity):
310 return IndividualValueList((entity.namespace.ontology._to_python(o, d) for o, d in entity.namespace.world._get_triples_sp_od(entity.storid, Prop.storid)),
--> 311 entity, Prop)
312
313 _get_value_for_class = _get_value_for_individual
D:\anaconda\envs\new_env\lib\site-packages\owlready2\prop.py in __init__(self, l, obj, Prop)
1045 __slots__ = ["_Prop"]
1046 def __init__(self, l, obj, Prop):
-> 1047 list.__init__(self, l)
1048 self._obj = obj
1049 self._Prop = Prop
D:\anaconda\envs\new_env\lib\site-packages\owlready2\prop.py in <genexpr>(.0)
308
309 def _get_values_for_individual(Prop, entity):
--> 310 return IndividualValueList((entity.namespace.ontology._to_python(o, d) for o, d in entity.namespace.world._get_triples_sp_od(entity.storid, Prop.storid)),
311 entity, Prop)
312
D:\anaconda\envs\new_env\lib\site-packages\owlready2\namespace.py in _to_python(self, o, d, main_type, main_onto, default_to_none)
260 if o in _universal_abbrev_2_datatype: return _universal_abbrev_2_datatype[o]
261 else: return self.world._get_by_storid(o, None, main_type, main_onto, None, default_to_none)
--> 262 else: return from_literal(o, d)
263 raise ValueError
264
D:\anaconda\envs\new_env\lib\site-packages\owlready2\base.py in from_literal(o, d)
56 if d == 0: return o
57 datatype, parser = _universal_abbrev_2_datatype_parser.get(d) or (None, None)
---> 58 if parser is None: raise ValueError("Cannot read literal of datatype %s!" % repr(d))
59 return parser(o)
60
ValueError: Cannot read literal of datatype 305!
如何获取 test2 的标签值?