如何在python中用sparql、rdflib查询本体

2024-10-01 11:25:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经开发了我自己的本体(我定义了我的类、属性等),我想用sparql来询问我的本体。在

在protégé2000(开源本体编辑器)中,一切正常,但是当我想用python实现我的请求sparql时,我遇到了一些问题。在

我用Java做了它,它起作用了,但这不是我想要的,我想用pyjnius(一个Python模块,以Python类的形式访问Java类)来实现它,但是没有任何东西起作用。在

如何使用sparql来查询我的本体?有没有办法在Python中使用jena?在

我就是这样用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();
    } 

这些属性包括:签名人、主题、部门、管理员等。。。。。(法语)sqarql请求基于这些属性

我想用Python来做,有人知道我怎么做?!在


Tags: pathlegetmodel属性本体queryowl
1条回答
网友
1楼 · 发布于 2024-10-01 11:25:30

如果你想用Jena Fuseki的话,看看这个。 Jena TDB in Python?

我可以推荐一种使用rdflib的方法。我对rdflib的工作仅限于简单的查询和序列化。最简单的方法是加载图形(以nt、ttl等任何RDF格式)查询图形并根据需要格式化结果。在

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]

我忘了接你的getText电话,小心点。 上面的代码是针对python2的,应该在三倍.nt数据

请评论并让我知道你对这个答案的看法。 关于rdflib的来源不多,所以如果你有任何与此相关的问题,请联系我,我很乐意探索。在

相关问题 更多 >