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】(15) 構成要素のパラメータへのアクセス

0
Posted at

4.2 構成要素のパラメータへのアクセス

構成要素のパラメータへのアクセスとは、論理演算子のパラメータと設定値、制約のパラメータと設定値を取得、設定することである。

論理演算子としてはANDとORである。このときの設定項目の値はクラスのリストとなる。

制約としては、SOME, ONLY, VALUE, MAX, MIN, そしてEXACTLYがパラメータで、設定値はクラス、インディビデュアル、値である。また基数については整数値も設定値になる。

4.1節のStreptococcusの例では、次のようにアクセスできる。

>>> onto.Streptococcus.equivalent_to[0]
bacteria.Bacterium & bacteria.has_shape.some(bacteria.Round) & bacteria.has_shape.only(bacteria.Round) & bacteria.has_grouping.some(bacteria.InSmallChain) & bacteria.has_grouping.only(Not(bacteria.Isolated)) & bacteria.gram_positive.value(True)
>>> onto.Streptococcus.equivalent_to[0].Classes[1]
bacteria.has_shape.some(bacteria.Round)
>>> onto.Streptococcus.equivalent_to[0].Classes[1].property
bacteria.has_shape
>>> onto.Streptococcus.equivalent_to[0].Classes[1].type
24
>>> onto.Streptococcus.equivalent_to[0].Classes[1].type == SOME
True
>>> onto.Streptococcus.equivalent_to[0].Classes[1].value
bacteria.Round

>>> constructor = onto.Streptococcus.equivalent_to[0]
>>> constructor.__class__
<class 'owlready2.class_construct.And'>
>>> isinstance(constructor, And)
True
>>> constructor.Classes
[bacteria.Bacterium, bacteria.has_shape.some(bacteria.Round), bacteria.has_shape.only(bacteria.Round), bacteria.has_grouping.some(bacteria.InSmallChain), bacteria.has_grouping.only(Not(bacteria.Isolated)), bacteria.gram_positive.value(True)]

Pseudomonasの方は、is_aでクラスのリストが得られるので、各要素を取ってから、調べればよい。

>>> onto.Pseudomonas.is_a
[bacteria.Bacterium, bacteria.has_shape.some(bacteria.Rod), bacteria.has_shape.only(bacteria.Rod), bacteria.has_grouping.some(bacteria.Isolated | bacteria.InPair), bacteria.gram_positive.value(False)]
>>> onto.Pseudomonas.is_a[2]
bacteria.has_shape.only(bacteria.Rod)
>>> onto.Pseudomonas.is_a[2].type
25
>>> onto.Pseudomonas.is_a[2].type == ONLY
True
>>> onto.Pseudomonas.is_a[2].property
bacteria.has_shape
>>> onto.Pseudomonas.is_a[2].value
bacteria.Rod

4.3 クラスのプロティとしての制約

クラスのプロパティとして、存在制約、全称制約、値制約を設定する。これらの制約は関係を持つ対象との間の条件に応じて設定される。
・クラスのすべての個体は、対象のクラスの少なくとも1つの個体と関係がある。これが存在制約である。protégéで"some"とする。
・クラスのすべての個体は対象のクラスの個体とのみ関係がある。これが全称制約である。protégéで"only"とする
・クラスの各個体は対象のクラスのインディビデュアル、またはデータ型の値と関係がある。これが値制約である。protégéでは特に指定されない。protégéではプロパティ設定ダイアログで、とりあえずData restriction creatorやObject restriction creatorでプロパティを指定してsomeなどを使ってプロパティを設定しておいて、そのプロパティを編集する時、Class expression editorを利用して、[プロパティ] value [値、インディビデュアル]と直接書く。また、owlready2でも制約として設定することはない。直接等式で関係づける。

プロパティの属性class_property_typeでは次のリスト型で指定する。
存在制約:["some"]
全称制約:["only"]
存在制約および全称制約:["some"], ["only"]

例を示す。

>>> from owlready2 import *
>>> onto = get_ontology('bacteria.owl').load()
>>> with onto:
...     onto.gram_positive.class_property_type = ["some"]
...     onto.has_shape.class_property_type = ["some"], ["only"]
...     onto.has_grouping.class_property_type = ["some"]
...
>>> with onto:
...     class Pseudomonas2(onto.Bacterium): pass
...     Pseudomonas2.gram_positive = False
...     Pseudomonas2.has_shape = onto.Rod
...     Pseudomonas2.has_grouping = [onto.Isolated | onto.InPair]
...
>>> Pseudomonas2.is_a
[bacteria.Bacterium, bacteria.gram_positive.value(False), bacteria.has_shape.some(bacteria.Rod), bacteria.has_shape.only(bacteria.Rod), bacteria.has_grouping.some(bacteria.Isolated | bacteria.InPair)]

4.4 Equivalentで定義されたクラス(defined class)

Defined classというのは次のように、他のクラスの積集合として定義されるクラスである。

Parent_class1 and Parent_class2 ...
and (Property some Class) ...
and (Property value individual) ...
and (Property only (Class ... or { individual, ...}))

andは集合演算子とみて、Python+owlready2ではequivalent_to属性で表示されるクラスの記述である。OWLではrdfs:subClassOfの並びで記述されることとなる。

Defined classでクラスを定義する場合には、クラス定義の先頭の行(classで始まる行)の次にdefined_class = Trueとすればよい。

例を示す。

>>> with onto:
...     class Corynebacterium(onto.Bacterium):
...         defined_class = True
...         gram_positive = False
...         has_shape = onto.Rod
...         has_grouping = [onto.InCluster]
...
>>> Corynebacterium.equivalent_to
[bacteria.Bacterium & bacteria.gram_positive.value(False) & bacteria.has_shape.some(bacteria.Rod) & bacteria.has_shape.only(bacteria.Rod) & bacteria.has_grouping.some(bacteria.InCluster)]

なお、存在制約、全称制約は4.3節で示した通りである。

またサブクラスはis_a属性で分かるが、親クラスだけとなる。

>>> Corynebacterium.is_a
[bacteria.Bacterium]
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?