gpt4 book ai didi

java - Jena 创建的 OWL 文件无法在 Protege 中打开

转载 作者:行者123 更新时间:2023-11-30 07:36:42 25 4
gpt4 key购买 nike

我在 Eclipse 中使用 Jena 和 Java 创建了简单的本体。它有3个类(class)。 “信息”是父类(super class),“图书信息”和“食品信息”是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软件打开它。它可以打开。但是,没有类可以看到里面的内容。为了在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com