gpt4 book ai didi

java - OWL-API 5.1.6 SWRL 规则生成和 RDFXMLDocumentFormat : rules are merged

转载 作者:行者123 更新时间:2023-12-02 10:55:15 32 4
gpt4 key购买 nike

什么:

我面临着我所说的问题,但它可能不是。

为了满足某些需求,我通过 OWL API 创建一系列 swrl 规则,这些规则在序列化后将通过简单的文件上传添加到 stardog 存储库中。

出于示例目的,我调整了代码来生成两个非常简单的 swrl 规则公理:对于给定的术语列表,如果某个项目具有所有术语,则必须将其分类为参数中的分类类。

这里,我选择了

  • 番茄 -> 关于蔬菜
  • 猴子、驴 -> 关于动物

如何:

我使用这个版本:

[根据 Ignazio 提示进行编辑]

compile group: 'net.sourceforge.owlapi', name: 'owlapi-distribution', version: '5.1.6'

所以我有类似的东西来产生 swrl 规则公理:

import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.formats.RDFXMLDocumentFormat;
import org.semanticweb.owlapi.model.*;

import java.io.File;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class StackOverflowExemple {

private final static String BASE_IRI = "http://foo.bar/exemple.owl";
private final static String CLASS_ITEM = BASE_IRI + "#Item";
private final static String CLASS_TERM = BASE_IRI + "#Term";
private final static String PROP_ISCLASSIFIED = BASE_IRI + "#isClassified";
private final static String PROP_HASTERM = BASE_IRI + "#hasTerm";
private final static String IND_IT = BASE_IRI + "#it";

public static void main(String[] args) throws Exception {
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLOntology onto = manager.createOntology();

SWRLRule rule1 = createRule(manager, Arrays.asList(new String[]{"tomato"}), BASE_IRI + "#aboutVegetables");
manager.applyChange(new AddAxiom(onto, rule1));

SWRLRule rule2 = createRule(manager, Arrays.asList(new String[]{"monkey", "donkey"}), BASE_IRI + "#aboutAnimals");
manager.applyChange(new AddAxiom(onto, rule2));

File output = new File("foo.rdf");
OWLDocumentFormat format = new RDFXMLDocumentFormat();
manager.saveOntology(onto, format, IRI.create(output));
}

private static SWRLRule createRule(OWLOntologyManager manager,
List<String> inputWords,
String classificationClass) throws Exception {

OWLDataFactory factory = manager.getOWLDataFactory();

OWLClass classItem = factory.getOWLClass(IRI.create(CLASS_ITEM));
OWLClass classTerm = factory.getOWLClass(IRI.create(CLASS_TERM));

/**
* Prepararing andecedent (has term conditions)
*/
SWRLVariable varItem = factory.getSWRLVariable(IRI.create(IND_IT));
OWLObjectProperty propHasTerm = factory.getOWLObjectProperty(IRI.create(PROP_HASTERM));

Set<SWRLAtom> antecedent = new HashSet<>();

for (String term : inputWords) {
OWLNamedIndividual termInd = factory.getOWLNamedIndividual(IRI.create(BASE_IRI + "#" + term));
SWRLIndividualArgument termIndArg = factory.getSWRLIndividualArgument(termInd);
SWRLObjectPropertyAtom propAtom = factory.getSWRLObjectPropertyAtom(propHasTerm,
varItem,
termIndArg);
antecedent.add(propAtom);
antecedent.add(factory.getSWRLClassAtom(classTerm, termIndArg));
antecedent.add(factory.getSWRLClassAtom(classItem, varItem));
}

/**
* Building consequent part
*/
OWLNamedIndividual classificationIndividual = factory.getOWLNamedIndividual(IRI.create(classificationClass));

OWLObjectProperty propClassified = factory.getOWLObjectProperty(IRI.create(PROP_ISCLASSIFIED));

SWRLObjectPropertyAtom propClassifiedAtom = factory.getSWRLObjectPropertyAtom(propClassified,
varItem,
factory.getSWRLIndividualArgument(classificationIndividual));

Set<SWRLAtom> consequent = new HashSet<>();
consequent.add(propClassifiedAtom);

/**
* Create the swrl rule
*/
SWRLRule rule = factory.getSWRLRule(antecedent,
consequent);

return rule;
}

}

出了什么问题:

问题出在输出上,swrl规则被合并了。

输出文件包含两个规则,没关系,但如果你看第一个规则,它包含第二个规则,或类似的东西。

