2014-06-26 26 views
0

我正试图学习使用Jena和RDF Triples的基础知识。同样使用Oracle数据库,所以按照他们的指导,我有一些示例程序正在运行,如Example7-18 SPARQL OPTIONAL Query。这个例子可以正常工作。它允许匹配的查询,如将年龄(整数文字)添加到Jena RDF三元组中,并使用SPARQL查询它们

where {?s <u:parentOf> ?o} 

where {<u:John> <u:parentOf> <u:Mary>} 

我想什么作为SPARQL By Example: The Cheat Sheet描述做的就是给约翰,玛丽和吉尔的各年龄,这样我就可以查询和年龄过滤器,第10页:

A . B . FILTER (…expr…) 

where {?s <u:parentOf> ?o . ?o <u:isAge> ?a . filter (?a < 20) } 

随着三元我只能添加字符串当前代码/ URI的节点,虽然我可以做一个三重如<u:John> <u:isAge> <u:35>,我不能过滤并用,例如,<操作上,年龄比较,所以这不是很有用。

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

+0

注意文字像''被视为在编辑器中的标记,所以你需要围绕着它在反引号'\'',否则没有人能看到它。我已经解决了这个问题。 –

回答

2

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

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

有在节点五级createLiteral方法,你可以使用createLiteral(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 <urn:ex:age> ?age . filter (?age < 30) }"); 
     QueryExecution exec = QueryExecutionFactory.create(query, model); 
     ResultSetFormatter.out(exec.execSelect()); 
    } 
} 
----------------------- 
| s    | age | 
======================= 
| <urn:ex:John> | 25 | 
----------------------- 
+0

谢谢,这很有帮助,但您忽略了资源之间的''关系。您是否也将它们与model.add一起添加,就好像它们是文字一样?如果我将我的关系作为文字或三元组添加,它会有什么不同? – user985366

+0

@ user985366我没有离开他们。你问的问题abtou **将年龄(整数文字)添加到Jena RDF三元组中,并使用SPARQL **查询它们,并且此答案显示如何执行这两项操作。您已经有添加创建URI节点的方法,并且如果您想使用Model接口和相关接口来执行此操作,则答案会包含指向这些接口的链接。 –

+0

如果你打算写一个像'?s:parentOf?o这样的模式。 ?o:hasAge?a',那么parentOf必须是资源(即空白节点和URI节点,但不是文字)之间的关系。文字不能成为三元组的主题,因此,如果您在parentOf中使用了文字,例如,':约翰:父母“玛丽”,你在第二个三联中运气不好,因为你不能说“玛丽”:hasAge 25'。 –