本文主要是介绍Jena进阶(二)本体推理规则(Rules)的介绍,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
本体的推理规则库是有一条条规则组成的。每条规则由主体(body)和头(head)组成,一条规则可以有一个主体和一个头,例如规则:[rule1:(?a fa:hasHusband ?b)(?a fa:isMotherOf ?c)->(?b fa:isFatherOf ?c)],其中规则的主体为:?a @hasHusband ?b,?a @isMotherOf ?c,头为:?b @isFatherOf ?c,也就是说有所有的主体可以推出头。?a @hasHusband ?b,?a @isMotherOf ?c,?b @isFatherOf ?c三者有一个称谓:ClauseEntry。例如方法Rule类中getbody()方法返回是一个ClauseEntry集合。他有2个元素?a @hasHusband ?b,?a @isMotherOf ?c。下面有个规则库:
- @prefix fa: <http://www.semanticweb.org/ontologies/2010/0/family.owl#>.@include <RDFS>.@include <OWL>.
- [rule1: (?a fa:hasHusband ?b)(?a fa:isMotherOf ?c)->(?b fa:isFatherOf ?c)]
- [rule2: (?a fa:hasHusband ?b)(?a fa:hasDaughter ?c)->(?b fa:isFartherOf ?c)]
- [rule3: (?a fa:hasHusband ?b)(?a fa:hasDaughter ?c)->(?b fa:hasDaughter ?c)]
- [rule4: (?a fa:hasHusband ?b)(?a fa:hasDaughter ?c)->(?c fa:hasFather ?b)]
- [rule5: (?a fa:hasHusband ?b)(?a fa:hasDaughter ?c)->(?c fa:isDaughterOf ?b)]
- [rule6: (?a fa:hasHusband ?b)(?a fa:hasSon ?c)->(?b fa:isFatherOf ?c)]
- [rule7: (?a fa:hasHusband ?b)(?a fa:hasSon ?c)->(?b fa:hasSon ?c)]
- [rule8: (?a fa:hasHusband ?b)(?a fa:hasSon ?c)->(?c fa:hasFather ?b)]
- [rule9: (?a fa:hasHusband ?b)(?a fa:hasSon ?c)->(?c fa:isSonOf ?b)]
- [rule10:(?a fa:hasWife ?b)(?a fa:isFatherOf ?c)->(?b fa:isMotherOf ?c)]
- [rule11:(?a fa:hasWife ?b)(?a fa:haDaughter ?c)->(?b fa:isMotherOf ?c)]
- [rule12:(?a fa:hasWife ?b)(?a fa:haDaughter ?c)->(?b fa:hasDaugher ?c)]
- [rule13:(?a fa:hasWife ?b)(?a fa:haDaughter ?c)->(?c fa:hasMother ?b)]
- [rule14:(?a fa:hasWife ?b)(?a fa:haDaughter ?c)->(?c fa:isDaughterOf ?b)]
- [rule15:(?a fa:hasWife ?b)(?a fa:hasSon ?c)->(?b fa:isMotherOf ?c)]
- [rule16:(?a fa:hasWife ?b)(?a fa:hasSon ?c)->(?b fa:hasSon ?c)]
- [rule17:(?a fa:hasWife ?b)(?a fa:hasSon ?c)->(?c fa:hasMother ?b)]
- [rule18:(?a fa:hasWife ?b)(?a fa:hasSon ?c)->(?c fa:isSonOf ?b)]
- [rule19:(?a fa:hasWife ?b)->(?a fa:isHusbandOf ?b)]
- [rule20:(?a fa:hasHusband ?b)->(?a fa:isWifeOf ?b)]
- [rule21:(?a fa:isHusbandOf ?b)->(?a fa:hasWife ?b)]
- [rule22:(?a fa:isWifeOf ?b)->(?a fa:hasHusband ?b)]
- [rule23:(?a fa:hasFather ?b) (?c fa:hasFather ?d) (?b fa:hasBrother ?d) notEqual(?b,?d)->(?a fa:hasCousin ?b)]
- [rule24:(?a fa:hasFather ?b)(?c fa:hasFather ?d)(?b fa:hasFather ?e)(?d fa:hasFather ?e) notEqual(?b,?d)->(?a fa:hasCousin ?c)]
- [rule25:(?a fa:hasSon ?b)(?b fa:hasSon ?c)->(?a fa:hasGrandSon ?c)]
- [rule26:(?a fa:hasSon ?b)(?b fa:hasDaughter ?c)->(?a fa:hasGrandDaughter ?c)]
- [rule27:(?a fa:hasFather ?b)(?b fa:hasBrother ?c)->(?a fa:hasUncle ?c)]
- [rule28:(?a fa:hasFather ?b)(?b fa:hasSister ?c)->(?a fa:hasAunt ?c)]
- [rule29:(?a fa:hasGrandSon ?b)->(?b fa:isGrandSonOf ?a)]
- [rule30:(?a fa:hasAunt ?b)->(?b fa:isAuntOf ?a)]
- [rule31:(?a fa:hasFather ?b)->(?b fa:isFatherOf ?a)]
- [rule32:(?a fa:hasMother ?b)->(?b fa:isMotherOf ?a)]
- [rule33:(?a fa:hasBrother ?b)->(?b fa:isBrotherOf ?a)]
- [rule34:(?a fa:hasSister ?b)->(?b fa:isSisterOf ?b)]
- [rule35:(?a fa:hasCousin ?b)->(?b fa:isCousinOf ?a)]
- [rule36:(?a fa:hasSon ?b)->(?b fa:isSonOf ?a)]
- [rule37:(?a fa:hasDaughter ?b)->(?b fa:isDaughterOf ?a)]
- [rule38:(?a fa:hasGrandDaughter ?b)->(?b fa:isGrandDaughterOf ?a)]
- [rule39:(?a fa:hasWife ?b)->(?b fa:isWifeOf ?a)]
- [rule40:(?a fa:hasHusband ?b)->(?b fa:isHusbandOf ?a)]
- [rule41:(?a fa:hasUncle ?b)->(?b fa:isUncleOf ?a)]
下面代码是根据这个规则库查个规则的例子
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- List<Rule> rules=Rule.rulesFromURL("file:f:/family.rules");
- System.out.println(rules.get(0));
- ClauseEntry[] body=rules.get(0).getBody();
- int j=rules.get(0).bodyLength();
- System.out.println(j);
- for (int i=0;i<j;i++){
- System.out.print(body[i]+",");
- }
- System.out.println("/n-------------------/n");
- ClauseEntry[] head=rules.get(0).getHead();
- int k=rules.get(0).headLength();
- for (int i=0;i<k;i++){
- System.out.print(head[i]+",");
- }
- System.out.println("/n-------------------/n");
- System.out.println(rules.get(0).getName());
- }
这篇关于Jena进阶(二)本体推理规则(Rules)的介绍的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!