Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 Jena: A Semantic Web Framework for Java Reporter C.F.Liao ( 廖峻鋒 ) May 17,2007.

Similar presentations


Presentation on theme: "Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 Jena: A Semantic Web Framework for Java Reporter C.F.Liao ( 廖峻鋒 ) May 17,2007."— Presentation transcript:

1 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 Jena: A Semantic Web Framework for Java Reporter C.F.Liao ( 廖峻鋒 ) May 17,2007

2 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 2 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 …

3 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 3 Outline  Introduction  Installing and Running Jena  RDF Model Operations  Inference Mechanisms  (Optional) SPARQL Joseki  Conclusion

4 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 4 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

5 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 5 RDF and OWL Processing API Model RDF Files Console Model I/O Model CRUD OWL

6 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 6 Jena Inference Mechanism Original Model Reasoner Rules Inferred Model OWL ReasonerVocabulary

7 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 7 SPARQL (W3C Standard) Model SELECT ?x WHERE { ?x locatedIn classroom} … ?x = {Jane, Mary, and Peter} SPARQL Query Engine SPARQL Query Engine

8 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 8 Review: The RDF Network Light Switch1 state on Literal Resource Bedroom locatedIn size 9 contains TV1 Property

9 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 9 Key Abstractions Light Switch1 status on RDFNode Bedroom locatedIn Browse the Jena Javadoc

10 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 10 Recommended Development Environment  JDK 1.5 +  Eclipse 3.2 +, JST (J2EE Standard Tools)  MySQL 4.x (optional)

11 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 11 Installing and Running Jena 1. Download JDK 5 2. Download and install Eclipse 3. Download Jena 4. Tuning your Eclipse 5. Create a java project 6. Append Jena libraries to your classpath 7. Use Jena API to create some RDFs demo

12 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 12 Downloading Jena  http://jena.sourceforge.net/

13 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 13 Typical Jena Usage Scenario RDF Consumer (Jena) query RDF Files RDF Producer (Jena) Model (Memory) load add

14 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 14 Advanced Jena Usage Scenario Persistent RDF Model (Relational Database) RDF Publishing Server (Joseki) SPARQL over HTTP RDF Producer (Jena) Remote RDF Consumer (Jena)

15 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 15 Lab Creating a Simple RDF Model

16 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 16 Objectives RDF Producer Model (Memory) addprint  Creating a simple RDF statement  Placing this RDF into a Model  Dumping the Model to system console

17 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 17 Creating RDF  2 ways Using Jena API Load from file

18 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 18 Creating 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.createDefaultModel(); Model model = ModelFactory.createDefaultModel(); Resource light = model.createResource(ns+"light1"); Resource light = model.createResource(ns+"light1"); Property lightStatus = model.createProperty(ns+"status"); Property lightStatus = model.createProperty(ns+"status"); Literal statusValue = model.createLiteral("off"); Literal statusValue = model.createLiteral("off"); light.addProperty(lightStatus, statusValue); light.addProperty(lightStatus, statusValue); model.write(System.out,"N-TRIPLE"); model.write(System.out,"N-TRIPLE"); light1 status off Model (light,status,off) ex1 demo

19 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 19 Lab Model I/O

20 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 20 Model I/O model.read("file:./bin/advai/rdf/smarthome.nt", "N-TRIPLE"); model.write(new FileWriter("test.nt"),"N-TRIPLE"); Model System.out FileWriter

21 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 21 Loading 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

22 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 22 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"); Model System.out FileWriter ex3 demo

23 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 23 Lab Model CRUD

24 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 24 Model CRUD  Create  Retrieve  Update  Delete RDF Consumer (Jena) Model query

25 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 25 Navigating a Model for(Iterator it = model.listStatements();it.hasNext();) { Statement stmt = (Statement) it.next(); System.out.println(stmt.getSubject().getLocalName()); Statement stmt = (Statement) it.next(); System.out.println(stmt.getSubject().getLocalName());} RDF Consumer Model query Reification Subject Property Object ex4 demo

