使用createOntologyModel时出现异常

发布于 2024-11-01 00:50:51 字数 3980 浏览 4 评论 0原文

package tutorial;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
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.ResultSetFormatter;
import com.hp.hpl.jena.rdf.model.ModelFactory;

public class Jena {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub


        InputStream in = new FileInputStream(new File("E:\\Applications\\workspace-protoge\\periodic.owl"));
        OntModel model2 = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM);
        model2.read( in, null );
        //prints out the RDF/XML structure
        in.close();
        System.out.println(" ");


        // Create a new query
        String queryString =        
          "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "+
            "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>  "+
            "select ?uri "+
            "where { "+
             "?uri rdfs:subClassOf <http://www.co-ode.org/roberts/pto.owl#Charge>  "+
            "} \n ";
        Query query = QueryFactory.create(queryString);

        System.out.println("----------------------");

        System.out.println("Query Result Sheet");

        System.out.println("----------------------");

        System.out.println("Direct&Indirect Descendants (model1)");

        System.out.println("-------------------");


        // Execute the query and obtain results
        QueryExecution qe = QueryExecutionFactory.create(query, model2);
        com.hp.hpl.jena.query.ResultSet results =  qe.execSelect();

        // Output query results    
        ResultSetFormatter.out(System.out, results, query);

    }

}

运行上面的代码时,我收到以下警告。 我不明白为什么

 WARN [main] (OntDocumentManager.java:1078) - An error occurred while attempting to read from http://www.cs.man.ac.uk/~stevensr/ontology/units.owl. Msg was 'java.net.SocketException: Permission denied: connect'.
com.hp.hpl.jena.shared.JenaException: java.net.SocketException: Permission denied: connect
    at com.hp.hpl.jena.rdf.arp.JenaReader.read(JenaReader.java:91)
    at com.hp.hpl.jena.rdf.model.impl.ModelCom.read(ModelCom.java:187)
    at com.hp.hpl.jena.util.FileManager.readModelWorker(FileManager.java:367)
    at com.hp.hpl.jena.util.FileManager.readModel(FileManager.java:335)
    at com.hp.hpl.jena.util.FileManager.readModel(FileManager.java:319)
    at com.hp.hpl.jena.ontology.OntDocumentManager.read(OntDocumentManager.java:1064)
    at com.hp.hpl.jena.ontology.OntDocumentManager$1.readModel(OntDocumentManager.java:1034)
    at com.hp.hpl.jena.rdf.model.impl.ModelMakerImpl.getModel(ModelMakerImpl.java:78)
    at com.hp.hpl.jena.ontology.OntDocumentManager.fetchLoadedImportModel(OntDocumentManager.java:1031)
    at com.hp.hpl.jena.ontology.OntDocumentManager.fetchPossiblyCachedImportModel(OntDocumentManager.java:1004)
    at com.hp.hpl.jena.ontology.OntDocumentManager.loadImport(OntDocumentManager.java:977)
    at com.hp.hpl.jena.ontology.OntDocumentManager.loadImports(OntDocumentManager.java:771)
    at com.hp.hpl.jena.ontology.OntDocumentManager.loadImports(OntDocumentManager.java:709)
    at com.hp.hpl.jena.ontology.impl.OntModelImpl.loadImports(OntModelImpl.java:1887)
    at com.hp.hpl.jena.ontology.impl.OntModelImpl.read(OntModelImpl.java:2050)
    at tutorial.Jena.main(Jena.java:30)
Caused by: java.net.SocketException: Permission denied: connect
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance
package tutorial;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
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.ResultSetFormatter;
import com.hp.hpl.jena.rdf.model.ModelFactory;

public class Jena {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub


        InputStream in = new FileInputStream(new File("E:\\Applications\\workspace-protoge\\periodic.owl"));
        OntModel model2 = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM);
        model2.read( in, null );
        //prints out the RDF/XML structure
        in.close();
        System.out.println(" ");


        // Create a new query
        String queryString =        
          "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "+
            "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>  "+
            "select ?uri "+
            "where { "+
             "?uri rdfs:subClassOf <http://www.co-ode.org/roberts/pto.owl#Charge>  "+
            "} \n ";
        Query query = QueryFactory.create(queryString);

        System.out.println("----------------------");

        System.out.println("Query Result Sheet");

        System.out.println("----------------------");

        System.out.println("Direct&Indirect Descendants (model1)");

        System.out.println("-------------------");


        // Execute the query and obtain results
        QueryExecution qe = QueryExecutionFactory.create(query, model2);
        com.hp.hpl.jena.query.ResultSet results =  qe.execSelect();

        // Output query results    
        ResultSetFormatter.out(System.out, results, query);

    }

}

On running the above code,i get the following warning.
I cannot understand why

 WARN [main] (OntDocumentManager.java:1078) - An error occurred while attempting to read from http://www.cs.man.ac.uk/~stevensr/ontology/units.owl. Msg was 'java.net.SocketException: Permission denied: connect'.
