Java 如何在eclipse上使用jena查询本体文件

Java 如何在eclipse上使用jena查询本体文件,java,sparql,jena,ontology,Java,Sparql,Jena,Ontology,我试图使用下面的java代码来构建一个本体并返回lion类的实例,但是当我试图运行该文件时,第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.

我试图使用下面的java代码来构建一个本体并返回lion类的实例,但是当我试图运行该文件时,第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() ;
}
}
}

下面将解决所有编译错误。我假设您打算使用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();
        }
    }
}
import org.apache.jena.atlas.io.IndentedWriter;
导入org.apache.log4j.Logger;
导入com.hp.hpl.jena.query.query;
导入com.hp.hpl.jena.query.QueryExecution;
导入com.hp.hpl.jena.query.QueryExecutionFactory;
导入com.hp.hpl.jena.query.QueryFactory;
导入com.hp.hpl.jena.query.QuerySolution;
导入com.hp.hpl.jena.query.ResultSet;
导入com.hp.hpl.jena.rdf.model.model;
导入com.hp.hpl.jena.rdf.model.ModelFactory;
导入com.hp.hpl.jena.rdf.model.RDFNode;
导入com.hp.hpl.jena.rdf.model.Resource;
导入com.hp.hpl.jena.util.FileManager;
导入com.hp.hpl.jena.词汇表.RDF;
公共类丛林1{
公共静态最终字符串jungle\u file=“jungle.n3”;
公共静态最终字符串NL=System.getProperty(“line.separator”);
私有静态最终记录器log=Logger.getLogger(“Jungle1”);
公共静态void main(字符串[]args){
//创建最简单的模型
//
最终模型m=ModelFactory.createDefaultModel();
//使用文件管理器将RDF文档读入模型
FileManager.get().readModel(m,jungle\u文件);
log.debug(“我们加载了一个带有no.statements=“+m.size())的模型);
最终字符串丛林=”http://www.lirmm.fr/jungle#";
最后一个字符串序言1=“前缀丛林:”;
最后一个字符串prolog2=“前缀rdf:”;
//查询字符串。
最后一个字符串queryString=prolog1+NL+prolog2+NL+“SELECT?individu WHERE{?individu rdf:type jungle:Lion}”;
最终查询=QueryFactory.create(queryString);
//用行号打印
序列化(新的缩进写入程序(System.out,true));
System.out.println();
//创建此查询的单个执行,应用于模型
//它被包装成一个数据集
final QueryExecution qexec=QueryExecutionFactory.create(query,m);
//创建(queryString,model);
System.out.println(“狮子座”);
试一试{
//假设:这是一个SELECT查询。
最终结果集rs=qexec.execSelect();
//结果的顺序未定义。
对于(;rs.hasNext();){
最终查询解决方案rb=rs.nextSolution();
//获取标题-变量名不包括“?”
最终RDFNode y=rb.get(“个人”);
系统输出打印(“uri:+y+”--);
最终资源z=rb.getResource(“individu”);
System.out.println(“plus simplement”+z.getLocalName());
}
}最后{
//QueryExecution对象应关闭以释放任何系统
//资源
qexec.close();
}
}
}
在当前目录中找不到该文件


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

该文件找不到,因此我将该文件的完整URL放入其中,以解决错误: 公共静态最终字符串jungle\u file=“”

灌输: 公共静态最终字符串jungle\u file=“jungle.n3”


非常感谢您的所有贡献

错误到底是什么?您可以提供堆栈跟踪吗?错误如下:线程“main”java.lang中出现异常。错误:未解决的编译问题:无法解决日志IndentedWriter无法解析为Jungle1.main(Jungle1.java:16)的类型什么是堆栈跟踪?我不熟悉Java,您可以在运行时获得未捕获异常的堆栈跟踪。在您的情况下,您的代码甚至没有编译,因此您没有堆栈跟踪。我已经导入了indentedWriter(import com.hp.hpl.jena.sparql.util.indentedWriter;),但是我得到了一个错误消息。再次,请提供所有错误消息。我看不懂你的心思:-)。:)同样的错误:线程“main”java.lang中出现异常。错误:未解决的编译问题:无法解决日志IndentedWriter无法解析为Jungle1.main(Jungle1.java:16)的类型好,请您适当地定义“log”变量,看看会发生什么?我想问题出在我开始导入com.hp.hpl.jena.sparql.util.IndentedWriter时
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)
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();
        }
    }
}
Exception in thread "main" org.apache.jena.riot.RiotNotFoundException: Not found: jungle.n3