2014-04-03 8 views
5

Ho provato ad usare il seguente codice java per eliminare un ontologia e restituire l'istanza del leone della classe ma quando ho provato a eseguire il file, sto ricevendo un errore sulla linea 16. Quindi sto aspettando il tuo aiuto, per favore!come interrogare un file ontologico con jena su eclipse

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

questo è il file 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 . 

l'errr è la seguente:

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

Che cos'è esattamente l'errore? Puoi fornire una traccia dello stack? – cybersam

+0

l'errore è la seguente: Exception in thread "main" java.lang.Error: problemi di compilazione non risolti: \t registro non può essere risolto \t IndentedWriter non può essere risolto in un tipo \t a Jungle1.main (Jungle1.java: 16) – Nanis

+0

Che cos'è la traccia dello stack? Sono nuovo di java – Nanis

risposta

3

Di seguito risolve tutti gli errori di compilazione. Presumo si intende utilizzare il pacchetto org.apache.log4j per la registrazione:

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

Ho importato indentedWriter (import com.hp.hpl.jena.sparql.util.IndentedWriter;) ma ho ricevuto un erreur – Nanis

+0

Ancora una volta, si prega di fornire * tutti * i messaggi di errore. Non riesco a leggerti nella mente :-). – cybersam

+0

:) gli stessi errori: Exception in thread "main" java.lang.Error: problemi di compilazione non risolte: \t registro non può essere risolto \t IndentedWriter non può essere risolto in un tipo \t a Jungle1.main (Jungle1.java: 16) – Nanis

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

Il file non si trova nella directory corrente.

Risolvere il problema con molte copie di Java: alcune di quelle più vecchie potrebbero aver bisogno di "file:" sul lato anteriore, ma le versioni più recenti no.

Problemi correlati