com.hp.hpl.jena.shared.JenaException: java.net.SocketException: Permission denied: connect
    at com.hp.hpl.jena.rdf.arp.JenaReader.read(JenaReader.java:91)
    at com.hp.hpl.jena.rdf.model.impl.ModelCom.read(ModelCom.java:187)
    at com.hp.hpl.jena.util.FileManager.readModelWorker(FileManager.java:367)
    at com.hp.hpl.jena.util.FileManager.readModel(FileManager.java:335)
    at com.hp.hpl.jena.util.FileManager.readModel(FileManager.java:319)
    at com.hp.hpl.jena.ontology.OntDocumentManager.read(OntDocumentManager.java:1064)
    at com.hp.hpl.jena.ontology.OntDocumentManager$1.readModel(OntDocumentManager.java:1034)
    at com.hp.hpl.jena.rdf.model.impl.ModelMakerImpl.getModel(ModelMakerImpl.java:78)
    at com.hp.hpl.jena.ontology.OntDocumentManager.fetchLoadedImportModel(OntDocumentManager.java:1031)
    at com.hp.hpl.jena.ontology.OntDocumentManager.fetchPossiblyCachedImportModel(OntDocumentManager.java:1004)
    at com.hp.hpl.jena.ontology.OntDocumentManager.loadImport(OntDocumentManager.java:977)
    at com.hp.hpl.jena.ontology.OntDocumentManager.loadImports(OntDocumentManager.java:771)
    at com.hp.hpl.jena.ontology.OntDocumentManager.loadImports(OntDocumentManager.java:709)
    at com.hp.hpl.jena.ontology.impl.OntModelImpl.loadImports(OntModelImpl.java:1887)
    at com.hp.hpl.jena.ontology.impl.OntModelImpl.read(OntModelImpl.java:2050)
    at tutorial.Jena.main(Jena.java:30)
Caused by: java.net.SocketException: Permission denied: connect
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乞讨 2024-11-08 00:50:51

异常跟踪的第一行清楚地说明了问题:

WARN [main] (OntDocumentManager.java:1078) - 
An error occurred while attempting to read from 
http://www.cs.man.ac.uk/~stevensr/ontology/units.owl. 
Msg was 'java.net.SocketException: Permission denied: connect'.

您正在尝试阅读http://www.cs.man.ac.uk/~stevensr/ontology/units.owl,并且失败。由于该文件确实存在(我刚刚检查过),因此很可能您没有连接到网络,或者您位于 Web 代理后面,因此您必须使用适当的代理设置来配置 JVM。

为什么你的代码要读取该文件?几乎可以肯定,这是因为您正在阅读的本体导入了单位本体。类似于:

<> a owl:Ontology ; 
   owl:imports <http://www.cs.man.ac.uk/~stevensr/ontology/units.owl>

该语句将被 OntModel 加载器识别,它将尝试获取并加载导入的本体。如果这不是您想要的,或者不方便(例如,因为您并不总是在网络上),那么您有三种补救措施:

  • 关闭导入处理:yourOntModel.getDocumentManager().setProcessImports(false);
  • 从源模型中删除 owl:imports 语句 - 关于它是否真正有用的观点各不相同,无论如何
  • 使用 Jena 的 LocationMapper提供替代位置>units.owl 文件,以便您仍然可以使用 owl:imports 语句,但实际文件将从其他位置读取(例如在计算机磁盘上)

The problem is clearly stated in the first line of the exception trace:

WARN [main] (OntDocumentManager.java:1078) - 
An error occurred while attempting to read from 
http://www.cs.man.ac.uk/~stevensr/ontology/units.owl. 
Msg was 'java.net.SocketException: Permission denied: connect'.

You are trying to read http://www.cs.man.ac.uk/~stevensr/ontology/units.owl, and failing. Since that file does exist (I just checked), the likelihood is either that you are not connected to a network, or that you are behind a web proxy, and so you will have to configure your JVM with the appropriate proxy settings.

Why is your code reading that file? Almost certainly, it's because the ontology you are reading imports the units ontology. Something like:

<> a owl:Ontology ; 
   owl:imports <http://www.cs.man.ac.uk/~stevensr/ontology/units.owl>

That statement will be recognised by the OntModel loader, which will attempt to fetch and load the imported ontology. If this is not what you want, or it's inconvenient (e.g. because you are not on a network always), then you have three remedies:

  • turn off imports processing: yourOntModel.getDocumentManager().setProcessImports(false);
  • remove the owl:imports statement from your source model - opinions vary as to whether or not it's really useful anyway
  • use Jena's LocationMapper to provide an alternative location for the units.owl file, so that you can still have the owl:imports statement, but the actual file will be read from somewhere else (e.g. on your computer's disk)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文