2016-02-26 69 views
-3

我有一個問題,我的查詢本體owl文件,僅顯示與主體和客體的標籤,但沒有類,屬性表...我無法使用Jena Sparql與Java查詢我的本體論?

程序可以成功讀取的本體論,但我需要從我的本體得到一些數據。

有沒有人有解決方案?請儘快修復。我會很感激。

這裏是我的代碼:

package ontologypro; 

import com.hp.hpl.jena.ontology.OntModel; 
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; 
import com.hp.hpl.jena.query.ResultSetFormatter; 
import com.hp.hpl.jena.rdf.model.ModelFactory; 
import com.hp.hpl.jena.ontology.OntModelSpec; 
import com.hp.hpl.jena.rdf.model.Model; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 

public class OntologyPro { 

    /** 
    * @param args the command line arguments 
    * @throws java.io.IOException 
    */ 

    public static void main (String args[]) throws IOException{ 

     // 
     String filename = "C:/Actor.owl"; 
     Model model=ModelFactory.createDefaultModel(); 
     OntModel model1=ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM); 

     try { 
      File file=new File(filename); 
      FileInputStream reader=new FileInputStream(file); 
      System.out.println("The absolute path of the file is:"+ file.getAbsolutePath()); 

      model.read(reader, "RDF/XML"); 
      model.close(); 

      // Create a SPARQL query from the given string. 
      String queryString = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "+ 
      "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 act: <http://www.semanticweb.org/project/ontologies/2016/0/Actor#>" + 
      "select ?subject ?object "+ 
      "where { "+ 
      " ?subject rdf:type ?object "+ 
      "} \n "; 

      Query query = QueryFactory.create(queryString); 

      try (// Execute the query and obtain results 
       QueryExecution qe = QueryExecutionFactory.create(query, model1)) { 
       com.hp.hpl.jena.query.ResultSet results = qe.execSelect(); 

       // Output query results 
       ResultSetFormatter.out(System.out, results, query); 
       qe.close(); 
      } 
     } catch(Exception e) { 
      System.out.println(e.getMessage()); 
     }   
    } 
} 

Here is the query is input: Please follow the link

+0

您是否收到任何錯誤?發佈的代碼有什麼問題? – aribeiro

+0

沒有錯誤,但是當我查詢本體時,我什麼也得不到;我已經創建了類,數據屬性,對象屬性以及一些具有保護功能的實例。即使Sparql保護我不能得到結果,這是另一個問題!有什麼建議麼 ? – DRAL

回答

0

的代碼將數據加載到model但查詢model1model1爲空。

+0

那麼請問如何解決? @AndyS – DRAL

+2

你一定是在開玩笑...那麼簡單地在你的代碼中查詢對象'model'呢? – AKSW