如何获取rdflib中的所有空白节点的所有项目
我是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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
非常好 - 您指出了一种数量(测量的东西类型) - 重量 - 一个值-70,但您还应该包括单位。尝试 qudt 建模,看起来像这样:
有很多定义的度量单位,请参见
现在:
因此,现在,使用共同的本体论[schema.org](https://schema.org]:for'person':
现在,使用更常见的数据模式,让我们回答您的问题。
您要做的就是循环循环以获取所需的空白节点,然后以空白节点作为另一个循环中的主题从那里遍历图。
而不是像
g.triples((...))
这样的代码,使用更简单的g.subjects(...)
,g.subject_objects(.. 。)
等。还有一个
g.value(..)
您可以通过指定您知道的其他两个三重值来获取任何主题,谓词或对象的单个值。因此,要获得体重&amp;使用上面建模的新数据进行Heigh值:
然后,使用普通Python,计算BMI:
当然,您的BMI算法在此处以Python表示,而不是RDF表示,但是您也可以在SPARQL中表达它,或者您可以从RDF结构中生成算法,可能是您的最终目标,但是这种简单的方法至少回答了您的一些问题。
您可以将
bmi_value
写回RDF数据,作为另一个Quantity
使用适当的QuantityKind
,unit
等。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:
There are lots of defined units of measure, see http://www.qudt.org/doc/DOC_VOCAB-UNITS.html
Now height of Bob:
So now, using the common ontology [schema.org](https://schema.org]: for 'Person':
Now, with that more common data pattern, let's answer your question.
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 simplerg.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:
Then, using normal Python, calculate BMI:
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 anotherQuantity
with appropriatequantityKind
,unit
etc. values