Java 語義網編程系列六:Jena原生推理

來源:互聯網
上載者:User

規則的含義:

規則是表示知識的一種方式,它通常超過了owl1的表達能力,語義web上的規則是典型的條件陳述式:if-then 字句。只有當特定的陳述為真,才會添加新的知識。

Jena架構內建了推理引擎和所識別的規則語言,其優點jena架構內建使用相對簡單,缺點並不是w3c的推理標準。

本體建模:

 

 

  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  }

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.