Jena 本体推理

一,使用Jena对OWL本体进行查询推理

前面在"使用jena持久化OWL本体到MySQL(http://bbs.w3china.org/dispbbs.asp?boardID=2&ID=60494)"中说到了把OWL本体在MySQL中的存取,现在终于能做些简单推理了。要主要规则和查询语句的语法。

 使用sparql对本体进行查询

1)InfModel是对常规Model的扩展,支持任何相关的推理能力

2)QueryExecution 执行sparql查询

3)ResultSetFormatter 获取查询结果集

4)实例中使用了本体文件Experts.owl

二,下面是一个简单的例子:

[java]  view plain copy
  1. public class ReasonerImpl implements IReasoner {  
  2.       
  3.     private InfModel inf = null;  
  4.   
  5.     /** 
  6.      * 获取一个推理接口 
  7.      * @param path 
  8.      * @return 
  9.      * @throws RulesetNotFoundException 
  10.      */  
  11.     private GenericRuleReasoner getReasoner(String path) throws RulesetNotFoundException {  
  12.           
  13.         List<Rule> rules = Rule.rulesFromURL(path);  //"file:./family/family.rules"  
  14.         GenericRuleReasoner reasoner = new GenericRuleReasoner(rules);  
  15.         reasoner.setOWLTranslation(true);  
  16.         reasoner.setDerivationLogging(true);  
  17.         reasoner.setTransitiveClosureCaching(true);  
  18.         return reasoner;  
  19.           
  20.     }  
  21.       
  22.     /** 
  23.      * 获取推理的本体 
  24.      * @param path 
  25.      * @return 
  26.      */  
  27.     private OntModel getOntModel(String path) {  
  28.           
  29.         Model model = ModelFactory.createDefaultModel();  
  30.         model.read(path);  //"file:./family/family.owl"  
  31.         OntModel ont = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_RDFS_INF,  
  32.                 model);  
  33.         Resource configuration = ont.createResource(); //a new anonymous resource linked to this model  
  34.         configuration.addProperty(ReasonerVocabulary.PROPruleMode  
  35.                 , "hybrid");  
  36.         return ont;  
  37.           
  38.     }  
  39.       
  40.     /** 
  41.      * InfModel是对常规Model的扩展,支持任何相关的推理能力 
  42.      * @param ontPath 
  43.      * @param rulePath 
  44.      * @return 
  45.      */  
  46.     public InfModel getInfModel(String rulePath, String ontPath) {  
  47.           
  48.         this.inf = ModelFactory.createInfModel(getReasoner(rulePath), getOntModel(ontPath));  
  49.         return this.inf;  
  50.           
  51.     }  
  52.       
  53.     /** 
  54.      * InfModel是对常规Model的扩展,支持任何相关的推理能力 
  55.      * @param model 
  56.      * @param rulePath 
  57.      * @return 
  58.      */  
  59.     public InfModel getInfModel(String rulePath, OntModel model) {  
  60.         this.inf = ModelFactory.createInfModel(getReasoner(rulePath), model);  
  61.         return this.inf;  
  62.     }  
  63.       
  64.     /** 
  65.      * 打印推理结果 
  66.      * @param a 
  67.      * @param b 
  68.      */  
  69.     public void printInferResult(Resource a, Resource b) {  
  70.           
  71.         StmtIterator stmtIter = this.inf.listStatements(a, null, b);  
  72.         if (!stmtIter.hasNext()) {  
  73.             System.out.println("there is no relation between "  
  74.                       + a.getLocalName() + " and " + b.getLocalName());  
  75.             System.out.println("\n-------------------\n");  
  76.         }  
  77.         while (stmtIter.hasNext()) {  
  78.             Statement s = stmtIter.nextStatement();  
  79.             System.out.println("Relation between " + a.getLocalName() + " and "  
  80.                       + b.getLocalName() + " is :");  
  81.             System.out.println(a.getLocalName() + " "  
  82.                       + s.getPredicate().getLocalName() + " " + b.getLocalName());  
  83.             System.out.println("\n-------------------\n");  
  84.         }  
  85.           
  86.     }  
  87.       
  88.     public void searchOnto(String queryString) {  
  89.           
  90.         Query query = QueryFactory.create(queryString);     
  91.         QueryExecution qe = QueryExecutionFactory.create(query, this.inf);  
  92.         ResultSet results = qe.execSelect();  
  93.           
  94.         ResultSetFormatter.out(System.out, results, query);  
  95.         qe.close();  
  96.           
  97.     }  
  98.       
  99. }  


执行查询语句

[java]  view plain copy
  1. public static void testQuery() {  
  2.         String ruleFile = "file:./expert/Expert.rules";  
  3.         String ontoFile = "file:./expert/Expert.owl";  
  4.         String queryString = "PREFIX Expert:<http://www.owl-ontologies.com/Expert.owl#> " +  
  5.             "SELECT ?expert ?subject " +  
  6.             "WHERE {?expert Expert:familiar_with ?subject} ";  
  7.           
  8.         IReasoner famRea = ReasonerFactory.createFamilyReasoner();  
  9.         famRea.getInfModel(ruleFile, ontoFile);  
  10.         famRea.searchOnto(queryString);  
  11.     }  


查询结果为:

[plain]  view plain copy
  1. ------------------------------------------------------------  
  2. | expert             | subject                             |  
  3. ============================================================  
  4. | Expert:ChenJianer  | Expert:Computer_Software_and_Theory |  
  5. | Expert:ZhaoHongJie | Expert:Computer_Applied_Technology  |  
  6. ------------------------------------------------------------  


源代码下载http://download.csdn.net/detail/welcome000yy/4770085



  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值