OWL Jena 创建的文件无法在 Protege 中打开
OWL file created by Jena doesn't open in Protege
我在 Eclipse 中使用 Jena 和 Java 创建了简单的 ontology。它有 3 classess。 "Information" 是超 class 和 "Book Information" 和 "Food Information" 是 2 个子 class。我为此使用了以下代码。
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软件打开。它可以打开。但是,没有 class 可以看到它的内部。我需要在此代码中更改哪些内容才能在 protage 中查看我生成的 ontology?
您的文件没有 ontology 声明。
添加后:
m.createOntology(NS + "ontology"); // I made up the IRI. It can be any valid IRI
ontology 现在应该是可读的了。我已经用 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>
我在 Eclipse 中使用 Jena 和 Java 创建了简单的 ontology。它有 3 classess。 "Information" 是超 class 和 "Book Information" 和 "Food Information" 是 2 个子 class。我为此使用了以下代码。
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软件打开。它可以打开。但是,没有 class 可以看到它的内部。我需要在此代码中更改哪些内容才能在 protage 中查看我生成的 ontology?
您的文件没有 ontology 声明。
添加后:
m.createOntology(NS + "ontology"); // I made up the IRI. It can be any valid IRI
ontology 现在应该是可读的了。我已经用 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>