Java 向Jena RDF三元组添加年龄(整数文本),并使用SPARQL查询它们

Java 向Jena RDF三元组添加年龄(整数文本),并使用SPARQL查询它们,java,oracle,rdf,sparql,jena,Java,Oracle,Rdf,Sparql,Jena,我正在努力学习使用Jena和RDF三元组的基础知识。还使用Oracle数据库,因此按照他们的指南,我有一些正在运行的示例程序,例如。这个例子写得很好。它允许匹配查询,例如 where {?s <u:parentOf> ?o} where {<u:John> <u:parentOf> <u:Mary>} where{s?o} 在哪里{} 我想做的是给John、Mary和Jill每人一个年龄,这样我就可以查询和筛选年龄,如第10页中所述: A .

我正在努力学习使用Jena和RDF三元组的基础知识。还使用Oracle数据库,因此按照他们的指南,我有一些正在运行的示例程序,例如。这个例子写得很好。它允许匹配查询,例如

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

where {<u:John> <u:parentOf> <u:Mary>}
where{s?o}
在哪里{}
我想做的是给John、Mary和Jill每人一个年龄,这样我就可以查询和筛选年龄,如第10页中所述:

A . B . FILTER ( …expr… )

where {?s <u:parentOf> ?o . ?o <u:isAge> ?a . filter ( ?a < 20 ) }
A。B过滤器(…expr…)
其中{s?o。?o?a.过滤器(?a<20)}
对于三元组的当前代码,我只能添加strings/URI节点,尽管我可以创建一个三元组,例如
,但我无法过滤并与该年龄段的
我已经四处寻找了一段时间,我怀疑这样做非常简单,但代码示例很难找到

注意,您需要一个像
“35”^^xsd:int
“35^^xsd:integer
这样的对象,它们是文字,而不是
,后者是(可能格式不正确)在我看来,这个例子是以一种不寻常的方式进行的,根据文档,它使用了一些不推荐的方法。无论如何,您可以看到它使用类(工厂类)中的方法创建URI节点:

Node类中有五个createLiteral方法,您可以使用该方法创建具有数据类型的文字。但是,该方法已被弃用,您确实应该使用中的一个方法

综上所述,我不知道示例是否使用图形接口而不是模型接口来创建三元组。您已经从以下位置获得了一个模型:

ModelOracleSem model = ModelOracleSem.createOracleSemModel(oracle, szModelName);
如果您使用接口,这个任务会容易得多,在接口中有这样的方法,您可以简单地调用
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() );
    }
}
import java.math.biginger;
导入com.hp.hpl.jena.query.query;
导入com.hp.hpl.jena.query.QueryExecution;
导入com.hp.hpl.jena.query.QueryExecutionFactory;
导入com.hp.hpl.jena.query.QueryFactory;
导入com.hp.hpl.jena.query.ResultSetFormatter;
导入com.hp.hpl.jena.rdf.model.model;
导入com.hp.hpl.jena.rdf.model.ModelFactory;
导入com.hp.hpl.jena.rdf.model.Property;
导入com.hp.hpl.jena.rdf.model.Resource;
公共类CreateAndQueryExample{
/**
*@param args
*/
公共静态void main(字符串[]args){
//您应该能够将其替换为Oracle模型
//生成代码。
模型模型=ModelFactory.createDefaultModel();
Resource john=model.createResource(“urn:ex:john”);
Resource mary=model.createResource(“urn:ex:mary”);
属性年龄=model.createProperty(“urn:ex:age”);
//使用Model.add方法
model.add(john,age,model.createTypedLiteral(新的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
//使用Resource.addProperty方法
mary.addProperty(age,model.createTypedLiteral(新的BigInteger(“35”)));
queryquery=QueryFactory.create(“select*where{s?age.filter(?age<30)}”);
QueryExecution exec=QueryExecutionFactory.create(查询,模型);
ResultSetFormatter.out(exec.execSelect());
}
}
-----------------------
|s |年龄|
=======================
|  | 25  |
-----------------------

请注意,像
这样的文本在编辑器中被视为标记,因此您需要将其环绕在背景标记
`
中,否则没有人会看到它。我已修复了这种情况。谢谢,这很有帮助,但您忽略了资源之间的“”关系。您是否也将它们添加到model.add中,就好像它们是文字一样?有什么区别ce如果我将关系添加为文字或三元组,会有什么影响?@user985366我没有遗漏它们。你的问题是添加年龄(整数文字)到Jena RDF三元组,并使用SPARQL查询它们,这个答案显示了如何做这两件事。您已经有了一种添加创建URI节点的方法,如果您想使用模型接口和相关接口来做这件事,答案有到这些接口的链接。如果您要编写类似于
?s:parentOf?o。?o:ha的模式sAge?a
,那么parentOf必须是资源之间的关系(即,空白节点和URI节点,但不是文字)。文字不能成为三元组的主题,因此如果在parentOf中使用文字,例如,
:John:parentOf“Mary”
,那么在第二个三元组中你就不走运了,因为你不能说
“Mary”:hasaage 25
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() );
    }
}