2016-04-19 51 views
-1

如何将一个OntModel实例插入到三重存储(如TDB)中使用ARQ(SPARQL处理器用于Jena? 。这只是简单地创建书籍,并添加这些到OntModel代码现在我要插入一个三重商店这样的:?使用ARQ(用于Jena的SPARQL处理器)将一个OntModel实例插入三重存储(如TDB)

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

    } 

任何想法非常感谢

+1

你为什么不回答这个问题而不打算给出任何关于你认为无用的信息 - 你甚至在开发本体的空间中工作吗?如果你无法回答这个问题,只需跳过它,看看你可以帮助的其他问题。 – ishmaelMakitla

回答

1

经过一番搜索,并与API玩弄。我遇到了这个very useful tutorial - 虽然它有一个特定的焦点,它确实给了我一些关于我需要做什么的好主意。我如何最终设法在Fuseki服务器上使用HTTP数据集访问器DatasetAccessor将我的OntModel插入/添加到我现有的dataset中。

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

最后感谢最终有用的教程,突出了如何弥合Jena API和Fuseki服务器之间的差距。我不明白为什么这个问题被低估 - 考虑纯文档和基于这个主题的教程。 – Macilias

0

这里介绍的教程很好,最后展示了如何通过http将OntModel转换成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 < - >嵌入式Fuseki的例子,请在这里添加!

相关问题