Jena a semantic web framework for java
This presentation is the property of its rightful owner.
Sponsored Links
1 / 44

Jena: A Semantic Web Framework for Java PowerPoint PPT Presentation


  • 85 Views
  • Uploaded on
  • Presentation posted in: General

Jena: A Semantic Web Framework for Java. Reporter C.F.Liao ( 廖峻鋒 ) May 17,2007. About Me. Education Ph.D. Candidate, CSIE ,NTU Advisor: Prof. Li-Chen Fu Research interests: Middleware for the smart living spaces. Professional Services SCJP / SCWCD

Download Presentation

Jena: A Semantic Web Framework for Java

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Jena a semantic web framework for java

Jena: A Semantic Web Framework for Java

Reporter

C.F.Liao (廖峻鋒)

May 17,2007


About me

About Me

  • Education

    • Ph.D. Candidate, CSIE ,NTU

    • Advisor: Prof. Li-Chen Fu

    • Research interests: Middleware for the smart living spaces.

  • Professional Services

    • SCJP / SCWCD

    • Lecturer, SL-750, Learning Services, Sun Microsystems Taiwan, Inc.

    • Columnist (Java EE), RUN! PC Magazine

    • Reviewer, Core JSF CHT edition


Outline

Outline

  • Introduction

  • Installing and Running Jena

  • RDF Model Operations

  • Inference Mechanisms

  • (Optional)

    • SPARQL

    • Joseki

  • Conclusion


Introduction

Introduction

  • What is Jena?

    • An open source semantic web framework written in Java

  • Jena is composed of

    • RDF Processing API

    • OWL Processing API

    • A rule-based reasoning engine

    • SPARQL query engine


Rdf and owl processing api

RDF and OWL Processing API

OWL

Model I/O

Console

OWL

Model CRUD

Model

Model I/O

OWL

RDF Files


Jena inference mechanism

Rules

Original

Model

Inferred

Model

Reasoner

Jena Inference Mechanism

OWL

OWL

OWL

ReasonerVocabulary


Sparql w3c standard

SPARQL (W3C Standard)

SELECT ?x WHERE { ?x locatedIn classroom}

SPARQL

Query Engine

Model

?x = {Jane, Mary, and Peter}


Review the rdf network

Property

Literal

Resource

locatedIn

TV1

Bedroom

contains

size

9

Review: The RDF Network

Light

Switch1

on

state


Key abstractions

RDFNode

Key Abstractions

Light

Switch1

on

status

locatedIn

Bedroom

Browse the Jena Javadoc


Recommended development environment

Recommended Development Environment

  • JDK 1.5 +

  • Eclipse 3.2 +, JST (J2EE Standard Tools)

  • MySQL 4.x (optional)


Installing and running jena

Installing and Running Jena

demo

  • Download JDK 5

  • Download and install Eclipse

  • Download Jena

  • Tuning your Eclipse

  • Create a java project

  • Append Jena libraries to your classpath

  • Use Jena API to create some RDFs


Downloading jena

Downloading Jena

  • http://jena.sourceforge.net/


Typical jena usage scenario

add

query

RDF Producer

(Jena)

Model

(Memory)

RDF Consumer

(Jena)

load

RDF Files

Typical Jena Usage Scenario


Advanced jena usage scenario

Advanced Jena Usage Scenario

RDF Publishing Server

(Joseki)

SPARQL over HTTP

RDF Producer

(Jena)

Persistent RDF Model

(Relational Database)

Remote RDF Consumer

(Jena)


Lab creating a simple rdf model

LabCreating a Simple RDF Model


Objectives

Model

(Memory)

Objectives

  • Creating a simple RDF statement

  • Placing this RDF into a Model

  • Dumping the Model to system console

print

add

RDF Producer


Creating rdf

Creating RDF

  • 2 ways

    • Using Jena API

    • Load from file


Creating a rdf statement

Model

light1

off

status

Creating a RDF statement

ex1 demo

// declare URI prefix

String ns = "http://www.ispace.tw/smarthome#";

Model model = ModelFactory.createDefaultModel();

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

Property lightStatus = model.createProperty(ns+"status");

Literal statusValue = model.createLiteral("off");

light.addProperty(lightStatus, statusValue);

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

(light,status,off)


Lab model i o

LabModel I/O


Model i o

Model

Model I/O

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

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

FileWriter

System.out


Loading rdf from an external file

bin

ReadRdfFromFileTest.class

Loading RDF from an External File

ex2 demo

