简体   繁体   English

使用 ARQ(Jena 的 SPARQL 处理器)将 OntModel 实例插入 Triple Store(如 TDB)

[英]Inserting an OntModel instance into a Triple Store (like TDB) using ARQ (SPARQL processor for Jena)

How can I insert an OntModel instance into a Triple Store (like TDB) using ARQ (SPARQL processor for Jena? I have the following code which simply creates books, and add these into an OntModel. Now I want to insert this into a Triple Store:如何使用 ARQ(Jena 的 SPARQL 处理器)将 OntModel 实例插入到 Triple Store(如 TDB)中?我有以下代码,它只是创建书籍,并将它们添加到 OntModel 中。现在我想将其插入到 Triple Store 中:

public static void createDummyBooks(){
        // Create an empty ontology model
        OntModel ontModel = ModelFactory.createOntologyModel();
        String ns = new String("http://www.iexample.com/book#");
        String baseURI = new String("http://www.iexample.com/book");
        Ontology onto = ontModel.createOntology(baseURI);

        //creating a book 
        OntClass book = ontModel.createClass(ns + "Book");
        OntClass nonFinctionBook = ontModel.createClass(ns + "NonFictionBook");
        OntClass fictionBook = ontModel.createClass(ns + "FictionBook");

        // Create datatype property 'hasAge'
        DatatypeProperty hasTtitle = ontModel.createDatatypeProperty(ns + "hasTitle");
        // 'hasAge' takes integer values, so its range is 'integer'
        // Basic datatypes are defined in the ‘vocabulary’ package
        hasTtitle.setDomain(book);
        hasTtitle.setRange(XSD.xstring); // com.hp.hpl.jena.vocabulary.XSD

        // Create individuals
        Individual theProgrammingBook = nonFinctionBook.createIndividual(ns + "ProgrammingBook");
        Individual theFantasyBook = fictionBook.createIndividual(ns + "FantasyBook");


        Literal bookTitle = ontModel.createTypedLiteral("Programming with Ishmael", XSDDatatype.XSDstring);
        Literal fantasyBookTitle = ontModel.createTypedLiteral("The adventures of Ishmael", XSDDatatype.XSDstring);
        // Create statement 'ProgrammingBook hasTitle "Programming with Ishmael" '
        Statement theProgrammingBookHasTitle = ontModel.createStatement(nonFinctionBook, hasTtitle, bookTitle);
        // Create statement 'FantasyBook hasTitle "The adventures of Ishmael" '
        Statement theFantasyBookHasTitle = ontModel.createStatement(theFantasyBook, hasTtitle, fantasyBookTitle);
        List<Statement> statements = new ArrayList<Statement>();    
        statements.add(theProgrammingBookHasTitle);
        statements.add(theFantasyBookHasTitle);

        ontModel.add(statements);
        //just displaying here - but how do I now write/insert this into my Triple Store/TDB using AQR API?
        ontModel.write(System.out, "RDF/XML-ABBREV");

    }

Any ideas?有任何想法吗? Much appreciated.非常感激。

After some searching and playing around with the API.经过一些搜索和使用 API 的尝试。 I came across this very useful tutorial - although it had a specific focus, it did give me some good idea about what I needed to do.我遇到了这个非常有用的教程- 虽然它有一个特定的重点,但它确实让我对我需要做什么有了一些很好的想法。 So this is how I eventually managed to insert/add my OntModel into my existing dataset on Fuseki Server using HTTP Dataset Accessor DatasetAccessor .所以这就是我最终设法使用HTTP Dataset Accessor DatasetAccessor将我的OntModel插入/添加到我在 Fuseki Server 上的现有dataset集的DatasetAccessor

//The Graph Store protocol for sem_tutorials (my dummy dataset) is http://localhost:3030/sem_tutorials/data
private static final String FUSEKI_SERVICE_DATASETS_URI = "http://localhost:3030/sem_tutorials/data";
private void testSavingModel(OntModel model){
  DatasetAccessor accessor = DatasetAccessorFactory.createHTTP(FUSEKI_SERVICE_DATASETS_URI);
 if(accessor != null){
    //because I already had a number of Triples there already - I am only adding this model
    accessor.add(model);
  }
}

It was that easy!就这么简单! So when I checked by running SPARQL query select * {?s ?p ?o} - the data was there!因此,当我通过运行 SPARQL 查询进行检查时, select * {?s ?p ?o} - 数据就在那里! I hope this will also be useful to those who are working on Semantic Web applications using Jena.我希望这对那些使用 Jena 开发语义 Web 应用程序的人也有用。

The tutorial presented here is great and finally showing how to transfer OntModel into Fuseki over http.这里提供的教程很棒,最后展示了如何通过 http 将 OntModel 传输到 Fuseki。 Here is an example how to do the same into embedded Fuseki 3.4.0 for completeness:以下是如何对嵌入式 Fuseki 3.4.0 执行相同操作以确保完整性的示例:

    // this will represent content of the db
    Dataset ds = DatasetFactory.createTxnMem();
    DatasetGraph dsg = ds.asDatasetGraph();

    // here some Jena Objects to be inserted
    String NS  = "http://myexample.com/#"
    OntModel m = ModelFactory.createOntologyModel();
    OntClass r = m.createClass( NS + Request.class.getName() );
    Individual i = r.createIndividual( NS + request.hashCode() );

    FusekiServer server = FusekiServer.create()
                .setPort(4321)
                .add("/ds", ds)
                .build();
    server.start();

    DatasetAccessor accessor = DatasetAccessorFactory.create(ds);

    //upload Jena Model into Fuseki
    Txn.executeWrite(dsg, () -> {
        accessor.add(m);
        TDB.sync(dsg);
        dsg.commit();
    });

    //query content of Fuseki
    Txn.executeWrite(dsg, () -> {
        Quad q = SSE.parseQuad("(_ :s :p _:b)");
        dsg.add(q);
    });

as with http DatasetAccessor is the key here.与 http 一样,DatasetAccessor 是这里的关键。 If you know how to optimize this example I came up with, please comment!如果你知道如何优化我想出的这个例子,请评论!

If you know more sources with examples regarding Jena API <-> embedded Fuseki please add those here too!如果您知道更多有关 Jena API <-> 嵌入式 Fuseki 示例的来源,请在此处添加!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM