Reputation: 21
I have developed my own ontology (I defined my classes, properties, etc.) and I want to interrogate my ontology with sparql.
in protégé 2000 (open-source ontology editor) everything works fine, but when I want to implement my request sparql in python I encountered some problems.
I did it in Java and it worked but it's not what I want, I wanted to it do with pyjnius
(A Python module to access Java classes as Python classes) but nothing worked also.
How I can use sparql to interrogate my ontology? Is there any way to use jena in Python?
that's how i did it with java :
try{
Model model = ModelFactory.createDefaultModel();
String FName = "C:\\Users\\p\\Desktop\\protégé project jour\\jour.owl";
InputStream inStr = FileManager.get().open(FName);
if (inStr == null) { throw new IllegalArgumentException("Fichier non trouvé");}
// Lire le fichier RDF vers le modèle précédemment créé.
model.read(inStr, "");
//****************************
String requete =
//***=====This is the query that works good in the ontology with properties between classes
"PREFIX OntoJO:<http://www.owl-ontologies.com/Ontology1400008538.owl#>" +
"SELECT ?path " +
"WHERE { "
+ " ?n OntoJO:signee_par '"+choixsignrech1.getText()+"' ."
+ " ?s OntoJO:mot_cle '"+choixclrech1.getText()+"' ."
+ " ?m OntoJO:secteur '"+choixsecrech1.getSelectedItem()+"' ."
+ " ?f OntoJO:ministere '"+choixminisrech1.getSelectedItem()+"' ."
+ " ?r OntoJO:synonymes '"+choixsyrech1.getText()+"' ."
+ "?n OntoJO:a_un_chemin ?y . "
+ "?s OntoJO:a_un_chemin ?y . "
+ "?m OntoJO:a_un_chemin ?y . "
+ "?f OntoJO:a_un_chemin ?y . "
+ "?r OntoJO:a_un_chemin ?y . "
+ "?y OntoJO:chemin ?path . }";
Query query = QueryFactory.create(requete);
QueryExecution qexec = QueryExecutionFactory.create(query, model);
try {
ResultSet results = qexec.execSelect();
while (results.hasNext()){
QuerySolution soln = results.nextSolution();
RDFNode name = soln.get("path");
System.out.println(name);
javax.swing.JOptionPane.showMessageDialog(this,soln.get("path"));
}
} finally
{
qexec.close();
}
the properties are: signee_par, mot_cle, secteur, ministere etc ..... ( in french ), the sqarql request is based on these properties
i wanna do it with python, anyone know how i can ?!
Upvotes: 2
Views: 3181
Reputation: 189
Look at this if you want to use Jena Fuseki. Jena TDB in Python?
I can suggest a way to use rdflib. My work with rdflib has been limited to simple queries and serialization. The simplest way would be to load your graph ( in any of the RDF formats nt,ttl etc.) Query the graph and format the results as required.
import rdflib
graph = rdflib.Graph()
graph = graph.parse("triples.nt",format = "nt")
query = "PREFIX OntoJO:<http://www.owl-ontologies.com /Ontology1400008538.owl#>" +\
"SELECT ?path " +\
"WHERE { "\
\
+ " ?n OntoJO:signee_par '"+choixsignrech1.getText()+"' ." \
+ " ?s OntoJO:mot_cle '"+choixclrech1.getText()+"' ." \
+ " ?m OntoJO:secteur '"+choixsecrech1.getSelectedItem()+"' ."\
+ " ?f OntoJO:ministere '"+choixminisrech1.getSelectedItem()+"' ."\
+ " ?r OntoJO:synonymes '"+choixsyrech1.getText()+"' ."\
+ "?n OntoJO:a_un_chemin ?y . "\
+ "?s OntoJO:a_un_chemin ?y . "\
+ "?m OntoJO:a_un_chemin ?y . "\
+ "?f OntoJO:a_un_chemin ?y . "\
+ "?r OntoJO:a_un_chemin ?y . "\
+ "?y OntoJO:chemin ?path . }"
result = graph.query(query)
#The result will be a QueryRow object because of the SELECT , look in the docs for further info.
for i in result:
print i[0]
I have neglected replacing your getText Calls, take care. The above code is in for python2 and should print all the results of the query on the triple.nt data
Please comment and let me know your views on this answers. There aren't many sources about rdflib, so ping me if you have any questions related to the same and I would happy to explore it.
Upvotes: 2