我试图将来自不同来源的两个相同的人联系起来。我的想法是,如果 IndividualA 缺少属性 P,但使用 OWL.sameAs 与现有 P 绑定到 IndividualB,那么推理器应该输出 IndividualA 有 P!我很困惑为什么这不起作用。这是我的代码。
public class Main {
static String NS_A = "http://www.namespacea.com/ontoa.owl#";
static String NS_B = "http://www.namespaceb.com/ontob.owl#";
static String NS_C = "http://www.namespacec.com/ontoc.owl#";
public static void main(String[] args) {
OntModelSpec spec = OntModelSpec.OWL_MEM_MINI_RULE_INF;
OntModel modelA = ModelFactory.createOntologyModel(spec);
OntModel modelB = ModelFactory.createOntologyModel(spec);
OntModel modelC = ModelFactory.createOntologyModel(spec);
// Populate Model A
OntClass animal = modelA.createClass(NS_A + "Animal");
OntProperty hasFeathers = modelA.createOntProperty(NS_A + "hasFeathers");
hasFeathers.addDomain(animal);
hasFeathers.addRange(XSD.xboolean);
// Populate Model B
OntClass duck = modelB.createClass(NS_B + "Duck");
duck.setSuperClass(animal);
Individual donaldA = modelB.createIndividual(NS_B + "DonaldDuck", duck);
// NEXT LINE IS REMOVED ON PURPOSE
//donaldA.addLiteral(hasFeathers, true);
//Populate Model C
Individual donaldB = modelC.createIndividual(NS_C + "DonaldDuck", duck);
// Donald hasFeathers IS DECLARED HERE
donaldB.addLiteral(hasFeathers, true);
// THIS TIES UP THE TWO INDIVIDUALS
donaldA.addProperty(OWL.sameAs, donaldB);
// Query
System.out.println(
"Does " + donaldA.getLocalName() +
" have feathers? " + "Ans. " + donaldA.getPropertyValue(hasFeathers).asLiteral().getValue()
);
}
}
结果是 NullPointerException。你怎么解决这个问题?
我想到的一个更困难的情况是,如果有个人 A、B、C、D 和 E st A isSameAs B、B isSameAs C 等等,其中 E 的 P = true。然后询问 A 是否有 P = true 应该说 TRUE!