On 29/08/13 20:24, Darius Miliauskas wrote:
> * + "INSERT {a base:MyClass ''New Individual' .} "*
> * +"WHERE {};*
INSERT DATA {<uriforindividual> a base:MyClass}
and it's not q query, it's an update. This isn't SQL :-) queries and
updates are separate; it's safer.
See UpdateAction for conveninece ways to execute an update or use
UpdateFactory/UpdateExecutionFactory.
Then write the file with model.write (or use
RDFDataMgr.read/RDFDataMgr.write).
Andy
Dear All,
Trying to make the basic example to understand how to insert data to RDF
ontology with INSERT of SPARQL. In SPARQL documentation I found many
examples how to insert to GRAPH, and others. However, I still can not write
the right query if I want let's say add a new member "Ana" to the class
"Person" in OWL/RDF ontology (the file of the ontology). I guess this
example would be useful for everybody.
1. How to write the query in String queryString?
2. Is there any suggestions how to update the file with the inserted data?
Here is my Java code of the method:
* public File insertNewIndividual(File owlFile) {*
*
*
* // INFO: Read ontology*
* OntModel model =
ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_MICRO_RULE_INF, null);
*
* try {*
* FileInputStream filein = new FileInputStream(owlFile);*
* model.read(filein, "RDF/XML");*
* } catch (IOException e) {*
* System.out.print("IOException: " + e.getMessage());*
* } catch (Exception e) {*
* System.out.print ("Problems reading ontology '" +
owlFile.getAbsolutePath() + "'. Exception message: " + e.getMessage());*
* }*
*
*
* /**
* * SPARQL query is used to insert a new entry in the ontology*
* */*
* String queryString = "PREFIX base: <
http://www.semanticweb.org/darius/ontologies/2013/6/myExample#>"*
* + "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#
"*
* + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"*
* + "PREFIX owl: <http://www.w3.org/2002/07/owl#>"*
* + "PREFIX foaf: <http://xmlns.com/foaf/0.1/>"*
* + "INSERT {a base:MyClass ''New Individual' .} "*
* +"WHERE {};*
*
*
* Query query = QueryFactory.create(queryString);*
* QueryExecution qe = QueryExecutionFactory.create(query, model);*
* com.hp.hpl.jena.query.ResultSet results = qe.execSelect();*
* //?ResultSetFormatter.out(System.out, results, query);*
* *
* try {*
* FileOutputStream out = new FileOutputStream(owlFile);*
* *
* //How to update the results in the file? :-)*
* } catch (FileNotFoundException ex) {*
*
Logger.getLogger(insertNewIndividual.class.getName()).log(Level.SEVERE,
null, ex);*
* }*
* qe.close();*
* model.close();*
* *
* return owlFile;*
* }*
Thanks,
Darius