簡體   English   中英

如何獲取rdflib中空白節點的所有項目

[英]How to get all items of a blank node in rdflib

我是 RDflib 的新手,我想實現這樣的功能:也就是說,Person hasProperty Weight(kg), Height(m), and BMI (Body Mass Index)=Weight/Height^2, so if Bob's Weight= 70、Height=1.75,如何從這個model推導出Bob的BMI? 我使用下面的RDF文件來存儲以上信息:

@prefix : <http://ex.org/BMI#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
:Person rdf:type owl:Class;
         :hasProperty :Height, :Weight, :BMI.
:OriginalProperty rdf:type owl:Class .
:DerivedProperty rdf:type owl:Class .
:Weight rdf:type owl:Class ;
        rdfs:subClassOf :OriginalProperty .
:Height rdf:type owl:Class ;
        rdfs:subClassOf :OriginalProperty .
:BMI rdf:type owl:Class ;
     rdfs:subClassOf :DerivedProperty ;
     :equalTo [:divide (:Weight [:power  :Height])] .
:MathOperator rdf:type owl:Class .
:equalTo rdf:type owl:Class ;
         rdfs:subClassOf :MathOperator .
:divide rdf:type owl:Class ;
           rdfs:subClassOf :MathOperator .
:power rdf:type owl:Class ;
       rdfs:subClassOf :MathOperator .
:Bob rdf:type owl:NamedIndividual , :Person ;
     :hasProperty :BMIOfBob ,
                  :HeightOfBob ,
                  :WeightOfBob .
:HeightOfBob rdf:type owl:NamedIndividual , :Height ;
            :hasValue 1.75 .
:WeightOfBob rdf:type owl:NamedIndividual , :Weight ;
            :hasValue 70 .
:BMIOfBob rdf:type owl:NamedIndividual ,:BMI.

我的python代碼如下:

from rdflib import Graph
from rdflib.namespace import Namespace
g = Graph()
x = Graph()
g.parse("BMI.ttl")
n = Namespace("http://ex.org/BMI#")
x = g.triples((None,n.equalTo,None))
for s, p, o in x:
    print(s,p,o)

只返回空白節點本身,不包括 BNode 的其他數據。

http://ex.org/BMI#BMI http://ex.org/BMI#equalTo n2dd6baee104f49189928ce08eb003834b1

如何獲取 BNode 的額外信息? 或者有沒有更好的方法用RDF實現這個model的功能?
感謝您的幫助和建議。

樂力士

  1. 始終將 RDF 數據通過轉換器進行驗證和整理。 試試http://rdftools.surroundaustralia.com/convert 使用它你會得到:
@prefix : <http://ex.org/BMI#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

:Bob a :Person,
        owl:NamedIndividual ;
    :hasProperty :BMIOfBob,
        :HeightOfBob,
        :WeightOfBob .

:divide a owl:Class ;
    rdfs:subClassOf :MathOperator .

:equalTo a owl:Class ;
    rdfs:subClassOf :MathOperator .

:power a owl:Class ;
    rdfs:subClassOf :MathOperator .

:BMIOfBob a :BMI,
        owl:NamedIndividual .

:DerivedProperty a owl:Class .

:HeightOfBob a :Height,
        owl:NamedIndividual ;
    :hasValue 1.75 .

:Person a owl:Class ;
    :hasProperty :BMI,
        :Height,
        :Weight .

:WeightOfBob a :Weight,
        owl:NamedIndividual ;
    :hasValue 70 .

:BMI a owl:Class ;
    :equalTo [ :divide ( :Weight [ :power :Height ] ) ] ;
    rdfs:subClassOf :DerivedProperty .

:OriginalProperty a owl:Class .

:Height a owl:Class ;
    rdfs:subClassOf :OriginalProperty .

:MathOperator a owl:Class .

:Weight a owl:Class ;
    rdfs:subClassOf :OriginalProperty .
  1. 以標准方式重塑您的數值 - 使用已建立的本體。 你做事的方式如下:
:WeightOfBob 
    a :Weight, owl:NamedIndividual ;
    :hasValue 70 .

非常好 - 你已經指出了數量類型(測量的事物類型) - 重量 - 和價值 - 70 但你還應該包括單位。 嘗試QUDT建模,它看起來像這樣:

@prefix qk: <http://qudt.org/vocab/quantitykind/> .
@prefix qudt: <http://qudt.org/schema/qudt/> .
@prefix unit: <http://qudt.org/vocab/unit/> .

:WeightOfBob 
    a qudt:Quantity ;
    qudt:hasQuantityKind qk:Weight ;
    qudt:numericValue 70 ;
    qudt:unit unit:KiloGM ;   # kilogram

有很多定義的度量單位,參見http://www.qudt.org/doc/DOC_VOCAB-UNITS.html

現在鮑勃的身高:

:WeightOfBob 
    a qudt:Quantity ;
    qudt:hasQuantityKind qk:Height ;
    qudt:numericValue 1.75 ;
    qudt:unit unit:M ;   # metre

所以現在,使用通用本體 [schema.org](https://schema.org]: 對於“人”:

@prefix : <http://ex.org/BMI#> .
@prefix sdo: <https://schema.org/> .

:Bob 
  a sdo:Person ;
  :hasProperty [
    a qudt:Quantity ;
    qudt:hasQuantityKind qk:Weight ;
    qudt:numericValue 70 ;
    qudt:unit unit:KiloGM ;   # kilogram
  ] ,
  [
    a qudt:Quantity ;
    qudt:hasQuantityKind qk:Height ;
    qudt:numericValue 1.75 ;
    qudt:unit unit:M ;   # metre
  ] ;
.

現在,使用更常見的數據模式,讓我們回答您的問題。

如何獲取 BNode 的額外信息?

您需要做的是遍歷圖形以獲取所需的空白節點,然后從那里遍歷圖形,將該空白節點作為另一個循環中的主題。

而不是像g.triples((...))這樣的代碼,使用更簡單的g.subjects(...)g.subject_objects(...)等。

還有一個g.value(..) function 您可以通過指定您知道的其他兩個三重值來獲取任何主語、謂語或 object 的單個值。

因此,要使用上面建模的新數據獲取重量和高度值:

# get the Blank Nodes for Bob for weight & height
height_bn = g.value(predicate=qudt.hasQuantityKind, object=qk.Height)
weight_bn = g.value(predicate=qudt.hasQuantityKind, object=qk.Weight)

# get the numeric values from those Blank Nodes
height_value = g.value(subject=height_bn, predicate=qudt.numericValue)
weight_value = g.value(subject=weight_bn, predicate=qudt.numericValue)

# if you needed to know units, you could do that here...

然后,使用正常的 Python,計算 BMI:

import math
bmi_value = float(weight_value) / math.pow(float(height_value), 2)

當然,你的 BMI 算法在這里用 Python 表示,而不是 RDF,但你也可以用 SPARQL 表示,或者你可以從 RDF 結構生成算法,這可能是你的最終目標,但這個簡單的方法至少回答了你的一些問題.

您也可以將該bmi_value作為另一個具有適當quantityKindunit等值的Quantity寫回到您的 RDF 數據中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM