如何在 OWLAPI 中使用 differentFrom vocab 生成三元组
How to produce triple with differentFrom vocab in OWLAPI
我尝试在 Java 中使用 OWLAPI 5 创建不同的个体公理。我想要的是使用 vocab owl:differentFrom 的简单三元组,例如:
test:A owl:differentFrom test:B
但我得到的是三倍使用 owl:AllDifferent:
_:genid234 rdf:type owl:AllDifferent
_:genid234 owl:distinctMembers _:genid236
_:genid236 rdf:rest _:genid235
_:genid236 rdf:rest rdf:nil
_:genid235 rdf:first test:A
_:genid235 rdf:type rdf:List
_:genid236 rdf:first test:B
_:genid236 rdf:type rdf:List
简单的代码是:
OWLNamedIndividual op1 = factory.getOWLNamedIndividual(baseIRI + A);
OWLNamedIndividual op2 = factory.getOWLNamedIndividual(baseIRI + B);
ont.add(factory.getOWLDifferentIndividualsAxiom(op1, op2));
而且我已经在 Protege 5.5.0 上尝试过,它产生与 OWLAPI 完全相同的三元组。
OWLAPI-v5 的另一种实现提供了一种直接处理底层 RDF 数据的方法 - ONT-API(顺便说一下,也存在基于 RDF 的 Protege 版本)。
如果通过 OWLAPI 接口为两个操作数工作,它会生成一个三元组。
如果您喜欢将数据存储在OWL 公理的形式,只需要用序列化解决这个特殊问题。
示例:
String iri = "http://test#";
OWLDataFactory factory = OntManagers.getDataFactory();
Ontology ont = OntManagers.createManager().createOntology();
ont.asGraphModel().setNsPrefix("test", iri);
OWLNamedIndividual ia = factory.getOWLNamedIndividual(iri + "A");
OWLNamedIndividual ib = factory.getOWLNamedIndividual(iri + "B");
OWLNamedIndividual ic = factory.getOWLNamedIndividual(iri + "C");
ont.add(factory.getOWLDifferentIndividualsAxiom(ia, ib, ic));
ont.add(factory.getOWLDifferentIndividualsAxiom(ia, ic));
OntModel g = ont.asGraphModel();
g.createIndividual(iri + "D").addDifferentIndividual(g.createIndividual(iri + "C"));
g.createDifferentIndividuals(g.createIndividual(iri + "e"), g.createIndividual(iri + "g"));
g.write(System.out, "ttl");
System.out.println("=".repeat(42));
ont.axioms().forEach(System.out::println);
输出:
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix test: <http://test#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
[ rdf:type owl:AllDifferent ;
owl:members ( test:e test:g )
] .
test:g rdf:type owl:NamedIndividual .
test:B rdf:type owl:NamedIndividual .
[ rdf:type owl:Ontology ] .
test:e rdf:type owl:NamedIndividual .
test:C rdf:type owl:NamedIndividual .
test:A rdf:type owl:NamedIndividual ;
owl:differentFrom test:C .
test:D rdf:type owl:NamedIndividual ;
owl:differentFrom test:C .
[ rdf:type owl:AllDifferent ;
owl:distinctMembers ( test:A test:B test:C )
] .
==========================================
Declaration(NamedIndividual(test:g))
Declaration(NamedIndividual(test:e))
Declaration(NamedIndividual(test:D))
Declaration(NamedIndividual(test:C))
Declaration(NamedIndividual(test:B))
Declaration(NamedIndividual(test:A))
DifferentIndividuals(test:C test:D)
DifferentIndividuals(test:A test:C)
DifferentIndividuals(test:e test:g)
DifferentIndividuals(test:A test:B test:C)
我尝试在 Java 中使用 OWLAPI 5 创建不同的个体公理。我想要的是使用 vocab owl:differentFrom 的简单三元组,例如:
test:A owl:differentFrom test:B
但我得到的是三倍使用 owl:AllDifferent:
_:genid234 rdf:type owl:AllDifferent
_:genid234 owl:distinctMembers _:genid236
_:genid236 rdf:rest _:genid235
_:genid236 rdf:rest rdf:nil
_:genid235 rdf:first test:A
_:genid235 rdf:type rdf:List
_:genid236 rdf:first test:B
_:genid236 rdf:type rdf:List
简单的代码是:
OWLNamedIndividual op1 = factory.getOWLNamedIndividual(baseIRI + A);
OWLNamedIndividual op2 = factory.getOWLNamedIndividual(baseIRI + B);
ont.add(factory.getOWLDifferentIndividualsAxiom(op1, op2));
而且我已经在 Protege 5.5.0 上尝试过,它产生与 OWLAPI 完全相同的三元组。
OWLAPI-v5 的另一种实现提供了一种直接处理底层 RDF 数据的方法 - ONT-API(顺便说一下,也存在基于 RDF 的 Protege 版本)。 如果通过 OWLAPI 接口为两个操作数工作,它会生成一个三元组。
如果您喜欢将数据存储在OWL 公理的形式,只需要用序列化解决这个特殊问题。
示例:
String iri = "http://test#";
OWLDataFactory factory = OntManagers.getDataFactory();
Ontology ont = OntManagers.createManager().createOntology();
ont.asGraphModel().setNsPrefix("test", iri);
OWLNamedIndividual ia = factory.getOWLNamedIndividual(iri + "A");
OWLNamedIndividual ib = factory.getOWLNamedIndividual(iri + "B");
OWLNamedIndividual ic = factory.getOWLNamedIndividual(iri + "C");
ont.add(factory.getOWLDifferentIndividualsAxiom(ia, ib, ic));
ont.add(factory.getOWLDifferentIndividualsAxiom(ia, ic));
OntModel g = ont.asGraphModel();
g.createIndividual(iri + "D").addDifferentIndividual(g.createIndividual(iri + "C"));
g.createDifferentIndividuals(g.createIndividual(iri + "e"), g.createIndividual(iri + "g"));
g.write(System.out, "ttl");
System.out.println("=".repeat(42));
ont.axioms().forEach(System.out::println);
输出:
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix test: <http://test#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
[ rdf:type owl:AllDifferent ;
owl:members ( test:e test:g )
] .
test:g rdf:type owl:NamedIndividual .
test:B rdf:type owl:NamedIndividual .
[ rdf:type owl:Ontology ] .
test:e rdf:type owl:NamedIndividual .
test:C rdf:type owl:NamedIndividual .
test:A rdf:type owl:NamedIndividual ;
owl:differentFrom test:C .
test:D rdf:type owl:NamedIndividual ;
owl:differentFrom test:C .
[ rdf:type owl:AllDifferent ;
owl:distinctMembers ( test:A test:B test:C )
] .
==========================================
Declaration(NamedIndividual(test:g))
Declaration(NamedIndividual(test:e))
Declaration(NamedIndividual(test:D))
Declaration(NamedIndividual(test:C))
Declaration(NamedIndividual(test:B))
Declaration(NamedIndividual(test:A))
DifferentIndividuals(test:C test:D)
DifferentIndividuals(test:A test:C)
DifferentIndividuals(test:e test:g)
DifferentIndividuals(test:A test:B test:C)