1. 程式人生 > >jena 簡單查詢(不帶推理,直接讀取owl檔案)

jena 簡單查詢(不帶推理,直接讀取owl檔案)

 //用的是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();
              
        }  
  
    }