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】(3)エンティティへのアクセス

Posted at

2.5 エンティティへのアクセス

バクテリアオントロジーbacteria.owlをget_ontology関数によってオブジェクトontoとしたのだった。これを入口として、バクテリアオントロジーの各クラス、インディビデュアル(個物)にアクセスする。

バクテリアオントロジーにはunknown_bacteriumというインディビデュアルが設定されている。このIRIは

"http://lesfleursdunormal.fr/static/_downloads/bacteria.owl#unknown_bacterium"

である。これをonto.unknown_bacteriumとしてPythonでは扱う。

>>> from owlready2 import *
>>> onto = get_ontology('http://lesfleursdunormal.fr/static/_downloads/bacteria.owl').load()
>>> onto.unknown_bacterium
bacteria.unknown_bacterium

IRIの参照部分、つまりアドレスのような意味合いを持つシャープ(#)の前の部分のことであるが、これはontoが表していると考えてもよい。それにドット(.)でunknown_bacteriumをつないでいる、という見方である。シャープまでのところはonto.base_iriとして得られる。

>>> onto.base_iri
'http://lesfleursdunormal.fr/static/_downloads/bacteria.owl#'

また、ontoにおけるクラス、インディビデュアルは辞書のようにして名前(文字列)としてアクセスできる。

>>> onto['unknown_bacterium']
bacteria.unknown_bacterium

なお、onto自体は次のようなオブジェクトとして示される。

>>> onto
get_ontology("http://lesfleursdunormal.fr/static/_downloads/bacteria.owl#")

unknown_bacteriumのIRIや名前は次のようにすれば取得できる。

>>> onto.unknown_bacterium.iri
'http://lesfleursdunormal.fr/static/_downloads/bacteria.owl#unknown_bacterium'
>>> onto.unknown_bacterium.name
'unknown_bacterium'

バクテリアオントロジーにはBacteriumクラスが定義されている。このIRIや名前の取得も同様である。

以下、インディビデュアル、インディビデュアル同士の関係(relation、『Ontologies with Python』ではプロパティをクラスとインディビデュアルで分けている)、クラス、プロパティの別に示す。

2.5.1 インディビデュアル

インディビデュアルとクラスの関係、すなわち

Individual is_instance_of Class.

という関係は次のように確認(判定)する。

>>> isinstance(onto.unknown_bacterium, onto.Bacterium)
True

Python+owlready2では__class__という属性でクラスを取得できる。

>>> onto.unknown_bacterium.__class__
bacteria.Bacterium

is_aという属性でクラスを取得できる。これはリストを返す。制約や論理生成子(logical constructor)も含まれる。

>>> onto.unknown_bacterium.is_a
[bacteria.Bacterium]

同値の関係にあるインディビデュアル、例えば、

aa same_as x.

という関係は、equivalent_to属性を使う。例えば、

>>> t002 = get_ontology('t002.rdf').load()
>>> t002.aa.equivanent_to
[t002.x]

である。ここで、次のような関係が定義されている。

AA subclass_of owl:thing.
aa is_a AA.
x is_a owl:thing.
aa same_as x.

2.5.2 インディビデュアルの関係

属性(attribute、アトリビュート)もドット(.)で取れる。例えば、

>>> onto.unknown_bacterium.gram_positive
True
>>> onto.unknown_bacterium.has_shape
bacteria.round1
>>> onto.unknown_bacterium.has_grouping
[bacteria.in_cluster1]

である。3つめのhas_groupingは分類された値(列挙)なのだが、クラスで定義されており、その意味でプロパティとも受け取れる。つまりオブジェクトプロパティもドット(.)で取れるということである。

in_cluster1がグループとしているオブジェクトは、逆関数(inverse property)のis_grouping_ofで取れる。

>>> onto.in_cluster1.is_grouping_of
[bacteria.unknown_bacterium]

get_propertiesメソッドを使えば、そのインディビデュアルがもつ関係の、つまりプロパティの一覧を取得できる。

>>> list(onto.unknown_bacterium.get_properties())
[bacteria.gram_positive, bacteria.has_grouping, bacteria.nb_colonies, bacteria.has_shape]

また逆関数の、元の関数とその定義域の組、すなわち(subject, property)の組を取得できる。round1というバクテリアの形状を定義したクラスがあるが、これはunknown_bacteriumの形の属性の分類となる。

>>> list(onto.round1.get_inverse_properties())
[(bacteria.unknown_bacterium, bacteria.has_shape)]

OWLではアトリビュートもオブジェクトプロパティもプロパティである。プロパティはその値域が値(データ)かオブジェクト(クラスやインディビデュアル)かでデータプロパティもしくはオブジェクトプロパティに別れるのみである。したがって、インディビデュアルの関係(relation)といっても、それはOWLとしてはプロパティのことである。

OWLでのデータプロパティの値域(XMLのデータ型として定義される)とPython+owlreadyでのデータ型との関係を次の表に示す。

表:OWLとPython+owlready2のデータ型の対応
OWLdatatype.png

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?