2014-04-03 27 views
5

我試圖使用下面的java代碼來爲一個本體論抽出一個本體並返回該類的實例,但是當我嘗試運行該文件時,出現錯誤在線16.所以我在等你的幫助請!如何在eclipse上用jena查詢本體文件

import com.hp.hpl.jena.rdf.model.*; 
import com.hp.hpl.jena.util.FileManager; 
import com.hp.hpl.jena.vocabulary.RDF; 
import com.hp.hpl.jena.sparql.util.IndentedWriter; 
import com.hp.hpl.jena.query.*; 
public class Jungle1 
{ 
public static final String jungle_file = "jungle.n3"; 
public static final String NL = System.getProperty("line.separator") ; 
public static void main(String[] args) { 
// create the simplest model there is 
// 
Model m = ModelFactory.createDefaultModel(); 
// use the file manager to read an RDF document into the model 
FileManager.get().readModel(m, jungle_file); 
log.debug("We have loaded a model with no. statements = " + m.size()); 
String jungle ="http://www.lirmm.fr/jungle#"; 
String prolog1 = "PREFIX jungle: <"+jungle+">" ; 
String prolog2 = "PREFIX rdf: <"+RDF.getURI()+">" ; 
// Query string. 
String queryString = prolog1 + NL + prolog2 + NL + 
"SELECT ?individu WHERE {?individu rdf:type jungle:Lion }" ; 
Query query = QueryFactory.create(queryString) ; 
// Print with line numbers 
query.serialize(new IndentedWriter(System.out,true)) ; 
System.out.println() ; 
// Create a single execution of this query, apply to a model 
// which is wrapped up as a Dataset 
QueryExecution qexec = QueryExecutionFactory.create(query, m) ; 
// Or QueryExecutionFactory.create(queryString, model) ; 
System.out.println("Les Lions : ") ; 
try { 
// Assumption: it’s a SELECT query. 
ResultSet rs = qexec.execSelect() ; 
// The order of results is undefined. 
for (; rs.hasNext() ;) 
{ 
QuerySolution rb = rs.nextSolution() ; 
// Get title - variable names do not include the ’?’ 
RDFNode y = rb.get("individu"); 
System.out.print("uri : "+y+"--- "); 
Resource z = (Resource) rb.getResource("individu"); 
System.out.println("plus simplement "+z.getLocalName()); 
} 
} 
finally 
{ 
// QueryExecution objects should be closed to free any system resources 
qexec.close() ; 
} 
} 
} 

這是N3文件:

# Base: http://www.lirmm.fr/jungle# 
@prefix xsd:  <http://www.w3.org/2001/XMLSchema#> . 
@prefix default: <http://www.lirmm.fr/jungle#> . 
@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#> . 

default:Carnivore 
     a  owl:Class ; 
     rdfs:subClassOf default:Animal ; 
     owl:equivalentClass 
       [ a  owl:Restriction ; 
       owl:allValuesFrom default:Animal ; 
       owl:onProperty default:eats 
       ] . 

default:Lea 
     a  default:Carnivore ; 
     default:hasScientificName 
       "Panthera leo"^^xsd:string . 

default:Animal 
     a  owl:Class ; 
     rdfs:subClassOf owl:Thing ; 
     rdfs:subClassOf 
       [ a  owl:Restriction ; 
       owl:cardinality "1"^^xsd:int ; 
       owl:onProperty default:hasScientificName 
       ] . 

default:Lion 
     a  owl:Class ; 
     rdfs:subClassOf default:Carnivore ; 
     owl:equivalentClass 
       [ a  owl:Restriction ; 
       owl:hasValue "Panthera leo"^^xsd:string ; 
       owl:onProperty default:hasScientificName 
       ] . 

default:eats 
     a  owl:ObjectProperty ; 
     rdfs:domain default:Animal ; 
     owl:inverseOf default:eaten_by . 

default:Cleo 
     a  default:Lion . 

default:Leo 
     a  default:Lion . 

<http://www.lirmm.fr/jungle> 
     a  owl:Ontology . 

default:Clea 
     a  default:Animal ; 
     default:hasScientificName 
       "Panthera leo"^^xsd:string . 

default:eaten_by 
     a  owl:ObjectProperty ; 
     rdfs:range default:Animal ; 
     owl:inverseOf default:eats . 

default:Herbivore 
     a  owl:Class ; 
     rdfs:subClassOf default:Animal . 

default:gigi 
     a  default:Giraffe ; 
     default:hasScientificName 
       "Giraffa camelopardalis"^^xsd:string . 

default:Plant 
     a  owl:Class . 

default:hasScientificName 
     a  owl:DatatypeProperty ; 
     rdfs:range xsd:string . 

default:Giraffe 
     a  owl:Class ; 
     rdfs:subClassOf default:Herbivore . 

的errr情況如下:

