+ Inference in Jena Mariano Rodriguez-Muro, Free University of Bozen-Bolzano
+
Inference in Jena
Mariano Rodriguez-Muro, Free University of Bozen-Bolzano
+Disclaimer
License This work is licensed under a
Creative Commons Attribution-Share Alike 3.0 License (http://creativecommons.org/licenses/by-sa/3.0/)
+Reading material
Foundations of Semantic Web Chapter 3.
Jena Documentation
+
RDFS reasoning in Jena
+Jena
Jena allows for a range of reasoners
Main objective: support RDFS and OWL
Pre-canned reasoning in the Ontology API
However, support is general, i.e.: Generic inference rule engine Support for arbitrary, rule-based processing of RDF
+Ontology Model (the simple way)
A normal model that is aware of inferences
Convenience methods to access inference related functionality
Simple recipes for reasoning in different laguages
+Creating ontology models
OntModel m = ModelFactory.createOntologyModel();
Default ontology model: OWL-Full in-memory storage RDFS inference
Hence a default OntModel is less performant than a simple Graph
+Ontology Models for specific languages OntModel m =
ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM );
Languages available:OntModelSpec.OWL_DL_MEMOntModelSpec.OWL_DL_MEM_RDFS_INF…OntModelSpec.OWL_LITE_MEMOntModelSpec.OWL_LITE_MEM_RDFS_INF…OntModelSpec.OWL_MEMOntModelSpec.OWL_MEM_MICRO_RULE_INFOntModelSpec.OWL_MEM_MINI_RULE_INFOntModelSpec.OWL_MEM_RDFS_INFOntModelSpec.OWL_MEM_RULE_INFOntModelSpec.RDFS_MEMOntModelSpec.RDFS_MEM_RDFS_INFOntModelSpec.RDFS_MEM_TRANS_INF
+Asserted vs. InferedOntModel base = ModelFactory.createOntologyModel( OWL_MEM );base.read( SOURCE, "RDF/XML" );
// create the reasoning model using the baseOntModel inf = ModelFactory.createOntologyModel( OWL_MEM_MICRO_RULE_INF, base );
// create a dummy paper for this exampleOntClass paper = base.getOntClass( NS + "Paper" );Individual p1 = base.createIndividual( NS + "paper1", paper );
// list the asserted typesfor (Iterator<Resource> i = p1.listRDFTypes(); i.hasNext(); ) { System.out.println( p1.getURI() + " is asserted in class " + i.next() );}
// list the inferred typesp1 = inf.getIndividual( NS + "paper1" );for (Iterator<Resource> i = p1.listRDFTypes(); i.hasNext(); ) { System.out.println( p1.getURI() + " is inferred to be in class " + i.next() );}
+
Inference engine in Jena
+Overview
Model Factory: entry point
Using a reasoner and a Model we create an “Inferred Graph”
Queries over the inferred graph
May use any Model implementation
May use InfModel for extra control over the infered graph
+Overview
Entry point for reasoners: ReasonerRegistry
+Available reasoners
Included reasoners: Transitive reasoner
Implements the transitive and reflexibe properties of rdfs:subPropertyOf and rdfs:subClassOf
RDFSConfigurable subset of RDFS entailments
OWL, OWL Mini, OWL MicoIncomplete implementations of OWL-Lite
DAML reasonerProvides DAML inferences
Generic rule reasonerGeneric rule-based reasoner with support for forward, backward and hybrid execution strategies
+Inference API
A Factory for each type of reasoner (ReasonerFactory instances)
Factory handles can be obtained with: theInstance() calls Using the global ReasonerRegistry and the URI that
identifies the reasoner type
Default reasoner can be accessed with: getTransitiveReasoner getRDFSReasoner getOWLReasoner, getOWLMiniReasoner,
getOWLMicroReasoner
+Inference through Models
Specific model implementations provide easy access to the reasoners for different Ontology configurations
Example: ModelFactory.createRDFSModel(Model)
provides an Model
// Build a trivial example data setModel rdfsExample = ModelFactory.createDefaultModel();Property p = rdfsExample.createProperty(NS, "p");Property q = rdfsExample.createProperty(NS, "q");rdfsExample.add(p, RDFS.subPropertyOf, q);rdfsExample.createResource(NS+"a").addProperty(p, "foo");
InfModel inf = ModelFactory.createRDFSModel(rdfsExample); // [1]
Resource a = inf.getResource(NS+"a"); System.out.println("Statement: " + a.getProperty(q));
Statement: [urn:x-hp-jena:eg/a, urn:x-hp-jena:eg/q, Literal<foo>]
Reasoner reasoner = ReasonerRegistry.getRDFSReasoner();
InfModel inf = ModelFactory.createInfModel(reasoner, rdfsExample);
or even more manually by
Reasoner reasoner = RDFSRuleReasonerFactory.theInstance().create(null); InfModel inf = ModelFactory.createInfModel(reasoner, rdfsExample);
Why create a reasoner instance?
+Configuring a reasoner
RDF triples (a resource) is used to configure the reasonerReasonerFactory.create(Resource configuration)
Additionally use Resoner.setParameter
Built-in parameter can be found in ReasonerVocabulary
Reasoner reasoner = RDFSRuleReasonerFactory.theInstance()Create(null); reasoner.setParameter(ReasonerVocabulary.PROPsetRDFSLevel, ReasonerVocabulary.RDFS_SIMPLE); InfModel inf = ModelFactory.createInfModel(reasoner, rdfsExample);
+Configuring a reasoner
RDF triples (a resource) is used to configure the reasonerReasonerFactory.create(Resource configuration)
Additionally use Resoner.setParameter
Built-in parameter can be found in ReasonerVocabularyResource config = ModelFactory.createDefaultModel()
.createResource() .addProperty(ReasonerVocabulary.PROPsetRDFSLevel, "simple"); Reasoner reasoner = RDFSRuleReasonerFactory.theInstance().create(config); InfModel inf = ModelFactory.createInfModel(reasoner, rdfsExample);
+Direct and indirect relations
Jena allows to distinguish between direct and indirect relationships
Types of relations for transitive properties: Asserted Inferred Direct
+Direct and indirect relations
Access is done through ALIASES
Jena proves aliases for subClassOf and subProperty of in ReasonerVocabulary directSubPropertyOf directSubClassOf
Directly using the Ontology APIcan facilitate access to these types of relations
+Tracing
Jena allows to keep track of the derivation of statements
Use InfModel.getDerivation(Statement) Iterator<RuleDerivation>
getRule() : Rule getConclusion() : Triple getMatces() : List<Triple>
The full trace can be obtained withDreivation.PrintTrace
+Tracing (example)
eg:A eg:p eg:B .eg:B eg:p eg:C .eg:C eg:p eg:D .
String rules = "[rule1: (?a eg:p ?b) (?b eg:p ?c) -> (?a eg:p ?c)]"; Reasoner reasoner = new GenericRuleReasoner(Rule.parseRules(rules)); reasoner.setDerivationLogging(true); InfModel inf = ModelFactory.createInfModel(reasoner, rawData);
+Tracing (example)
eg:A eg:p eg:B .eg:B eg:p eg:C .eg:C eg:p eg:D .
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); } } out.flush();
+Tracing (example)
Statement is [urn:x-hp:eg/A, urn:x-hp:eg/p, urn:x-hp:eg/D]Rule rule1 concluded (eg:A eg:p eg:D) <- Fact (eg:A eg:p eg:B) Rule rule1 concluded (eg:B eg:p eg:D) <- Fact (eg:B eg:p eg:C) Fact (eg:C eg:p eg:D)
+The RDFS reasoner
Support for MOST RDFS entailments
Accessed from: ModelFactory.createRDFSModel or ReasonerRegistry.getRDFSReasoner
In FULL mode all entailments except:= bNode closure. Example:
eg:a eg:p nnn^^datatype .we should introduce the corresponding blank nodes:
eg:a eg:p :anon1 .:anon1 rdf:type datatype .
+The RDFS reasoner
RDFSRuleReasoner configuration: Full (expensive)
All entailment except bNode closure and rdfD1 Default
Ommits container membership properties, x rdfs:type :Resource and every predicate is a :Property (rdf1, rdfs4a, rdfs4b)
SimpleTransitive closure for subPropertyOf and subClassOf, domain and range and implications of subPropertyOf and subClassOf
reasoner.setParameter(ReasonerVocabulary.PROPsetRDFSLevel, ReasonerVocabulary.RDFS_SIMPLE);