gpt4 book ai didi

java - 通过 OWL API 查询 OWL 本体时出现问题

转载 作者:行者123 更新时间:2023-12-02 02:34:24 27 4
gpt4 key购买 nike

我有 OWL API 示例中使用的本体。

private static final String KOALA = "<?xml version=\"1.0\"?>\n"
+ "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\" xmlns:owl=\"http://www.w3.org/2002/07/owl#\" xmlns=\"http://protege.stanford.edu/plugins/owl/owl-library/koala.owl#\" xml:base=\"http://protege.stanford.edu/plugins/owl/owl-library/koala.owl\">\n"
+ " <owl:Ontology rdf:about=\"\"/>\n"
+ " <owl:Class rdf:ID=\"Female\"><owl:equivalentClass><owl:Restriction><owl:onProperty><owl:FunctionalProperty rdf:about=\"#hasGender\"/></owl:onProperty><owl:hasValue><Gender rdf:ID=\"female\"/></owl:hasValue></owl:Restriction></owl:equivalentClass></owl:Class>\n"
+ " <owl:Class rdf:ID=\"Marsupials\"><owl:disjointWith><owl:Class rdf:about=\"#Person\"/></owl:disjointWith><rdfs:subClassOf><owl:Class rdf:about=\"#Animal\"/></rdfs:subClassOf></owl:Class>\n"
+ " <owl:Class rdf:ID=\"Student\"><owl:equivalentClass><owl:Class><owl:intersectionOf rdf:parseType=\"Collection\"><owl:Class rdf:about=\"#Person\"/><owl:Restriction><owl:onProperty><owl:FunctionalProperty rdf:about=\"#isHardWorking\"/></owl:onProperty><owl:hasValue rdf:datatype=\"http://www.w3.org/2001/XMLSchema#boolean\">true</owl:hasValue></owl:Restriction><owl:Restriction><owl:someValuesFrom><owl:Class rdf:about=\"#University\"/></owl:someValuesFrom><owl:onProperty><owl:ObjectProperty rdf:about=\"#hasHabitat\"/></owl:onProperty></owl:Restriction></owl:intersectionOf></owl:Class></owl:equivalentClass></owl:Class>\n"
+ " <owl:Class rdf:ID=\"KoalaWithPhD\"><owl:versionInfo>1.2</owl:versionInfo><owl:equivalentClass><owl:Class><owl:intersectionOf rdf:parseType=\"Collection\"><owl:Restriction><owl:hasValue><Degree rdf:ID=\"PhD\"/></owl:hasValue><owl:onProperty><owl:ObjectProperty rdf:about=\"#hasDegree\"/></owl:onProperty></owl:Restriction><owl:Class rdf:about=\"#Koala\"/></owl:intersectionOf></owl:Class></owl:equivalentClass></owl:Class>\n"
+ " <owl:Class rdf:ID=\"University\"><rdfs:subClassOf><owl:Class rdf:ID=\"Habitat\"/></rdfs:subClassOf></owl:Class>\n"
+ " <owl:Class rdf:ID=\"Koala\"><rdfs:subClassOf><owl:Restriction><owl:hasValue rdf:datatype=\"http://www.w3.org/2001/XMLSchema#boolean\">false</owl:hasValue><owl:onProperty><owl:FunctionalProperty rdf:about=\"#isHardWorking\"/></owl:onProperty></owl:Restriction></rdfs:subClassOf><rdfs:subClassOf><owl:Restriction><owl:someValuesFrom><owl:Class rdf:about=\"#DryEucalyptForest\"/></owl:someValuesFrom><owl:onProperty><owl:ObjectProperty rdf:about=\"#hasHabitat\"/></owl:onProperty></owl:Restriction></rdfs:subClassOf><rdfs:subClassOf rdf:resource=\"#Marsupials\"/></owl:Class>\n"
+ " <owl:Class rdf:ID=\"Animal\"><rdfs:seeAlso>Male</rdfs:seeAlso><rdfs:subClassOf><owl:Restriction><owl:onProperty><owl:ObjectProperty rdf:about=\"#hasHabitat\"/></owl:onProperty><owl:minCardinality rdf:datatype=\"http://www.w3.org/2001/XMLSchema#int\">1</owl:minCardinality></owl:Restriction></rdfs:subClassOf><rdfs:subClassOf><owl:Restriction><owl:cardinality rdf:datatype=\"http://www.w3.org/2001/XMLSchema#int\">1</owl:cardinality><owl:onProperty><owl:FunctionalProperty rdf:about=\"#hasGender\"/></owl:onProperty></owl:Restriction></rdfs:subClassOf><owl:versionInfo>1.1</owl:versionInfo></owl:Class>\n"
+ " <owl:Class rdf:ID=\"Forest\"><rdfs:subClassOf rdf:resource=\"#Habitat\"/></owl:Class>\n"
+ " <owl:Class rdf:ID=\"Rainforest\"><rdfs:subClassOf rdf:resource=\"#Forest\"/></owl:Class>\n"
+ " <owl:Class rdf:ID=\"GraduateStudent\"><rdfs:subClassOf><owl:Restriction><owl:onProperty><owl:ObjectProperty rdf:about=\"#hasDegree\"/></owl:onProperty><owl:someValuesFrom><owl:Class><owl:oneOf rdf:parseType=\"Collection\"><Degree rdf:ID=\"BA\"/><Degree rdf:ID=\"BS\"/></owl:oneOf></owl:Class></owl:someValuesFrom></owl:Restriction></rdfs:subClassOf><rdfs:subClassOf rdf:resource=\"#Student\"/></owl:Class>\n"
+ " <owl:Class rdf:ID=\"Parent\"><owl:equivalentClass><owl:Class><owl:intersectionOf rdf:parseType=\"Collection\"><owl:Class rdf:about=\"#Animal\"/><owl:Restriction><owl:onProperty><owl:ObjectProperty rdf:about=\"#hasChildren\"/></owl:onProperty><owl:minCardinality rdf:datatype=\"http://www.w3.org/2001/XMLSchema#int\">1</owl:minCardinality></owl:Restriction></owl:intersectionOf></owl:Class></owl:equivalentClass><rdfs:subClassOf rdf:resource=\"#Animal\"/></owl:Class>\n"
+ " <owl:Class rdf:ID=\"DryEucalyptForest\"><rdfs:subClassOf rdf:resource=\"#Forest\"/></owl:Class>\n"
+ " <owl:Class rdf:ID=\"Quokka\"><rdfs:subClassOf><owl:Restriction><owl:hasValue rdf:datatype=\"http://www.w3.org/2001/XMLSchema#boolean\">true</owl:hasValue><owl:onProperty><owl:FunctionalProperty rdf:about=\"#isHardWorking\"/></owl:onProperty></owl:Restriction></rdfs:subClassOf><rdfs:subClassOf rdf:resource=\"#Marsupials\"/></owl:Class>\n"
+ " <owl:Class rdf:ID=\"TasmanianDevil\"><rdfs:subClassOf rdf:resource=\"#Marsupials\"/></owl:Class>\n"
+ " <owl:Class rdf:ID=\"MaleStudentWith3Daughters\"><owl:equivalentClass><owl:Class><owl:intersectionOf rdf:parseType=\"Collection\"><owl:Class rdf:about=\"#Student\"/><owl:Restriction><owl:onProperty><owl:FunctionalProperty rdf:about=\"#hasGender\"/></owl:onProperty><owl:hasValue><Gender rdf:ID=\"male\"/></owl:hasValue></owl:Restriction><owl:Restriction><owl:onProperty><owl:ObjectProperty rdf:about=\"#hasChildren\"/></owl:onProperty><owl:cardinality rdf:datatype=\"http://www.w3.org/2001/XMLSchema#int\">3</owl:cardinality></owl:Restriction><owl:Restriction><owl:allValuesFrom rdf:resource=\"#Female\"/><owl:onProperty><owl:ObjectProperty rdf:about=\"#hasChildren\"/></owl:onProperty></owl:Restriction></owl:intersectionOf></owl:Class></owl:equivalentClass></owl:Class>\n"
+ " <owl:Class rdf:ID=\"Degree\"/>\n <owl:Class rdf:ID=\"Gender\"/>\n"
+ " <owl:Class rdf:ID=\"Male\"><owl:equivalentClass><owl:Restriction><owl:hasValue rdf:resource=\"#male\"/><owl:onProperty><owl:FunctionalProperty rdf:about=\"#hasGender\"/></owl:onProperty></owl:Restriction></owl:equivalentClass></owl:Class>\n"
+ " <owl:Class rdf:ID=\"Person\"><rdfs:subClassOf rdf:resource=\"#Animal\"/><owl:disjointWith rdf:resource=\"#Marsupials\"/></owl:Class>\n"
+ " <owl:ObjectProperty rdf:ID=\"hasHabitat\"><rdfs:range rdf:resource=\"#Habitat\"/><rdfs:domain rdf:resource=\"#Animal\"/></owl:ObjectProperty>\n"
+ " <owl:ObjectProperty rdf:ID=\"hasDegree\"><rdfs:domain rdf:resource=\"#Person\"/><rdfs:range rdf:resource=\"#Degree\"/></owl:ObjectProperty>\n"
+ " <owl:ObjectProperty rdf:ID=\"hasChildren\"><rdfs:range rdf:resource=\"#Animal\"/><rdfs:domain rdf:resource=\"#Animal\"/></owl:ObjectProperty>\n"
+ " <owl:FunctionalProperty rdf:ID=\"hasGender\"><rdfs:range rdf:resource=\"#Gender\"/><rdf:type rdf:resource=\"http://www.w3.org/2002/07/owl#ObjectProperty\"/><rdfs:domain rdf:resource=\"#Animal\"/></owl:FunctionalProperty>\n"
+ " <owl:FunctionalProperty rdf:ID=\"isHardWorking\"><rdfs:range rdf:resource=\"http://www.w3.org/2001/XMLSchema#boolean\"/><rdfs:domain rdf:resource=\"#Person\"/><rdf:type rdf:resource=\"http://www.w3.org/2002/07/owl#DatatypeProperty\"/></owl:FunctionalProperty>\n"
+ " <Degree rdf:ID=\"MA\"/>\n</rdf:RDF>";

