【问题标题】:how to query an ontology file with jena on eclipse如何在eclipse上用jena查询本体文件
【发布时间】:2014-04-03 18:32:44
【问题描述】:

我尝试使用以下 java 代码来获取本体并返回狮子类的实例,但是当我尝试运行该文件时,我在第 16 行遇到错误。所以我正在等待请帮忙!

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 .

错误如下:

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)

【问题讨论】:

  • 究竟是什么错误?你能提供一个堆栈跟踪吗?
  • 错误如下:线程“main”中的异常java.lang.Error:未解决的编译问题:无法解析日志IndentedWriter无法解析为Jungle1.main的类型(Jungle1.java: 16)
  • 什么是堆栈跟踪?我是java新手
  • 您会在运行时获得未捕获异常的堆栈跟踪。在你的情况下,你的代码甚至没有编译,所以你没有堆栈跟踪。

标签: java sparql jena ontology


【解决方案1】:

以下解决所有编译错误。我假设您打算使用 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();
        }
    }
}

【讨论】:

  • 我已经导入了 indentedWriter (import com.hp.hpl.jena.sparql.util.IndentedWriter;) 但我得到了一个错误
  • 再次,请提供所有错误信息。我无法读懂你的想法:-)。
  • :) 相同的错误:线程“main”中的异常 java.lang.Error:未解决的编译问题:无法解析日志 IndentedWriter 无法解析为 Jungle1.main 的类型(Jungle1.java: 16)
  • 好的,你能适当地定义'log'变量,看看会发生什么吗?
  • 当我想导入 com.hp.hpl.jena.sparql.util.IndentedWriter 时,我认为问题出在第一个
【解决方案2】:
Exception in thread "main" org.apache.jena.riot.RiotNotFoundException: Not found: jungle.n3

在当前目录中找不到该文件。

修复 Java 的多个副本可能会有所帮助 - 其中一些较旧的副本可能需要在前面加上“file:”,但较新的版本不需要。

【讨论】:

    【解决方案3】:

    该文件未找到,因此我已输入该文件的完整 URL,以解决错误: public static final String Jungle_file = "http://www.lirmm.fr/~mougenot/Enseignement/FMIN321/jungle.n3"

    在: public static final String Jungle_file = "jungle.n3"

    非常感谢您的所有贡献

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多