Jena:如何使用数值添加属性?

时间:2012-03-10 16:19:58

标签: java rdf jena ontology

如何使用Jena将floatintdate等数值添加到Resource

我假设我必须使用resource.addProperty(Property, String, RDFDataType),但是如何为上述数据类型实例化正确的RDFDataType?

2 个答案:

答案 0 :(得分:13)

关于打字文字的'官方'文档在这里: http://incubator.apache.org/jena/documentation/notes/typed-literals.html

您可以使用Jena 模型 addLiteral 添加方法,例如:

    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 是一个接口,因此您无法直接实例化它。但是,请查看实现该接口的类。您会发现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 .

答案 1 :(得分:1)

这些是RDF中的literals。您可以利用例如rdf:value属性将数值作为文字添加到资源中。您可以在这些文字上指定datatypes,例如xsd:int