将年龄(整数文字)添加到Jena RDF三元组中,并使用SPARQL对其进行查询 [英] Adding age (integer literals) to Jena RDF triples, and querying on them with SPARQL

查看:95
本文介绍了将年龄(整数文字)添加到Jena RDF三元组中,并使用SPARQL对其进行查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习使用Jena和RDF Triples的基础知识.还可以使用Oracle数据库,因此遵循他们的指南,我正在运行一些示例程序,例如

I'm trying to learn the basics of working with Jena and RDF Triples. Also working with an Oracle database, so following their guide I have some sample programs running, such as Example7-18 SPARQL OPTIONAL Query. The example works fine as written. It allows for matching queries such as

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

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

我想做的就是给约翰,玛丽和吉尔一个年龄,这样我就可以按照

What I'd like to do is give John, Mary and Jill an age each, so that I can query and filter on age as described in SPARQL By Example: The Cheat Sheet, page 10:

A . B . FILTER ( …expr… )

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

使用当前的三元组代码,我只能添加字符串/URI节点,尽管我可以创建诸如<u:John> <u:isAge> <u:35>之类的三元组,但是我无法进行过滤和与之比较,例如<该年龄段的运算符,因此它不是很有用.

With the current code for triples I can only add strings/URI Nodes, and although I could make a triple such as <u:John> <u:isAge> <u:35>, I cannot filter and make comparisons with, e.g., the < operator on that age, so it is not very useful.

我已经四处寻找了,我怀疑这样做很简单,但是很难找到代码示例.

I've been looking around for a while now and I suspect that doing this is very simple but code samples have been hard to find.

推荐答案

请注意,您需要一个像"35"^^xsd:int"35^^xsd:integer这样的对象,它们是文字,而不是像<u:35>这样的URI(可能是畸形的).我认为该示例以某种不同寻常的方式执行操作,并且根据文档,它使用了一些不推荐使用的方法.无论如何,您可以看到它使用

Note that you want an object like "35"^^xsd:int or "35^^xsd:integer, which are literals, not <u:35> which is a (perhaps malformed) URI. The example is, in my opinion, doing things in a somewhat unusual way, and according to the documentation, it's using some deprecated methods. At any rate, you can see that its creating URI nodes with methods from the Node class (a factory class):

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

Node类中有五个createLiteral方法,您可以使用

There are five createLiteral methods in the Node class, and you could use createLiteral(String lex, RDFDatatype dtype) to create a literal with a datatype. That method is deprecated though, and you really should be using one of the methods from NodeFactory instead.

所有这些,我不知道示例是否有理由使用图形接口而不是Model接口来创建三元组.您已经从以下位置获得了模型:

All that said, I don't know if there's any reason that the example uses the graph interface for creating triples rather than the Model interface. You've already got a model from:

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

,如果您使用

and this task is much easier to do if you use the Model interface, where you have methods like createTypedLiteral so that you can simply call createTypedLiteral(30) and get back a suitable literal. Here's an example:

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  |
-----------------------

这篇关于将年龄(整数文字)添加到Jena RDF三元组中,并使用SPARQL对其进行查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