Reasoner

advertisement
Una piattaforma inferenziale per il
Web Semantico: Jena2
Roma, 2006
Web Semantico
Jena2: a semantic web
toolkit
M. Missikoff – F. Taglino
LEKS, IASI-CNR
1
Summary


Introduction
Jena 2 Inference Support







Rules and Jena2 syntax
Reasoner
Inferred Knowledge
Reconciliation using Jena2
Managing RDF with Jena2
Managing OWL and RDFS with Jena 2
RDQL
2
Introduction





Jena2 is a Semantic Web programmers’s
toolkit
Provides a programming environment for
knowledge representation language (i.e.,
RDF, OWL).
Result of the HP Labs Semantic Web
Programme
Developed in Java
Open Source (http://jena.sourceforge.net/)
These slides refer to the version 2.2 of the Jena package
3
Main modules




Inference API, for deriving additional
knowledge from graphs
RDF API, for manipulating RDF graphs
Ontology API, for manipulating
ontologies (e.g., RDF(S), OWL,
DAML+OIL)
RDQL, for querying RDF graphs
4
Relationships among the modules
uses
Ontology API
Manipulating of Ontologies
Inference API
RDQL
Inferencing on ontologies
(reasoner engines)
Querying
RDF graphs
RDF API
Manipulating RDF graphs
(set of triples)
5
Jena2
Architecture and Inference
Support
6
Jena2 basic definitions







Raw facts (ABox: assertion box), ground facts from
which we intend to derive additional knowledge;
Schemas, (TBox: terminological box) contain
definition of classes and properties (schema);
Rules, their application allows to derive new facts
Knowledge: facts, schemas and rules
Inference, the abstract process of deriving
knowledge
Reasoner, a specific software that performs inference
Inferred knowledge: the output of a reasoner
7
Jena2 Inference Support
Rules
• Spec Rules
1
Inference
Engine
• General Rule
Inference Engine Configuration
1
Reasoning
Mode:
• Forward
• Backward
• Hybrid
2
Inference Execution
Raw
Facts
• Model
3
Reasoner
• Reasoner
4
Inferred
Facts
• InfModel
8
Jena2 Inference Support (cont)

Configuration of the inference Engine:




Rules definitions.
Choose of the Reasoning mode.
Reading of the Raw facts.
Derivation of the inferred knowledge by
applyng the rules associated with the reasoner.
9
Jena2 rules structure
Name: Antecedents
Consequents
The rule specification comprises:
 a list of antecendents, the rule’s body
 a list of consequents , the rule’s head
 a name (optional), the rule’s identifier
 a direction, the reasoning mode
In forward-chaining:
 A rule is satisfied if the body is true
 Facts in the head of a satisfied rule are inferred
[(?a has_father ?b) (?b has_brother ?c) -> (?a has_uncle ?c)]
10
Jena2 rule elements
Each element in the head or body can be a:
 TriplePattern, a triple of Nodes. A Node can be:


Functor, in heads they represent actions, in bodies they
represent builtin predicates. A functor comprises a:



wildcard (*), variable (?x), uri or literal
name
list of arguments (Nodes of any type except functor nodes).
Functors are built-in (i.e., sum, difference) or user definied
(i.e., stringConcatenation).
Rule

Embedded, therefore, in a rule
11
Jena2 rule Syntax
Structure
(nodes nodep nodeo)
TriplePattern
FuncName(node1, ... , nodeN )
Functor
[Name: body -> head ]
Embedded rule
Node
*
Wildcards
?var
Variable
‘lit’
Literal
http://...
Uri
info:age
Local Name
23
Number
Name
[Rule1:
(?a ns:pA ?c)
->
Functor
fun(k ,?d)
(?a ns:pB ?d)
]
Triple
Pattern
12
More Rules
[Rule2:
(?x type boiler) (?x has_temp ?y) (?y gt 80)
->
fun(switch_off ,?x) (?x has_status “off” )
]
[Transitivity: (?a p ?b) (?b p ?c) -> (?a p ?c) ]
[Simmetry: (?a p ?b) -> (?b p ?a) ]
13
Reasoning modes (rule direction)
Defines the way in which the rules can be applied
 Forward, for deriving new facts
 Backward, for checking satisfiability of goals
 Hybrid, mix of forward and backward rules
forward
Antecedents
Consequents
backward
14
Reasoning modes:
Forward chaining
Knowledge (Facts + Rules)
Steps to be repeated until no more satisfied rules:
 Identify satisfied rules
 For each satisfied rule, facts in the head are added


E.g.
Facts: (a, b, d, e)
Rules: R1:(a,b → c,d) R2:(c,e → f,g)
Applying R1 c is added as new fact
Applying R2 f,g are added as new facts
Results (Inferred knowledge): c,f,g
15
Reasoning modes:
Backward chaining
Knowledge (Facts + Rules + Goals)
 A goal is satisfied if it belongs to facts or is inferred by rules
Steps to be repeated until all the goals are satisfied or no more
goals can be satisfied:
 Identify rules that contain goals in the head
 For each such rules, facts in the body are added as new
goals
 E.g.
Facts: (a, b, d)
Rules: R1:(c ← a,b)
R2:(e ← c,d)
Goals: (e) and (c) as intermediate step
a and b are true, then c is satisfied (R2)
c and d are true, then e is satisfied (R1)

16
Reasoning modes:
Hybrid



Knowledge (Facts + Fwd rules + Bwd rules + Goals)
New facts are added by using forward rules
Goals are tried to be satisfied by applying backward
rules; inferred facts are used too.
17
Using a Jena2 reasoner
Fact
(Raw)
A Reasoner allows to
derive additional RDF
assertions from a set of
raw fact.
Reasoner
Rule + Inference Mode
+ Inference Engine
Fact
(Inferred)
18
Reasoner




The Reasoner interface is the interface to which all
reasoners conform to.
The ReasonerRegistry also provides convenient
access to prebuilt instances of the main supplied
reasoners.
Each Reasoner is accessible by an interface and it is
configurable with new rules and reasoning
mode.
All the Reasoner are obtainable by a
GenericRuleReasoner totally configurable.
19
Inferred Knowledge




The inferred knowledge is represented by the InfModel
interface which extends the Model interface.
Taking as input the raw data and the reasoner the
createInfModel method of the ModelFactory class
generates an InfModel.
An InfModel instance cointains a link to the raw data, the
inferred knowledge, and the Reasoner.
The getDerivation method of the InfModel Interface
shows the deductions that generate an inferred statement.
20
Example: a reasoner for the
transitive rule
[Transitivity: (?a eg:p ?b) (?b eg:p ?c) -> (?a eg:p ?c)]
1
Forward
1
Inference
Engine
2
eg:A eg:p eg:B
eg:B eg:p eg:C
3
Reasoner
4
eg:A eg:p eg:B
eg:B eg:p eg:C
+
eg:A eg:p eg:C
22
Example: a reasoner for the
transitive rule (cont)
1. Definition of the transitive rule:
[Transitivity: (?a eg:p ?b) (?b eg:p ?c) -> (?a eg:p ?c)]
forward is the reasoning mode.
2. forward
3. Creation of the Reasoner
reasoner with defined parameters.
4 Raw Fact:
eg:A eg:p eg:B
eg:B eg:p eg:C
5. Inferred Fact:
eg:A eg:p eg:C
• Body of the Rule:
(eg:A eg:p eg:B) matches (?a eg:p ?b)
(eg:B eg:p eg:C) matches (?b eg:p ?c)
• Variable context:
(?a = eg:A) (?b = eg:B) (?c = eg:C)
• Head of the rule:
(?a eg:P ?c) generates (eg:A eg:P eg:C)
23
Implementing and using a
reasoner
[Rule1:
]
(?a fatherOf ?b)
(?a brotherOf ?c)
->
(?c uncleOf ?b)
FORWARD
MODE
<Person rdf:ID=“John”>
<fatherOf rdf:resource=“Mike”/>
<fatherOf rdf:resource=“Tom”/>
</Person>
<Person rdf:about=“John”>
<brotherOf rdf:resource=“Harry”/>
</Person>
relatives.rules
<Person rdf:ID=“Harry”/>
<Person rdf:ID=“John”>
<fatherOf rdf:resource=“Mike”/>
<fatherOf rdf:resource=“Tom”/>
</Person>
<Person rdf:about=“John”>
<brotherOf rdf:resource=“Harry”/>
</Person>
<Person rdf:ID=“Harry”>
<uncleOf rdf:resource=“Mike”/>
<uncleOf rdf:resource=“Tom”/>
</Person>
Inferred Facts
InfModel
persons.rdf
REASONER + DATA
24
Implementing and using a
reasoner (the Java code)
Registrare il proprio namespace
// Register a namespace for use in the demo
se nel file di dati (.rdf) ne viene
String exampleURI = "http://xyz/#";
utilizzato uno
PrintUtil.registerPrefix(“example", exampleURI);
// Create an (RDF) specification of a forward reasoner which
Modificare il nome del file
// loads its data from an external file.
Model m = ModelFactory.createDefaultModel();
per utilizzare le proprie
Resource configuration = m.createResource();
regole
configuration.addProperty(ReasonerVocabulary.PROPruleMode, “forward");
configuration.addProperty(ReasonerVocabulary.PROPruleSet, “relatives.rules");
// Create an instance of such a reasoner
Reasoner reasoner = GenericRuleReasonerFactory.theInstance().create(configuration);
// Load test data
Model myData = ModelLoader.loadModel("file:persons.rdf");
InfModel infmodel = ModelFactory.createInfModel(reasoner, myData); Modificare il nome del file
per utilizzare i propri dati
// Query for all things related to “Harry" by “uncleOf"
Property p = myData.getProperty(exampleURI, “uncleOf");
Resource a = myData.getResource(exampleURI + “Harry");
StmtIterator i = infmodel.listStatements(a, p, (RDFNode)null);
25
Reconciliation using Jena2
Schema A
reconciliation
Rules
Schema B
Data A
Reasoner
Data B
26
An example of reconciliation:
the SchemaA and the Ontology
Ontology
SchemaA
Person
HumanBeing
hasName
name
age
Name
hasAge
firstName
rdfs:Literal
Rdfs:Literal
lastName
27
An example of reconciliation:
Semantic clashes
Naming
<rdfs:Class rdf:ID=“Person”/>
<rdf:Property rdf:ID=“name”>
<rdfs:domain rdf:resource=“#Person”>
<rdfs:range rdf:resource=“rdfs:Literal”>
</rdf:Property>
<rdfs:Class rdf:ID=“HumanBeing”/>
Structural <rdfs:Class rdf:ID=“Name”/>
(split)
<rdf:Property rdf:ID=“firstName”>
<rdf:Property rdf:ID=“age”>
<rdfs:domain rdf:resource=“#Person”>
<rdfs:range rdf:resource=“rdfs:Literal”>
</rdf:Property>
SchemaA.rdf
Naming
Ontology.rdf
<rdfs:domain rdf:resource=“#Name”>
<rdfs:range rdf:resource=“rdfs:Literal”>
</rdf:Property>
<rdf:Property rdf:ID=“lastName”>
<rdfs:domain rdf:resource=“#Name”>
<rdfs:range rdf:resource=“rdfs:Literal”>
</rdf:Property>
<rdf:Property rdf:ID=“hasName”>
<rdfs:domain rdf:resource=“#HumanBeing”>
<rdfs:range rdf:resource=“#Name”>
</rdf:Property>
<rdf:Property rdf:ID=“hasAge”>
<rdfs:domain rdf:resource=“#HumanBeing”>
<rdfs:range rdf:resource=“rdfs:Literal”>
</rdf:Property>
28
An example of reconciliation:
rules from Schema to Ontology
a: namespace for SchemaA
SchemaA.Person = Ontology.HumanBeing
o: namespace
[R1: (?a rdf:type a:Person) -> (?a rdf:type o:HumanBeing)]
for ontology
User defined
SchemaA.name = Ontology.hasName
functors (java
[R2: (?a rdf:type a:Person) (?a a:name ?b) ->
methods)
split(?b, ‘-’, ?c,?d)
blankNode(?e) (?e rdf:type o:Name) (?e o:firstName ?c)
(?e o:lastName ?d) (?a o:hasName ?e)]
SchemaA.age = Ontology.hasAge
[R3: (?a rdf:type a:Person) (?a a:age ?b) -> (?a o:hasAge ?b)]
<a:Person rdf:ID=“JS”>
<a:name>John-Smith</a:name>
<a:age>23</a:age>
</a:Person>
instanceA.rdf
Rules
application
Ontology
instance
<o:HumanBeing rdf:ID=“JS”>
<o:hasName rdf:resource=“#XYZ”>
<o:hasAge>23</o:hasAge >
</o:HumanBeing >
<o:Name rdf:ID=“XYZ”>
<o:firstName>John</o:firstName>
<o:lastName>Smith</o:lastName>
</o:Name>
29
Managing RDF with Jena2
RDF API
30
Main features
Set of API for creating and manipulating RDF
graphs (also called models) for:
 Creating RDF graphs
 Writing and reading RDF graphs
 Navigating and Querying RDF graphs
 Performing operations on RDF graphs:


Union, Intersection, Difference
In-memory & persistent storage of RDF model
with database like MySQL, PostgreSQL e Oracle.
32
Creating, an RDF graph


In Jena a graph is called a model and it is
represented by the Model interface
Resources, properties and literals have their own
interfaces
Create an empty model
static String personURI = "http://somewhere/JS";
static String fullName = "John Smith";
Model model = ModelFactory.createDefaultModel();
Resource johnSmith = model.createResource(personURI);
johnSmith.addProperty(FN, fullName);
http://xyz/JS
FN
John Smith
Create the resource
Add the property
33
Jena: Reading and writing RDF

Reading RDF, means instantiating an RDF
model starting from a serialization of an RDF graph


Writing RDF, means producing a serialization
of an RDF model


read method of the Model interface
write method of the Model interface
Several serializations are allowed: RDF/XML,
RDF/XML-ABBREV, N3, N-TRIPLE
34
Navigating a model
For accessing information held in a Model
 Retrieving a Resource by its URI


The doc
to be
inspected
Resource johnSmith = model.getResource(“http://xyz/JS”)
Accessing properties (and value) of a Resource


Resource father = johnsmith.getProperty(Father).getResource()
String fullname = johnsmith.getProperty(FN).getString()
http://xyz/JS
Father
http://xyz/MJ
FN
John Smith
35
Querying a model




If resources’ URI are unknown
Only support for limited queries
More powerful queries can be expressed in RDQL
The listStatements(S,P,O) method of the Model returns all
the statements in the model matching the specified
subject (S), predicate (P) and object (O)


If a null is supplied as any parameter, it matches anything
I.e., model.listStatements(null, FN, null) returns all the
statements whose subject has a FN (full name) as
predicate, independently of the object
36
Operations on models

Three common set operations for manipulating
models as whole are supported:

Union, Intersection, Difference
http://xyz/JS
FN
John Smith
Father
http://xyz/JS
UNION
http://xyz/MJ
Email
FN
John Smith
[email protected]
=
Same nodes are
merged and
duplicate arcs
are dropped
http://xyz/JS
FN
John Smith
Father
http://xyz/MJ
Email
[email protected]
37
Managing ontology with Jena2
Ontology API
38
Main features


Manipulate ontology information expressed in
RDFS, OWL and DAML+OIL
Language-neutral (independent of the specific
language)




Same basic classes for dealing with any language
For each language a profile which lists the permitted
constructs
Extend the Jena RDF API
Can be integrated with an ontology reasoner
39
Creating an ontology model


An ontology model (OntModel interface) is an
extension of the Jena RDF model (Model
Interface)
A binding for a specific ontology language needs to
be specified (language profile)
E.g. creation of a model for handling RDFS ontologies
OntModel m =
ModelFactory.createOntologyModel(ProfileRegistry.RDFS_MEM)
Specifies the
language profile
40
The generic ontology type:
OntResource interface



Provides a common super-type for all the ontology
constructs.
Extends the RDF Resource interface
Collects methods for
Resource
accessing common
attributes of ontology
resources (i.e., label,
OntResource
comment, …)
OntClass
OntProperty
…
41
Handling ontology Classes




Classes are the basic building blocks of an ontology
A class is represented in Jena by an OntClass
Collects methods for accessing attributes of
ontology classes (i.e., subclasses, superclasses, …)
Retrieve an existing
Resource
Eg.:
Resource r = m.getResource(“http://xyz/Person" );
OntClass person = (OntClass) r.as( OntClass.class );
Iterator i = person.listSubClasses();
Get the sub classes
Convert the Resource
into an ontology class
42
Handling ontology Properties

Property is represented by OntProperty interface

ObjectProperty and DatatypeProperty as specializations
Collects methods for accessing attributes of
properties (i.e., subproperties, domain, range, …)


Create an
ObjectProperty
Eg.:
ObjectProperty drive = m.createObjectProperty( “http://xyz/drive”);
drive.addDomain(person);
drive.addRange(car);
Specify the domain
and range
43
More complex class expressions

Restriction: defines a class by imposing contraints
on properties having the class as domain (e.g.,
within the class Carnivore the range of the property
Eat takes value in instances of the Meat class )
<owl:Class rdf:ID=“Carnivore">
<rdfs:subClassOf rdf:resource=“#Animal"/>
<rdfs:subClassOf>
<owl:Restriction>
<owl:onProperty rdf:resource="#Eat" />
<owl.allValuesFrom rdf:resource="#Meat">
</owl:Restriction>
</rdfs:subClassOf>
</owl:Class>

Defining class by intersection, union and
complement of classes
44
RDQL processing module
45
RDQL





Language for querying RDF models
SQL-like syntax (but only AND clause).
It only queries the information held in the models,
there is no inference being done.
It is based on the jena syntax.
A query on a model provides a set of bindings
that represent the answer to the query.
Query (input)
Query
--->
Model (set of triples) ---> Set of bindings (output)
Model
Bindings
46
Graph pattern

RDQL consider a query as a graph patterns that
have to match the base graph in order to identify
a set of bindings
Query
SELECT ?x , ?y
Graph Pattern
?x
WHERE (?x, <vcard:FN>, ?y)
vcard:FN
SQL-like syntax, but only
AND clauses allowed
?y
47
Example of RDQL query
Base graph
Graph pattern
?x
OVERLAPPING
vcard:FN
?y
Bindings set
Bindings set:
Set of variable
name-value pairs
for the variables
used in the query.
Variabili
Valori
?x
http://.../JohnSmith
?y
“John Smith”
48
Sources
Jena2

http://jena.sourceforge.net/
Jena2 Inference support

http://jena.sourceforge.net/inference/
Jena2 Java Doc

http://jena.sourceforge.net/javadoc/index.html
49
Download
Related flashcards

Computer science

25 cards

MSN

28 cards

Software

43 cards

Science software

65 cards

Create Flashcards