2

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 ?!

Mehdi
  • 21
  • 1
  • 6
  • 2
    What is "Protege 2000"? And please add much more information, i.e. the SPARQL query and the python code. And what means "some problems"? Can't you imagine that without any information about it nobody can really help you. – UninformedUser Aug 06 '16 at 11:19
  • @AKSW i'm sorry that i didn't explain my point, i just edited the post, i showed you the java code coz i don't know how i really do it with python, i tried but nothing bro :(. – Mehdi Aug 06 '16 at 12:57
  • 1
    It would be helpful to see what you tried... I've worked with the Kivy framework a bit and am writing a Python Android app currently... Pyjnius comes from Kivy.org for those that don't know. – Jarvis Aug 06 '16 at 13:08
  • 1
    @Mehdi Why not using an RDF/SPARQL lib for Python then? RDFLib is what I suggest: https://rdflib.readthedocs.io/en/stable/ – UninformedUser Aug 06 '16 at 13:55
  • @Jarvis i couldn't install it, i try all the solutions that i found i've this error : not able to assign machine() = amdto cpu using cpu = 'i386' instead and for jnius there's a probleme in the jnius.c " const_char" undeclaredidentifier. i'm workinf on win 8.1 64 bits python 2.7.10 64bits – Mehdi Aug 06 '16 at 16:07
  • @AKSW yeah i saw that but i don't know how actually it works, so i'm gonna see it and try again. i'll tell ya if it will work. thx bro – Mehdi Aug 06 '16 at 16:08
  • I've not worked with Python on Windows... I'll check into something when I get home. – Jarvis Aug 06 '16 at 17:22

1 Answers1

2

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.

Community
  • 1
  • 1
Abhishek P
  • 189
  • 2
  • 9
  • thx for yr answer, i tried what u did and it didn't work :(, i've this error: IOError: [Errno 2] No such file or directory: u'C:\\Users\\Mehdi\\workspace\\My_work\\Test\\triples.nt'. i replace it by: filename = 'JO Ontology modified 09 june 2014 with properties.owl' graph.load(filename, format='xml') graph= graph.parse(filename, format='xml') also there is an error : +"?lois OntoJO:a_un_chemin ?y . "\ TypeError: bad operand type for unary +: 'str' – Mehdi Aug 07 '16 at 22:04
  • graph =rdflib.Graph() filename = r'JO Ontology modified 09 june 2014 with properties.owl' graph.load(filename, format='xml') qres = graph.query( " " " SELECT * WHERE { ?s ?p ?o } limit 5 " " " ) for res in qres.result: print res this is work, this is to make sure i get result back, but when i wanna do some sparql request, i've some errors :( – Mehdi Aug 07 '16 at 22:21
  • @Mehdi You dont have to use where in the query and the unary error is for you to remove the + if you are not adding two strings. And about the second comment , what do you mean by SPARQL request, give me an example. – Abhishek P Aug 08 '16 at 10:25
  • """ PREFIX OntoJO: SELECT ?path WHERE { ?lois OntoJO:ministere_lois ?ministere_lois . ?lois OntoJO:a_un_chemin ?y . ?y OntoJO:chemin ?path }""", ) lois it's a name class, ministere_lois it's a data_property and a_un_chemin it's a object property, i run it with java it works (not the same syntax of course, but when i run it with python now i get nothing. – Mehdi Aug 08 '16 at 10:39
  • Check for python syntax errors, like the comma after the query, run the queries in an incremental manner, one set of conditions after another and check if it works. – Abhishek P Aug 08 '16 at 11:13