3.4 エンティティを修正する
エンティティの修正ではwith構文は必要ない。
>>> my_bacterium.gram_positive=True
>>> my_bacterium.has_shape=Rod()
>>> my_bacterium.has_shape
onto.rod2
owlready2のプロパティのレンジは、プロパティが関数プロパティ(functional)なら値となるが、そうでないなら複数の値を持つリストとなる。このリストはPythonのリストクラスではない。
>>> my_bacterium.has_grouping.__class__
<class 'owlready2.prop.IndividualValueList'>
>>> [].__class__
<class 'list'>
メソッドはappend()、insert()、remove()となる。
>>> my_bacterium.has_grouping=[Isolated()]
>>> my_bacterium.has_grouping.append(InPair())
>>> my_bacterium.has_grouping
[onto.isolated2, onto.inpair1]
3.5 名前空間でエンティティを作る
オントロジーの下で、つまりwith [ontology]: としてエンティティを作るのだが、これはオントロジーの基底となっている名前空間のもとでエンティティを作っているということだ。名前空間の下で、つまりwith [namespace]: としてもよい。名前空間はget_namespaceメソッドで作成・設定するのだった。
>>> obo = onto.get_namespace('http://purl.obolibrary.org/obo/')
>>> with obo:
... class OBOBacterium(Thing): pass
...
>>> OBOBacterium
obo.OBOBacterium
>>> OBOBacterium.iri
'http://purl.obolibrary.org/obo/OBOBacterium'
>>> with obo:
... my_bacterium = OBOBacterium('my_bacterium')
...
>>> my_bacterium.iri
'http://purl.obolibrary.org/obo/my_bacterium'
ここでエンティティを確認してみる。
>>> onto.search(iri='*')
[.anonymous, test.org.onto.owl, onto.Bacterium, ..., obo.my_bacterium]
my_bacteriumはobo.my_bacteriumであるとわかる。ここで、my_bacteriumの名前空間を変えるには、文字列でIRIを変えてしまうことである。
>>> my_bacterium.iri
'http://purl.obolibrary.org/obo/my_bacterium'
>>> my_bacterium.iri = 'http://test.org/other_onto.owl#bacterium1'
>>> my_bacterium.iri
'http://test.org/other_onto.owl#bacterium1'
エンティティを確認してみると、名前空間はother_ontoになっている。
>>> onto.search(iri='*')
[.anonymous, test.org.onto.owl, onto.Bacterium, ..., other_onto.bacterium1]
other_ontoの名前空間のエンティティにアクセスするためにはget_namespaceを利用する。
>>> get_namespace('http://test.org/other_onto.owl').bacterium1
other_onto.bacterium1
>>> get_namespace('http://test.org/other_onto.owl').bacterium1.name
'bacterium1'