开发者

Jena, DBpedia: RDF and model name

I'm using dbpedia in my app, and I'm using Jena for the semantic logic.

In Jena, the classes are:

Model: a set of statements http://jena.sourceforge.net/javadoc/com/hp/hpl/jena/rdf/model/Model.html Resource: http://jena.sourceforge.net/javadoc/com/hp/hpl/jena/rdf/model/Resource.html

In dbpedia, the rdf code of a resource is this: e.g. http://dbpedia.org/resource/Frederick_of_Sweden becomes http://dbpedia.org/data/Frederick_of_Sweden.rdf

If I call:

Model model = maker.createModel( "http://dbpedia.org/data/Frederick_of_Sweden.rdf")

A m开发者_运维技巧odel with the name 'http://dbpedia.org/data/Frederick_of_Sweden.rdf' is created. But I actually need to call it 'http://dbpedia.org/resource/Frederick_of_Sweden', to be consistent with the rdf statements. How do I name a model?

If I want to navigate the graph and reach other nodes, which is the best way to store these statements? Do I need a separate model for each dbpedia resource, or can I merge all the statements in one big model?

Thanks for any hint! Mulone


I don't think the way you create your models really affect the way you download the data. You can create your model with the URI identifier that you like.

Here I show an example that can give some ideas of how to separate the way you get your triples from DBPedia and the way you store them in your backend system.

From your question I assume you are using a database backend so my code is targeted to that kind of backend in Jena.

import java.sql.SQLException;
import com.hp.hpl.jena.util.FileManager;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.SimpleSelector;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.db.DBConnection;
import com.hp.hpl.jena.rdf.model.ModelMaker;
import com.hp.hpl.jena.rdf.model.ModelFactory;

public class TestJena {

    public static void main(String[] args) throws java.lang.ClassNotFoundException, java.sql.SQLException {
        Class.forName("com.mysql.jdbc.Driver");            

        //The database backend initialization.
        DBConnection connection = new DBConnection(MY_DB, USER, PASS, "mysql");
        ModelMaker dbMaker = ModelFactory.createModelRDBMaker(connection);

        //A file manager to get the triples from the DBPedia revolvable URI. 
        FileManager fManager = FileManager.get();
        fManager.addLocatorURL();
        Model linkedDataModel =
              fManager.loadModel("http://dbpedia.org/data/Frederick_of_Sweden.rdf");


        //Now we copy the in-memory model into our DB backend. 
        //When the model is created you can give it the name that you like.
        Model dbModel = 
              dbMaker.createModel("http://dbpedia.org/resource/Frederick_of_Sweden");

        dbModel.add(linkedDataModel);

        StmtIterator iter = dbModel.listStatements();
        while (iter.hasNext()) {
            Statement stmt = iter.nextStatement();
            System.out.println(stmt);
        }


        linkedDataModel.close();
        dbModel.close();
        connection.close();
}

This example prints ...

[http://dbpedia.org/resource/Frederick_i_of_sweden, http://dbpedia.org/ontology/wikiPageRedirects, http://dbpedia.org/resource/Frederick_of_Sweden]
[http://dbpedia.org/resource/Frederick_I_%28of_Sweden%29, http://dbpedia.org/ontology/wikiPageRedirects, http://dbpedia.org/resource/Frederick_of_Sweden]
[http://dbpedia.org/resource/Frederick_I,_Landgrave_of_Hesse-Kassel, http://dbpedia.org/ontology/wikiPageRedirects, http://dbpedia.org/resource/Frederick_of_Sweden]
[http://dbpedia.org:8890/data/Frederick_of_Sweden.rdf, http://xmlns.com/foaf/0.1/primaryTopic, http://dbpedia.org/resource/Frederick_of_Sweden]
(....)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