使用ARQ(Jena的SPARQL处理器)将OntModel实例插入三重存储(如TDB)

时间:2016-04-19 13:49:35

标签: java sparql jena triplestore arq

如何使用ARQ(Jena的SPARQL处理器)将OntModel实例插入三重存储(如TDB)?我有以下代码,只需创建书籍,并将这些添加到OntModel中。现在我想将其插入到三重商店:

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

    }

有什么想法吗?非常感谢。

2 个答案:

答案 0 :(得分:1)

经过一些搜索和玩弄API。我遇到了这个very useful tutorial - 尽管它有一个特定的焦点,它确实给了我一些关于我需要做什么的好主意。因此,我最终设法使用 HTTP数据集访问器 OntModel将我的dataset插入/添加到Fuseki Server上的现有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);
  }
}

就这么简单!因此,当我通过运行SPARQL查询select * {?s ?p ?o}进行检查时 - 数据就在那里! 我希望这对那些使用Jena从事语义Web应用程序工作的人也很有用。

答案 1 :(得分:0)

这里介绍的教程很棒,最后展示了如何通过http将OntModel转移到Fuseki。下面是一个如何对嵌入式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);
    });

与http DatasetAccessor一样是关键。如果你知道如何优化这个例子,我想出来,请评论!

如果您了解有关Jena API的示例的更多来源&lt; - &gt;嵌入式Fuseki也请在这里添加!