-- 作者:qwertry
-- 发布时间:11/9/2011 8:54:00 AM
-- 在JENA推理中,如何完成已知一个Resource和一个Property得出相应Resource
希望将下面的源代码改造一下,但是不成功 mport java.util.List; import com.hp.hpl.jena.ontology.OntModel; import com.hp.hpl.jena.ontology.OntModelSpec; import com.hp.hpl.jena.rdf.model.*; import com.hp.hpl.jena.reasoner.*; import com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasoner; import com.hp.hpl.jena.reasoner.rulesys.Rule; import com.hp.hpl.jena.util.*; import com.hp.hpl.jena.vocabulary.ReasonerVocabulary; public class FamilyReasoner { public void InferenceRelation(Resource a, Resource b) { Model model = ModelFactory.createDefaultModel(); model.read("file:E:/Project/ontology/FamilyReasoner/family.owl"); List rules = Rule.rulesFromURL("file:E:/Project/ontology/FamilyReasoner/family.rules"); GenericRuleReasoner reasoner = new GenericRuleReasoner(rules); reasoner.setOWLTranslation(true); reasoner.setDerivationLogging(true); reasoner.setTransitiveClosureCaching(true); OntModel om = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_RULE_INF, //这里使用OWL_DL_MEM效果也一样 model); Resource configuration = om.createResource(); configuration.addProperty(ReasonerVocabulary.PROPruleMode, "hybrid"); InfModel inf = ModelFactory.createInfModel(reasoner, om); StmtIterator stmtIter = inf.listStatements(a, null, b); if (!stmtIter.hasNext()) { //进入这一步表示两资源之间没关系,与differentFrom还是有差别的。 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"); // System.out.println(s); } } public static void main(String[] args) { FamilyReasoner f = new FamilyReasoner(); Model m = FileManager.get().loadModel("file:E:/Project/ontology/FamilyReasoner/family.owl"); String NS = "http://www.semanticweb.org/ontologies/2010/0/family.owl#"; Resource Jim = m.getResource(NS + "Jim"); Resource John = m.getResource(NS + "John"); Resource Lucy = m.getResource(NS + "Lucy"); Resource Kate = m.getResource(NS + "Kate"); Resource Sam = m.getResource(NS + "Sam"); Resource James = m.createResource(NS + "James"); Resource Anna = m.getResource(NS + "Anna"); Resource Holly = m.createResource(NS + "Holly"); f.InferenceRelation(Jim, John); f.InferenceRelation(John, Jim); f.InferenceRelation(John, Sam); f.InferenceRelation(Lucy, John); f.InferenceRelation(Kate, Sam); f.InferenceRelation(Sam, Kate); f.InferenceRelation(James, John); //f.InferenceRelation(Anna, Holly); //f.InferenceRelation(Sam, James);这个例子能够把规则貌似推翻,哎!人家的什么破规则 } }
|