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:"+
            "PREFIX xsd:"+
            "PREFIX owl:"+
            "PREFIX rdfs:"+
            "PREFIX base:"+
            "PREFIX iqas:";
            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();
              
        }  
  
    }  

你可能感兴趣的:(Ontology)