Download presentation
Presentation is loading. Please wait.
1
Jena2: a semantic web toolkit
Una piattaforma inferenziale per il Web Semantico: Jena2 Roma, 2006 Web Semantico Jena2: a semantic web toolkit M. Missikoff – F. Taglino LEKS, IASI-CNR
2
Summary Introduction Jena 2 Inference Support Managing RDF with Jena2
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 ( 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
uses Ontology API Manipulating of Ontologies Inference API Inferencing on ontologies (reasoner engines) RDQL Querying RDF graphs RDF API Manipulating RDF graphs (set of triples)
6
Jena2 Architecture 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
Jena2 Inference Support
Inference Engine Configuration Rules Spec Rules Reasoning Mode: Forward Backward Hybrid Inference Engine General Rule 1 1 2 Inference Execution Raw Facts Model Reasoner Inferred Facts InfModel 3 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 Name: The rule specification comprises:
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)]
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 * Structure (nodes nodep nodeo) TriplePattern
FuncName(node1, ... , nodeN ) Functor [Name: body -> head ] Embedded rule Node * Wildcards ?var Variable ‘lit’ Literal Uri info:age Local Name 23 Number [Rule1: (?a ns:pA ?c) -> fun(k ,?d) (?a ns:pB ?d) ] Name Triple Pattern Functor
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 forward Antecedents Consequents 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 fact Applying R2 f,g are added as new facts Results (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 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)
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 Reasoner Fact (Raw)
Rule + Inference Mode + Inference Engine A Reasoner allows to derive additional RDF assertions from a set of raw fact. Fact (Inferred)
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.
21
Available reasoners Transitive reasoner : implements the transitive and symmetric properties of rdfs:subPropertyOf and rdfs:subClassOf. RDFS rule reasoner: Implements a configurable subset of the RDFS entailments. OWL reasoners: A set of useful but incomplete implementation of the OWL sub languages. DAML micro reasoner: for compatibility with Jena1. Generic rule reasoner: support user defined rules Can work in Forward, Backward and Hybrid modes
22
Example: a reasoner for the transitive rule
[Transitivity: (?a eg:p ?b) (?b eg:p ?c) -> (?a eg:p ?c)] 1 Inference Engine Forward 1 2 eg:A eg:p eg:B eg:B eg:p eg:C + eg:A eg:p eg:C eg:A eg:p eg:B eg:B eg:p eg:C Reasoner 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:B eg:B 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) eg:A eg:p eg:C
24
Implementing and using a reasoner
<Person rdf:ID=“John”> <fatherOf rdf:resource=“Mike”/> <fatherOf rdf:resource=“Tom”/> </Person> <Person rdf:about=“John”> <brotherOf rdf:resource=“Harry”/> <Person rdf:ID=“Harry”> <uncleOf rdf:resource=“Mike”/> <uncleOf rdf:resource=“Tom”/> FORWARD MODE [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”/> Inferred Facts InfModel persons.rdf REASONER + DATA
25
Implementing and using a reasoner (the Java code)
Registrare il proprio namespace se nel file di dati (.rdf) ne viene utilizzato uno // Register a namespace for use in the demo String exampleURI = " 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
26
Reconciliation using Jena2
Rules Schema A Schema B Data A Data B Reasoner
27
An example of reconciliation: the SchemaA and the Ontology
Person HumanBeing hasName hasAge name age Name firstName rdfs:Literal Rdfs:Literal lastName
28
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> <rdf:Property rdf:ID=“age”> <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”> <rdf:Property rdf:ID=“hasName”> <rdfs:domain rdf:resource=“#HumanBeing”> <rdfs:range rdf:resource=“#Name”> <rdf:Property rdf:ID=“hasAge”> Structural (split) SchemaA.rdf Naming Ontology.rdf
29
An example of reconciliation: rules from Schema to Ontology
a: namespace for SchemaA SchemaA.Person = Ontology.HumanBeing [R1: (?a rdf:type a:Person) -> (?a rdf:type o:HumanBeing)] o: namespace for ontology User defined functors (java methods) 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)] <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> Rules application instanceA.rdf Ontology instance
30
Managing RDF with Jena2 RDF API
31
RDF (quick recap) RDF is a standard (W3C recommendation) for describing resources A resource is anything that can be identified (i.e., the person John Smith) Resources are identified by an URI Resources can have properties Properties are resources and then identified by URIs Properties have values Literals or Resources
32
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.
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 Create an empty model static String personURI = " static String fullName = "John Smith"; Model model = ModelFactory.createDefaultModel(); Resource johnSmith = model.createResource(personURI); johnSmith.addProperty(FN, fullName); FN John Smith Create the resource Add the property
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 model For accessing information held in a Model
The doc to be inspected For accessing information held in a Model Retrieving a Resource by its URI Resource johnSmith = model.getResource(“ Accessing properties (and value) of a Resource Resource father = johnsmith.getProperty(Father).getResource() String fullname = johnsmith.getProperty(FN).getString() Father FN John Smith
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 UNION FN Father FN John Smith John Smith = Same nodes are merged and duplicate arcs are dropped FN Father John Smith
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 Collects methods for accessing common attributes of ontology resources (i.e., label, comment, …) Resource OntResource OntClass OntProperty …
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.: Retrieve an existing Resource Resource r = m.getResource(“ ); OntClass person = (OntClass) r.as( OntClass.class ); Iterator i = person.listSubClasses(); 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.: Create an ObjectProperty ObjectProperty drive = m.createObjectProperty( “ drive.addDomain(person); drive.addRange(car); 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 (input) > Model (set of triples) ---> Set of bindings (output) Query Model Bindings
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 Graph Pattern Query ?x ?y vcard:FN SELECT ?x , ?y WHERE (?x, <vcard:FN>, ?y) SQL-like syntax, but only AND clauses allowed
48
Example of RDQL query Base graph Graph pattern ?x ?y Bindings set
vcard:FN OVERLAPPING Bindings set Bindings set: Set of variable name-value pairs for the variables used in the query. Variabili Valori ?x ?y “John Smith”
49
Sources Jena2 Jena2 Inference support Jena2 Java Doc
Jena2 Inference support Jena2 Java Doc
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.