Are typed literals “tricky” in RDF4J?

心不动则不痛 提交于 2019-12-24 00:59:45

问题


In RDF, I can have an entity that is bound by a single property to multiple literal values. Those values can also be typed to add clarity.

:dumpTruck :weight "heavy"^^xsd:string .
:dumpTruck :weight "36000"^^xsd:integer .

In SPARQL, I can query for just they type I want

SELECT  ?w
WHERE
  { :dumpTruck  :weight  ?w
    FILTER ( datatype(?w) = xsd:integer )
  }

Is there something like getStatement in RDF4J that can be constrained by datatype like that?


回答1:


There's no a priori constraint possible, but you can post-process your result to filter on the datatype. For example you could use something along these lines (untested but you get the drift hopefully):

QueryResults.stream(conn.getStatements(dumptruck, weight, null))
            .filter(s -> ((Literal)s.getObject()).getDatatype().equals(XMLSchema.INTEGER))
            .collect(Collectors.toList());

Or alternatively, stick in a Model first and then filter:

Model m = QueryResults.asModel(conn.getStatements(dumptruck, weight, null));
List intValues = Models.objectLiterals(m).stream().filter(l -> l.getDatatype().equals(XMLSchema.INTEGER)).collect(Collectors.toList()); 

I'm sure there's a couple of other/handier shortcuts available that I haven't thought of. The RDF4J docs have tutorials and examples to spare, however.



来源:https://stackoverflow.com/questions/45308356/are-typed-literals-tricky-in-rdf4j

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!