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

Java 使用ARQ(用于Jena的SPARQL处理器)将OntModel实例插入三重存储(如TDB),java,sparql,jena,triplestore,arq,Java,Sparql,Jena,Triplestore,Arq,如何使用ARQ(SPARQL processor for Jena)将OntModel实例插入到三重存储(如TDB)中?我有以下代码,可以简单地创建书籍,并将其添加到OntModel中。现在我想将其插入到三重存储中: public static void createDummyBooks(){ // Create an empty ontology model OntModel ontModel = ModelFactory.createOntologyModel

如何使用ARQ(SPARQL processor for Jena)将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");

    }
publicstaticvoid createDummyBooks(){
//创建一个空的本体模型
OntModel OntModel=ModelFactory.createOntologyModel();
字符串ns=新字符串(“http://www.iexample.com/book#");
String baseURI=新字符串(“http://www.iexample.com/book");
Ontology-on=ontModel.createOntology(baseURI);
//创作一本书
OntClass book=ontModel.createClass(ns+“book”);
OntClass NonPictionBook=ontModel.createClass(ns+“NonPictionBook”);
OntClass-virtualbook=ontModel.createClass(ns+“virtualbook”);
//创建数据类型属性“hasAge”
DatatypeProperty hasTtitle=ontModel.createDatatypeProperty(ns+“hasTtitle”);
//“hasAge”接受整数值,因此其范围为“integer”
//基本数据类型在“词汇表”中定义™ 包裹
hasTtitle.setDomain(book);
hasTtitle.setRange(XSD.xstring);//com.hp.hpl.jena.词汇表.XSD
//创造个人
单个theProgrammingBook=nonInctionBook.createIndividual(ns+“ProgrammingBook”);
个人theFantasyBook=虚构书.createIndividual(ns+“FantasyBook”);
Literal bookTitle=ontModel.createTypedLiteral(“使用Ishmael编程”,XSDDatatype.XSDstring);
Literal fantasyBookTitle=ontModel.createTypedLiteral(“伊什梅尔历险记”,XSDDatatype.XSDstring);
//创建语句“ProgrammingBook Hasttle”和“Ishmael编程”
语句theprogrammingbookhistle=ontModel.createStatement(非inctionbook,hasTtitle,bookTitle);
//创建语句“FantasyBook Hasttle”以实玛利历险记
语句thefantasybookhistle=ontModel.createStatement(theFantasyBook、hasTtitle、fantasyBookTitle);
列表语句=新的ArrayList();
添加(程序设计书);
报表。添加(本刊);
ontModel.add(语句);
//只是在这里显示-但是我现在如何使用AQR API将其写入/插入我的Triple Store/TDB?
ontModel.write(System.out,“RDF/XML-ABBREV”);
}

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

在搜索和使用API之后。我遇到了这个问题-虽然它有一个特定的重点,但它确实给了我一些关于我需要做什么的好主意。因此,这就是我最终如何使用将我的
OntModel
插入/添加到Fuseki服务器上现有的
数据集中的方法HTTP数据集访问器
数据集访问器

//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应用程序的人也有用。

这里提供的教程很棒,最后展示了如何通过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 embedded Fuseki的例子,请也在这里添加这些例子!

为什么你会投票否决这个问题,而不费心指出你认为没有帮助的是什么?你甚至在开发本体论的领域工作吗?如果你不能帮助回答这个问题,就跳过它,看一看其他问题,您可以提供帮助。最后感谢最后有用的教程,重点介绍如何弥合Jena API和Fuseki服务器之间的差距。考虑到此主题的纯文档和教程基础,我不理解为什么这个问题被否决。