输出文件内容(仅swrl规则部分):

<rdf:Description>
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#Imp"/>
<swrl:body>
<rdf:Description>
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#AtomList"/>
<rdf:first>
<rdf:Description>
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#IndividualPropertyAtom"/>
<swrl:propertyPredicate rdf:resource="http://foo.bar/exemple.owl#hasTerm"/>
<swrl:argument1 rdf:resource="http://foo.bar/exemple.owl#it"/>
<swrl:argument2 rdf:resource="http://foo.bar/exemple.owl#tomato"/>
</rdf:Description>
</rdf:first>
<rdf:rest>
<rdf:Description>
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#AtomList"/>
<rdf:first>
<rdf:Description>
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#ClassAtom"/>
<swrl:classPredicate rdf:resource="http://foo.bar/exemple.owl#Term"/>
<swrl:argument1 rdf:resource="http://foo.bar/exemple.owl#tomato"/>
</rdf:Description>
</rdf:first>
<rdf:rest>
<rdf:Description>
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#AtomList"/>
<rdf:first>
<rdf:Description>
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#Imp"/>
<swrl:body>
<rdf:Description>
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#AtomList"/>
<rdf:first>
<rdf:Description>
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#ClassAtom"/>
<swrl:classPredicate rdf:resource="http://foo.bar/exemple.owl#Term"/>
<swrl:argument1 rdf:resource="http://foo.bar/exemple.owl#monkey"/>
</rdf:Description>
</rdf:first>
<rdf:rest>
<rdf:Description>
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#AtomList"/>
<rdf:first>
<rdf:Description>
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#IndividualPropertyAtom"/>
<swrl:propertyPredicate rdf:resource="http://foo.bar/exemple.owl#hasTerm"/>
<swrl:argument1 rdf:resource="http://foo.bar/exemple.owl#it"/>
<swrl:argument2 rdf:resource="http://foo.bar/exemple.owl#monkey"/>
</rdf:Description>
</rdf:first>
<rdf:rest>
<rdf:Description>
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#AtomList"/>
<rdf:first>
<rdf:Description>
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#IndividualPropertyAtom"/>
<swrl:propertyPredicate rdf:resource="http://foo.bar/exemple.owl#hasTerm"/>
<swrl:argument1 rdf:resource="http://foo.bar/exemple.owl#it"/>
<swrl:argument2 rdf:resource="http://foo.bar/exemple.owl#donkey"/>
</rdf:Description>
</rdf:first>
<rdf:rest>
<rdf:Description>
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#AtomList"/>
<rdf:first>
<rdf:Description>
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#ClassAtom"/>
<swrl:classPredicate rdf:resource="http://foo.bar/exemple.owl#Term"/>
<swrl:argument1 rdf:resource="http://foo.bar/exemple.owl#donkey"/>
</rdf:Description>
</rdf:first>
<rdf:rest>
<rdf:Description>
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#AtomList"/>
<rdf:first>
<rdf:Description>
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#ClassAtom"/>
<swrl:classPredicate rdf:resource="http://foo.bar/exemple.owl#Item"/>
<swrl:argument1 rdf:resource="http://foo.bar/exemple.owl#it"/>
</rdf:Description>
</rdf:first>
<rdf:rest rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"/>
</rdf:Description>
</rdf:rest>
</rdf:Description>
</rdf:rest>
</rdf:Description>
</rdf:rest>
</rdf:Description>
</rdf:rest>
</rdf:Description>
</swrl:body>
<swrl:head>
<rdf:Description>
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#AtomList"/>
<rdf:first>
<rdf:Description>
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#IndividualPropertyAtom"/>
<swrl:propertyPredicate rdf:resource="http://foo.bar/exemple.owl#isClassified"/>
<swrl:argument1 rdf:resource="http://foo.bar/exemple.owl#it"/>
<swrl:argument2 rdf:resource="http://foo.bar/exemple.owl#aboutAnimals"/>
</rdf:Description>
</rdf:first>
<rdf:rest rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"/>
</rdf:Description>
</swrl:head>
</rdf:Description>
</rdf:first>
<rdf:rest rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"/>
</rdf:Description>
</rdf:rest>
</rdf:Description>
</rdf:rest>
</rdf:Description>
</swrl:body>
<swrl:head>
<rdf:Description>
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#AtomList"/>
<rdf:first>
<rdf:Description>
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#IndividualPropertyAtom"/>
<swrl:propertyPredicate rdf:resource="http://foo.bar/exemple.owl#isClassified"/>
<swrl:argument1 rdf:resource="http://foo.bar/exemple.owl#it"/>
<swrl:argument2 rdf:resource="http://foo.bar/exemple.owl#aboutVegetables"/>
</rdf:Description>
</rdf:first>
<rdf:rest rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"/>
</rdf:Description>
</swrl:head>
</rdf:Description>
<rdf:Description>
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#Imp"/>
<swrl:body>
<rdf:Description>
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#AtomList"/>
<rdf:first>
<rdf:Description>
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#ClassAtom"/>
<swrl:classPredicate rdf:resource="http://foo.bar/exemple.owl#Term"/>
<swrl:argument1 rdf:resource="http://foo.bar/exemple.owl#monkey"/>
</rdf:Description>
</rdf:first>
<rdf:rest>
<rdf:Description>
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#AtomList"/>
<rdf:first>
<rdf:Description>
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#IndividualPropertyAtom"/>
<swrl:propertyPredicate rdf:resource="http://foo.bar/exemple.owl#hasTerm"/>
<swrl:argument1 rdf:resource="http://foo.bar/exemple.owl#it"/>
<swrl:argument2 rdf:resource="http://foo.bar/exemple.owl#monkey"/>
</rdf:Description>
</rdf:first>
<rdf:rest>
<rdf:Description>
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#AtomList"/>
<rdf:first>
<rdf:Description>
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#IndividualPropertyAtom"/>
<swrl:propertyPredicate rdf:resource="http://foo.bar/exemple.owl#hasTerm"/>
<swrl:argument1 rdf:resource="http://foo.bar/exemple.owl#it"/>
<swrl:argument2 rdf:resource="http://foo.bar/exemple.owl#donkey"/>
</rdf:Description>
</rdf:first>
<rdf:rest>
<rdf:Description>
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#AtomList"/>
<rdf:first>
<rdf:Description>
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#ClassAtom"/>
<swrl:classPredicate rdf:resource="http://foo.bar/exemple.owl#Term"/>
<swrl:argument1 rdf:resource="http://foo.bar/exemple.owl#donkey"/>
</rdf:Description>
</rdf:first>
<rdf:rest>
<rdf:Description>
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#AtomList"/>
<rdf:first>
<rdf:Description>
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#ClassAtom"/>
<swrl:classPredicate rdf:resource="http://foo.bar/exemple.owl#Item"/>
<swrl:argument1 rdf:resource="http://foo.bar/exemple.owl#it"/>
</rdf:Description>
</rdf:first>
<rdf:rest rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"/>
</rdf:Description>
</rdf:rest>
</rdf:Description>
</rdf:rest>
</rdf:Description>
</rdf:rest>
</rdf:Description>
</rdf:rest>
</rdf:Description>
</swrl:body>
<swrl:head>
<rdf:Description>
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#AtomList"/>
<rdf:first>
<rdf:Description>
<rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#IndividualPropertyAtom"/>
<swrl:propertyPredicate rdf:resource="http://foo.bar/exemple.owl#isClassified"/>
<swrl:argument1 rdf:resource="http://foo.bar/exemple.owl#it"/>
<swrl:argument2 rdf:resource="http://foo.bar/exemple.owl#aboutAnimals"/>
</rdf:Description>
</rdf:first>
<rdf:rest rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"/>
</rdf:Description>
</swrl:head>
</rdf:Description>

帮助:

显然我看到两种可能性:

  • 我以错误的方式使用该库(我已经成功使用了此代码:/)
  • 库存在问题(序列化)

所以,如果您有任何提示,遇到类似的问题,或者只是看看我错在哪里,如果您能分享它,我将很高兴:-)

谢谢

最佳答案

Owlapi 3.5.0 很古老。您看到的结果似乎是 RDF/XML 渲染中的问题。

要尝试两件事:

  • 改用函数语法或 OWL/XML,看看这是否有帮助

  • 使用较新的 owlapi 版本。您的代码应该使用 5.1.6 进行编译,并进行一些小的更改,其中大部分应该仅针对包名称。

编辑:正如所讨论的,实际使用的版本是5.1.6。看到的问题是一个错误,已在版本 5.1.7 中修复。

关于java - OWL-API 5.1.6 SWRL 规则生成和 RDFXMLDocumentFormat : rules are merged,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51816992/

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