1 10 package com.hp.hpl.jena.reasoner.test; 11 12 import com.hp.hpl.jena.rdf.model.*; 13 import com.hp.hpl.jena.reasoner.rulesys.*; 14 import com.hp.hpl.jena.reasoner.*; 15 import com.hp.hpl.jena.util.FileManager; 16 import com.hp.hpl.jena.util.PrintUtil; 17 import com.hp.hpl.jena.vocabulary.RDFS; 18 import com.hp.hpl.jena.vocabulary.ReasonerVocabulary; 19 20 import java.io.PrintWriter ; 21 import java.util.*; 22 23 29 public class ManualExample { 30 31 32 public void test1() { 33 String NS = "urn:x-hp-jena:eg/"; 34 35 Model rdfsExample = ModelFactory.createDefaultModel(); 37 Property p = rdfsExample.createProperty(NS, "p"); 38 Property q = rdfsExample.createProperty(NS, "q"); 39 rdfsExample.add(p, RDFS.subPropertyOf, q); 40 rdfsExample.createResource(NS+"a") 41 .addProperty(p, "foo"); 42 43 Resource config = ModelFactory.createDefaultModel() 47 .createResource() 48 .addProperty(ReasonerVocabulary.PROPsetRDFSLevel, "simple"); 49 Reasoner reasoner = RDFSRuleReasonerFactory.theInstance().create(config); 50 InfModel inf = ModelFactory.createInfModel(reasoner, rdfsExample); 54 Resource a = inf.getResource(NS+"a"); 55 Statement s = a.getProperty(q); 56 System.out.println("Statement: " + s); 57 } 58 59 60 public void test2(String fname) { 61 System.out.println("Testing " + fname); 62 Model data = FileManager.get().loadModel(fname); 63 InfModel infmodel = ModelFactory.createRDFSModel(data); 64 ValidityReport validity = infmodel.validate(); 65 if (validity.isValid()) { 66 System.out.println("OK"); 67 } else { 68 System.out.println("Conflicts"); 69 for (Iterator i = validity.getReports(); i.hasNext(); ) { 70 ValidityReport.Report report = (ValidityReport.Report)i.next(); 71 System.out.println(" - " + report); 72 } 74 } 75 } 76 77 78 public void test3() { 79 String egNS = PrintUtil.egNS; Model rawData = ModelFactory.createDefaultModel(); 82 Property p = rawData.createProperty(egNS, "p"); 83 Resource A = rawData.createResource(egNS + "A"); 84 Resource B = rawData.createResource(egNS + "B"); 85 Resource C = rawData.createResource(egNS + "C"); 86 Resource D = rawData.createResource(egNS + "D"); 87 A.addProperty(p, B); 88 B.addProperty(p, C); 89 C.addProperty(p, D); 90 91 String rules = "[rule1: (?a eg:p ?b) (?b eg:p ?c) -> (?a eg:p ?c)]"; 93 Reasoner reasoner = new GenericRuleReasoner(Rule.parseRules(rules)); 94 reasoner.setDerivationLogging(true); 95 InfModel inf = ModelFactory.createInfModel(reasoner, rawData); 96 97 PrintWriter out = new PrintWriter (System.out); 98 for (StmtIterator i = inf.listStatements(A, p, D); i.hasNext(); ) { 99 Statement s = i.nextStatement(); 100 System.out.println("Statement is " + s); 101 for (Iterator id = inf.getDerivation(s); id.hasNext(); ) { 102 Derivation deriv = (Derivation) id.next(); 103 deriv.printTrace(out, true); 104 } 105 } 106 out.flush(); 107 } 108 109 110 public void test4() { 111 String egNS = PrintUtil.egNS; Model rawData = ModelFactory.createDefaultModel(); 114 Property first = rawData.createProperty(egNS, "concatFirst"); 115 Property second = rawData.createProperty(egNS, "concatSecond"); 116 Property p = rawData.createProperty(egNS, "p"); 117 Property q = rawData.createProperty(egNS, "q"); 118 Property r = rawData.createProperty(egNS, "r"); 119 Resource A = rawData.createResource(egNS + "A"); 120 Resource B = rawData.createResource(egNS + "B"); 121 Resource C = rawData.createResource(egNS + "C"); 122 A.addProperty(p, B); 123 B.addProperty(q, C); 124 r.addProperty(first, p); 125 r.addProperty(second, q); 126 127 String rules = 129 "[r1: (?c eg:concatFirst ?p), (?c eg:concatSecond ?q) -> " + " [r1b: (?x ?c ?y) <- (?x ?p ?z) (?z ?q ?y)] ]"; Reasoner reasoner = new GenericRuleReasoner(Rule.parseRules(rules)); 130 InfModel inf = ModelFactory.createInfModel(reasoner, rawData); 132 Iterator list = inf.listStatements(A, null, (RDFNode)null); 134 System.out.println("A * * =>"); 135 while (list.hasNext()) { 136 System.out.println(" - " + list.next()); 137 } 138 } 139 140 public static void main(String [] args) { 141 try { 142 new ManualExample().test4(); 147 } catch (Exception e) { 148 System.out.println("Problem: " + e); 149 e.printStackTrace(); 150 } 151 } 152 } 153 154 155 156 | Popular Tags |