我使用此代码( https://neo4j.com/blog/using-owl-with-neo4j/ )来查询 owl 本体:

OWLReasoner reasoner = reasonerFactory.createNonBufferingReasoner(ontology);
if (!reasoner.isConsistent()) {

logger.log(Level.INFO, "Ontology is inconsistent");
//throw your exception of choice here
throw new Exception("Ontology is inconsistent");
}

try{

// Node thingNode = getOrCreateNodeWithUniqueFactory("owl:Thing");

for (OWLClass c :ontology.getClassesInSignature(true)) {
String classString = c.toString();
if (classString.contains("#")) {
classString = classString.substring(
classString.indexOf("#")+1,classString.lastIndexOf(">"));
}
System.out.println("classString: "+classString);

// Node classNode = getOrCreateNodeWithUniqueFactory(classString);

NodeSet<OWLClass> superclasses = reasoner.getSuperClasses(c, true);

if (superclasses.isEmpty()) {
//classNode.createRelationshipTo(thingNode,DynamicRelationshipType.withName("isA"));
} else {
for (org.semanticweb.owlapi.reasoner.Node<OWLClass>
parentOWLNode: superclasses) {
OWLClassExpression parent = parentOWLNode.getRepresentativeElement();
String parentString = parent.toString();
if (parentString.contains("#")) {
parentString = parentString.substring(
parentString.indexOf("#")+1,
parentString.lastIndexOf(">"));
}

System.out.println("parentString: "+parentString);
// Node parentNode = getOrCreateNodeWithUniqueFactory(parentString);
// classNode.createRelationshipTo(parentNode, DynamicRelationshipType.withName("isA"));
}
}

for (org.semanticweb.owlapi.reasoner.Node<OWLNamedIndividual> in
: reasoner.getInstances(c, true)) {
OWLNamedIndividual i = in.getRepresentativeElement();
String indString = i.toString();
if (indString.contains("#")) {
indString = indString.substring(
indString.indexOf("#")+1,indString.lastIndexOf(">"));
}

System.out.println("indString: "+indString);

// Node individualNode = getOrCreateNodeWithUniqueFactory(indString);
// individualNode.createRelationshipTo(classNode, DynamicRelationshipType.withName("isA"));


for (OWLObjectPropertyExpression objectProperty:
ontology.getObjectPropertiesInSignature()) { System.out.println("objectProperty: "+objectProperty);
for
(org.semanticweb.owlapi.reasoner.Node<OWLNamedIndividual>
object: reasoner.getObjectPropertyValues(i,
objectProperty)) {
String reltype = objectProperty.toString();
reltype = reltype.substring(reltype.indexOf("#")+1,
reltype.lastIndexOf(">"));
String s =
object.getRepresentativeElement().toString();
s = s.substring(s.indexOf("#")+1,
s.lastIndexOf(">"));

System.out.println("reltype: "+reltype+" s: "+s);
// Node objectNode = getOrCreateNodeWithUniqueFactory(s);
// individualNode.createRelationshipTo(objectNode, DynamicRelationshipType.withName(reltype));
}
}
for (OWLDataPropertyExpression dataProperty:
ontology.getDataPropertiesInSignature()) { System.out.println("dataProperty: "+dataProperty.asOWLDataProperty());
for (OWLLiteral object: reasoner.getDataPropertyValues(
i, dataProperty.asOWLDataProperty())) {
String reltype =
dataProperty.asOWLDataProperty().toString();
reltype = reltype.substring(reltype.indexOf("#")+1,
reltype.lastIndexOf(">"));
String s = object.toString();

System.out.println("reltype: "+reltype+" s: "+s);

// individualNode.setProperty(reltype, s);
}
}

}
}

但我观察到它不会返回指定的个体和对象属性表达式的对象属性值。

reasoner.getObjectPropertyValues(i, objectProperty)

不返回任何项目。为什么?

正如我在其他帖子中读到的那样,我什至尝试了其他 Reasoner(HermiT、Pellet),但结果没有改变。怎么解决呢?

最佳答案

您正在使用的本体中没有断言对象属性;所以推理者什么也没找到,因为没有什么可找到的。

加载本体后添加以下代码,以便您可以测试您的代码。

final OWLDataFactory factory = manager.getOWLDataFactory();
ontology.add(factory.getOWLObjectPropertyAssertionAxiom(factory.getOWLObjectProperty("#HelloWorldProperty"), factory.getOWLNamedIndividual("#FirstIndividual"), factory.getOWLNamedIndividual("#SecondIndividual")));
reasoner.flush();

您的代码看起来不错,但有点复杂。您可以使用“getFlattened()”获得更短的代码

for (final OWLNamedIndividual i : reasoner.getInstances(c, true).getFlattened())

关于java - 通过 OWL API 查询 OWL 本体时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46606072/

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