//用的是protege 裏面的people demo
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
public class myOntology {
public static void main(String[] args) {
// String owlpath = "F:\\Ontology1385473236444.owl";
// 創建一個本體模型,這裏使用的是前一段時間設計的IIPO本體,附帶實例。
OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
model.read("file:F:\\Ontology1385473236444.owl");
// 創建一個查詢語句
String prefix="PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>"+
"PREFIX xsd:<http://www.w3.org/2000/10/XMLSchema#>"+
"PREFIX owl:<http://www.w3.org/2002/07/owl#>"+
"PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>"+
"PREFIX base:<http://www.project.com/d2o_owl>"+
"PREFIX iqas:<http://owl.man.ac.uk/2006/07/sssw/people#>";
String strquery="ASK {iqas:Mick iqas:drives iqas:Q123_ABC }";
//" WHERE { iqas:Com1 rdf:type ?subject}";
Query query=QueryFactory.create(prefix+strquery);
// 創建一個查詢
// 執行查詢,獲得結果
QueryExecution qe = QueryExecutionFactory.create(query, model);
//ResultSet results = qe.execSelect();//select 類型
boolean results = qe.execAsk() ;//ASK類型
// 向控制檯輸出結果s
//ResultSetFormatter.out(System.out, results, query);//select
System.out.println(results);//ask
// 釋放資源
qe.close();
}
}
jena 簡單查詢(不帶推理,直接讀取owl文件)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.