Question

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 .

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.

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)
Was it helpful?

Solution

Premature end of file. means that the input was not properly formed XML and is truncated in someway or tags not closed appropriately.

OTHER TIPS

AndyS pointed out in an answer that the premature end of file is just what it sounds like: the input file is malformed. 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. 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. 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

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. From the 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. If the resource is referenced in an RDF List, i.e. as the object of an rdf:first statement in a list cell, this reference is not removed. 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. 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.

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top