1 / 44

Jena: A Semantic Web Framework for Java

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

Télécharger la présentation

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Jena: A Semantic Web Framework for Java Reporter C.F.Liao (廖峻鋒) May 17,2007

  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. Outline • Introduction • Installing and Running Jena • RDF Model Operations • Inference Mechanisms • (Optional) • SPARQL • Joseki • Conclusion

  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. RDF and OWL Processing API OWL Model I/O Console OWL Model CRUD Model Model I/O OWL RDF Files

  6. Rules Original Model Inferred Model Reasoner Jena Inference Mechanism OWL OWL OWL ReasonerVocabulary

  7. SPARQL (W3C Standard) SELECT ?x WHERE { ?x locatedIn classroom} … SPARQL Query Engine Model ?x = {Jane, Mary, and Peter}

  8. Property Literal Resource locatedIn TV1 Bedroom contains size 9 Review: The RDF Network Light Switch1 on state

  9. RDFNode Key Abstractions Light Switch1 on status locatedIn Bedroom Browse the Jena Javadoc

  10. Recommended Development Environment • JDK 1.5 + • Eclipse 3.2 +, JST (J2EE Standard Tools) • MySQL 4.x (optional)

  11. 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

  12. Downloading Jena • http://jena.sourceforge.net/

  13. add query RDF Producer (Jena) Model (Memory) RDF Consumer (Jena) load RDF Files Typical Jena Usage Scenario

  14. Advanced Jena Usage Scenario RDF Publishing Server (Joseki) SPARQL over HTTP RDF Producer (Jena) Persistent RDF Model (Relational Database) Remote RDF Consumer (Jena)

  15. LabCreating a Simple RDF Model

  16. Model (Memory) Objectives • Creating a simple RDF statement • Placing this RDF into a Model • Dumping the Model to system console print add RDF Producer

  17. Creating RDF • 2 ways • Using Jena API • Load from file

  18. 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)

  19. LabModel I/O

  20. 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

  21. 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

  22. 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

  23. LabModel CRUD

  24. Model CRUD • Create • Retrieve • Update • Delete query Model RDF Consumer (Jena)

  25. 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

  26. 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

  27. 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

  28. 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;

  29. LabBasic Inference

  30. GenericReasoner Data (Model) OwlReasoner InfModel ModelFactory Jena Inference Mechanism ReasonerRegistry RDFSReasoner Existing Model + Reasoner ModelFactory New Model (InfModel) Application

  31. 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

  32. LabOntological Inference

  33. Modeling a Domain with Schema and Data Schema Level Data Level

  34. Data (Model) OwlReasoner Schema (OntModel) InfModel ModelFactory Separating Schema and Data ReasonerRegistry GenericReasoner RDFSReasoner Application

  35. type locatedIn type locatedIn transitive inverse contains type contains type Bedroom Building Person Room Home light1 Jane Light Example: The Smart Home Ontology

  36. 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

  37. 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);

  38. LabCascading Inference

  39. Data (Model) OwlReasoner ontInfModel Schema (OntModel) infModel ModelFactory ModelFactory Combining Generic and OWL Reasoners Rules GenericReasoner

  40. 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”

  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. 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. 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. 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

More Related