首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jena NoReaderForLangException: owl

Jena NoReaderForLangException: owl
EN

Stack Overflow用户
提问于 2013-10-07 19:58:32
回答 1查看 462关注 0票数 0

我的问题是使用Jena时,当我使用Turtle Ontolgy时,它工作得很好,而当我使用任何其他本体时,如OWL或RDFS,它显示相同的错误

线程"main“com.hp.hpl.jena.shared.NoReaderForLangException: owl在com.hp.hpl.jena.rdf.model.impl.RDFReaderFImpl.getReader(RDFReaderFImpl.java:110)的com.hp.hpl.jena.rdf.model.impl.ModelCom.read(ModelCom.java:225)在com.hp.hpl.jena.ontology.impl.OntModelImpl.read(OntModelImpl.java:2169)的symenticweb.SymenticWeb.main(SymenticWeb.java:109)出现异常结果:1

第109行为model.read(inputStream,null,inputFileFormat);

我的代码是

代码语言:javascript
复制
package symenticweb;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Iterator;

import org.mindswap.pellet.jena.PelletReasonerFactory;

import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.reasoner.Reasoner;
import com.hp.hpl.jena.reasoner.ValidityReport;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;

public class SymenticWeb
{
   /**
    * This program takes 4 parameters an input file name 
    * an output file name an input file format a reasoning 
    * level {RDFS, OWL-DL}
    */
   public static void main(String[] args)
   {
       //validate the program arguments
//      if(args.length != 4) 
//      {   
//         System.err.println("Usage: java InferenceExample "
//            + "<input file> <input format> <output file> "
//            + "<none|rdfs|owl>");
//         return;
//      }

      String inputFileName;
       inputFileName = "C:\\Users\\Harjinder\\Documents\\NetBeansProjects\\SymenticWeb\\src\\test\\abc.owl";
      String inputFileFormat = "owl";
      String outputFileName = "C:\\New folder\\abc.txt";
      String reasoningLevel = "none";

      //create an input stream for the input file
      FileInputStream inputStream = null;
      PrintWriter writer = null;
      try 
      {
         inputStream = new FileInputStream(inputFileName);
      } catch (FileNotFoundException e) {
         System.err.println("'" + inputFileName 
            + "' is an invalid input file.");
         return;
      }

      //create an output print writer for the results
      try 
      {
         writer = new PrintWriter(outputFileName);
      } catch (FileNotFoundException e) {
         System.err.println("'" + outputFileName 
            + "' is an invalid output file.");
         return;
      }

      //create the appropriate jena model
      OntModel model = null;
      if("none".equals(reasoningLevel.toLowerCase()))
      {
         /*
          * "none" is jena model with OWL_DL
          * ontologies loaded and no inference enabled
          */
         model = ModelFactory.createOntologyModel(
            OntModelSpec.OWL_DL_MEM);
      }
      else if("rdfs".equals(reasoningLevel.toLowerCase()))
      {
         /*
          * "rdfs" is jena model with OWL_DL
          * ontologies loaded and RDFS inference enabled 
          */
         model = ModelFactory.createOntologyModel(
            OntModelSpec.OWL_DL_MEM_RDFS_INF); 
      }
      else if("owl".equals(reasoningLevel.toLowerCase()))
      {
         /*
          * "owl" is jena model with OWL_DL ontologies
          * wrapped around a pellet-based inference model
          */
         Reasoner reasoner = 
            PelletReasonerFactory.theInstance().create();
         Model infModel = ModelFactory.createInfModel(
            reasoner, ModelFactory.createDefaultModel());
         model = ModelFactory.createOntologyModel(
            OntModelSpec.OWL_DL_MEM, infModel);
      }
      else
      {
         //invalid inference setting
         System.err.println("Invalid inference setting, "
            + "choose one of <none|rdfs|owl>.");
         return;
      }

      //load the facts into the model
      model.read(inputStream, null, inputFileFormat);

      //validate the file
      ValidityReport validityReport = model.validate();
      if(validityReport != null && !validityReport.isValid())
      {
         Iterator i = validityReport.getReports();
         while(i.hasNext())
         {
            System.err.println(
               ((ValidityReport.Report)i.next()).getDescription());
         }
         return;
      }

      //Iterate over the individuals, print statements about them
      ExtendedIterator iIndividuals = model.listIndividuals();
      while(iIndividuals.hasNext())
      {
         Individual i = (Individual)iIndividuals.next();
         printIndividual(i, writer);
      }
      iIndividuals.close();

      writer.close();
      model.close();
   }

   /**
    * Print information about the individual
    * @param i The individual to output
    * @param writer The writer to which to output
    */
   public static void printIndividual(
      Individual i, PrintWriter writer)
   {
      //print the local name of the individual (to keep it terse)
      writer.println("Individual: " + i.getLocalName());

      //print the statements about this individual
      StmtIterator iProperties = i.listProperties();
      while(iProperties.hasNext())
      {
         Statement s = (Statement)iProperties.next();
         writer.println("  " + s.getPredicate().getLocalName() 
            + " : " + s.getObject().toString());
      }
      iProperties.close();
      writer.println();
   }
}
EN

回答 1

Stack Overflow用户

发布于 2013-10-07 22:18:08

OWL可以被序列化为RDF,RDF可以被序列化为许多不同的格式。Jena是一个基于RDF的工具,您需要拥有本体的RDF序列化,以便Jena能够读取它。

幸运的是,以.owl结尾的文件通常(但不总是)是OWL本体的RDF编码的RDF/XML序列化。您还没有向我们展示您的本体,所以我们还不能确定,但最有可能的是,如果您更改

代码语言:javascript
复制
String inputFileFormat = "owl";

代码语言:javascript
复制
String inputFileFormat = "RDF/XML";

你会好起来的。(假设您的.owl文件是RDF/XML。)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19224318

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档