Top Banner
+ Inference in Jena Mariano Rodriguez-Muro, Free University of Bozen-Bolzano
27

SWT Lecture Session 8 - Inference in jena

Jan 13, 2015

Download

Education

 
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: SWT Lecture Session 8 - Inference in jena

+

Inference in Jena

Mariano Rodriguez-Muro, Free University of Bozen-Bolzano

Page 2: SWT Lecture Session 8 - Inference in jena

+Disclaimer

License This work is licensed under a

Creative Commons Attribution-Share Alike 3.0 License (http://creativecommons.org/licenses/by-sa/3.0/)

Page 3: SWT Lecture Session 8 - Inference in jena

+Reading material

Foundations of Semantic Web Chapter 3.

Jena Documentation

Page 4: SWT Lecture Session 8 - Inference in jena

+

RDFS reasoning in Jena

Page 5: SWT Lecture Session 8 - Inference 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

Page 6: SWT Lecture Session 8 - Inference in jena

+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

Page 7: SWT Lecture Session 8 - Inference in jena

+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

Page 8: SWT Lecture Session 8 - Inference in jena

+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

Page 9: SWT Lecture Session 8 - Inference in jena

+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() );}

Page 10: SWT Lecture Session 8 - Inference in jena

+

Inference engine in Jena

Page 11: SWT Lecture Session 8 - Inference 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

Page 12: SWT Lecture Session 8 - Inference in jena

+Overview

Entry point for reasoners: ReasonerRegistry

Page 13: SWT Lecture Session 8 - Inference in jena

+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

Page 14: SWT Lecture Session 8 - Inference in jena

+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

Page 15: SWT Lecture Session 8 - Inference in jena

+Inference through Models

Specific model implementations provide easy access to the reasoners for different Ontology configurations

Example: ModelFactory.createRDFSModel(Model)

provides an Model

Page 16: SWT Lecture Session 8 - Inference in jena

// 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>]

Page 17: SWT Lecture Session 8 - Inference in jena

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?

Page 18: SWT Lecture Session 8 - Inference in jena

+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);

Page 19: SWT Lecture Session 8 - Inference in jena

+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);

Page 20: SWT Lecture Session 8 - Inference in jena

+Direct and indirect relations

Jena allows to distinguish between direct and indirect relationships

Types of relations for transitive properties: Asserted Inferred Direct

Page 21: SWT Lecture Session 8 - Inference in jena

+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

Page 22: SWT Lecture Session 8 - Inference in jena

+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

Page 23: SWT Lecture Session 8 - Inference in jena

+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);

Page 24: SWT Lecture Session 8 - Inference in jena

+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();

Page 25: SWT Lecture Session 8 - Inference in jena

+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)

Page 26: SWT Lecture Session 8 - Inference in jena

+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 .

Page 27: SWT Lecture Session 8 - Inference in jena

+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);