2016-04-19 12 views
-1

Wie kann ich eine OntModel-Instanz in einen Triple Store (wie TDB) mit ARQ (SPARQL-Prozessor für Jena?) Einfügen . Code, der einfach Bücher erstellt, und fügen sie diese in eine OntModel Jetzt möchte ich dies in einem Triple-Shop einfügen:?.Einfügen einer OntModel-Instanz in einen Triple Store (wie TDB) mit ARQ (SPARQL-Prozessor für Jena)

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

    } 

Irgendwelche Ideen Sehr geschätzt

+1

Warum würden Sie diese Frage ablehnen, ohne sich darum zu kümmern, was Sie nicht hilfreich finden - arbeiten Sie sogar im Bereich der Entwicklung von Ontologien? Wenn Sie die Frage nicht beantworten können, überspringen Sie sie einfach und sehen Sie sich andere Fragen an, bei denen Sie helfen können. – ishmaelMakitla

Antwort

1

nach einigem Suchen und herum~~POS=TRUNC mit dem API Ich stieß auf diese very useful tutorial - obwohl es einen bestimmten Fokus hatte, gab es mir eine gute Idee über das, was ich tun musste wie ich es schließlich schaffte, meine OntModel in meine bestehende dataset auf Fuseki Server unter Verwendung HTTP Dataset AccessorDatasetAccessor einfügen/hinzufügen.

//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); 
    } 
} 

Es war so einfach! Also, als ich überprüft, indem Sie SPARQL Abfrage select * {?s ?p ?o} - die Daten waren da! Ich hoffe, dass dies auch für diejenigen nützlich ist, die an Semantic Web-Anwendungen mit Jena arbeiten.

+1

Endlich danke für das endlich nützliche Tutorial, das hervorhebt, wie man die Lücke zwischen Jena API und Fuseki Server schließt. Ich kann nicht verstehen, warum diese Frage abgelehnt wurde - in Anbetracht der reinen Dokumentation und der Tutorial-Basis zu diesem Thema. – Macilias

0

Das hier vorgestellte Tutorial ist großartig und zeigt schließlich, wie man OntModel über http in Fuseki überträgt. Hier ist ein Beispiel, wie diese in eingebetteter Fuseki 3.4.0 auf Vollständigkeit zu tun:

// 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); 
    }); 

wie mit http DatasetAccessor ist hier der Schlüssel. Wenn Sie wissen, wie Sie dieses Beispiel optimieren können, kommentieren Sie mich bitte!

Wenn Sie mehr Quellen mit Beispielen zu Jena API < -> eingebettete Fuseki wissen, fügen Sie diese bitte auch hier!