简体   繁体   English

耶拿-无法将模型写入猫头鹰文件

[英]Jena - Unable to write model to owl file

I created ontology in protege. 我创造了本体。

Now I am trying to read this ontology (owl) file, and after adding a resource, I want to write it back to local file . 现在,我尝试读取此本体(owl)文件,并在添加资源后将其写回到本地文件。

I wrote this code. 我写了这段代码。 It loads the ontology correctly and also resource is created successfully but it is not writing the model back to the owl file. 它可以正确加载本体,并且可以成功创建资源,但是不会将模型写回到owl文件中。

Can somebody guide me, whats wrong with code? 有人可以指导我,代码有什么问题吗?

static final String inputFileName  = "./target/resources/myontology.owl";
static final String baseURI = "http://www.semanticweb.org/administrator/ontologies/2014/2/untitled-ontology-5";
....

public Resource createIndividual() 
{
    Model model = ModelFactory.createDefaultModel();

    InputStream in = FileManager.get().open(inputFileName);

     if (in == null) 
     {
         throw new IllegalArgumentException( "File: " + inputFileName + " not found");
     }       
     model.read(in, "");
     try {
        in.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

     Resource resource = model.createResource(baseURI+"#"+getRandomUUID());

     FileWriter out=null;
    try {
        out = new FileWriter( inputFileName );
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try 
    {
        model.write( out, "RDF/XML-ABBREV" );
    }
    finally 
    {
       try 
       {
           out.close();
       }
       catch (IOException closeException) 
       {
           // ignore
       }
    }
    return resource;
}

While running it also show some below errors/warnings messages. 在运行时,它还会显示一些下面的错误/警告消息。

SEVERE: Exception thrown: org.apache.jena.riot.RiotException: Premature end of file.
org.apache.jena.riot.system.ErrorHandlerFactory$ErrorHandlerStd.fatal(ErrorHandlerFactory.java:136)
org.apache.jena.riot.lang.LangRDFXML$ErrorHandlerBridge.fatalError(LangRDFXML.java:252)
com.hp.hpl.jena.rdf.arp.impl.ARPSaxErrorHandler.fatalError(ARPSaxErrorHandler.java:48)
com.hp.hpl.jena.rdf.arp.impl.XMLHandler.warning(XMLHandler.java:209)
com.hp.hpl.jena.rdf.arp.impl.XMLHandler.fatalError(XMLHandler.java:239)
org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source)
org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
org.apache.xerces.impl.XMLScanner.reportFatalError(Unknown Source)
org.apache.xerces.impl.XMLDocumentScannerImpl$XMLDeclDispatcher.dispatch(Unknown Source)
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
com.hp.hpl.jena.rdf.arp.impl.RDFXMLParser.parse(RDFXMLParser.java:151)
com.hp.hpl.jena.rdf.arp.ARP.load(ARP.java:119)
org.apache.jena.riot.lang.LangRDFXML.parse(LangRDFXML.java:142)
org.apache.jena.riot.RDFParserRegistry$ReaderRIOTFactoryImpl$1.read(RDFParserRegistry.java:142)
org.apache.jena.riot.RDFDataMgr.process(RDFDataMgr.java:859)
org.apache.jena.riot.RDFDataMgr.read(RDFDataMgr.java:255)
org.apache.jena.riot.RDFDataMgr.read(RDFDataMgr.java:241)
org.apache.jena.riot.adapters.RDFReaderRIOT_Web.read(RDFReaderRIOT_Web.java:62)
com.hp.hpl.jena.rdf.model.impl.ModelCom.read(ModelCom.java:253)

Premature end of file. means that the input was not properly formed XML and is truncated in someway or tags not closed appropriately. 表示输入的格式不正确,并且以某种方式被截断或标签未正确关闭。

AndyS pointed out in an answer that the premature end of file is just what it sounds like: the input file is malformed. AndyS在一个答案中指出,文件的过早结尾听起来像:输入文件格式错误。 However, you've got another problem with the code that you've got that may be confusing. 但是,您的代码还有另一个问题,可能会造成混淆。 You're not actually changing the model, so when you write it back out, you'll have the same thing that you read in the first place. 您实际上并没有更改模型,所以当您将其写回时,将拥有与最初阅读的内容相同的东西。 You're not actually adding anying to the model with the statement 您实际上并没有在语句中添加任何内容

Resource resource = model.createResource(baseURI+"#"+getRandomUUID());

A model, whether it's a plain RDF model, or an OntModel based on OWL, is a collection of triples. 一个模型,无论是普通的RDF模型还是基于OWL的OntModel,都是三元组的集合。 Although it's often convenient to think of a resource being associated with a model (and indeed, resources in Jena are associated with a model), a model is a set of triples, not a set of resources. 尽管通常认为资源与模型相关联是很方便的(实际上,Jena 中的资源与模型相关联),但是模型是一组三元组,而不是一组资源。 When you create a resource with Model's createResource method, you're creating a resource that has a reference to the model so that when you do things like 当您使用Model的createResource方法创建资源时,正在创建具有对模型的引用的资源,以便在执行类似操作时

resource.addProperty(property,object);

you're adding the triple 您正在添加三元组

[resource property object]

to the model. 到模型。 For any resource, the resource only "exists" in the model if there's as triple that has the resource as a subject, predicate, or object. 对于任何资源,只有在三元组以资源为主题,谓词或宾语的情况下,该资源才“存在”于模型中。 If you don't create such a triple, then you won't see any reference to the resource in the output. 如果您没有创建这样的三元组,那么您将在输出中看不到对该资源的任何引用。 After all, what could reference the resource? 毕竟,什么可以引用该资源?

This is why, for instance, OntResource has a remove method that removes all triples that involve the resource. 例如,这就是为什么OntResource具有remove方法,该方法删除涉及该资源的所有三元组。 From the JavaDoc: 从JavaDoc:

remove 去掉

 void remove() 

Removes this resource from the ontology by deleting any statements that refer to it, as either statement-subject or statement-object. 通过删除引用该资源的任何语句(语句主体或语句对象),从本体中删除该资源。 If this resource is a property, this method will not remove statements whose predicate is this property. 如果此资源是属性,则此方法将不会删除谓词为该属性的语句。

Caveat: Jena RDF models contain statements, not resources per se, so this method simulates removal of an object by removing all of the statements that have this resource as subject or object, with one exception. 注意:Jena RDF模型本身包含语句,而不包含资源,因此,该方法通过删除所有以该资源为主体或对象的语句来模拟对象的删除,但有一个例外。 If the resource is referenced in an RDF List, ie as the object of an rdf:first statement in a list cell, this reference is not removed. 如果资源是在RDF列表中引用的,即作为列表单元格中rdf:first语句的对象,则不会删除此引用。 Removing an arbitrary rdf:first statement from the midst of a list, without doing other work to repair the list, would leave an ill-formed list in the model. 从列表中间删除任意rdf:first语句,而不做其他工作来修复列表,将在模型中留下格式错误的列表。 Therefore, if this resource is known to appear in a list somewhere in the model, it should be separately deleted from that list before calling this remove method. 因此,如果已知该资源出现在模型的某个列表中,则应在调用此remove方法之前将其从该列表中单独删除。

To make this clearer, see the following example that shows that creating a resource doesn't add any triples to the model: 为了更清楚地说明这一点,请参见以下示例,该示例显示创建资源不会向模型添加任何三元组:

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.vocabulary.RDF;
import com.hp.hpl.jena.vocabulary.RDFS;

public class TriplesNotResourcesExample {
    public static void main(String[] args) {
        String NS = "https://stackoverflow.com/q/22643081/1281433/";
        Model model = ModelFactory.createDefaultModel();
        Resource resource = model.createResource( NS+"resource" );
        System.out.println( "<!-- BEFORE ADDING PROPERTY -->" );
        model.write( System.out );
        resource.addProperty( RDF.type, RDFS.Resource );
        System.out.println( "\n<!-- AFTER ADDING PROPERTY -->" );
        model.write( System.out );
    }
}
<!-- BEFORE ADDING PROPERTY -->
<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" > 
</rdf:RDF>

<!-- AFTER ADDING PROPERTY -->
<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" > 
  <rdf:Description rdf:about="https://stackoverflow.com/q/22643081/1281433/resource">
    <rdf:type rdf:resource="http://www.w3.org/2000/01/rdf-schema#Resource"/>
  </rdf:Description>
</rdf:RDF>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM