1 jena2: a semantic web toolkit m. missikoff – f. taglino leks, iasi-cnr una piattaforma...
TRANSCRIPT
1
Jena2: a semantic web toolkit
M. Missikoff – F. TaglinoLEKS, IASI-CNR
Una piattaforma inferenziale per ilWeb Semantico: Jena2
Roma, 2006
Web Semantico
2
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
3
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
4
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
5
Relationships among the modules
Inference APIInferencing on ontologies
(reasoner engines)
Ontology APIManipulating of Ontologies
RDQLQuerying
RDF graphs
RDF APIManipulating RDF graphs
(set of triples)
uses
6
Jena2Architecture and Inference Support
7
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
8
Inference Engine Configuration
Inference Execution
Jena2 Inference Support
InferenceEngine
• General Rule1
Inferred Facts
• InfModel
Rules• Spec Rules
1
RawFacts
• Model
ReasoningMode:
• Forward• Backward
• Hybrid
2
Reasoner• Reasoner3 4
9
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.
10
Jena2 rules structure
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 modeIn forward-chaining: A rule is satisfied if the body is true Facts in the head of a satisfied rule are
inferred
Antecedents ConsequentsName:
[(?a has_father ?b) (?b has_brother ?c) -> (?a has_uncle ?c)]
11
Jena2 rule elements
Each element in the head or body can be a: TriplePattern, a triple of Nodes. A Node can be:
wildcard (*), variable (?x), uri or literal Functor, in heads they represent actions, in bodies
they represent builtin predicates. A functor comprises a:
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
12
Jena2 rule Syntax
[Rule1: (?a ns:pA ?c)
-> fun(k ,?d) (?a ns:pB ?d)
]
Structure
(nodes nodep nodeo) TriplePattern
FuncName(node1, ... , nodeN )
Functor
[Name: body -> head ] Embedded rule
Name
Functor
Node
* Wildcards
?var Variable
‘lit’ Literal
http://... Uri
info:age Local Name
23 Number
TriplePattern
13
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) ]
14
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
Antecedents Consequents
forward
backward
15
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 factApplying R2 f,g are added as new factsResults (Inferred knowledge): c,f,g
16
Reasoning modes:Backward chaining Knowledge (Facts + Rules + Goals) A goal is satisfied if it belongs to facts or is inferred
by rulesSteps 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)
17
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.
18
Using a Jena2 reasoner
Fact(Raw)
Fact (Inferred)
ReasonerRule + Inference Mode
+ Inference Engine
A Reasoner allows to derive additional RDF assertions from a set of raw fact.
19
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.
20
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.
22
Example: a reasoner for the transitive rule
1
eg:A eg:p eg:Beg:B eg:p eg:C
+eg:A eg:p eg:C
eg:A eg:p eg:Beg:B eg:p eg:C
1
[Transitivity: (?a eg:p ?b) (?b eg:p ?c) -> (?a eg:p ?c)]
2
Reasoner
InferenceEngine
Forward
3 4
23
Example: a reasoner for the transitive rule (cont)1. Definition of the transitive rule: 2. forward is the reasoning mode.3. Creation of the reasoner with defined
parameters.4 Raw Fact:
5. Inferred Fact:
[Transitivity: (?a eg:p ?b) (?b eg:p ?c) -> (?a eg:p ?c)]
forward
Reasoner
eg:A eg:p eg:Beg:B eg:p eg:C
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)
24
Implementing and using a reasoner
[Rule1: (?a fatherOf ?b) (?a brotherOf ?c) -> (?c uncleOf ?b)
]
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=“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>
persons.rdf
InfModel
REASONER + DATA
FORWARDMODE
Inferred Facts
25
Implementing and using a reasoner (the Java code) // Register a namespace for use in the demoString exampleURI = "http://xyz/#";PrintUtil.registerPrefix(“example", exampleURI); // Create an (RDF) specification of a forward reasoner which // loads its data from an external file. Model m = ModelFactory.createDefaultModel(); Resource configuration = m.createResource();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); // 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);
Modificare il nome del file per utilizzare le proprie regole
Modificare il nome del file per utilizzare i propri dati
Registrare il proprio namespace se nel file di dati (.rdf) ne viene utilizzato uno
26
Reconciliation using Jena2
Reasoner
reconciliationRulesSchema A
Data A
Schema B
Data B
27
An example of reconciliation:the SchemaA and the Ontology
PersonPerson
Rdfs:LiteralRdfs:Literal
name age
HumanBeingHumanBeing
NameName
rdfs:Literalrdfs:Literal
hasName hasAge
firstName
lastName
SchemaA Ontology
28
An example of reconciliation:Semantic clashes
<rdfs:Class rdf:ID=“Person”/>
<rdf:Property rdf:ID=“name”> <rdfs:domain rdf:resource=“#Person”> <rdfs:range rdf:resource=“rdfs:Literal”></rdf:Property>
<rdf:Property rdf:ID=“age”> <rdfs:domain rdf:resource=“#Person”> <rdfs:range rdf:resource=“rdfs:Literal”></rdf:Property>
<rdfs:Class rdf:ID=“HumanBeing”/>
<rdfs:Class rdf:ID=“Name”/>
<rdf:Property rdf:ID=“firstName”> <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>
SchemaA.rdf
Ontology.rdf
Naming
Naming
Structural(split)
29
An example of reconciliation:rules from Schema to Ontology
SchemaA.name = Ontology.hasName[R2: (?a rdf:type a:Person) (?a a:name ?b) -> 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)]
SchemaA.Person = Ontology.HumanBeing[R1: (?a rdf:type a:Person) -> (?a rdf:type o:HumanBeing)]
<a:Person rdf:ID=“JS”> <a:name>John-Smith</a:name> <a:age>23</a:age></a:Person>
<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>
instanceA.rdf
a: namespace for SchemaA
o: namespace for ontologyUser defined
functors (java methods)
Ontologyinstance
Rulesapplication
30
Managing RDF with Jena2 RDF API
32
Main featuresSet 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.
33
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
static String personURI = "http://somewhere/JS"; static String fullName = "John Smith";
Model model = ModelFactory.createDefaultModel();
Resource johnSmith = model.createResource(personURI);
johnSmith.addProperty(FN, fullName);
FN
John SmithJohn Smith
Create an empty model
Create the resourceAdd the property
http://xyz/JShttp://xyz/JS
34
Jena: Reading and writing RDF Reading RDF, means instantiating an RDF
model starting from a serialization of an RDF graph read method of the Model interface
Writing RDF, means producing a serialization of an RDF model write method of the Model interface
Several serializations are allowed: RDF/XML, RDF/XML-ABBREV, N3, N-TRIPLE
35
Navigating a modelFor accessing information held in a Model Retrieving a Resource by its URI
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()
FN
John SmithJohn Smith
Fatherhttp://xyz/JShttp://xyz/JS http://xyz/MJhttp://xyz/MJ
The doc to be
inspected
36
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
37
Operations on models Three common set operations for
manipulating models as whole are supported: Union, Intersection, Difference
FN
John SmithJohn Smith
http://xyz/JShttp://xyz/JS
http://xyz/MJhttp://xyz/MJ
Father FN
John SmithJohn Smith
http://xyz/JShttp://xyz/JS
[email protected]@world.com
UNION
=
FN
John SmithJohn Smith
http://xyz/JShttp://xyz/JS
http://xyz/MJhttp://xyz/MJ
Father Email
[email protected]@world.com
Same nodes are merged and duplicate arcs are dropped
38
Managing ontology with Jena2 Ontology API
39
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
40
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
41
The generic ontology type: OntResource interface Provides a common super-type for all the
ontology constructs. Extends the RDF Resource interface
OntResourceOntResource
OntClassOntClass OntPropertyOntProperty
ResourceResource
…
Collects methods for accessing common attributes of ontology resources (i.e., label, comment, …)
42
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, …)
Eg.:Resource r = m.getResource(“http://xyz/Person" );OntClass person = (OntClass) r.as( OntClass.class ); Iterator i = person.listSubClasses();
Retrieve an existing Resource
Convert the Resource into an ontology class
Get the sub classes
43
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, …)
Eg.:ObjectProperty drive = m.createObjectProperty( “http://xyz/drive”); drive.addDomain(person); drive.addRange(car);
Create an ObjectProperty
Specify the domain and range
44
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 )
Defining class by intersection, union and complement of classes
<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>
45
RDQL processing module
46
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 Model Bindings
Query (input) ---> Model (set of triples) ---> Set of bindings (output)
47
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
SELECT ?x , ?y
WHERE (?x, <vcard:FN>, ?y)
Query
?x
?y
vcard:FN
Graph Pattern
SQL-like syntax, but only AND clauses allowed
48
Example of RDQL query
?x
?y
vcard:FN
Base graphGraph pattern
Variabili Valori
?x http://.../JohnSmith
?y “John Smith”
Bindings setBindings set:Set of variable name-value pairs for the variables used in the query.
OVERLAPPING
49
Sources
Jena2 http://jena.sourceforge.net/
Jena2 Inference support http://jena.sourceforge.net/inference/
Jena2 Java Doc http://jena.sourceforge.net/javadoc/index.html