简体   繁体   中英

OWL file created by Jena doesn't open in Protege

I have created simple ontology using Jena and Java in Eclipse. It has 3 classess. "Information" is super class and "Book Information" and "Food Information" is 2 sub classes. I used following code for this.

import java.io.*;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.*;

public class Create_Ontology extends Object{ 
      public static void main (String args[]) {

         String NS = "http://localhost/new/";
         OntModel m = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
         String tempst=NS+"#";
         m.setNsPrefix("new", tempst);

        String a= "Information";
        String b= "Book Information";
        String c= "Food Information";

         // Create a new class named "Information"
              OntClass Info = m.createClass(NS + a); 

        // Create a new class named "Book Information" 
              OntClass BookInfo = m.createClass(NS+b); 

        // Create a new class named "Food Information"
              OntClass FoodInfo = m.createClass(NS + c); 

           Info.addSubClass(BookInfo);
           Info.addSubClass(FoodInfo);

         m.write(System.out);
        try {
              m.write(new FileWriter("C:/wamp/www/new/onto1.owl"),"RDF/XML");
              m.write(new FileWriter("C:/wamp/www/new/onto2.owl"), "N3");         
              } catch (IOException e) {
              e.printStackTrace();
              }

      }
}

The OWL file it generated contains the following:

<?xml version="1.0" encoding="windows-1252"?>
<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
    xmlns:new="http://localhost/new/#"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" > 
  <rdf:Description rdf:about="http://localhost/new/Book Information">
    <rdfs:subClassOf rdf:resource="http://localhost/new/Information"/>
    <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://localhost/new/Food Information">
    <rdfs:subClassOf rdf:resource="http://localhost/new/Information"/>
    <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://localhost/new/Information">
    <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
  </rdf:Description>
</rdf:RDF>

I have tried to open it using protage software. It could open. But, no classes to see inside it. What are the things I need to change in this code for view my generated ontology in protage?

Your file does not have an ontology declaration.

After adding it:

m.createOntology(NS + "ontology"); // I made up the IRI. It can be any valid IRI

The ontology should now be readable. I have parsed and saved it with OWL API (same library Protege uses) and it looks like this:

(Functional Syntax)

Prefix(:=<http://localhost/new/ontology#>)
Prefix(new:=<http://localhost/new/#>)
Prefix(owl:=<http://www.w3.org/2002/07/owl#>)
Prefix(rdf:=<http://www.w3.org/1999/02/22-rdf-syntax-ns#>)
Prefix(xml:=<http://www.w3.org/XML/1998/namespace>)
Prefix(xsd:=<http://www.w3.org/2001/XMLSchema#>)
Prefix(rdfs:=<http://www.w3.org/2000/01/rdf-schema#>)
Ontology(<http://localhost/new/ontology>
Declaration(Class(<http://localhost/new/Information>))
Declaration(Class(<http://localhost/new/Book%20Information>))
Declaration(Class(<http://localhost/new/Food%20Information>))
SubClassOf(<http://localhost/new/Book%20Information> <http://localhost/new/Information>)
SubClassOf(<http://localhost/new/Food%20Information> <http://localhost/new/Information>)
)

In RDF/XML:

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:new="http://localhost/new/#"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#" > 
  <rdf:Description rdf:about="http://localhost/new/Information">
    <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://localhost/new/Food Information">
    <rdfs:subClassOf rdf:resource="http://localhost/new/Information"/>
    <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://localhost/new/ontology">
    <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Ontology"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://localhost/new/Book Information">
    <rdfs:subClassOf rdf:resource="http://localhost/new/Information"/>
    <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
  </rdf:Description>
</rdf:RDF>

And as saved by the OWL API:

<?xml version="1.0"?>
<rdf:RDF xmlns="http://localhost/new/ontology#"
     xml:base="http://localhost/new/ontology"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     xmlns:owl="http://www.w3.org/2002/07/owl#"
     xmlns:xml="http://www.w3.org/XML/1998/namespace"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
    <owl:Ontology rdf:about="http://localhost/new/ontology"/>
    <owl:Class rdf:about="http://localhost/new/Information"/>
    <owl:Class rdf:about="http://localhost/new/Book%20Information">
        <rdfs:subClassOf rdf:resource="http://localhost/new/Information"/>
    </owl:Class>
    <owl:Class rdf:about="http://localhost/new/Food%20Information">
        <rdfs:subClassOf rdf:resource="http://localhost/new/Information"/>
    </owl:Class>
</rdf:RDF>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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