070517 Jena

6,768 views

Published on

Published in: Technology, Education

070517 Jena

  1. 1. Jena: A Semantic Web Framework for Java Reporter C.F.Liao ( 廖峻鋒 ) May 17,2007
  2. 2. About Me <ul><li>Education </li></ul><ul><ul><li>Ph.D. Candidate, CSIE ,NTU </li></ul></ul><ul><ul><li>Advisor: Prof. Li-Chen Fu </li></ul></ul><ul><ul><li>Research interests: Middleware for the smart living spaces. </li></ul></ul><ul><li>Professional Services </li></ul><ul><ul><li>SCJP / SCWCD </li></ul></ul><ul><ul><li>Lecturer, SL-750, Learning Services, Sun Microsystems Taiwan, Inc. </li></ul></ul><ul><ul><li>Columnist (Java EE), RUN! PC Magazine </li></ul></ul><ul><ul><li>Reviewer, Core JSF CHT edition </li></ul></ul><ul><ul><li>… </li></ul></ul>
  3. 3. Outline <ul><li>Introduction </li></ul><ul><li>Installing and Running Jena </li></ul><ul><li>RDF Model Operations </li></ul><ul><li>Inference Mechanisms </li></ul><ul><li>(Optional) </li></ul><ul><ul><li>SPARQL </li></ul></ul><ul><ul><li>Joseki </li></ul></ul><ul><li>Conclusion </li></ul>
  4. 4. Introduction <ul><li>What is Jena? </li></ul><ul><ul><li>An open source semantic web framework written in Java </li></ul></ul><ul><li>Jena is composed of </li></ul><ul><ul><li>RDF Processing API </li></ul></ul><ul><ul><li>OWL Processing API </li></ul></ul><ul><ul><li>A rule-based reasoning engine </li></ul></ul><ul><ul><li>SPARQL query engine </li></ul></ul>
  5. 5. RDF and OWL Processing API Model RDF Files Console Model I/O Model I/O Model CRUD OWL OWL OWL
  6. 6. Jena Inference Mechanism OWL OWL OWL ReasonerVocabulary Original Model Reasoner Rules Inferred Model
  7. 7. SPARQL (W3C Standard) Model SELECT ?x WHERE { ?x locatedIn classroom} … ?x = {Jane, Mary, and Peter} SPARQL Query Engine
  8. 8. Review: The RDF Network Light Switch1 state on Literal Resource Bedroom locatedIn size 9 contains TV1 Property
  9. 9. Key Abstractions Light Switch1 status on Bedroom locatedIn Browse the Jena Javadoc RDFNode
  10. 10. Recommended Development Environment <ul><li>JDK 1.5 + </li></ul><ul><li>Eclipse 3.2 +, JST (J2EE Standard Tools) </li></ul><ul><li>MySQL 4.x (optional) </li></ul>
  11. 11. Installing and Running Jena <ul><li>Download JDK 5 </li></ul><ul><li>Download and install Eclipse </li></ul><ul><li>Download Jena </li></ul><ul><li>Tuning your Eclipse </li></ul><ul><li>Create a java project </li></ul><ul><li>Append Jena libraries to your classpath </li></ul><ul><li>Use Jena API to create some RDFs </li></ul>demo
  12. 12. Downloading Jena <ul><li>http://jena.sourceforge.net/ </li></ul>
  13. 13. Typical Jena Usage Scenario RDF Consumer (Jena) query RDF Files RDF Producer (Jena) Model (Memory) load add
  14. 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. 15. Lab Creating a Simple RDF Model
  16. 16. Objectives <ul><li>Creating a simple RDF statement </li></ul><ul><li>Placing this RDF into a Model </li></ul><ul><li>Dumping the Model to system console </li></ul>RDF Producer add print Model (Memory)
  17. 17. Creating RDF <ul><li>2 ways </li></ul><ul><ul><li>Using Jena API </li></ul></ul><ul><ul><li>Load from file </li></ul></ul>
  18. 18. Creating a RDF statement <ul><li>// declare URI prefix </li></ul><ul><li>String ns = &quot;http://www.ispace.tw/smarthome#&quot;; </li></ul><ul><li>Model model = ModelFactory. createDefaultModel (); </li></ul><ul><li>Resource light = model.createResource(ns+&quot; light1 &quot;); </li></ul><ul><li>Property lightStatus = model.createProperty(ns+&quot; status &quot;); </li></ul><ul><li>Literal statusValue = model.createLiteral(&quot; off &quot;); </li></ul><ul><li>light.addProperty(lightStatus, statusValue); </li></ul><ul><li>model.write(System. out ,&quot;N-TRIPLE&quot;); </li></ul>(light,status,off) ex1 demo light1 status off Model
  19. 19. Lab Model I/O
  20. 20. Model I/O <ul><li>model.read(&quot;file:./bin/advai/rdf/smarthome.nt&quot;, &quot;N-TRIPLE&quot;); </li></ul><ul><li>model.write( new FileWriter(&quot;test.nt&quot;),&quot;N-TRIPLE&quot;); </li></ul>System.out FileWriter Model
  21. 21. Loading RDF from an External File <ul><li>Model model = </li></ul><ul><li>FileManager . get () </li></ul><ul><li>. loadModel ( </li></ul><ul><li>“ file: ./bin/advai/rdf/smarthome.nt &quot;, </li></ul><ul><li>&quot; N-TRIPLE &quot;); </li></ul>src advai rdf smarthome.nt ReadRdfFromFileTest.java MyJenaProject ex2 demo bin ReadRdfFromFileTest.class
  22. 22. Dumping the Model <ul><li>model.write(System. out ,&quot;N-TRIPLE&quot;); </li></ul><ul><li>model.write(System. out ,&quot;N3&quot;); </li></ul><ul><li>model.write(new FileWriter(&quot;test.nt&quot;),“RDF/XML&quot;); </li></ul><ul><li>model.write(new FileWriter(&quot;test.nt&quot;),“RDF/XML-ABBREV&quot;); </li></ul>System.out FileWriter ex3 demo Model
  23. 23. Lab Model CRUD
  24. 24. Model CRUD <ul><li>Create </li></ul><ul><li>Retrieve </li></ul><ul><li>Update </li></ul><ul><li>Delete </li></ul>RDF Consumer (Jena) Model query
  25. 25. Navigating a Model <ul><li>for (Iterator it = model.listStatements() ;it.hasNext();) </li></ul><ul><li>{ </li></ul><ul><li>Statement stmt = (Statement) it.next(); System. out .println(stmt.getSubject().getLocalName()); </li></ul><ul><li>} </li></ul>RDF Consumer Model Reification ex4 demo query Subject Property Object
  26. 26. Querying Model <ul><li>“ Select * from MODEL where SUBJECT=‘Jane’ ” </li></ul><ul><li>Resource jane = model.getResource(&quot;http://...Jane&quot;); </li></ul><ul><li>Selector selector = </li></ul><ul><li>new SimpleSelector( jane, (Property) null , (Resource) null ); </li></ul><ul><li>for (Iterator it = model.listStatements(selector) ;it.hasNext();){ …} </li></ul>RDF Consumer Model Reification selector ex5 demo
  27. 27. Updating Model <ul><li>Using Statement’s changeObject() method: </li></ul><ul><li>Resource jane = model.getResource(&quot;http://www.try.idv.tw/try#Jane&quot;); </li></ul><ul><li>Resource home = </li></ul><ul><li>model.getResource(&quot;http://www.try.idv.tw/try#Home&quot;); </li></ul><ul><li>Property locatedIn = </li></ul><ul><li>model.getProperty(&quot;http://www.try.idv.tw/try#locatedIn&quot;); </li></ul><ul><li>jane.getProperty(locatedIn). changeObject (home); </li></ul>RDF Consumer ex6 demo New object
  28. 28. Removing a Statement from Model <ul><li>Using Statement’s remove() method: </li></ul><ul><li>Resource jane = model.getResource(&quot;http://www.try.idv.tw/try#Jane&quot;); </li></ul><ul><li>Property locatedIn = </li></ul><ul><li>model.getProperty(&quot;http://www.try.idv.tw/try#locatedIn&quot;); </li></ul><ul><li>jane.getProperty(locatedIn). remove ; </li></ul>ex7 demo
  29. 29. Lab Basic Inference
  30. 30. Jena Inference Mechanism ReasonerRegistry RDFSReasoner Application Existing Model + Reasoner New Model (InfModel) ModelFactory Data (Model) GenericReasoner OwlReasoner ModelFactory InfModel
  31. 31. A Simple RDFS Reasoner Example <ul><li>Property teachesAtProp = data.createProperty(ns, &quot;teachesAt&quot;); </li></ul><ul><li>Property worksAtProp = data.createProperty(ns, &quot;worksAt&quot;); </li></ul><ul><li>data.add(teachesAtProp, RDFS.subPropertyOf, worksAtProp); </li></ul><ul><li>data.createResource(ns + &quot;jane&quot;).addProperty(teachesAtProp, &quot;NTU&quot;); </li></ul><ul><li>Reasoner reasoner = ReasonerRegistry. getRDFSReasoner() ; </li></ul><ul><li>InfModel infModel = ModelFactory.createInfModel(reasoner, data); </li></ul>(Jane, teachesAt, NTU) RDFS Example demo (Jane, worksAt, NTU) teachesAt worksAt
  32. 32. Lab Ontological Inference
  33. 33. Modeling a Domain with Schema and Data Schema Level Data Level
  34. 34. Separating Schema and Data ReasonerRegistry RDFSReasoner GenericReasoner Application Data (Model) Schema (OntModel) OwlReasoner ModelFactory InfModel
  35. 35. Example: The Smart Home Ontology locatedIn contains inverse transitive Building Room Person Light type type type Home Bedroom Jane type light1 contains locatedIn
  36. 36. Creating OntModel with Ontology API <ul><li>OntModel m = </li></ul><ul><li>ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM); </li></ul><ul><li>m.createClass(ns + &quot; Building &quot;); </li></ul><ul><li>m.createClass(ns + &quot; Room &quot;); </li></ul><ul><li>m.createClass(ns + &quot; Person &quot;); </li></ul><ul><li>m.createClass(ns + “ Light &quot;); </li></ul><ul><li>ObjectProperty contains = </li></ul><ul><li>m.createObjectProperty(ns + &quot;contains&quot;, true).convertToTransitiveProperty(); </li></ul><ul><li>ObjectProperty locatedIn = </li></ul><ul><li>m.createObjectProperty(ns + &quot;locatedIn&quot;).convertToTransitiveProperty(); </li></ul><ul><li>contains.setInverseOf(locatedIn); </li></ul>ex8 demo Inverse relationship Create 4 Class Definitions properties Model Configuration
  37. 37. A Ont Reasoner Example <ul><li>OntModel schema = ModelFactory.createOntologyModel(…); </li></ul><ul><li>schema.read(&quot;file:./bin/advai/schema.owl&quot;); </li></ul><ul><li>Model data = </li></ul><ul><li>FileManager.get().loadModel(&quot;file:./bin/advai/data.rdf&quot;); </li></ul><ul><li>Reasoner reasoner = ReasonerRegistry.getOWLReasoner(); </li></ul><ul><li>InfModel infModel = </li></ul><ul><li>ModelFactory .createInfModel(reasoner, schema, data); </li></ul>ex10 demo
  38. 38. Lab Cascading Inference
  39. 39. Combining Generic and OWL Reasoners GenericReasoner Rules Data (Model) Schema (OntModel) OwlReasoner ontInfModel ModelFactory infModel ModelFactory
  40. 40. An “Openlight” Rule <ul><li>@prefix h: <http://www.ispace.tw/smarthome#> . </li></ul><ul><li>@include <RDFS>. </li></ul><ul><li>@include <OWL>. </li></ul><ul><li>[openlight: </li></ul><ul><li>(?a h:locatedIn h:bedroom) </li></ul><ul><li>(?a rdf:type h:Person) </li></ul><ul><li>(?b rdf:type h:Light) </li></ul><ul><li>(?b h:status 'off') -> drop(3) (?b h:status 'on') </li></ul><ul><li>] </li></ul>“ if a person is in the bedroom then open all light”
  41. 41. Cascading Reasoners <ul><li>OntModel schema = </li></ul><ul><li>ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_TRANS_INF); </li></ul><ul><li>schema.read(&quot;file:./bin/advai/inf/schema-inf-owl.owl&quot;); </li></ul><ul><li>Model data = FileManager.get().loadModel(&quot;file:./bin/advai/inf/data-inf-owl.rdf&quot;); </li></ul><ul><li>Reasoner owlReasoner = ReasonerRegistry.getOWLReasoner(); </li></ul><ul><li>InfModel owlInfModel = ModelFactory.createInfModel(owlReasoner, schema, data); </li></ul><ul><li>GenericRuleReasoner reasoner = </li></ul><ul><li>new GenericRuleReasoner(Rule.rulesFromURL(&quot;file:./bin/advai/inf/myrule.rule&quot;)); </li></ul><ul><li>reasoner.setDerivationLogging(true); </li></ul><ul><li>InfModel infModel = ModelFactory.createInfModel(reasoner, owlInfModel); </li></ul>ex11 demo
  42. 42. Summary <ul><li>What we have learned </li></ul><ul><ul><li>RDF / Model CRUD </li></ul></ul><ul><ul><li>OWL </li></ul></ul><ul><ul><li>OWL inference </li></ul></ul><ul><ul><li>OWL + Generic Rule engine inference </li></ul></ul><ul><li>Further readings </li></ul><ul><ul><li>Jena Javadocs </li></ul></ul><ul><ul><li>Be sure to understand the meaning of advanced configuration mechanisms of models and reasoners. </li></ul></ul>
  43. 43. Querying Model with SPARQL <ul><li>Query query = …(Query String); </li></ul><ul><li>QueryExecution qexec = QueryExecutionFactory. create (query, model ); </li></ul><ul><li>try </li></ul><ul><li>{ </li></ul><ul><li>ResultSet results = qexec.execSelect(); </li></ul><ul><li>for (; results.hasNext();) </li></ul><ul><li>{ </li></ul><ul><li>QuerySolution soln = results.nextSolution(); </li></ul><ul><li>… // the solutions may be Resources or Literals </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>finally </li></ul><ul><li>{ </li></ul><ul><li>qexec.close(); </li></ul><ul><li>} </li></ul>Sparql Demo
  44. 44. Persisting RDF Model <ul><li>Download required bundles </li></ul><ul><ul><li>MySQL DB </li></ul></ul><ul><ul><li>MySQL JDBC Driver </li></ul></ul><ul><li>Install MySQL database </li></ul><ul><li>Create jenadb </li></ul><ul><ul><li>create database jenatest character set utf8 ; </li></ul></ul><ul><li>Persist with ModelMaker </li></ul>

×