0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【owlready2】(19) RDFトリプルとWorldを扱う(owlready2におけるRDFトリプルへのアクセス)

Posted at

5.3.5 owlready2におけるRDFトリプルへのアクセス

owlready2でもRDFのクアッドストアにアクセスする方法をもつが、RDFlibとは異なった特徴がある。一つに、IRIは長い文字列であり、容量をとるということで、storid(store ID)というIRIの省略記号によりデータ管理をしていることである。_abbreviate()と_unabbreviate()メソッドで、IRIとstoridを解釈できる。python+owlready2でのエンティティのオブジェクトからはstorid属性でアクセスできる。以下に例を示す。

>>> from owlready2 import *
>>> onto = get_ontology('bacteria.owl').load()
>>> from rdflib import *
>>> graph = default_world.as_rdflib_graph()
>>> default_world._abbreviate(onto.Staphylococcus.iri)
324
>>> default_world._unabbreviate(324)
'http://lesfleursdunormal.fr/static/_downloads/bacteria.owl#Staphylococcus'
>>> onto.Staphylococcus.storid
324
>>> default_world._get_by_storid(324)
bacteria.Staphylococcus

_get_triples_spod_spod(subject, predicate, object or value, datatype)メソッドはRDFlibのtriples()メソッドと同様の、SVO(SPO)検索のメソッドである。storidを用いた検索である。

>>> default_world._get_triples_spod_spod(324, None, None, None)
[(324, 6, 11, None), (324, 9, 322, None), (324, 33, -20, None)]
>>> default_world._unabbreviate(6)
'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'
>>> default_world._unabbreviate(11)
'http://www.w3.org/2002/07/owl#Class'
>>> default_world._unabbreviate(9)
'http://www.w3.org/2000/01/rdf-schema#subClassOf'
>>> default_world._unabbreviate(322)
'http://lesfleursdunormal.fr/static/_downloads/bacteria.owl#Coccus'
>>> default_world._unabbreviate(33)
'http://www.w3.org/2002/07/owl#equivalentClass'
>>> default_world._unabbreviate(-20)
Traceback (most recent call last):
  File "<python-input-15>", line 1, in <module>
    default_world._unabbreviate(-20)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^
  File "C:\Users\morit\AppData\Local\Programs\Python\Python313\Lib\site-packages\owlready2\triplelite.py", line 367, in _unabbreviate
    return self.execute("SELECT iri FROM resources WHERE storid=? LIMIT 1", (storid,)).fetchone()[0]
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^
TypeError: 'NoneType' object is not subscriptable
>>> list(graph.query('''
... SELECT ?p ?o WHERE{
... <%s> ?p ?o .
... }''' % onto.Staphylococcus.iri))
[(rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('http://www.w3.org/2002/07/owl#Class')), (rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#subClassOf'), rdflib.term.URIRef('http://lesfleursdunormal.fr/static/_downloads/bacteria.owl#Coccus')), (rdflib.term.URIRef('http://www.w3.org/2002/07/owl#equivalentClass'), rdflib.term.BNode('20'))]
>>> default_world._unabbreviate(20)
'http://www.w3.org/2002/07/owl#onDataRange'

storid=324はStaphylococcusであり、これを主語subjectとしたRDFトリプルは(324,6,11), (324,9,322), (324,33.-20)であった。それらstoridは_unabbreviate()メソッドによりIRIはわかるが、storid=-20は負の値であり、エラーとなる。空ノード(無名ノード)は負となるので、これはrdflib.term.BNode('20')を意味する。

>>> list(graph.triples((None, None, None)))

として検索すると、

(rdflib.term.BNode('20'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('http://www.w3.org/2002/07/owl#Class'))
(rdflib.term.BNode('20'), rdflib.term.URIRef('http://www.w3.org/2002/07/owl#intersectionOf'), rdflib.term.BNode('25'))
(rdflib.term.URIRef('http://lesfleursdunormal.fr/static/_downloads/bacteria.owl#Staphylococcus'), rdflib.term.URIRef('http://www.w3.org/2002/07/owl#equivalentClass'), rdflib.term.BNode('20'))

が見つかることから、Staphylococcusクラスの必要十分条件記述のための無名ノードであることがわかる。

_to_rdf()メソッドはエンティティか属性値を指定して、エンティティの(storid,None)の組か値とデータ型の組(値,storid)を返す。逆のメソッドは_to_python()メソッドである。8という値については次のようになる。

>>> default_world._to_rdf(8)
(8, 43)
>>> default_world._to_python(8,43)
8

>>> default_world._unabbreviate(43)
'http://www.w3.org/2001/XMLSchema#integer'

データ型はxsd:integerとわかる。エンティティStaphylococcusについては次のようになる。

>>> default_world._to_rdf(onto.Staphylococcus)
(324, None)
>>> default_world._to_python(323,None)
bacteria.Bacillus

owlready2にはクアッドストアをstoridで操作する多くのメソッドが提供されている。たとえば、次のような、存在の問い合わせ、削除、追加のメソッドがある。

>>> default_world._has_obj_triple_spo(324,6,11)
True
>>> default_world._del_obj_triple_spo(324,6,11)
>>> onto._add_obj_triple_spo(324,6,11)

他にも、_get_triples_s_p()や_get_obj_triples_sp_o()、_get_data_triples_s_pod()など多数用意されている。『Ontologies with Python』にはメソッドの命名規則やメソッド名が示されている。しかし、簡易的にはtriples()、add()、remove()メソッドを使えばよく、またSPARQLも利用できる。storidを利用した処理に特別なメリットが無い場合は、使う必要はないと思われる。

オントロジー言語OWLを使う上で重要なトピックスとして、自動推論(オントロジー(情報)モデリングのチェックや、推論によるデータの検索や生成)があるが、Pythonを利用したモデリングやデータアクセスで直接使うものではないので省いた。推論システムを作りたい場合は必要になるだろう。モデルのチェックならばprotégéの上で推論してチェックすればよいのではなかろうか。

以上で、Python+owlready2によるオントロジーOWLの使い方についての説明を終わることにする。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?