2014-04-03 6 views
5

Ich habe versucht, den folgenden Java-Code verwenden, um eine Ontologie Ruß und Rückgabe der Instanz der Klasse Löwe, aber wenn ich versuchte, die Datei auszuführen, erhalte ich einen Fehler in Zeile 16. Ich warte also bitte auf deine Hilfe!Wie Abfrage einer Ontologie-Datei mit jena auf Eklipse

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() ; 
} 
} 
} 

dies ist die N3-Datei:

# 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 . 

die errr ist wie folgt:

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

Was genau ist der Fehler? Können Sie einen Stack-Trace bereitstellen? – cybersam

+0

der Fehler ist wie folgt: Exception in thread „main“ java.lang.Error: Ungelöste Kompilierungsproblemen: \t Protokoll kann nicht \t IndentedWriter nicht geklärt werden können \t bei Jungle1.main (Jungle1.java auf eine Art gelöst werden: 16) – Nanis

+0

Was ist Stack-Trace? Ich bin neu in Java – Nanis

Antwort

3

Nachfolgend löst alle Kompilierungsfehlern. Ich nehme an, Sie beabsichtigen, das org.apache.log4j Paket für die Anmeldung zu verwenden:

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

Ich habe eingerücktWriter (import com.hp.hpl.jena.sparql.util.InducedWriter;) importiert, aber ich habe ein erreur bekommen – Nanis

+0

Auch hier bitte * alle * Fehlermeldungen. Ich kann deine Gedanken nicht lesen :-). – cybersam

+0

:) die gleichen Fehler: Exception in thread "main" java.lang.Error: Ungelöste Kompilierungsproblemen: \t Protokoll kann nicht \t IndentedWriter nicht geklärt werden können \t bei Jungle1.main (Jungle1.java auf eine Art gelöst werden: 16) – Nanis

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

Die Datei wird nicht im aktuellen Verzeichnis gefunden.

Die Reparatur der vielen Kopien von Java kann helfen - einige dieser älteren Kopien benötigen möglicherweise "file:" auf der Vorderseite, aber neuere Versionen nicht.