且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将年龄(整数文字)添加到Jena RDF三元组中,并使用SPARQL对其进行查询

更新时间:2023-01-10 08:25:56

请注意,您需要一个像"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方法,您可以使用 NodeFactory .

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

,如果您使用 createTypedLiteral ,这样您就可以简单地调用createTypedLiteral(30)并获取合适的文字.这是一个示例:

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