我已经在Eclipse中使用Jena和Java创建了简单的本体。它有3个班级。 “信息”是超类,“图书信息”和“食品信息”是2个子类。我为此使用了以下代码。

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();
              }

      }
}


它生成的OWL文件包含以下内容:

<?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>


我试图使用protage软件打开它。它可以打开。但是,里面没有类。为了按顺序查看生成的本体,我需要在此代码中进行哪些更改?

最佳答案

您的文件没有本体声明。

添加后:

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


本体现在应该是可读的。我已经使用OWL API(Protege使用的相同库)解析并保存了它,它看起来像这样:

(功能语法)

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>)
)


在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>


并由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>

关于java - Jena创建的OWL文件无法在Protege中打开,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35295746/

10-13 02:41