gpt4 book ai didi

java - 如何使用 Jena OntModel.listClasses() 方法?

转载 作者:行者123 更新时间:2023-11-30 06:18:11 28 4
gpt4 key购买 nike

我正在尝试使用 Jena OntModel 来获取 the direct relations的一个本体。问题来自listClasses()方法。

我在网上搜索了一段时间的提示,但没有找到与我的问题相关的答案。

所以这里有一个完整的例子,用最少的数据和代码显示出了什么问题。

例如,我有一个基本本体(N-Triple 格式):

<http://weblab.ow2.org/wookie#Anti-social_behaviour> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://weblab.ow2.org/wookie#CriminalEvent>.
<http://weblab.ow2.org/wookie#Robbery> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://weblab.ow2.org/wookie#CriminalEvent>.
<http://weblab.ow2.org/wookie#Vehicle_crime> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://weblab.ow2.org/wookie#CriminalEvent>.
<http://weblab.ow2.org/wookie#Bicycle_theft> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://weblab.ow2.org/wookie#CriminalEvent>.
<http://weblab.ow2.org/wookie#CriminalEvent> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://weblab.ow2.org/wookie#Event>.
<http://weblab.ow2.org/wookie#Event> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://weblab.ow2.org/wookie#WookieThing>.
<http://weblab.ow2.org/wookie#Event> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#Class>.

我基本上希望能够获取所有类,并为每个类获取子类。

我使用以下 JAVA 代码:

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.util.FileManager;

