44
Intelligent Intelligent S S pace pace 國國國國國國國國國國國國國 國國國國國國國國 國國國國國國國國國國國國國 國國國國國國國國 Jena: A Semantic Web Jena: A Semantic Web Framework for Java Framework for Java Reporter Reporter C.F.Liao ( C.F.Liao ( 廖廖廖 廖廖廖 ) May 17,2007 May 17,2007

070517 Jena

  • Upload
    yuhana

  • View
    7.890

  • Download
    3

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

Jena: A Semantic Web Jena: A Semantic Web Framework for JavaFramework for Java

ReporterReporter

C.F.Liao (C.F.Liao ( 廖峻鋒廖峻鋒 ))

May 17,2007May 17,2007

Page 2: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

2

About MeAbout Me

EducationEducation Ph.D. Candidate, CSIE ,NTU Advisor: Prof. Li-Chen Fu Research interests: Middleware for the smart living

spaces. Professional ServicesProfessional Services

SCJP / SCWCD Lecturer, SL-750, Learning Services, Sun Microsystems Taiwan, Inc. Columnist (Java EE), RUN! PC Magazine Reviewer, Core JSF CHT edition …

Page 3: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

3

OutlineOutline

IntroductionIntroduction Installing and Running JenaInstalling and Running Jena RDF Model OperationsRDF Model Operations Inference MechanismsInference Mechanisms (Optional)(Optional)

SPARQL Joseki

ConclusionConclusion

Page 4: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

4

IntroductionIntroduction

What is Jena?What is Jena? An open source semantic web framework written in

Java

Jena is composed ofJena is composed of RDF Processing API OWL Processing API A rule-based reasoning engine SPARQL query engine

Page 5: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

5

RDF and OWL Processing APIRDF and OWL Processing API

Model

RDF Files

Console

Model I/O

Model I/O

Model CRUD

OWL

OWL

OWL

Page 6: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

6

Jena Inference MechanismJena Inference Mechanism

OriginalModel

Reasoner

Rules

InferredModel

OWLOWL

OWL

ReasonerVocabulary

Page 7: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

7

SPARQL (W3C Standard)SPARQL (W3C Standard)

Model

SELECT ?x WHERE { ?x locatedIn classroom}…

?x = {Jane, Mary, and Peter}

SPARQLQuery Engine

SPARQLQuery Engine

Page 8: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

8

Review: The RDF NetworkReview: The RDF Network

LightSwitch1 state

on Literal

Resource

Bedroom

locatedIn

size

9

contains

TV1

Property

Page 9: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

9

Key AbstractionsKey Abstractions

LightSwitch1 status

on

RDFNode

Bedroom

locatedIn

Browse the Jena Javadoc

Page 10: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

10

Recommended Development EnvironmentRecommended Development Environment

JDK 1.5 +JDK 1.5 + Eclipse 3.2 +, JST (J2EE Standard Tools)Eclipse 3.2 +, JST (J2EE Standard Tools) MySQL 4.x (optional)MySQL 4.x (optional)

Page 11: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

11

Installing and Running JenaInstalling and Running Jena

1.1. Download JDK 5Download JDK 5

2.2. Download and install EclipseDownload and install Eclipse

3.3. Download JenaDownload Jena

4.4. Tuning your EclipseTuning your Eclipse

5.5. Create a java projectCreate a java project

6.6. Append Jena libraries to your classpathAppend Jena libraries to your classpath

7.7. Use Jena API to create some RDFsUse Jena API to create some RDFs

demo

Page 12: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

12

Downloading JenaDownloading Jena

http://jena.sourceforge.net/http://jena.sourceforge.net/

Page 13: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

13

Typical Jena Usage ScenarioTypical Jena Usage Scenario

RDF Consumer(Jena)

query

RDF Files

RDF Producer(Jena)

Model(Memory)

load

add

Page 14: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

14

Advanced Jena Usage ScenarioAdvanced Jena Usage Scenario

Persistent RDF Model(Relational Database)

RDF Publishing Server(Joseki)

SPARQL over HTTP

RDF Producer(Jena)

Remote RDF Consumer(Jena)

Page 15: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

15

LabLab

Creating a Simple RDF ModelCreating a Simple RDF Model

Page 16: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

16

ObjectivesObjectives

RDF ProducerModel(Memory)

addprint

Creating a simple RDF statementCreating a simple RDF statement Placing this RDF into a Placing this RDF into a ModelModel Dumping the Dumping the ModelModel to system console to system console

Page 17: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

17

Creating RDFCreating RDF

2 ways2 ways Using Jena API Load from file

Page 18: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

18

Creating a RDF statementCreating a RDF statement

// declare URI prefix// declare URI prefix String ns = "http://www.ispace.tw/smarthome#";String ns = "http://www.ispace.tw/smarthome#"; Model model = ModelFactory.Model model = ModelFactory.createDefaultModelcreateDefaultModel();();

Resource light = model.createResource(ns+"Resource light = model.createResource(ns+"light1light1");");

Property lightStatus = model.createProperty(ns+"Property lightStatus = model.createProperty(ns+"statusstatus");"); Literal statusValue = model.createLiteral("Literal statusValue = model.createLiteral("offoff");"); light.addProperty(lightStatus, statusValue);light.addProperty(lightStatus, statusValue); model.write(System.model.write(System.outout,"N-TRIPLE");,"N-TRIPLE");

light1

status

off

Model

(light,status,off)

ex1 demo

Page 19: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

19

LabLab

Model I/OModel I/O

Page 20: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

20

Model I/OModel I/O

model.read("file:./bin/advai/rdf/smarthome.nt", "N-TRIPLE");model.read("file:./bin/advai/rdf/smarthome.nt", "N-TRIPLE");

model.write(model.write(newnew FileWriter("test.nt"),"N-TRIPLE"); FileWriter("test.nt"),"N-TRIPLE");

Model

System.out

FileWriter

Page 21: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

21

Loading RDF from an External FileLoading RDF from an External File

Model model =

FileManager.get()

.loadModel(

“file:./bin/advai/rdf/smarthome.nt",

"N-TRIPLE");

src advai rdf smarthome.nt

ReadRdfFromFileTest.java

MyJenaProject bin

ReadRdfFromFileTest.class

ex2 demo

Page 22: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

22

Dumping the ModelDumping the Model

model.write(System.model.write(System.outout,"N-TRIPLE");,"N-TRIPLE");

model.write(System.model.write(System.outout,"N3");,"N3");

model.write(new FileWriter("test.nt"),“RDF/XML");model.write(new FileWriter("test.nt"),“RDF/XML");

model.write(new FileWriter("test.nt"),“model.write(new FileWriter("test.nt"),“RDF/XML-ABBREVRDF/XML-ABBREV");");

Model

System.out

FileWriter

ex3 demo

Page 23: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

23

LabLab

Model CRUDModel CRUD

Page 24: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

24

Model CRUDModel CRUD

CreateCreate RetrieveRetrieve UpdateUpdate DeleteDelete

RDF Consumer(Jena)

Model

query

Page 25: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

25

Navigating a ModelNavigating a Model

forfor(Iterator it = (Iterator it = model.listStatements()model.listStatements();it.hasNext(););it.hasNext();)

{{

Statement stmt = (Statement) it.next(); SystStatement stmt = (Statement) it.next(); System.em.outout.println(stmt.getSubject().getLocalName());.println(stmt.getSubject().getLocalName());

}}

RDF Consumer Model

query

Reification

Subject

Property

Object

ex4 demo

Page 26: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

26

Querying ModelQuerying Model

“ “ Select * from MODEL where SUBJECT=‘Jane’ ”Select * from MODEL where SUBJECT=‘Jane’ ”

Resource jane = model.getResource("http://...Jane");Resource jane = model.getResource("http://...Jane");

Selector selector = Selector selector = newnew SimpleSelector( SimpleSelector(jane, (Property)jane, (Property)nullnull, (Resource), (Resource)nullnull);); forfor(Iterator it = (Iterator it = model.listStatements(selector)model.listStatements(selector);it.hasNext();){ …};it.hasNext();){ …}

RDF Consumer Model

Reificationselector

ex5 demo

Page 27: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

27

Updating ModelUpdating Model

Using Statement’s changeObject() method:

Resource jane = model.getResource("http://www.try.idv.tw/try#Jane");Resource jane = model.getResource("http://www.try.idv.tw/try#Jane");Resource home =Resource home = model.getResource("http://www.try.idv.tw/try#Home");model.getResource("http://www.try.idv.tw/try#Home");Property locatedIn = Property locatedIn = model.getProperty("http://www.try.idv.tw/try#locatedIn");model.getProperty("http://www.try.idv.tw/try#locatedIn"); jane.getProperty(locatedIn).jane.getProperty(locatedIn).changeObjectchangeObject(home);(home);

RDF ConsumerNew object

ex6 demo

Page 28: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

28

Removing a Statement from ModelRemoving a Statement from Model

Using Statement’s remove() method:

Resource jane = model.getResource("http://www.try.idv.tw/try#Jane");Resource jane = model.getResource("http://www.try.idv.tw/try#Jane");

Property locatedIn = Property locatedIn =

model.getProperty("http://www.try.idv.tw/try#locatedIn");model.getProperty("http://www.try.idv.tw/try#locatedIn");

jane.getProperty(locatedIn).jane.getProperty(locatedIn).removeremove;;

ex7 demo

Page 29: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

29

LabLab

Basic InferenceBasic Inference

Page 30: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

30

Jena Inference MechanismJena Inference Mechanism

Data(Model)

ReasonerRegistry

RDFSReasoner

GenericReasoner

OwlReasoner

Application

ModelFactory

InfModel

Existing Model + Reasoner

New Model(InfModel)

ModelFactory

Page 31: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

31

A Simple RDFS Reasoner ExampleA Simple RDFS Reasoner Example

Property teachesAtProp = data.createProperty(ns, "teachesAt");Property teachesAtProp = data.createProperty(ns, "teachesAt");

Property worksAtProp = data.createProperty(ns, "worksAt");Property worksAtProp = data.createProperty(ns, "worksAt");

data.add(teachesAtProp, RDFS.subPropertyOf, worksAtProp);data.add(teachesAtProp, RDFS.subPropertyOf, worksAtProp);

data.createResource(ns + "jane").addProperty(teachesAtProp, "NTU");data.createResource(ns + "jane").addProperty(teachesAtProp, "NTU");

Reasoner reasoner = ReasonerRegistry.Reasoner reasoner = ReasonerRegistry.getRDFSReasoner()getRDFSReasoner();;

InfModel infModel = ModelFactory.createInfModel(reasoner, data);InfModel infModel = ModelFactory.createInfModel(reasoner, data);

(Jane, teachesAt, NTU)

RDFS Example demo

teachesAt

worksAt

(Jane, worksAt, NTU)

Page 32: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

32

LabLab

Ontological InferenceOntological Inference

Page 33: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

33

Modeling a Domain with Schema and DataModeling a Domain with Schema and Data

Schema Level

Data Level

Page 34: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

34

Separating Schema and DataSeparating Schema and Data

Data(Model)

Schema(OntModel)

ReasonerRegistry

RDFSReasoner

GenericReasoner

OwlReasoner

Application

ModelFactory

InfModel

Page 35: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

35

Example: The Smart Home OntologyExample: The Smart Home Ontology

locatedIn

contains

inversetransitive

Building

Room

Person

Light

type

type

type

Home

Bedroom

Jane

typelight1

contains

locatedIn

Page 36: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

36

Creating OntModel with Ontology APICreating OntModel with Ontology API

OntModel m = OntModel m = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM);ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM); m.createClass(ns + "m.createClass(ns + "BuildingBuilding");"); m.createClass(ns + "m.createClass(ns + "RoomRoom");"); m.createClass(ns + "m.createClass(ns + "PersonPerson");");m.createClass(ns + “m.createClass(ns + “LightLight");");

ObjectProperty ObjectProperty containscontains = = m.createObjectProperty(ns + "contains", true).convertToTransitiveProperty();m.createObjectProperty(ns + "contains", true).convertToTransitiveProperty(); ObjectProperty ObjectProperty locatedInlocatedIn = = m.createObjectProperty(ns + "locatedIn").convertToTransitiveProperty();m.createObjectProperty(ns + "locatedIn").convertToTransitiveProperty();

contains.setInverseOf(locatedIn);contains.setInverseOf(locatedIn);

ex8 demo

Create 4 Class Definitions

Inverse relationship

properties

Model Configuration

Page 37: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

37

A Ont Reasoner ExampleA Ont Reasoner Example

OntModel OntModel schemaschema = ModelFactory.createOntologyModel(…); = ModelFactory.createOntologyModel(…);

schema.read("file:./bin/advai/schema.owl");schema.read("file:./bin/advai/schema.owl");

Model Model datadata = =

FileManager.get().loadModel("file:./bin/advai/data.rdf");FileManager.get().loadModel("file:./bin/advai/data.rdf");

Reasoner Reasoner reasonerreasoner = ReasonerRegistry.getOWLReasoner(); = ReasonerRegistry.getOWLReasoner();

InfModel InfModel infModelinfModel = =

ModelFactoryModelFactory.createInfModel(reasoner, schema, data);.createInfModel(reasoner, schema, data);

ex10 demo

Page 38: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

38

LabLab

Cascading InferenceCascading Inference

Page 39: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

39

Combining Generic and OWL ReasonersCombining Generic and OWL Reasoners

Data(Model)

Schema(OntModel)

GenericReasoner

OwlReasoner

ontInfModel

ModelFactory

infModel

ModelFactory

Rules

Page 40: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

40

An “Openlight” RuleAn “Openlight” Rule

@prefix h: <http://www.ispace.tw/smarthome#> .

@include <RDFS>.

@include <OWL>.

[openlight:

(?a h:locatedIn h:bedroom)

(?a rdf:type h:Person)

(?b rdf:type h:Light)

(?b h:status 'off') -> drop(3) (?b h:status 'on')

] “if a person is in the bedroom then open all light”

Page 41: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

41

Cascading ReasonersCascading Reasoners

OntModel schema = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_TRANS_INF); schema.read("file:./bin/advai/inf/schema-inf-owl.owl");

