java - 如何清理 Jena InfModel 结果?

标签 java jena owl inference

我正在耶拿从事推理工作。我从 Jena official tutorial 开始 我尝试使用以下代码在控制台中显示 InfModel 结果:

infModel.write(System.out, "RDF/XML-ABBREV");

但是结果并不干净,它显示了许多不必要的信息。它显示:

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
    xmlns="urn:x-hp:eg/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" > 
  <rdf:Description rdf:about="http://www.w3.org/2001/XMLSchema#double">
    <rdf:type rdf:resource="http://www.w3.org/2000/01/rdf-schema#Datatype"/>
    <rdf:type rdf:resource="http://www.w3.org/2000/01/rdf-schema#Resource"/>
    <rdf:type rdf:resource="http://www.w3.org/2000/01/rdf-schema#Class"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://www.w3.org/2001/XMLSchema#float">
    <rdf:type rdf:resource="http://www.w3.org/2000/01/rdf-schema#Datatype"/>
    <rdf:type rdf:resource="http://www.w3.org/2000/01/rdf-schema#Class"/>
    <rdf:type rdf:resource="http://www.w3.org/2000/01/rdf-schema#Resource"/>
    <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
    <owl:disjointWith rdf:resource="http://www.w3.org/2001/XMLSchema#short"/>
    <owl:disjointWith rdf:resource="http://www.w3.org/2001/XMLSchema#byte"/>
    <owl:disjointWith rdf:resource="http://www.w3.org/2001/XMLSchema#decimal"/>
    <owl:disjointWith rdf:resource="http://www.w3.org/2001/XMLSchema#date"/>
    <owl:disjointWith rdf:resource="http://www.w3.org/2001/XMLSchema#integer"/>
    <owl:disjointWith rdf:resource="http://www.w3.org/2001/XMLSchema#duration"/>
    <owl:disjointWith rdf:resource="http://www.w3.org/2001/XMLSchema#unsignedLong"/>
    <owl:disjointWith rdf:resource="http://www.w3.org/2001/XMLSchema#unsignedShort"/>
    <owl:disjointWith rdf:resource="http://www.w3.org/2001/XMLSchema#nonPositiveInteger"/>
    <owl:disjointWith rdf:resource="http://www.w3.org/2001/XMLSchema#boolean"/>
    <owl:disjointWith rdf:resource="http://www.w3.org/2001/XMLSchema#long"/>
    <owl:disjointWith rdf:resource="http://www.w3.org/2001/XMLSchema#unsignedInt"/>
    <owl:disjointWith rdf:resource="http://www.w3.org/2001/XMLSchema#time"/>
    <owl:disjointWith rdf:resource="http://www.w3.org/2001/XMLSchema#dateTime"/>
    <owl:disjointWith rdf:resource="http://www.w3.org/2001/XMLSchema#int"/>
    <owl:disjointWith rdf:resource="http://www.w3.org/2001/XMLSchema#string"/>
    <owl:disjointWith rdf:resource="http://www.w3.org/2001/XMLSchema#nonNegativeInteger"/>
    <owl:disjointWith rdf:resource="http://www.w3.org/2001/XMLSchema#unsignedByte"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://www.w3.org/2001/XMLSchema#unsignedLong">
    <rdf:type rdf:resource="http://www.w3.org/2000/01/rdf-schema#Datatype"/>
    <rdfs:subClassOf rdf:resource="http://www.w3.org/2001/XMLSchema#unsignedLong"/>
    <rdfs:subClassOf rdf:resource="http://www.w3.org/2001/XMLSchema#integer"/>
    <rdfs:subClassOf rdf:resource="http://www.w3.org/2001/XMLSchema#long"/>
    <rdfs:subClassOf rdf:resource="http://www.w3.org/2001/XMLSchema#nonNegativeInteger"/>
    <rdf:type rdf:resource="http://www.w3.org/2000/01/rdf-schema#Class"/>
    <rdf:type rdf:resource="http://www.w3.org/2000/01/rdf-schema#Resource"/>
    <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
    <owl:disjointWith rdf:resource="http://www.w3.org/2001/XMLSchema#float"/>
    <owl:disjointWith rdf:resource="http://www.w3.org/2001/XMLSchema#time"/>
    <owl:disjointWith rdf:resource="http://www.w3.org/2001/XMLSchema#dateTime"/>
    <owl:disjointWith rdf:resource="http://www.w3.org/2001/XMLSchema#string"/>
    <owl:disjointWith rdf:resource="http://www.w3.org/2001/XMLSchema#duration"/>
    <owl:disjointWith rdf:resource="http://www.w3.org/2001/XMLSchema#date"/>
    <owl:disjointWith rdf:resource="http://www.w3.org/2001/XMLSchema#boolean"/>
    <owl:equivalentClass rdf:resource="http://www.w3.org/2001/XMLSchema#unsignedLong"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://www.w3.org/2002/07/owl#backwardCompatibleWith">
    <rdfs:domain rdf:resource="http://www.w3.org/2002/07/owl#Ontology"/>
    <rdfs:range rdf:resource="http://www.w3.org/2002/07/owl#Ontology"/>
    <rdfs:subPropertyOf rdf:resource="http://www.w3.org/2002/07/owl#backwardCompatibleWith"/>
    <rdfs:range rdf:resource="http://www.w3.org/2000/01/rdf-schema#Resource"/>
    <rdf:type rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#Property"/>
    <rdf:type rdf:resource="http://www.w3.org/2000/01/rdf-schema#Resource"/>
    <rdfs:domain rdf:resource="http://www.w3.org/2000/01/rdf-schema#Resource"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://www.w3.org/2001/XMLSchema#unsignedByte">
    <rdf:type rdf:resource="http://www.w3.org/2000/01/rdf-schema#Datatype"/>
    <rdfs:subClassOf rdf:resource="http://www.w3.org/2001/XMLSchema#unsignedLong"/>
    <rdfs:subClassOf rdf:resource="http://www.w3.org/2001/XMLSchema#unsignedInt"/>
    <rdfs:subClassOf rdf:resource="http://www.w3.org/2001/XMLSchema#int"/>
    <rdfs:subClassOf rdf:resource="http://www.w3.org/2001/XMLSchema#integer"/>
    <rdfs:subClassOf rdf:resource="http://www.w3.org/2001/XMLSchema#long"/>
    <rdfs:subClassOf rdf:resource="http://www.w3.org/2001/XMLSchema#byte"/>
    <rdfs:subClassOf rdf:resource="http://www.w3.org/2001/XMLSchema#short"/>
    <rdfs:subClassOf rdf:resource="http://www.w3.org/2001/XMLSchema#unsignedByte"/>
    <rdfs:subClassOf rdf:resource="http://www.w3.org/2001/XMLSchema#nonNegativeInteger"/>
    <rdfs:subClassOf rdf:resource="http://www.w3.org/2001/XMLSchema#unsignedShort"/>
    <rdf:type rdf:resource="http://www.w3.org/2000/01/rdf-schema#Class"/>
    <rdf:type rdf:resource="http://www.w3.org/2000/01/rdf-schema#Resource"/>
    <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
    <owl:disjointWith rdf:resource="http://www.w3.org/2001/XMLSchema#dateTime"/>
    <owl:disjointWith rdf:resource="http://www.w3.org/2001/XMLSchema#boolean"/>
    <owl:disjointWith rdf:resource="http://www.w3.org/2001/XMLSchema#time"/>
    <owl:disjointWith rdf:resource="http://www.w3.org/2001/XMLSchema#float"/>
    <owl:disjointWith rdf:resource="http://www.w3.org/2001/XMLSchema#string"/>
    <owl:disjointWith rdf:resource="http://www.w3.org/2001/XMLSchema#duration"/>
    <owl:disjointWith rdf:resource="http://www.w3.org/2001/XMLSchema#date"/>
    <owl:equivalentClass rdf:resource="http://www.w3.org/2001/XMLSchema#unsignedByte"/>
  </rdf:Description>
  <rdf:Description rdf:nodeID="A0">
    <rdf:type rdf:resource="http://www.w3.org/2000/01/rdf-schema#Resource"/>
    <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Thing"/>
    <rdf:type rdf:resource="urn:x-hp:eg/Bundle"/>
    <rdf:type rdf:resource="urn:x-hp:eg/GameBundle"/>
    <owl:sameAs rdf:nodeID="A0"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://www.w3.org/2001/XMLSchema#hexBinary">
    <rdf:type rdf:resource="http://www.w3.org/2000/01/rdf-schema#Datatype"/>
    <rdf:type rdf:resource="http://www.w3.org/2000/01/rdf-schema#Class"/>
    <rdf:type rdf:resource="http://www.w3.org/2000/01/rdf-schema#Resource"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://www.w3.org/2002/07/owl#Restriction">
    <rdfs:subClassOf rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
    <rdf:type rdf:resource="http://www.w3.org/2000/01/rdf-schema#Class"/>
    <rdfs:subClassOf rdf:resource="http://www.w3.org/2000/01/rdf-schema#Resource"/>
    <rdfs:subClassOf rdf:resource="http://www.w3.org/2002/07/owl#Restriction"/>
    <rdf:type rdf:resource="http://www.w3.org/2000/01/rdf-schema#Resource"/>
    <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
    <owl:equivalentClass rdf:resource="http://www.w3.org/2002/07/owl#Restriction"/>
  </rdf:Description>
  <rdf:Description rdf:about="urn:x-hp:eg/Bundle">
    <rdfs:subClassOf rdf:resource="http://www.w3.org/2002/07/owl#Thing"/>
    <rdf:type rdf:resource="http://www.w3.org/2000/01/rdf-schema#Class"/>
    <rdfs:subClassOf rdf:resource="http://www.w3.org/2000/01/rdf-schema#Resource"/>
    <rdfs:subClassOf rdf:resource="urn:x-hp:eg/Bundle"/>
    <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
    <rdf:type rdf:resource="http://www.w3.org/2000/01/rdf-schema#Resource"/>
    <owl:equivalentClass rdf:resource="urn:x-hp:eg/Bundle"/>
  </rdf:Description>
</rdf:RDF>

你知道如何最正确地显示InfModel吗? 感谢您的帮助。

最佳答案

But the result is not clean,

结果非常干净;它是格式良好的数据,具有从您提供的数据中得出的逻辑推论。

it shows many informations that are not necessary.

我不确定您的意思是:(i) 数据不正确(即,它不一定来自输入)还是 (ii) 这只是您不感兴趣的数据(即,它是您不需要的数据)。 (i) 不正确;推断语句确实是从数据中得出的。例如,xsd:float 与 xsd:date 不相交。 (ii)可能是正确的;您还没有告诉使用您需要哪些数据和不需要哪些数据,因此我们无法真正说出您想要或不想要哪些数据。您也没有显示您的代码,因此很难知道您的推论来自何处。

如果您使用 Pellet,那么您可能会对 How can I extract all inferences? 感兴趣。来自 Pellet FAQ,其中描述了 ModelExtractor 的使用:

   // Create an inference extractor
   ModelExtractor extractor = new ModelExtractor(model);

   // Extract default set of inferences
   Model inferences = extractor.extractModel();

如果您不使用 Pellet,那么您仍然可以使用 Jena 模型的 listStatements 来列出模型中的一些语句。您可以创建一个选择器,仅提取您认为“必要”的语句。 (除非您告诉我们什么是“必要的”,否则我们无法真正帮助您实现选择器。)

listStatements

StmtIterator listStatements(Selector s)

List the statements matching a selector. A statment is considered to match if the test method of s returns true when called on s.

模型还有一个 add(StmtIterator) 方法,因此您可以列出 InfModel 中的语句,并将它们添加到另一个模型,然后打印第二个模型。

关于java - 如何清理 Jena InfModel 结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24570287/

相关文章:

python - rdflib "repeat node-elements"OWL/XML 文件解析错误

class - OWL 不同类的相同对象属性

rdf - 我如何在 OWL 中声明属性必须具有一组有序值?

java - 在 Android 中保存 JPEG 文件而不丢失像素信息

java - 当我访问某个网址时,如何下载在网络浏览器中自动下载的文件?

java - 是否有适用于 MongoDB 的 DbUnit 替代方案?

java - 将网站的所有数据存储在 Apache Jena 上作为 RDF?

java - API Jena - 我的界面的按钮 "Next"无法正常工作

java - onBackPressed 并调用刷新函数 android

java - Jena NoClassDefFoundError 与 Maven