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】(10)Pythonでオントロジーを作成(多重の定義と前方宣言から)

0
Posted at

3.6 多重の定義と前方宣言

異なる変数に同じIRIのエンティティを生成しても、そのエンティティは同じとすることである。

>>> with onto:
...     bacterium_a = Bacterium('the_bacterium')
...     bacterium_b = Bacterium('the_bacterium', gram_positive=False)
...
>>> bacterium_a == bacterium_b
True
>>> bacterium_a is bacterium_b
True
>>> bacterium_a.iri
'http://test.org/onto.owl#the_bacterium'
>>> bacterium_b.iri
'http://test.org/onto.owl#the_bacterium'
>>> onto.search(iri='*')
[.anonymous, test.org.onto.owl, onto.Bacterium, ..., onto.the_bacterium]

特に複数のエンティティが生成されている様子はない。
前方宣言とは、関数・メソッドなど利用するときに、その利用よりも先に定義がないといけないということである。Pythonは、コードを上から順に実行するので、関数が呼び出される前にその関数が定義されていることである。

>>> with onto:
...     class Bacterium(Thing): pass
...     class Shape(Thing): pass
...     class has_shape(Bacterium >> Shape): pass
...     class Bacterium(Thing):
...         has_shape = Shape

この例では、Bacteriumのクラス定義をhas_shapeのプロパティ定義の前にしておくことで、has_shapeのドメインに利用可能となっている。また、Bacteriumの処理を記述するためにShapeの定義の後でBacteriumの定義を再度行っているというものになる。

※ただし、実行の時点で定義されていればよいのであって、関数定義の順番を問うものではない。例えば、次はfun2()の利用を記述するよりも後にdef func2():と定義されている。

>>> def fun1(): fun2()
...
>>> def fun2(): print("hello!")
...
>>> fun1()
hello!

3.7 エンティティの削除

インディビデュアルを次のように作成する。

>>> t1 = onto.Bacterium()
>>> t2 = onto.Bacterium()
>>> onto.search(iri='*')
[.anonymous, ..., bacteria.unknown_bacterium, bacteria.bacterium1, bacteria.bacterium2]

これらのインディビデュアルを削除するにはdestroy_entityを用いる。

>>> destroy_entity(t1)
>>> onto.search(iri='*')
[.anonymous, ..., bacteria.unknown_bacterium, bacteria.bacterium2]

クラス、プロパティも同様である。

>>> from owlready2 import *
>>> onto_tmp = get_ontology('http://tmp.org/tmp.owl')
>>>with onto_tmp:
...  class tmp1(Thing): pass
...  class tmp2(Thing): pass
...  class prop1(tmp1 >> tmp2): pass
...  class prop2(tmp2 >> tmp1): pass

>>> onto_tmp.search(iri='*')
[.anonymous, tmp.org.tmp.owl, tmp.tmp1, tmp.tmp2, tmp.prop1, tmp.prop2]

>>> destroy_entity(onto_tmp.tmp1)
>>> destroy_entity(onto_tmp.prop1)
>>> onto_tmp.search(iri='*')
[.anonymous, tmp.org.tmp.owl, tmp.tmp2, tmp.prop2]
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?