Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to query an ontology file with jena on eclipse

I tried to use the following java code in order to soot an ontology and return the instance of the class lion but when I tried to run the file, I'm getting an error on line 16. So I'm waiting for your help please!

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

this is the N3 file:

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

the errr is as follow:

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)
like image 349
Nanis Avatar asked Oct 01 '22 03:10

Nanis


1 Answers

The following solves all compilation errors. I assume you intend to use the org.apache.log4j package for logging:

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();
        }
    }
}
like image 65
cybersam Avatar answered Oct 03 '22 16:10

cybersam