您的位置:首页 > 其它

jena SPARQL查询的简单应用

2015-04-02 11:06 387 查看
一个个简单的三元组,繁杂的世界,二进制的忧伤谁能懂..

接上篇应用OWL API旅游数据本体的建立,接下来就是从这个本体数据库里查询需要的内容

SPARQL的全部基础就是这个简单的概念:尝试去找到能够匹配一个给定图模式的那些三元组集合。他和SQL有许多相似之处,学习起来也比较简单。

从之前建立的owl中都去本体,新建查询(位于青岛的名字中有华严寺的10个景点),然后执行查询,遍历结果显示。

public static void main(String[] args) throws OWLOntologyCreationException, OWLOntologyStorageException, IOException{
OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);

ontModel.read("file:/home/fssqawj/example.owl");
String queryString = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" +
"PREFIX owl: <http://www.w3.org/2002/07/owl#>" +
"PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>" +
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>" +
"PREFIX my: <http://www.semanticweb.org/fssqawj/ontologies/2015/2/untitled-ontology-2#>" +
"SELECT ?subject WHERE { ?subject my:locateCity my:青岛. ?subject my:hasName ?o. FILTER regex(?o,\"^华严寺)} limit 10";

Query query = QueryFactory.create(queryString);
QueryExecution qe = QueryExecutionFactory.create(query, ontModel);
ResultSet results = qe.execSelect();

while (results.hasNext()) {
QuerySolution qs = results.next();
System.out.println(qs.get("subject"));
System.out.println(qs.get("o"));
}

// ResultSetFormatter.out(System.out, results, query);
qe.close();
System.out.println("done");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: