On 04/09/13 13:08, Darius Miliauskas wrote:
Dear Andy,
thanks for your piece of code and explanations, I can compile the code if I
use "model.write(out, "RDF/XML");" not "RDFDataMgr.write(System.out, model,
Lang.TTL);" but nothing is added in the file (no changes in the file which
stores the ontology).
Maybe you are using an old version of the code.
Regarding "RDFDataMgr.write(System.out, model, Lang.TTL);", I got the error
that TTL can not be find.
... with no deprecations? There is an old legacy class lang as well.
(and Lang should be package "org.apache.jena.riot", not
"org.openjena.riot")
Check the version of the code - the current release is 2.10.1 and you
need all the jars on the classpath.
Andy
My code is the following:
package org.apache.mahout.inserttordfexample;
import java.io.File;
/**
* this app just try/test the functionality of INSERT using SPARQL
*
* @param args data stream
* @author DARIUS MILIAUSKAS
*/
public class App {
public static void main(String[] args) {
// INFO: File containing information about rooms, cities, prices,
persons
java.io.File file = new
java.io.File(java.net.URLDecoder.decode(InsertRoom.class.getClassLoader().getResource("MyModel.owl").getFile()));
InsertRoom ir = new InsertRoom();
ir.insertRoom(file);
}
}
package org.apache.mahout.inserttordfexample;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.update.Update;
import com.hp.hpl.jena.update.UpdateAction;
import com.hp.hpl.jena.update.UpdateExecutionFactory;
import com.hp.hpl.jena.update.UpdateFactory;
import com.hp.hpl.jena.update.UpdateProcessor;
import com.hp.hpl.jena.update.UpdateRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.jena.riot.RDFDataMgr;
import org.openjena.riot.Lang;
/**
*
* @author DARIUS MILIAUSKAS
*/
public class InsertRoom {
public void insertRoom(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#>"
+ "PREFIX foaf: <http://xmlns.com/foaf/0.1/>"
+ "INSERT DATA {<
http://www.semanticweb.org/darius/ontologies/2013/6/mymodel#Kristina> a
base:Person.} ";
UpdateRequest query = UpdateFactory.create(queryString);
UpdateAction.execute(query, model);
//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.write(System.out, model, Lang.TTL);
} catch (FileNotFoundException ex) {
Logger.getLogger(InsertRoom.class.getName()).log(Level.SEVERE,
null, ex);
}
//query.close(); // INFO: This needs to be closed after retrieving
the results, because it won't work!
model.close(); //OntModel is closed as well.
//return owlFile;
}
}
My sample of ontology:
<?xml version="1.0"?>
<!DOCTYPE rdf:RDF [
<!ENTITY owl "http://www.w3.org/2002/07/owl#" >
<!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >
<!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" >
<!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
]>
<rdf:RDF xmlns="http://www.semanticweb.org/darius/ontologies/2013/8/mymodel#
"
xml:base="http://www.semanticweb.org/darius/ontologies/2013/8/mymodel"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<owl:Ontology rdf:about="
http://www.semanticweb.org/darius/ontologies/2013/8/mymodel"/>
<!--
///////////////////////////////////////////////////////////////////////////////////////
//
// Classes
//
///////////////////////////////////////////////////////////////////////////////////////
-->
<!--
http://www.semanticweb.org/darius/ontologies/2013/8/mymodel#MusicStyle -->
<owl:Class rdf:about="
http://www.semanticweb.org/darius/ontologies/2013/8/mymodel#MusicStyle"/>
<!-- http://www.semanticweb.org/darius/ontologies/2013/8/mymodel#Person-->
<owl:Class rdf:about="
http://www.semanticweb.org/darius/ontologies/2013/8/mymodel#Person"/>
<!--
///////////////////////////////////////////////////////////////////////////////////////
//
// Individuals
//
///////////////////////////////////////////////////////////////////////////////////////
-->
<!-- http://www.semanticweb.org/darius/ontologies/2013/8/mymodel#Ana -->
<owl:NamedIndividual rdf:about="
http://www.semanticweb.org/darius/ontologies/2013/8/mymodel#Ana">
<rdf:type rdf:resource="
http://www.semanticweb.org/darius/ontologies/2013/8/mymodel#Person"/>
</owl:NamedIndividual>
<!-- http://www.semanticweb.org/darius/ontologies/2013/8/mymodel#Electro-->
<owl:NamedIndividual rdf:about="
http://www.semanticweb.org/darius/ontologies/2013/8/mymodel#Electro">
<rdf:type rdf:resource="
http://www.semanticweb.org/darius/ontologies/2013/8/mymodel#MusicStyle"/>
</owl:NamedIndividual>
<!-- http://www.semanticweb.org/darius/ontologies/2013/8/mymodel#House-->
<owl:NamedIndividual rdf:about="
http://www.semanticweb.org/darius/ontologies/2013/8/mymodel#House">
<rdf:type rdf:resource="
http://www.semanticweb.org/darius/ontologies/2013/8/mymodel#MusicStyle"/>
</owl:NamedIndividual>
<!--
http://www.semanticweb.org/darius/ontologies/2013/8/mymodel#Veronika -->
<owl:NamedIndividual rdf:about="
http://www.semanticweb.org/darius/ontologies/2013/8/mymodel#Veronika">
<rdf:type rdf:resource="
http://www.semanticweb.org/darius/ontologies/2013/8/mymodel#Person"/>
</owl:NamedIndividual>
</rdf:RDF>
Thanks,
Darius Miliauskas
2013/9/1 Andy Seaborne <[email protected]>
On 31/08/13 17:45, Darius Miliauskas wrote:
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.
Ontologies are graphs.
Anyway, it compiles after some changes, but I can not see any
changes
in the file. My code looks the following way:
This works for me: I don't have your data. The output shows that it
contains the triple added.
If this does not work for you as you expect, please provide a complete,
minimal example, a description of what does happen for you and the reason
why it is not as you expect.
Andy
public class R2 {
public static void main(String...args) {
Model model =
//ModelFactory.**createDefaultModel() ;
ModelFactory.**createOntologyModel(**OntModelSpec.OWL_MEM_MICRO_**RULE_INF,
null);
//RDFDataMgr.read(model, "D.ttl") ;
String updateString = "PREFIX base: <http://www.semanticweb.org/**
darius/ontologies/2013/6/**mymodel#<http://www.semanticweb.org/darius/ontologies/2013/6/mymodel#>
"
+ "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#>
"
+ "INSERT DATA {<http://www.semanticweb.org/**
darius/ontologies/2013/6/**mymodel#Kristina<http://www.semanticweb.org/darius/ontologies/2013/6/mymodel#Kristina>>
a base:Person.} ";
UpdateRequest query = UpdateFactory.create(**updateString);
UpdateAction.execute(query, model);
// Resource r = model.createResource("http://**
www.semanticweb.org/darius/**ontologies/2013/6/mymodel#**Kristina<http://www.semanticweb.org/darius/ontologies/2013/6/mymodel#Kristina>")
;
// StmtIterator siter = r.listProperties() ;
// for ( ; siter.hasNext() ; )
// System.out.println(siter.next(**)) ;
RDFDataMgr.write(System.out, model, Lang.TTL) ;
model.close(); //OntModel is closed as well.
}
}