public static void testQuery() {
System.out.print("over,sir!");
String ruleFile = "file:D:/eclipse-workspace/Plant2.rules";//推理规则文件
System.out.print("over,sir!");
String ontoFile = "file:D:/eclipse-workspace/Plant-tuili.owl";//本体库
System.out.print("over,sir!");
String queryString = "PREFIX Plant:
"SELECT ?hanxiao ?geographical_distribution " +
"WHERE {?hanxiao Plant:be_distributed_in ?geographical_distribution} ";
System.out.print("over,sir!");
IReasoner famRea = ReasonerFactory.createFamilyReasoner();
famRea.getInfModel(ruleFile, ontoFile);
famRea.searchOnto(queryString);
}
二、Plant2.rules规则文件
@prefix fa:
@include
@include
[rule1:(?x http://www.semanticweb.org/zg/ontologies/Plant.owl#be_grown_in ?y)(?y http://www.semanticweb.org/zg/ontologies/Plant.owl#be_related_to ?z) -> (?x http://www.semanticweb.org/zg/ontologies/Plant.owl#be_distributed_in ?z)]
三、其他相关代码文件
1、IReasoner.java
package tuili;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.InfModel;
import com.hp.hpl.jena.rdf.model.Resource;
public interface IReasoner {
public InfModel getInfModel(String ontPath, String rulePath);
public InfModel getInfModel(String rulePath, OntModel model);
public void printInferResult(Resource a, Resource b);
public void searchOnto(String queryString);
}
2、ReasonerFactory .java
package tuili;
public class ReasonerFactory {
public static IReasoner createFamilyReasoner() {
IReasoner familyReasoner = new ReasonerImpl();
return familyReasoner;
}
}
3、ReasonerImpl.java
package tuili;
import java.util.List;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.rdf.model.InfModel;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasoner;
import com.hp.hpl.jena.reasoner.rulesys.Rule;
import com.hp.hpl.jena.shared.RulesetNotFoundException;
import com.hp.hpl.jena.vocabulary.ReasonerVocabulary;
public class ReasonerImpl implements IReasoner {
private InfModel inf = null;
/**
* 获取一个推理接口
* @param path
* @return
* @throws RulesetNotFoundException
*/
private GenericRuleReasoner getReasoner(String path) throws RulesetNotFoundException {
List
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();
}
}