📄 example05.java
字号:
changes.add(new OntologyChangeEvent(computeBMI,OntologyChangeEvent.ChangeType.ADD));
// We now provide classification of people according to their BMI. For this purpose, we use
// the kaon2:ifTrue(F,A1,...,An) predicate. In this case, F is the formula written in exactly the
// same way as for kaon2:evaluate, and Ai are arguments. The extension of this predicate is equal
// to the set of assertions of the above form where evaluating F on Ai results in Boolean.TRUE.
Rule isTwig=KAON2Manager.factory().rule(
KAON2Manager.factory().literal(true,twig,new Term[] { PERSON }),
new Literal[] {
KAON2Manager.factory().literal(true,bmi,new Term[] { PERSON,BMI }),
KAON2Manager.factory().literal(true,KAON2Manager.factory().ifTrue(2),
new Term[] {
KAON2Manager.factory().constant("$1 < 20"),
BMI
})
});
changes.add(new OntologyChangeEvent(isTwig,OntologyChangeEvent.ChangeType.ADD));
Rule isNormalWeight=KAON2Manager.factory().rule(
KAON2Manager.factory().literal(true,normalWeight,new Term[] { PERSON }),
new Literal[] {
KAON2Manager.factory().literal(true,bmi,new Term[] { PERSON,BMI }),
KAON2Manager.factory().literal(true,KAON2Manager.factory().ifTrue(2),
new Term[] {
KAON2Manager.factory().constant("20 <= $1 && $1 < 25"),
BMI
})
});
changes.add(new OntologyChangeEvent(isNormalWeight,OntologyChangeEvent.ChangeType.ADD));
Rule isChubby=KAON2Manager.factory().rule(
KAON2Manager.factory().literal(true,chubby,new Term[] { PERSON }),
new Literal[] {
KAON2Manager.factory().literal(true,bmi,new Term[] { PERSON,BMI }),
KAON2Manager.factory().literal(true,KAON2Manager.factory().ifTrue(2),
new Term[] {
KAON2Manager.factory().constant("25 <= $1 && $1 < 30"),
BMI
})
});
changes.add(new OntologyChangeEvent(isChubby,OntologyChangeEvent.ChangeType.ADD));
Rule isWalrus=KAON2Manager.factory().rule(
KAON2Manager.factory().literal(true,walrus,new Term[] { PERSON }),
new Literal[] {
KAON2Manager.factory().literal(true,bmi,new Term[] { PERSON,BMI }),
KAON2Manager.factory().literal(true,KAON2Manager.factory().ifTrue(2),
new Term[] {
KAON2Manager.factory().constant("30 <= $1 && $1 < 40"),
BMI
})
});
changes.add(new OntologyChangeEvent(isWalrus,OntologyChangeEvent.ChangeType.ADD));
Rule isWhale=KAON2Manager.factory().rule(
KAON2Manager.factory().literal(true,whale,new Term[] { PERSON }),
new Literal[] {
KAON2Manager.factory().literal(true,bmi,new Term[] { PERSON,BMI }),
KAON2Manager.factory().literal(true,KAON2Manager.factory().ifTrue(2),
new Term[] {
KAON2Manager.factory().constant("$1 > 40"),
BMI
})
});
changes.add(new OntologyChangeEvent(isWhale,OntologyChangeEvent.ChangeType.ADD));
// We add the axoms to the ontology.
ontology.applyChanges(changes);
// We are now ready to see how people are fairing. On general issues related to running queries, pl;ease consult
// Exmaple 5. We now ask for the BMI of all people:
Reasoner reasoner=ontology.createReasoner();
System.out.println("The BMI of people is as follows:");
System.out.println("--------------------------------");
Query getBMI=reasoner.createQuery(new Literal[] {
KAON2Manager.factory().literal(true,bmi,new Term[] { PERSON,BMI })
},new Variable[] { PERSON,BMI });
getBMI.open();
while (!getBMI.afterLast()) {
Term[] tupleBuffer=getBMI.tupleBuffer();
System.out.println("The BMI of '"+tupleBuffer[0].toString()+"' is "+tupleBuffer[1].toString()+".");
getBMI.next();
}
// If you don't plan to use a query any more, you don't have to bother closing it; you can
// simply dispose of it.
getBMI.dispose();
System.out.println("--------------------------------");
// What happened to Chuck? The he wasn't included in this list, since his weight has been specified as a string,
// which effectively caused a type error. However, KAON2 does not throw an exception on type errors. The reason
// for this is that exceptions are hard to caputre in logic. Instead, consider what actually happened.
// The rule for evaluating BMI of chuck was invoked on this literal:
//
// kaon2:evaluate("$1/($2*$2)",184,"74",BMI)
//
// This is equivalent to asking whether there is an atomic fact matching this literal. Well, the answer is
// that there is no such fact, so the rule 'failed' for Chuck. This corresponds to the standard logical notions.
// Let us now see who the walruses are:
System.out.println();
System.out.println("The walruses are:");
System.out.println("--------------------------------");
// To get the extension of entire predicate, Reasoner contains the following shortcut:
Query getWalrus=reasoner.createQuery(walrus);
getWalrus.open();
while (!getWalrus.afterLast()) {
Term[] tupleBuffer=getWalrus.tupleBuffer();
System.out.println("'"+tupleBuffer[0].toString()+"' is a walrus.");
getWalrus.next();
}
getWalrus.dispose();
System.out.println("--------------------------------");
// Do not forget to clean up!
reasoner.dispose();
connection.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -