使用jena 进行RDF推理
生活随笔
收集整理的這篇文章主要介紹了
使用jena 进行RDF推理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
JENA 推理
規則:
[transitiveRule: (?A demo:p ?B), (?B demo:p ?C) -> (?A demo:p ?C) ] [symmetricRule: (?Y demo:p ?X) -> (?X demo:p ?Y) ]數據:
@prefix : <http://jena.hpl.hp.com/demo#> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix demo: <http://jena.hpl.hp.com/demo#> .demo:c demo:p demo:a .demo:b demo:p demo:d .demo:a demo:p demo:b .進行推理:
package iie.ac.jenaTest;import org.apache.jena.rdf.model.InfModel; import org.apache.jena.rdf.model.Model; import org.apache.jena.rdf.model.ModelFactory; import org.apache.jena.rdf.model.Property; import org.apache.jena.rdf.model.RDFNode; import org.apache.jena.rdf.model.Resource; import org.apache.jena.rdf.model.StmtIterator; import org.apache.jena.reasoner.Reasoner; import org.apache.jena.reasoner.rulesys.GenericRuleReasonerFactory; import org.apache.jena.util.FileManager; import org.apache.jena.util.PrintUtil; import org.apache.jena.vocabulary.ReasonerVocabulary;public class ReasonerRuleTest {public static void main(String[] args) {// Register a namespace for use in the demoString demoURI = "http://jena.hpl.hp.com/demo#";PrintUtil.registerPrefix("demo", demoURI);// Create an (RDF) specification of a hybrid reasoner which// loads its data from an external file.Model m = ModelFactory.createDefaultModel();Resource configuration = m.createResource();configuration.addProperty(ReasonerVocabulary.PROPruleMode, "hybrid");configuration.addProperty(ReasonerVocabulary.PROPruleSet, "data/demo.rules");// Create an instance of such a reasonerReasoner reasoner = GenericRuleReasonerFactory.theInstance().create(configuration);// Load test dataModel data = FileManager.get().loadModel("file:data/demoData.ttl");InfModel infmodel = ModelFactory.createInfModel(reasoner, data);// Query for all things related to "a" by "p"Property p = data.getProperty(demoURI, "p");Resource a = data.getResource(demoURI + "a");StmtIterator i = infmodel.listStatements(a , p, (RDFNode)null);while (i.hasNext()) {System.out.println(" - " + PrintUtil.print(i.nextStatement()));//Statement s = i.nextStatement();//System.out.println("<"+s.getSubject()+"> <" + s.getPredicate() + "> <" + s.getObject() + "> .");}} }輸出結果為:
(demo:a demo:p demo:d) (demo:a demo:p demo:a) (demo:a demo:p demo:c) (demo:a demo:p demo:b)github:https://github.com/vincentduan/RDF-JENA/tree/master/JenaInference
總結
以上是生活随笔為你收集整理的使用jena 进行RDF推理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jena fuseki RDF应用
- 下一篇: Java异常体系