This was already asked and answered on Stack Overflow [1]. The problem was:
1. The eg: prefix stands for "urn:x-hp:eg/", not "urn:x-hp-jena:eg/". It is a bit confusing because the documentation never explicitly states what it will be, and the documentation does use the latter in some of the examples, too. 2. RDFS reasoning requires a reasoner that does RDFS reasoning. The subproperty inference won't be performed by the custom rules provided here. //JT [1] http://stackoverflow.com/q/24786035/1281433 On Wed, Jul 16, 2014 at 3:18 PM, Maciek Niemczyk <[email protected]> wrote: > >> Betreff: Problems with Reasoning - even the simple explamples from Docu does >> not work! >> >> Hi >> >> i have a serious problem to get any reasoner up and running. >> Also the examples from the documentation: >> https://jena.apache.org/documentation/inference/ >> does not work here. >> I transferred the example into a unit test, so that the problem might be >> easier reproduced. >> >> Is reasoning limited to certain environment like a spatial JDK or so on, or >> am i getting something wrong? >> >> Thanks >> >> Here the (your example) code (as java unit test): >> >> import static org.junit.Assert.assertNotNull; >> >> import java.io.PrintWriter; >> import java.util.Iterator; >> >> import org.junit.Before; >> import org.junit.Test; >> >> 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.Property; >> 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.Derivation; >> import com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasoner; >> import com.hp.hpl.jena.reasoner.rulesys.Rule; >> import com.hp.hpl.jena.vocabulary.RDFS; >> >> public class ReasonerTest { >> >> String NS = "urn:x-hp-jena:eg/"; >> >> // Build a trivial example data set >> Model model = ModelFactory.createDefaultModel(); >> InfModel inf; >> >> Resource A = model.createResource(NS + "A"); >> Resource B = model.createResource(NS + "B"); >> Resource C = model.createResource(NS + "C"); >> Resource D = model.createResource(NS + "D"); >> >> Property p = model.createProperty(NS, "p"); >> Property q = model.createProperty(NS, "q"); >> >> >> @Before >> public void init() { >> >> // Some small examples (subProperty) >> model.add(p, RDFS.subPropertyOf, q); >> model.createResource(NS + "A").addProperty(p, "foo"); >> >> String rules = "[rule1: (?a eg:p ?b) (?b eg:p ?c) -> (?a eg:p ?c)]"; >> GenericRuleReasoner reasoner = new >> GenericRuleReasoner(Rule.parseRules(rules)); >> reasoner.setDerivationLogging(true); >> inf = ModelFactory.createInfModel(reasoner, model); >> >> // Derivations >> A.addProperty(p, B); >> B.addProperty(p, C); >> C.addProperty(p, D); >> } >> >> >> @Test >> public void subProperty() { >> Statement statement = A.getProperty(q); >> System.out.println("Statement: " + statement); >> assertNotNull(statement); >> } >> >> >> @Test >> public void derivations() { >> String trace = null; >> PrintWriter out = new PrintWriter(System.out); >> for (StmtIterator i = inf.listStatements(A, p, D); i.hasNext(); ) { >> Statement s = i.nextStatement(); >> System.out.println("Statement is " + s); >> for (Iterator id = inf.getDerivation(s); id.hasNext(); ) { >> Derivation deriv = (Derivation) id.next(); >> deriv.printTrace(out, true); >> trace += deriv.toString(); >> } >> } >> out.flush(); >> assertNotNull(trace); >> } >> >> @Test >> public void listStatements() { >> StmtIterator stmtIterator = inf.listStatements(); >> while(stmtIterator.hasNext()) { >> System.out.println(stmtIterator.nextStatement()); >> } >> } >> } >> >> -- Joshua Taylor, http://www.cs.rpi.edu/~tayloj/