log4j:WARN No appenders could be found for logger (org.apache.jena.riot.stream.JenaIOEnvironment). 
log4j:WARN Please initialize the log4j system properly. 
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. 
Exception in thread "main" org.apache.jena.riot.RiotNotFoundException: Not found: jungle.n3 
    at org.apache.jena.riot.RDFDataMgr.open(RDFDataMgr.java:831) 
    at org.apache.jena.riot.RDFDataMgr.open(RDFDataMgr.java:813) 
    at org.apache.jena.riot.RDFDataMgr.parse(RDFDataMgr.java:684) 
    at org.apache.jena.riot.RDFDataMgr.read(RDFDataMgr.java:208) 
    at org.apache.jena.riot.RDFDataMgr.read(RDFDataMgr.java:141) 
    at org.apache.jena.riot.RDFDataMgr.read(RDFDataMgr.java:130) 
    at org.apache.jena.riot.adapters.AdapterFileManager.readModelWorker(AdapterFileManager.java:291) 
    at com.hp.hpl.jena.util.FileManager.readModel(FileManager.java:369) 
    at com.hp.hpl.jena.util.FileManager.readModel(FileManager.java:353) 
    at Jungle1.main(Jungle1.java:28) 
+0

究竟是什麼錯誤?你能提供一個堆棧跟蹤嗎? – cybersam

+0

誤差如下:例外在線程「主要」 java.lang.Error的:未解決的編譯問題: \t日誌不能得到解決 \t IndentedWriter不能被解析爲一個類型 \t在Jungle1.main(Jungle1.java: 16) – Nanis

+0

什麼是堆棧跟蹤?我是java的新手 – Nanis

回答

3

下解決所有編譯錯誤。我假設你打算使用org.apache.log4j包日誌:

import org.apache.jena.atlas.io.IndentedWriter; 
import org.apache.log4j.Logger; 

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.QuerySolution; 
import com.hp.hpl.jena.query.ResultSet; 
import com.hp.hpl.jena.rdf.model.Model; 
import com.hp.hpl.jena.rdf.model.ModelFactory; 
import com.hp.hpl.jena.rdf.model.RDFNode; 
import com.hp.hpl.jena.rdf.model.Resource; 
import com.hp.hpl.jena.util.FileManager; 
import com.hp.hpl.jena.vocabulary.RDF; 

public class Jungle1 { 
    public static final String jungle_file = "jungle.n3"; 
    public static final String NL = System.getProperty("line.separator"); 

    private static final Logger log = Logger.getLogger("Jungle1"); 

    public static void main(String[] args) { 
     // create the simplest model there is 
     // 
     final Model m = ModelFactory.createDefaultModel(); 
     // use the file manager to read an RDF document into the model 
     FileManager.get().readModel(m, jungle_file); 
     log.debug("We have loaded a model with no. statements = " + m.size()); 
     final String jungle = "http://www.lirmm.fr/jungle#"; 
     final String prolog1 = "PREFIX jungle: <" + jungle + ">"; 
     final String prolog2 = "PREFIX rdf: <" + RDF.getURI() + ">"; 
     // Query string. 
     final String queryString = prolog1 + NL + prolog2 + NL + "SELECT ?individu WHERE {?individu rdf:type jungle:Lion }"; 
     final Query query = QueryFactory.create(queryString); 
     // Print with line numbers 
     query.serialize(new IndentedWriter(System.out, true)); 
     System.out.println(); 
     // Create a single execution of this query, apply to a model 
     // which is wrapped up as a Dataset 
     final QueryExecution qexec = QueryExecutionFactory.create(query, m); 
     // Or QueryExecutionFactory.create(queryString, model) ; 
     System.out.println("Les Lions : "); 
     try { 
      // Assumption: it’s a SELECT query. 
      final ResultSet rs = qexec.execSelect(); 
      // The order of results is undefined. 
      for (; rs.hasNext();) { 
       final QuerySolution rb = rs.nextSolution(); 
       // Get title - variable names do not include the ’?’ 
       final RDFNode y = rb.get("individu"); 
       System.out.print("uri : " + y + "--- "); 
       final Resource z = rb.getResource("individu"); 
       System.out.println("plus simplement " + z.getLocalName()); 
      } 
     } finally { 
      // QueryExecution objects should be closed to free any system 
      // resources 
      qexec.close(); 
     } 
    } 
} 
+0

我已經導入了indentedWriter(導入com.hp.hpl.jena.sparql.util.IndentedWriter;)但我得到了一個錯誤 – Nanis

+0

再次,請提供*所有*錯誤消息。我無法讀懂你的想法:-)。 – cybersam

+0

:)同樣的錯誤:異常在線程「主要」 java.lang.Error的:未解決的編譯問題: \t日誌不能得到解決 \t IndentedWriter不能被解析爲一個類型 \t在Jungle1.main(Jungle1.java: 16) – Nanis

1
Exception in thread "main" org.apache.jena.riot.RiotNotFoundException: Not found: jungle.n3 

該文件不在當前目錄中找到。

修復Java的許多副本可能會有所幫助 - 其中一些較舊的副本可能需要「file:」,但較新的版本不需要。