如何使用 Jena 将 float
、int
或 date
等数值添加到Resource
?
我假设我必须使用resource.addProperty(Property, String, RDFDataType)
,但是如何为上述数据类型实例化正确的RDFDataType?
最佳答案
关于类型化文字的“官方”文档在这里: http://incubator.apache.org/jena/documentation/notes/typed-literals.html
您可以使用 Jena Model 的 addLiteral 和 add 方法,例如:
Model model = ...
model.addLiteral (subject, predicate, 10);
model.addLiteral (subject, predicate, 0.5);
model.addLiteral (subject, predicate, (float)0.5);
model.addLiteral (subject, predicate, ResourceFactory.createTypedLiteral(20));
model.addLiteral (subject, predicate, ResourceFactory.createTypedLiteral(0.99));
model.addLiteral (subject, predicate, true);
model.add (subject, predicate, ResourceFactory.createTypedLiteral("2012-03-11", XSDDatatype.XSDdate));
model.add (subject, predicate, ResourceFactory.createTypedLiteral("P2Y", XSDDatatype.XSDduration));
RDFDatatype 是一个接口(interface),因此您无法直接实例化它。但是,请查看实现该接口(interface)的类。您会发现 XSDDatatype 就是这些类之一。还有其他的。
如果您想查看完整示例,请查看此处: https://github.com/castagna/jena-examples/blob/master/src/main/java/org/apache/jena/examples/ExampleDataTypes_01.java 。 ExampleDataTypes_01.java 的输出是以下 RDF(使用 Turtle 格式序列化):
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix example: <http://example.org/> .
example:s
example:p1 "10"^^xsd:int ;
example:p2 "0.5"^^xsd:double ;
example:p3 "0.5"^^xsd:float ;
example:p4 "20"^^xsd:int ;
example:p5 "0.99"^^xsd:double ;
example:p6 "true"^^xsd:boolean ;
example:p7 "2012-03-11"^^xsd:date ;
example:p8 "P2Y"^^xsd:duration .
关于java - Jena:如何添加带有数值的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9647986/