Jena学习笔记(3)——本体推理

一,使用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

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

public class ReasonerImpl implements IReasoner {
	
	private InfModel inf = null;

	/**
	 * 获取一个推理接口
	 * @param path
	 * @return
	 * @throws RulesetNotFoundException
	 */
	private GenericRuleReasoner getReasoner(String path) throws RulesetNotFoundException {
		
		List<Rule> rules = Rule.rulesFromURL(path);  //"file:./family/family.rules"
		GenericRuleReasoner reasoner = new GenericRuleReasoner(rules);
		reasoner.setOWLTranslation(true);
		reasoner.setDerivationLogging(true);
		reasoner.setTransitiveClosureCaching(true);
		return reasoner;
		
	}
	
	/**
	 * 获取推理的本体
	 * @param path
	 * @return
	 */
	private OntModel getOntModel(String path) {
		
		Model model = ModelFactory.createDefaultModel();
		model.read(path);  //"file:./family/family.owl"
		OntModel ont = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_RDFS_INF,
				model);
		Resource configuration = ont.createResource(); //a new anonymous resource linked to this model
		configuration.addProperty(ReasonerVocabulary.PROPruleMode
				, "hybrid");
		return ont;
		
	}
	
	/**
	 * InfModel是对常规Model的扩展,支持任何相关的推理能力
	 * @param ontPath
	 * @param rulePath
	 * @return
	 */
	public InfModel getInfModel(String rulePath, String ontPath) {
		
		this.inf = ModelFactory.createInfModel(getReasoner(rulePath), getOntModel(ontPath));
		return this.inf;
		
	}
	
	/**
	 * InfModel是对常规Model的扩展,支持任何相关的推理能力
	 * @param model
	 * @param rulePath
	 * @return
	 */
	public InfModel getInfModel(String rulePath, OntModel model) {
		this.inf = ModelFactory.createInfModel(getReasoner(rulePath), model);
		return this.inf;
	}
	
	/**
	 * 打印推理结果
	 * @param a
	 * @param b
	 */
	public void printInferResult(Resource a, Resource b) {
		
		StmtIterator stmtIter = this.inf.listStatements(a, null, b);
		if (!stmtIter.hasNext()) {
			System.out.println("there is no relation between "
				      + a.getLocalName() + " and " + b.getLocalName());
			System.out.println("\n-------------------\n");
		}
		while (stmtIter.hasNext()) {
			Statement s = stmtIter.nextStatement();
			System.out.println("Relation between " + a.getLocalName() + " and "
				      + b.getLocalName() + " is :");
			System.out.println(a.getLocalName() + " "
				      + s.getPredicate().getLocalName() + " " + b.getLocalName());
			System.out.println("\n-------------------\n");
		}
		
	}
	
	public void searchOnto(String queryString) {
		
		Query query = QueryFactory.create(queryString);	  
	    QueryExecution qe = QueryExecutionFactory.create(query, this.inf);
	    ResultSet results = qe.execSelect();
	    
	    ResultSetFormatter.out(System.out, results, query);
	    qe.close();
	    
	}
	
}


执行查询语句

public static void testQuery() {
		String ruleFile = "file:./expert/Expert.rules";
		String ontoFile = "file:./expert/Expert.owl";
		String queryString = "PREFIX Expert:<http://www.owl-ontologies.com/Expert.owl#> " +
	    	"SELECT ?expert ?subject " +
	    	"WHERE {?expert Expert:familiar_with ?subject} ";
		
		IReasoner famRea = ReasonerFactory.createFamilyReasoner();
		famRea.getInfModel(ruleFile, ontoFile);
		famRea.searchOnto(queryString);
	}


查询结果为:

------------------------------------------------------------
| expert             | subject                             |
============================================================
| Expert:ChenJianer  | Expert:Computer_Software_and_Theory |
| Expert:ZhaoHongJie | Expert:Computer_Applied_Technology  |
------------------------------------------------------------


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




你可能感兴趣的:(Jena学习笔记(3)——本体推理)