26 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 26 Querying Model “ Select * from MODEL where SUBJECT=‘Jane’ ” Resource jane = model.getResource("http://...Jane"); Selector selector = new SimpleSelector(jane, (Property)null, (Resource)null); new SimpleSelector(jane, (Property)null, (Resource)null); for(Iterator it = model.listStatements(selector);it.hasNext();){ …} RDF Consumer Model Reification selector ex5 demo

27 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 27 Updating Model 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"); model.getResource("http://www.try.idv.tw/try#Home"); Property locatedIn = model.getProperty("http://www.try.idv.tw/try#locatedIn"); model.getProperty("http://www.try.idv.tw/try#locatedIn"); jane.getProperty(locatedIn).changeObject(home); RDF Consumer New object ex6 demo

28 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 28 Removing a Statement from Model 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"); model.getProperty("http://www.try.idv.tw/try#locatedIn"); jane.getProperty(locatedIn).remove; ex7 demo

29 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 29 Lab Basic Inference

30 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 30 Jena Inference Mechanism Data (Model) ReasonerRegistry RDFSReasoner GenericReasoner OwlReasoner Application ModelFactory InfModel Existing Model + Reasoner New Model (InfModel) ModelFactory

31 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 31 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) RDFS Example demo teachesAt worksAt (Jane, worksAt, NTU)

32 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 32 Lab Ontological Inference

33 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 33 Modeling a Domain with Schema and Data Schema Level Data Level

34 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 34 Separating Schema and Data Data (Model) Schema (OntModel) ReasonerRegistry RDFSReasoner GenericReasoner OwlReasoner Application ModelFactory InfModel

35 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 35 Example: The Smart Home Ontology locatedIn contains inverse transitive Building Room Person Light type Home Bedroom Jane type light1 contains locatedIn

36 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 36 Creating OntModel with Ontology API OntModel m = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM); ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM); m.createClass(ns + "Building"); m.createClass(ns + "Building"); m.createClass(ns + "Room"); m.createClass(ns + "Room"); m.createClass(ns + "Person"); m.createClass(ns + "Person"); m.createClass(ns + “Light"); ObjectProperty contains = ObjectProperty contains = m.createObjectProperty(ns + "contains", true).convertToTransitiveProperty(); m.createObjectProperty(ns + "contains", true).convertToTransitiveProperty(); ObjectProperty locatedIn = ObjectProperty locatedIn = 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

37 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 37 A Ont Reasoner Example OntModel schema = ModelFactory.createOntologyModel(…); OntModel schema = ModelFactory.createOntologyModel(…); schema.read("file:./bin/advai/schema.owl"); schema.read("file:./bin/advai/schema.owl"); Model data = Model data = FileManager.get().loadModel("file:./bin/advai/data.rdf"); FileManager.get().loadModel("file:./bin/advai/data.rdf"); Reasoner reasoner = ReasonerRegistry.getOWLReasoner(); Reasoner reasoner = ReasonerRegistry.getOWLReasoner(); InfModel infModel = InfModel infModel = ModelFactory.createInfModel(reasoner, schema, data); ModelFactory.createInfModel(reasoner, schema, data); ex10 demo

38 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 38 Lab Cascading Inference

39 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 39 Combining Generic and OWL Reasoners Data (Model) Schema (OntModel) GenericReasoner OwlReasoner ontInfModel ModelFactory infModel ModelFactory Rules

40 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 40 An “Openlight” Rule @prefix h:. @include. [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”

41 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 41 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

42 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 42 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.

43 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 43 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

44 Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 44 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


Download ppt "Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 Jena: A Semantic Web Framework for Java Reporter C.F.Liao ( 廖峻鋒 ) May 17,2007."

Similar presentations


Ads by Google