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】(16) RDFトリプルとWorldを扱う

0
Posted at

5.RDFトリプルとWorldを扱う

Owlready2はすべてのRDFトリプルを蓄積するWorldオブジェクト(インスタンス)を持つ。owlready2は永続的記憶としてRDBのsqlite3を利用しており、このRDBの単位でWorldのインスタンスがある。Pythonでowlready2をインポートすれば、default_worldというデフォルトのWorldを扱える。

Worldでは直接にRDFトリプルを扱うことになる。そのためにRDFlibというパッケージをインポートする。またRDFlibを利用すればSPARQLというRDFグラフを検索するための検索言語(query language)を利用できる。

5.1 RDFトリプル

RDF(resource description framework)は、セマンティック・ウェブの概念に基づく、形式的なリソースの記述のためのグラフ(ネットワーク)モデルである。1つのエッジと両端のノードをもって、主語S、述語P、目的語Oとし、これが1つのRDFトリプルである。

例として、バクテリアオントロジーをPython+owlready2で読み込んで、グラフを表示させる。

>>> from owlready2 import *
>>> onto = get_ontology('bacteria.owl').load()
>>> onto.graph.dump()
<http://lesfleursdunormal.fr/static/_downloads/bacteria.owl> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Ontology> .
<http://lesfleursdunormal.fr/static/_downloads/bacteria.owl#has_shape> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://lesfleursdunormal.fr/static/_downloads/bacteria.owl#has_shape> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#FunctionalProperty> .
<http://lesfleursdunormal.fr/static/_downloads/bacteria.owl#has_shape> <http://www.w3.org/2000/01/rdf-schema#domain> <http://lesfleursdunormal.fr/static/_downloads/bacteria.owl#Bacterium> .
~~~

一番初めのRDFトリプルは、

http://lesfleursdunormal.fr/static/_downloads/bacteria.owlはowl:Ontologyのrdfs:typeである。

ということである。

owlready2は、オントロジー記述の保持のために内部にsqlite3というRDB (relational database)を用いている。そのデータベースのことquadstore(クアッドストア)と呼ぶが、これはRDFトリプルにオントロジー(1つのグラフ構造)の情報を加えた4つ組(quad)をレコードとしてデータを管理・操作するためである。

従って、上の例では、このバクテリアオントロジーbacteria.owlがオントロジーである。またRDFトリプルをデータとするので、分岐のある階層的な構造については中間に無名のノードを利用することとなる。

5.2 RDFlibを使う

5.2.1 RDFトリプルを読む

RDFlibもオントロジーを扱うために、Pythonでインポートして利用できるパッケージである。RDFlibとowlready2はクアッドストアが異なるのだが、owlready2のas_rdflib_graph()メソッドにより、RDFlibで利用可能なクアッドストア/グラフにすることができる。

>>> from rdflib import *
>>> graph = default_world.as_rdblib_graph()

>>> graph.triples((URIRef('http://lesfleursdunormal.fr/static/_downloads/bacteria.owl#Staphylococcus'), None, None))
<generator object Graph.triples at 0x0000021E517D4370>
>>> list(graph.triples((URIRef('http://lesfleursdunormal.fr/static/_downloads/bacteria.owl#Staphylococcus'), None, None\)))
[(rdflib.term.URIRef('http://lesfleursdunormal.fr/static/_downloads/bacteria.owl#Staphylococcus'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('http://www.w3.org/2002/07/owl#Class')), 
(rdflib.term.URIRef('http://lesfleursdunormal.fr/static/_downloads/bacteria.owl#Staphylococcus'), rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#subClassOf'), rdflib.term.URIRef('http://lesfleursdunormal.fr/static/_downloads/bacteria.owl#Coccus')), 
(rdflib.term.URIRef('http://lesfleursdunormal.fr/static/_downloads/bacteria.owl#Staphylococcus'), rdflib.term.URIRef('http://www.w3.org/2002/07/owl#equivalentClass'), rdflib.term.BNode('20'))]

上の例では、Staphylococcusを主語とするRDFを検索したものだ。RDFlibではIRIを表すためにURIRef([IRI文字列])とする。またBNode('番号')は無名ノード(空ノード、blank node)である。BNode('20')とは無名ノード_:20のことである。5.1節でとったRDFトリプルからこれに関係するものを取り出してみると、次であった。

_:25 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> _:26 .
_:26 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> _:21 .
_:26 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> _:27 .
_:27 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> _:22 .
_:27 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> _:28 .
_:28 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> _:23 .
_:28 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> _:29 .
_:29 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> _:24 .
_:29 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> .
_:20 <http://www.w3.org/2002/07/owl#intersectionOf> _:25 .
_:20 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class> .
_:21 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Restriction> .
_:21 <http://www.w3.org/2002/07/owl#onProperty> <http://lesfleursdunormal.fr/static/_downloads/bacteria.owl#has_shape> .
_:21 <http://www.w3.org/2002/07/owl#someValuesFrom> <http://lesfleursdunormal.fr/static/_downloads/bacteria.owl#Round> .
_:22 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Restriction> .
_:22 <http://www.w3.org/2002/07/owl#onProperty> <http://lesfleursdunormal.fr/static/_downloads/bacteria.owl#has_shape> .
_:22 <http://www.w3.org/2002/07/owl#allValuesFrom> <http://lesfleursdunormal.fr/static/_downloads/bacteria.owl#Round> .
_:23 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Restriction> .
_:23 <http://www.w3.org/2002/07/owl#onProperty> <http://lesfleursdunormal.fr/static/_downloads/bacteria.owl#has_grouping> .
_:23 <http://www.w3.org/2002/07/owl#someValuesFrom> <http://lesfleursdunormal.fr/static/_downloads/bacteria.owl#InCluster> .
_:24 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Restriction> .
_:24 <http://www.w3.org/2002/07/owl#onProperty> <http://lesfleursdunormal.fr/static/_downloads/bacteria.owl#gram_positive> .

無名ノードを段付けと表現すれば、次のような階層構造となる。

Staphylococcus
  -(rdfs:type)-owl:Class
  -(rdfs:subClassOf)-#occus
  -(owl:equivalentClass)
    -(rdfs:type)-Class
    -(owl:intersectionOf)
      -(rdfs:rest)
        -(rdfs:first)
          -(rdfs:type)-owl:Restriction
          -(owl:onProperty)-has_shape
          -(owl:someValueFrom)-Round
        -(rdfs:rest)
          -(rdfs:first)
            -(rdfs:type)-owl:Restriction
            -(owl:onPropert)-has_shape
            -(owl:allValueFrom)-Round
          -(rdfs:rest)
            -(rdfs:first)
              -(rdfs:type)-owl:Restriction
              -(owl:onProperty)-has_grouping
              -(owl:someValueFrom)-InCluster
            -(rdfs:rest)
              -(rdfs:fist)
                -(rdfs:type)-owl:Restriction
                -(owl:onProperty)-gram_positive
              -(rdfs:rest)-rdfs:nil
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?