[英]Inserting an OntModel instance into a Triple Store (like TDB) using ARQ (SPARQL processor for Jena)
如何使用 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");
}
有任何想法嗎? 非常感激。
經過一些搜索和使用 API 的嘗試。 我遇到了這個非常有用的教程- 雖然它有一個特定的重點,但它確實讓我對我需要做什么有了一些很好的想法。 所以這就是我最終設法使用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);
}
}
就這么簡單! 因此,當我通過運行 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 <-> 嵌入式 Fuseki 示例的來源,請在此處添加!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.