Java 语义网编程系列六:Jena原生推理

规则的含义:

规则是表示知识的一种方式,它通常超过了owl1的表达能力,语义web上的规则是典型的条件语句:if-then 字句。只有当特定的陈述为真,才会添加新的知识。

Jena框架自带了推理引擎和所识别的规则语言,其优点jena框架自带使用相对简单,缺点并不是w3c的推理标准。

本体建模:

Java 语义网编程系列六:Jena原生推理

 

 

  1 import com.clarkparsia.sparqlowl.parser.antlr.SparqlOwlParser.string_return;

  2 

  3 import com.hp.hpl.jena.ontology.OntModel;

  4 

  5 import com.hp.hpl.jena.ontology.OntModelSpec;

  6 

  7 import com.hp.hpl.jena.rdf.model.InfModel;

  8 

  9 import com.hp.hpl.jena.rdf.model.Model;

 10 

 11 import com.hp.hpl.jena.rdf.model.ModelFactory;

 12 

 13 import com.hp.hpl.jena.reasoner.Reasoner;

 14 

 15 import com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasoner;

 16 

 17 import com.hp.hpl.jena.reasoner.rulesys.Rule;

 18 

 19  

 20 

 21 public final class JenaRule

 22 

 23 {

 24 

 25 JenaModel jenaModel = null ;

 26 

 27  

 28 

 29 /**

 30 

 31  * jena原生推理机推理

 32 

 33  * @param ruleString

 34 

 35  * @param filePathString

 36 

 37  */

 38 

 39 public void reasoner(String ruleString, String filePathString)

 40 

 41 {

 42 

 43               jenaModel = new JenaModel();

 44 

 45               OntModel ontModel = jenaModel.createOntologyModel(OntModelSpec.OWL_DL_MEM,filePathString);

 46 

 47               Reasoner reasoner = new GenericRuleReasoner(Rule.parseRules(ruleString));

 48 

 49               InfModel resulInfModel = ModelFactory.createInfModel(reasoner, ontModel);

 50 

 51               jenaModel.outPut(resulInfModel);

 52 

 53 }

 54 

 55  

 56 

 57 /**

 58 

 59  * 拼接谓语

 60 

 61  * @param prefixString

 62 

 63  * @param predicateString

 64 

 65  * @return

 66 

 67  */

 68 

 69 public String getPredicate(String prefixString,String predicateString )

 70 

 71 {

 72 

 73 return "<"+prefixString+predicateString+">";

 74 

 75 }

 76 

 77 }

 78 

 79  

 80 

 81 Rule 规则:

 82 

 83 @Test

 84 

 85  public void testJenaRuleReasoner()

 86 

 87  {

 88 

 89  JenaModel jenaModel = new JenaModel();

 90 

 91  jenaModel.outPut(jenaModel.createOntologyModel(OntModelSpec.OWL_DL_MEM,fileString));

 92 

 93  

 94 

 95  System.out.println("推理后");

 96 

 97  

 98 

 99  JenaRule jenaRule = new JenaRule();

100 

101      StringBuffer ruleStringBuffer = new StringBuffer();

102 

103      //规则一: 妈妈的男性同胞为叔叔

104 

105      ruleStringBuffer.append("[rule1:");

106 

107      ruleStringBuffer.append("(?person "+ jenaRule.getPredicate(prefixString,"hasParent")+" ?parent),");

108 

109      ruleStringBuffer.append("(?parent "+jenaRule.getPredicate(prefixString, "hasSex") +" ?sex)," );

110 

111      ruleStringBuffer.append("equal(?sex,\"女\"),");

112 

113      ruleStringBuffer.append("(?parent "+jenaRule.getPredicate(prefixString, "hasSibling") +" ?sibling)," );

114 

115      ruleStringBuffer.append("(?sibling "+jenaRule.getPredicate(prefixString, "hasSex") +" ?siblingsex)," );

116 

117      ruleStringBuffer.append("equal(?siblingsex ,\"男\")");

118 

119      ruleStringBuffer.append("->");

120 

121      ruleStringBuffer.append("(?person " + jenaRule.getPredicate(prefixString, "hasUncle")+ " ?sibling )]");

122 

123      jenaRule.reasoner(ruleStringBuffer.toString(), fileString);

124 

125  }

 

 

你可能感兴趣的:(java)