如何获取rdflib中的所有空白节点的所有项目

发布于 2025-01-23 21:13:38 字数 1883 浏览 4 评论 0原文

我是RDFLIB的新手,我想实现这样的功能: 可以说,人hasproperty重量(kg),身高(m), 和BMI(体重指数)=重量/高度^2,因此,如果Bob的重量= 70,高度= 1.75,则如何从该模型中推断出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实现此模型的功能?
感谢您的任何帮助和建议。

Joylix

I'm new to RDflib, I want to implement a feature like this:
To say, Person hasProperty Weight(kg) , Height(m),
and BMI (Body Mass Index)=Weight/Height^2, so if Bob's Weight=70, Height=1.75, How to deduce Bob's BMI from this model?
I use the following RDF file to store the above information:

@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.

my python code is as follows:

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)

Only the blank node itself is returned, excluding other data of the BNode.

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

How can I get additional information of the BNode? Or is there a better way to implement the functionality of this model with RDF?
Thank you for any help and advice.

Joylix

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

策马西风 2025-01-30 21:13:38
  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

有很多定义的度量单位,请参见

现在:

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

因此,现在,使用共同的本体论[schema.org](https://schema.org]:for'person':

@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(..)您可以通过指定您知道的其他两个三重值来获取任何主题,谓词或对象的单个值。

因此,要获得体重&amp;使用上面建模的新数据进行Heigh值:

# 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写回RDF数据,作为另一个Quantity使用适当的QuantityKindunit等。

  1. Always put your RDF data through a converter to validate it and tidy it up. Try http://rdftools.surroundaustralia.com/convert. Using that you get:
@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. Remodel your numeric values in a standard way - using established ontologies. The way you've done things like:
:WeightOfBob 
    a :Weight, owl:NamedIndividual ;
    :hasValue 70 .

is pretty good - you've indicated a quantity kind (type of thing measured) - weight - and a value - 70 but you should also include units. Try QUDT modelling, which would look like this:

@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

There are lots of defined units of measure, see http://www.qudt.org/doc/DOC_VOCAB-UNITS.html

Now height of Bob:

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

So now, using the common ontology [schema.org](https://schema.org]: for 'Person':

@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
  ] ;
.

Now, with that more common data pattern, let's answer your question.

How can I get additional information of the BNode?

What you've got to do is loop through the graph to get the Blank Node you want and then traverse the graph from there with that Blank Node as a subject in another loop.

Rather than code like g.triples((...)), use the simpler g.subjects(...), g.subject_objects(...) etc.

There is also a g.value(..) function you can use to get single values for any subject, predicate or object by specifying the other two triple values you know.

So, to get the weight & heigh values using the new data modelled above:

# 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...

Then, using normal Python, calculate BMI:

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

Sure, your BMI algorithm here is expressed in Python, not RDF, but you could also express it in SPARQL or you could generate the algorithm from RDF structures, wich may be your ultimate goal, but this simple method answers at least some of your questions.

You can write that bmi_value back into your RDF data too as another Quantity with appropriate quantityKind, unit etc. values

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文