-1

How can I insert an OntModel instance into a Triple Store (like TDB) using ARQ (SPARQL processor for Jena? I have the following code which simply creates books, and add these into an OntModel. Now I want to insert this into a 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");

    }

Any ideas? Much appreciated.

ishmaelMakitla
  • 3,784
  • 3
  • 26
  • 32
  • 1
    Why would you vote down this question without bothering to give any indication as to what is it you find unhelpful - are you even working in the space of developing Ontologies? If you cannot help answer the question, just skip it and look at other questions where you can help. – ishmaelMakitla Jan 10 '17 at 10:01

2 Answers2

1

After some searching and playing around with the API. I came across this very useful tutorial - although it had a specific focus, it did give me some good idea about what I needed to do. So this is how I eventually managed to insert/add my OntModel into my existing dataset on Fuseki Server using HTTP Dataset Accessor 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);
  }
}

It was that easy! So when I checked by running SPARQL query select * {?s ?p ?o} - the data was there! I hope this will also be useful to those who are working on Semantic Web applications using Jena.

ishmaelMakitla
  • 3,784
  • 3
  • 26
  • 32
  • 1
    Finally thanks for the finally useful tutorial highlighting how to bridge the gap between Jena API and Fuseki server. I can not understand why this question has been downvoted - considering the pure documentation and tutorial base on this topic. – Macilias Sep 22 '17 at 16:28
0

The tutorial presented here is great and finally showing how to transfer OntModel into Fuseki over http. Here is an example how to do the same into embedded Fuseki 3.4.0 for completeness:

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

as with http DatasetAccessor is the key here. If you know how to optimize this example I came up with, please comment!

If you know more sources with examples regarding Jena API <-> embedded Fuseki please add those here too!

Macilias
  • 3,279
  • 2
  • 31
  • 43