【发布时间】:2016-12-12 17:01:14
【问题描述】:
我已经开发了自己的本体(我定义了我的类、属性等),我想用 sparql 询问我的本体。
在 protégé 2000(开源本体编辑器)中一切正常,但是当我想在 python 中实现我的请求 sparql 时遇到了一些问题。
我用 Java 完成了它,它工作了,但这不是我想要的,我想用pyjnius(一个 Python 模块,将 Java 类作为 Python 类访问)但也没有任何效果。
如何使用 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();
}
属性有:signee_par、mot_cle、secteur、ministere 等.....(法语),sqarql 请求基于这些属性
我想用 python 来做,有人知道我该怎么做吗?!
【问题讨论】:
-
什么是“Protege 2000”?请添加更多信息,即 SPARQL 查询和 python 代码。什么是“一些问题”?你不能想象如果没有任何关于它的信息,没有人能真正帮助你。
-
@AKSW 对不起,我没有解释我的观点,我只是编辑了帖子,我向你展示了 java 代码,因为我不知道我是如何用 python 真正做到的,我试过但没有兄弟:(。
-
看看你的尝试会很有帮助......我已经使用 Kivy 框架工作了一点,目前正在编写一个 Python Android 应用程序...... Pyjnius 来自 Kivy.org,适用于那些不知道。
-
@Mehdi 为什么不为 Python 使用 RDF/SPARQL 库呢?我建议使用 RDFLib:rdflib.readthedocs.io/en/stable
-
@Jarvis 我无法安装它,我尝试了所有我发现出现此错误的解决方案:无法使用 cpu = 'i386' 分配 machine() = amdto cpu jnius 在 jnius.c “const_char”未声明的标识符中有问题。我在 win 8.1 64 bits python 2.7.10 64bits 上工作
标签: python sparql jena rdflib pyjnius