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】(11)Pythonでオントロジーを作成(オントロジーを削除するから)

Last updated at Posted at 2025-12-06

3.8 オントロジーを削除する

前節(3.7節)の引き続きであるが、ここでtmp_onto=get_ontology('http://tmp.org/tmp.wol') の内容を一気に削除するには、destroyメソッドを用いる。

>>> onto_tmp.destroy()
>>> onto_tmp.search(iri='*')
[.anonymous]
>>> type(onto_tmp)
<class 'owlready2.namespace.Ontology'>

ただし、onto_tmpというオントロジークラスのオブジェクト(インスタンス)がなくなったわけではない。引き続きオントロジーとして利用できる。

>>> with onto_tmp:
...   class tmp10(Thing): pass
...   class prop10(tmp10 >> bool): pass
...
>>> onto_tmp.search(iri='*')
[.anonymous, tmp.tmp10, tmp.prop10]

3.9 オントロジーを保存する

onto.save(filename)

とすればよい。filenameは文字列である。
Python+owlready2でオントロジーを作って保存する例である。

>>> 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.save('tmp.rdf')

tmp.rdfは以下のとおりである。

<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
         xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
         xmlns:owl="http://www.w3.org/2002/07/owl#"
         xml:base="http://tmp.org/tmp.owl"
         xmlns="http://tmp.org/tmp.owl#">

<owl:Ontology rdf:about="http://tmp.org/tmp.owl"/>

<owl:ObjectProperty rdf:about="#prop1">
  <rdfs:domain rdf:resource="#tmp1"/>
  <rdfs:range rdf:resource="#tmp2"/>
</owl:ObjectProperty>

<owl:ObjectProperty rdf:about="#prop2">
  <rdfs:domain rdf:resource="#tmp2"/>
  <rdfs:range rdf:resource="#tmp1"/>
</owl:ObjectProperty>

<owl:Class rdf:about="#tmp1">
  <rdfs:subClassOf rdf:resource="http://www.w3.org/2002/07/owl#Thing"/>
</owl:Class>

<owl:Class rdf:about="#tmp2">
  <rdfs:subClassOf rdf:resource="http://www.w3.org/2002/07/owl#Thing"/>
</owl:Class>

</rdf:RDF>

saveのオプションにはformatがあり、デフォルトはformat="rdfxml"でありRDF/XLM形式を意味する。ほかにタートルファイル形式(N-Triple)がありforamt="ntriples"とする。

>>> onto_tmp.save('tmp.ttl', format='ntriples')

tmp.ttlの内容は以下の通りである。

<http://tmp.org/tmp.owl> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Ontology> .
<http://tmp.org/tmp.owl#tmp1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class> .
<http://tmp.org/tmp.owl#tmp1> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://www.w3.org/2002/07/owl#Thing> .
<http://tmp.org/tmp.owl#tmp2> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class> .
<http://tmp.org/tmp.owl#tmp2> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://www.w3.org/2002/07/owl#Thing> .
<http://tmp.org/tmp.owl#prop1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://tmp.org/tmp.owl#prop1> <http://www.w3.org/2000/01/rdf-schema#domain> <http://tmp.org/tmp.owl#tmp1> .
<http://tmp.org/tmp.owl#prop1> <http://www.w3.org/2000/01/rdf-schema#range> <http://tmp.org/tmp.owl#tmp2> .
<http://tmp.org/tmp.owl#prop2> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://tmp.org/tmp.owl#prop2> <http://www.w3.org/2000/01/rdf-schema#domain> <http://tmp.org/tmp.owl#tmp2> .
<http://tmp.org/tmp.owl#prop2> <http://www.w3.org/2000/01/rdf-schema#range> <http://tmp.org/tmp.owl#tmp1> .

3.10 オントロジーのインポート

Python+owlready2では、Python環境で処理しているときは、get_ontology([オントロジー]).load()でオントロジーを読み込めば、そのままオントロジーを利用できる。ここでのインポートはRDFファイルにインポートしているオントロジーを追記する方法である。

例えば前節(3.10節)でRDFファイルを出力した後に、bacteria.owlを読み込んだとしよう。そして、onto_tmpにインポートの設定をして、ファイル出力してみよう。

>>> onto_bac = get_ontology('bacteria.owl').load()
>>> onto_tmp.imported_ontologies.append(onto_bac)
>>> onto_tmp.save('tmp2.rdf')

tmp2.rdfのowl:Ontologyの記述には次のようにbacteria.owlのインポートが加えられる。

<owl:Ontology rdf:about="http://tmp.org/tmp.owl">
  <owl:imports rdf:resource="http://lesfleursdunormal.fr/static/_downloads/bacteria.owl"/>
</owl:Ontology>
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?