Model data = FileManager.get().loadModel("file:./bin/advai/inf/data-inf-owl.rdf"); Reasoner owlReasoner = ReasonerRegistry.getOWLReasoner(); InfModel owlInfModel = ModelFactory.createInfModel(owlReasoner, schema, data);

GenericRuleReasoner reasoner = new GenericRuleReasoner(Rule.rulesFromURL("file:./bin/advai/inf/myrule.rule")); reasoner.setDerivationLogging(true); InfModel infModel = ModelFactory.createInfModel(reasoner, owlInfModel);

ex11 demo

Page 42: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

42

SummarySummary

What we have learnedWhat we have learned RDF / Model CRUD OWL OWL inference OWL + Generic Rule engine inference

Further readingsFurther readings Jena Javadocs Be sure to understand the meaning of advanced confi

guration mechanisms of models and reasoners.

Page 43: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

43

Querying Model with SPARQLQuerying Model with SPARQL

Query query = …(Query String);QueryExecution qexec = QueryExecutionFactory.create(query, model);

try { ResultSet results = qexec.execSelect(); for (; results.hasNext();) { QuerySolution soln = results.nextSolution(); …// the solutions may be Resources or Literals } } finally { qexec.close(); } Sparql Demo

Page 44: 070517 Jena

IntelligentIntelligent SSpacepace國立台灣大學資訊工程研究所 智慧型空間實驗室國立台灣大學資訊工程研究所 智慧型空間實驗室

44

Persisting RDF ModelPersisting RDF Model

Download required bundlesDownload required bundles MySQL DB MySQL JDBC Driver

Install MySQL databaseInstall MySQL database Create jenadbCreate jenadb

create database jenatest character set utf8 ;

Persist with ModelMakerPersist with ModelMaker