将年龄(整数文字)添加到Jena RDF三元组,并使用SPARQL查询它们

我正在尝试学习使用Jena和RDF Triples的基础知识。 还使用Oracle数据库,因此按照他们的指南我运行了一些示例程序,例如Example7-18 SPARQL OPTIONAL Query 。 该示例在写入时工作正常。 它允许匹配查询,如

where {?s  ?o} where {  } 

我想做的是给John,Mary和Jill一个年龄,以便我可以查询和过滤年龄,如SPARQL中所述:例如:备忘单 ,第10页:

 A . B . FILTER ( …expr… ) where {?s  ?o . ?o  ?a . filter ( ?a < 20 ) } 

使用三元组的当前代码,我只能添加字符串/ URI节点,虽然我可以创建一个三元组,例如 ,但我无法过滤并与之比较,例如,那个年龄的<操作员,所以它不是很有用。

我一直在寻找一段时间,我怀疑这样做非常简单,但很难找到代码示例。

请注意,您需要一个像"35"^^xsd:int"35^^xsd:integer ,它们是文字,而不是这是一个(可能是格式错误的)URI。例如,在我看来,以一种不同寻常的方式做事,根据文档,它使用了一些不赞成使用的方法。无论如何,你可以看到它使用Node类(工厂类)中的方法创建URI节点:

 Node.createURI("u:John") Node.createURI("u:parentOf") Node.createURI("u:Mary") 

Node类中有五个createLiteral方法,您可以使用createLite(String lex,RDFDatatype dtype)来创建具有数据类型的文字。 但是这个方法已被弃用了,你真的应该使用NodeFactory中的一个方法。

总而言之,我不知道该示例是否有任何理由使用图形界面来创建三元组而不是模型接口。 你已经有了一个模型来自:

 ModelOracleSem model = ModelOracleSem.createOracleSemModel(oracle, szModelName); 

如果您使用Model接口,那么您可以更容易地执行此任务,其中您可以使用createTypedLiteral等方法,以便您可以简单地调用createTypedLiteral(30)并返回合适的文字。 这是一个例子:

 import java.math.BigInteger; import com.hp.hpl.jena.query.Query; import com.hp.hpl.jena.query.QueryExecution; import com.hp.hpl.jena.query.QueryExecutionFactory; import com.hp.hpl.jena.query.QueryFactory; import com.hp.hpl.jena.query.ResultSetFormatter; import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.ModelFactory; import com.hp.hpl.jena.rdf.model.Property; import com.hp.hpl.jena.rdf.model.Resource; public class CreateAndQueryExample { /** * @param args */ public static void main(String[] args) { // You should be able to replace this with the Oracle model // producing code. Model model = ModelFactory.createDefaultModel(); Resource john = model.createResource( "urn:ex:John" ); Resource mary = model.createResource( "urn:ex:Mary" ); Property age = model.createProperty( "urn:ex:age" ); // Using Model.add method model.add( john, age, model.createTypedLiteral( new BigInteger( "25" ))); // "25"^^xsd:integer // model.add( john, age, model.createTypedLiteral( 25 )); // "25"^^xsd:int // model.add( john, age, model.createTypedLiteral( 25l )); // "25"^^xsd:long // Using the Resource.addProperty method mary.addProperty( age, model.createTypedLiteral( new BigInteger( "35" ))); Query query = QueryFactory.create( "select * where { ?s  ?age . filter ( ?age < 30 ) }" ); QueryExecution exec = QueryExecutionFactory.create( query, model ); ResultSetFormatter.out( exec.execSelect() ); } } 
 ----------------------- | s | age | ======================= |  | 25 | -----------------------