public class Main {

public static void main(final String [] argv) throws FileNotFoundException, IOException {

final OntModel model = ModelFactory.createOntologyModel();

model.read(FileManager.get().open("./src/test/resources/eventOnto.n3"), "", "N-TRIPLE");

// This part allows to check that the ontology model is really loaded and that inference is
// correctly done. WORKING
final List<Statement> statements = model.listStatements().toList();
Collections.sort(statements, new Comparator<Statement>() {
@Override
public int compare(final Statement o1, final Statement o2) {
return o1.toString().compareTo(o2.toString());
}
});

for (final Statement statement : statements) {
System.out.println(statement);
}

System.out.println("-------------------------------------------------");

// Listing all the classes.
final List<OntClass> classes = model.listClasses().toList();
for (final OntClass ontclass : classes) {
System.out.println(ontclass);
}

System.out.println("-------------------------------------------------");

// Bug got nothing. So try with a SPARQL query...
final Query query = QueryFactory.create("PREFIX rdf:<http://www.w3.org/2000/01/rdf-schema#> SELECT distinct ?class WHERE {?class a rdf:Class.}");
final ResultSet queryResult = QueryExecutionFactory.create(query, model).execSelect();
while (queryResult.hasNext()) {
System.out.println(queryResult.next());
}
// and got many results...

}

打印显示本体模型已正确加载并完成基本推理。它还显示 getClasses() 不返回任何内容,而要求类的 SPARQL 查询获得了每个类。

[http://weblab.ow2.org/wookie#Anti-social_behaviour, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://www.w3.org/2000/01/rdf-schema#Class]
[http://weblab.ow2.org/wookie#Anti-social_behaviour, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://www.w3.org/2000/01/rdf-schema#Resource]
[http://weblab.ow2.org/wookie#Anti-social_behaviour, http://www.w3.org/2000/01/rdf-schema#subClassOf, http://weblab.ow2.org/wookie#Anti-social_behaviour]
[http://weblab.ow2.org/wookie#Anti-social_behaviour, http://www.w3.org/2000/01/rdf-schema#subClassOf, http://weblab.ow2.org/wookie#CriminalEvent]
[http://weblab.ow2.org/wookie#Anti-social_behaviour, http://www.w3.org/2000/01/rdf-schema#subClassOf, http://weblab.ow2.org/wookie#Event]
[http://weblab.ow2.org/wookie#Anti-social_behaviour, http://www.w3.org/2000/01/rdf-schema#subClassOf, http://weblab.ow2.org/wookie#WookieThing]
[http://weblab.ow2.org/wookie#Anti-social_behaviour, http://www.w3.org/2000/01/rdf-schema#subClassOf, http://www.w3.org/2000/01/rdf-schema#Resource]
[http://weblab.ow2.org/wookie#Anti-social_behaviour, http://www.w3.org/2000/01/rdf-schema#subClassOf, http://www.w3.org/2002/07/owl#Thing]
...(many other lines)...
-------------------------------------------------
-------------------------------------------------
( ?class = rdf:Property )
( ?class = rdf:List )
( ?class = rdfs:Resource )
( ?class = rdfs:Class )
( ?class = rdf:Statement )
( ?class = rdfs:Literal )
( ?class = <http://weblab.ow2.org/wookie#Event> )
( ?class = rdf:XMLLiteral )
( ?class = owl:Thing )
( ?class = <http://weblab.ow2.org/wookie#WookieThing> )
( ?class = <http://weblab.ow2.org/wookie#CriminalEvent> )
( ?class = rdfs:Container )
( ?class = <http://weblab.ow2.org/wookie#Robbery> )
( ?class = <http://weblab.ow2.org/wookie#Bicycle_theft> )
( ?class = rdf:Seq )
( ?class = rdf:Alt )
( ?class = <http://weblab.ow2.org/wookie#Anti-social_behaviour> )
( ?class = <http://weblab.ow2.org/wookie#Vehicle_crime> )
( ?class = rdfs:ContainerMembershipProperty )
( ?class = rdf:Bag )
( ?class = rdfs:Datatype )

有人知道为什么 OntModel.getClasses() 不返回任何东西吗?

最佳答案

您得不到结果有两个原因。

  1. 默认的本体配置文件是一个 OWL 配置文件,因此您可以使用 getOntologyModel 获得一个 OWL 模型,而无需特定的 OntModelSpec。相反,您可能应该使用 RDFS 本​​体模型。这将使 listClasses 查找 rdfs:Class 的实例而不是 owl:Class。不过,这仍然只会得到一个结果,因为您只声明了一个东西是 rdfs:Class。
  2. 您只将一个东西声明为 rdfs:Class,其余的东西应该被推断为 rdfs:Classes,因为它们是其他东西的子类。这意味着您需要使用推理模型。在这种情况下,您可能需要一个 RDFS 推理模型,但由于 Jena 的(部分)OWL 推理器包含 RDFS 规则,您也可以使用 OWL 模型。

以下代码将您的数据加载到几种不同类型的模型中并显示 listClasses 的结果:

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.jena.riot.Lang;
import org.apache.jena.riot.RDFDataMgr;

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.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;

public class ListClassesExample {
public static void main(String[] args) throws IOException {
String content =
"<http://weblab.ow2.org/wookie#Anti-social_behaviour> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://weblab.ow2.org/wookie#CriminalEvent>.\n" +
"<http://weblab.ow2.org/wookie#Robbery> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://weblab.ow2.org/wookie#CriminalEvent>.\n" +
"<http://weblab.ow2.org/wookie#Vehicle_crime> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://weblab.ow2.org/wookie#CriminalEvent>.\n" +
"<http://weblab.ow2.org/wookie#Bicycle_theft> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://weblab.ow2.org/wookie#CriminalEvent>.\n" +
"<http://weblab.ow2.org/wookie#CriminalEvent> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://weblab.ow2.org/wookie#Event>.\n" +
"<http://weblab.ow2.org/wookie#Event> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://weblab.ow2.org/wookie#WookieThing>.\n" +
"<http://weblab.ow2.org/wookie#Event> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#Class>.";

final Model base = ModelFactory.createDefaultModel();
try ( InputStream in = new ByteArrayInputStream( content.getBytes() )) {
RDFDataMgr.read( base, in, Lang.NTRIPLES );
}

System.out.println( "== OWL Classes (no inference) ==" );
OntModel owlOntology = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM, base );
for ( OntClass klass : owlOntology.listClasses().toList() ) {
System.out.println( klass );
}

System.out.println( "== RDFS Classes (no inference) ==" );
OntModel rdfsOntology = ModelFactory.createOntologyModel( OntModelSpec.RDFS_MEM, base );
for ( OntClass klass : rdfsOntology.listClasses().toList() ) {
System.out.println( klass );
}

System.out.println( "== RDFS Classes (with inference) ==" );
OntModel rdfsOntologyInf = ModelFactory.createOntologyModel( OntModelSpec.RDFS_MEM_RDFS_INF, base );
for ( OntClass klass : rdfsOntologyInf.listClasses().toList() ) {
System.out.println( klass );
}

System.out.println( "== End ==");
}
}
== OWL Classes (no inference) ==
== RDFS Classes (no inference) ==
http://weblab.ow2.org/wookie#Event
== RDFS Classes (with inference) ==
http://www.w3.org/1999/02/22-rdf-syntax-ns#Property
http://www.w3.org/1999/02/22-rdf-syntax-ns#List
http://www.w3.org/2000/01/rdf-schema#Resource
http://www.w3.org/2000/01/rdf-schema#Class
http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement
http://www.w3.org/2000/01/rdf-schema#Literal
http://weblab.ow2.org/wookie#Event
http://weblab.ow2.org/wookie#WookieThing
http://weblab.ow2.org/wookie#CriminalEvent
http://www.w3.org/2000/01/rdf-schema#Container
http://weblab.ow2.org/wookie#Robbery
http://weblab.ow2.org/wookie#Bicycle_theft
http://www.w3.org/1999/02/22-rdf-syntax-ns#Seq
http://www.w3.org/1999/02/22-rdf-syntax-ns#Alt
http://weblab.ow2.org/wookie#Anti-social_behaviour
http://weblab.ow2.org/wookie#Vehicle_crime
http://www.w3.org/2000/01/rdf-schema#ContainerMembershipProperty
http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag
http://www.w3.org/2000/01/rdf-schema#Datatype
http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral
== End ==

这有一些您没有提到的类,因为 RDFS 公理包含它们。如果您只想在数据中声明为 rdfs:Classes 的内容及其子类,则可以使用 SPARQL 查询:

String query = "\n" +
"prefix rdfs: <"+RDFS.getURI()+">\n" +
"\n" +
"select distinct ?class where {\n" +
" { ?class a rdfs:Class } union\n" +
" { ?class rdfs:subClassOf|^rdfs:subClassOf [] }\n" +
"}";
ResultSet results = QueryExecutionFactory.create( query, base ).execSelect();
System.out.println( query );
ResultSetFormatter.out( results );
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>

select distinct ?class where {
{ ?class a rdfs:Class } union
{ ?class rdfs:subClassOf|^rdfs:subClassOf [] }
}
--------------------------------------------------------
| class |
========================================================
| <http://weblab.ow2.org/wookie#Event> |
| <http://weblab.ow2.org/wookie#Bicycle_theft> |
| <http://weblab.ow2.org/wookie#Anti-social_behaviour> |
| <http://weblab.ow2.org/wookie#WookieThing> |
| <http://weblab.ow2.org/wookie#Vehicle_crime> |
| <http://weblab.ow2.org/wookie#CriminalEvent> |
| <http://weblab.ow2.org/wookie#Robbery> |
--------------------------------------------------------

关于java - 如何使用 Jena OntModel.listClasses() 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24997387/

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