【问题标题】:Jena: How to add properties with numeric values?Jena:如何添加具有数值的属性?
【发布时间】:2012-03-10 16:19:57
【问题描述】:

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

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

【问题讨论】:

    标签: java rdf jena ontology


    【解决方案1】:

    关于类型化文字的“官方”文档在这里: http://incubator.apache.org/jena/documentation/notes/typed-literals.html

    您可以使用 Jena ModeladdLiteraladd 方法,例如:

        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 .
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 我们如何打印字符串值的数据类型?例如model.addLiteral(主题,谓词,“测试str”);它应该像这样打印但不打印:example:p11 "test str"^^xsd:string ;
      猜你喜欢
      • 2020-05-03
      • 2018-01-20
      • 1970-01-01
      • 1970-01-01
      • 2013-12-08
      • 2015-03-03
      • 2019-08-19
      • 2021-01-15
      • 2022-12-09
      相关资源
      最近更新 更多