Dear All,
thanks, Andy, for a piece of advice! So, I followed your advices, and
rewrite the code in that way. However, at the end met some problems. In the
case I use "UpdateExecutionFactory", I need GraphStore as a variable which
I do not have because I have only ontology, not a graph.
Anyway, it compiles after some changes, but I can not see any changes
in the file. My code looks the following way:
public void 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/mymodel#>"
+ "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#>"
+ "INSERT DATA {<
http://www.semanticweb.org/darius/ontologies/2013/6/mymodel#Kristina> a
base:Person.} ";
UpdateRequest query = UpdateFactory.create(queryString);
UpdateAction.execute(query, model);
//An alternative way
//UpdateAction.parseExecute(queryString, model);
// UpdateProcessor qe = UpdateExecutionFactory.create(query, model);
//com.hp.hpl.jena.query.ResultSet results = qe.execSelect();
try {
FileOutputStream out = new FileOutputStream(owlFile);
model.write(out, "RDF/XML");
//RDFDataMgr does not work because can not declare RDFFormat
//RDFDataMgr.write(out, model, public static RDFFormatVariant
PLAIN);
} catch (FileNotFoundException ex) {
Logger.getLogger(InsertRoom.class.getName()).log(Level.SEVERE,
null, ex);
}
model.close(); //OntModel is closed as well.
}
Thanks,
Darius
2013/8/29 Andy Seaborne <[email protected]>
> 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#<http://www.semanticweb.org/darius/ontologies/2013/6/myExample#>
>> >"*
>> * + "PREFIX rdf: <http://www.w3.org/1999/02/22-**
>> rdf-syntax-ns# <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
>>
>>> "*
>>>
>> * + "PREFIX rdfs:
>> <http://www.w3.org/2000/01/**rdf-schema#<http://www.w3.org/2000/01/rdf-schema#>
>> >"*
>> * + "PREFIX owl:
>> <http://www.w3.org/2002/07/**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
>>
>>
>