InfModel infmodel = ModelFactory.createInfModel(reasoner, m);
Resource vegetarian = infmodel.getResource(source + "Vegetarian");
Resource margherita = infmodel.getResource(source + "Example-Margherita");
if (infmodel.contains(margherita, RDF., vegetarian)) {
System.out.println("Margherita is a memberOf Vegetarian pizza");
}
上面给出的例子是由正式的pizza.owl形成的。在这只猫头鹰中,Example-Margherita是Margherita类的一个人。所以,它已经写在owl文件中了。然而,问题在于推理者应该推断出margherita例子也应该是素食披萨。 任何人都可以举个例子来说明如何在Protege中找到个人可能的推断类吗?(Protege正确地推断出Example-Margherita是一个素食比萨饼。但是,我无法以编程方式推断出来)
答案 0 :(得分:9)
我解决了我的问题。我认为我的本体论存在问题。因此,我创建了另一个本体来推断个体。我创建的本体包含Person的人和子类:MalePerson,FemalePerson和MarriedPerson。并且,有两个对象属性(hasSpouse,hasSibling)和一个数据类型属性(hasAge)。 而且,我创造了3个人。 John - MalePerson - hasAge(20) - hasSibling(Jane) Jane - FemalePerson - hasSibling(John) - hasSpouse(Bob) Bob - MalePerson - hasSpouse(简)
而且,我为MalePerson和FemalePerson课程设置了两个限制。 对于MalePerson: hasSpouse最大1 hasSpouse只有MalePerson 对于FemalePerson: hasSpouse最大1 hasSpouse only FemalePerson
最后,我让MarriedPerson成为一个定义的类。在推理之前,MarriedPerson没有个人。然而,模型应该推断Jane和Bob结婚了。因此,最后,MarriedPerson班应该有2个人。
当我使用Jena在Java中运行此代码时,我得到了2个推断的个体。
OntModel ontModel = ModelFactory.createOntologyModel();
InputStream in = FileManager.get().open(inputFileName);
if (in == null) {
throw new IllegalArgumentException( "File: " + inputFileName + " not found");
}
ontModel.read(in, "");
Reasoner reasoner = ReasonerRegistry.getOWLReasoner();
reasoner = reasoner.bindSchema(ontModel);
// Obtain standard OWL-DL spec and attach the Pellet reasoner
OntModelSpec ontModelSpec = OntModelSpec.OWL_DL_MEM;
ontModelSpec.setReasoner(reasoner);
// Create ontology model with reasoner support
OntModel model = ModelFactory.createOntologyModel(ontModelSpec, ontModel);
// MarriedPerson has no asserted instances
// However, if an inference engine is used, two of the three
// individuals in the example presented here will be
// recognized as MarriedPersons
//ns is the uri
OntClass marPerson = model.getOntClass(ns + "OWLClass_00000003866036241880"); // this is the uri for MarriedPerson class
ExtendedIterator married = marPerson.listInstances();
while(married.hasNext()) {
OntResource mp = (OntResource)married.next();
System.out.println(mp.getURI());
} // this code returns 2 individuals with the help of reasoner