Model model =

FileManager.get()

.loadModel(

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

"N-TRIPLE");

src

advai

rdf

MyJenaProject

smarthome.nt

ReadRdfFromFileTest.java


Dumping the model

Model

Dumping the Model

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

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

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

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

ex3 demo

FileWriter

System.out


Lab model crud

LabModel CRUD


Model crud

Model CRUD

  • Create

  • Retrieve

  • Update

  • Delete

query

Model

RDF Consumer

(Jena)


Navigating a model

query

Subject

Property

Object

Navigating a Model

ex4 demo

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

{

Statement stmt = (Statement) it.next(); System.out.println(stmt.getSubject().getLocalName());

}

Reification

RDF Consumer

Model


Querying model

Querying Model

ex5 demo

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

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

Selector selector =

new SimpleSelector(jane, (Property)null, (Resource)null);

for(Iterator it = model.listStatements(selector);it.hasNext();){ …}

Reification

selector

RDF Consumer

Model


Updating model

New object

Updating Model

ex6 demo

Using Statement’s changeObject() method:

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

Resource home =

model.getResource("http://www.try.idv.tw/try#Home");

Property locatedIn =

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

jane.getProperty(locatedIn).changeObject(home);

RDF Consumer


Removing a statement from model

Removing a Statement from Model

ex7 demo

Using Statement’s remove() method:

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

Property locatedIn =

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

jane.getProperty(locatedIn).remove;


Lab basic inference

LabBasic Inference


Jena inference mechanism1

GenericReasoner

Data

(Model)

OwlReasoner

InfModel

ModelFactory

Jena Inference Mechanism

ReasonerRegistry

RDFSReasoner

Existing Model

+ Reasoner

ModelFactory

New Model

(InfModel)

Application


A simple rdfs reasoner example

worksAt

teachesAt

A Simple RDFS Reasoner Example

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

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

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

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

Reasoner reasoner = ReasonerRegistry.getRDFSReasoner();

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

(Jane, teachesAt, NTU)

(Jane, worksAt, NTU)

RDFS Example demo


Lab ontological inference

LabOntological Inference


Modeling a domain with schema and data

Modeling a Domain with Schema and Data

Schema Level

Data Level


Separating schema and data

Data

(Model)

OwlReasoner

Schema

(OntModel)

InfModel

ModelFactory

Separating Schema and Data

ReasonerRegistry

GenericReasoner

RDFSReasoner

Application


Example the smart home ontology

type

locatedIn

type

locatedIn

transitive

inverse

contains

type

contains

type

Bedroom

Building

Person

Room

Home

light1

Jane

Light

Example: The Smart Home Ontology


Creating ontmodel with ontology api

Model Configuration

Create 4 Class Definitions

properties

Creating OntModel with Ontology API

ex8 demo

OntModel m =

ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM);

m.createClass(ns + "Building");

m.createClass(ns + "Room");

m.createClass(ns + "Person");

m.createClass(ns + “Light");

ObjectProperty contains =

m.createObjectProperty(ns + "contains", true).convertToTransitiveProperty();

ObjectProperty locatedIn =

m.createObjectProperty(ns + "locatedIn").convertToTransitiveProperty();

contains.setInverseOf(locatedIn);

Inverse relationship


A ont reasoner example

A Ont Reasoner Example

ex10 demo

OntModel schema = ModelFactory.createOntologyModel(…);

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

Model data =

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

Reasoner reasoner = ReasonerRegistry.getOWLReasoner();

InfModel infModel =

ModelFactory.createInfModel(reasoner, schema, data);


Lab cascading inference

LabCascading Inference


Combining generic and owl reasoners

Data

(Model)

OwlReasoner

ontInfModel

Schema

(OntModel)

infModel

ModelFactory

ModelFactory

Combining Generic and OWL Reasoners

Rules

GenericReasoner


An openlight rule

An “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”


Cascading reasoners

Cascading 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


Summary

Summary

  • What we have learned

    • RDF / Model CRUD

    • OWL

    • OWL inference

    • OWL + Generic Rule engine inference

  • Further readings

    • Jena Javadocs

    • Be sure to understand the meaning of advanced configuration mechanisms of models and reasoners.


Querying model with sparql

Querying 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


Persisting rdf model

Persisting RDF Model

  • Download required bundles

    • MySQL DB

    • MySQL JDBC Driver

  • Install MySQL database

  • Create jenadb

    • create database jenatest character set utf8 ;

  • Persist with ModelMaker


  • Login