Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Jena Programming


Published on

This slide describes how to make Semantic Web applications using Jena framework.

Published in: Technology

Jena Programming

  1. 1. Dr. Myungjin Lee
  2. 2. What is the Jena? a Java framework for building Semantic Web applications A Java API for RDF Developed by Brian McBride of HP Derived from SiRPAC API Can parse, create, and search RDF models Easy to use
  3. 3. the Jena Framework includes: an API for reading, processing and writing RDF data in XML, N -triples and Turtle formats; an ontology API for handling OWL and RDFS ontologies; a rule-based inference engine for reasoning with RDF and OW L data sources; stores to allow large numbers of RDF triples to be efficiently s tored on disk; a query engine compliant with the latest SPARQL specification servers to allow RDF data to be published to other application s using a variety of protocols, including SPARQL
  4. 4. What can we do? User interface and applications Trust Proof Unifying logic ARQ Ontologies: Rules: Cryptography Querying: OWL RIF/SWRL SPARQL Taxonomies: RDFS Jena(ARP) Data interchange: RDF Syntax: XML SDB + TDB Identifiers: URI Character set: UNICODE
  5. 5. Install and Run Jena Get package from Unzip it Setup environments (CLASSPATH) Online documentation Tutorial API Doc
  6. 6. Resource Description Framework a general method for conceptual description or modeling of information, especially web resources
  7. 7. Graph and Model Graph a simpler Java API intended for extending Jenas functionality Model a rich Java API with many convenience methods for Java application developers
  8. 8. Nodes and Triples Statement Resource Resource Property Literal Model
  9. 9. Interface Hierarchy
  10. 10. Core Classes What is a Model? one RDF graph which is represented by the Model interface Ontology Model (RDF Graph) Jena Programming Interface
  11. 11. Core Classes ModelFactory Class methods for creating standard kinds of Model Model Interface creating resources, properties and literals and the S tatements adding statements removing statements from a model querying a model and set operations for combining models.
  12. 12. How to make Model read Methods Model read(String url, String base, String lang) Model read(InputStream in, String base, String lang) Model read(Reader reader, String base, String lang) Parameters base - the base uri to be used when converting relative URIs to absolute URIs lang - "RDF/XML", "N-TRIPLE", "TURTLE" (or "TTL") and "N3"
  13. 13. How to make ModelModel model = ModelFactory.createDefaultModel();InputStream in = FileManager.get().open(“BadBoy.owl");, null, “RDF/XML”);
  14. 14. How to write Model write Methods Model write(OutputStream out, String lang, String base) Model write(Writer writer, String lang, String base)model.write(System.out);model.write(System.out, "N-TRIPLE");String fn = “temp/test.xml”;model.write(new PrintWriter(new FileOutputStream(fn)));
  15. 15. Interfaces related to nodes Resource Interface An RDF Resource Property Interface An RDF Property RDFNode Interface Interface covering RDF resources and literals
  16. 16. How to create nodes from Model Interface to create new nodes whose model is this model from ResourceFactory Class to create resources and properties are not associat ed with a user-modifiable model
  17. 17. Resource and Property Methods Resource createResource(String uri) Property createProperty(String uriref)Resource s = model.createResource(“…”);Property p = ResourceFactory.createProperty(“…”);
  18. 18. Literal Un-typed Literal Literal createLiteral(String v, String language) Literal createLiteral(String v, boolean wellFormed)Literal l1 = model.createLiteral("chat", "en")Literal l2 = model.createLiteral("<em>chat</em>", true);
  19. 19. Literal Typed Literal Literal createTypedLiteral(Object value)Literal l = model.createTypedLiteral(new Integer(25)); Java class xsd type Java class xsd type Float float Byte byte Double double BigInteger integer Integer int BigDecimal decimal Long long Boolean Boolean Short short String string
  20. 20. RDF Triple and Statement Interface RDF Triple (Statement) arc in an RDF Model asserts a fact about a resource consists of subject, predicate, and object Triple Subject Predicate Object Resource Property RDFNode Jena Programming Interface Statement
  21. 21. Interfaces related to triples Statement Interface represents a triple consists of Resource, Property, and RDFNode StmtIterator Interface a set of statements (triples)
  22. 22. Getting TriplesStmtIterator iter = model.listStatements();while (iter.hasNext()) { Statement stmt = iter.nextStatement(); Resource subject = stmt.getSubject(); Property predicate = stmt.getPredicate(); RDFNode object = stmt.getObject();}
  23. 23. To list the ‘selected’ statements in the Model// making nodes for queryResource husband =model.createResource("");Property type =model.createProperty("");// querying a modelStmtIterator iter = model.listStatements(husband, type, (RDFNode) null);while (iter.hasNext()) { Statement stmt = iter.nextStatement(); Resource subject = stmt.getSubject(); Property predicate = stmt.getPredicate(); RDFNode object = stmt.getObject(); System.out.println(subject + " " + predicate + " " + object);}
  24. 24. Add a relation of resourceResource husband =model.createResource("");Property marry =model.createProperty("");Resource wife =model.createResource("");husband.addProperty(marry, wife);
  25. 25. Ontologies and reasoning to derive additional truths about the conceptsJena2 inference subsystem to allow a range of inference engines or reasoners to be plugged into Jena
  26. 26. Transitive reasoner Provides support for storing and traversing class and property lattices. This implements just the transitive and reflexive properties of rdfs:subPropertyOf and rdfs:subClassOf.RDFS rule reasoner Implements a configurable subset of the RDFS entailments.OWL, OWL Mini, OWL Micro Reasoners A set of useful but incomplete implementation of the OWL/Lite subset of the OWL/Full language.DAML micro reasoner Used internally to enable the legacy DAML API to provide minimal (RDFS scale) inferencing.Generic rule reasoner A rule based reasoner that supports user defined rules. Forward chaining, tabled backward chaining and hybrid execution strategies are supported.
  27. 27. hasWife Person inverseOftype subClassOf subClassOf type hasHusband Male Female type type Myungj hasWife Yejin in Lee Son hasHusband
  28. 28. Jena OWL reasonersReasoner reasoner = ReasonerRegistry.getOWLReasoner();InfModel infmodel = ModelFactory.createInfModel(reasoner,model);Property husband =model.createProperty("");StmtIterator iterHusband = infmodel.listStatements(null,husband, myungjin);while (iterHusband.hasNext()) { Statement stmt = iterHusband.nextStatement(); Resource subject = stmt.getSubject(); Property predicate = stmt.getPredicate(); RDFNode object = stmt.getObject(); System.out.println(subject + " " + predicate + " " +object);}
  29. 29. If someones husband is Myungjin Lee, she is a happy woman. subClassOf Happy Person Woman subClassOf subClassOfMale Female typetype typeMyungj hasWife Yejinin Lee Son
  30. 30. family.rules1. @prefix base: <>.2. [HappyWoman: (?x rdf:type base:Female), (base:MyungjinLee base:hasWife ?x) -> (?x rdf:type base:HappyWoman)]
  31. 31. Resource configuration = model.createResource();configuration.addProperty (ReasonerVocabulary.PROPruleMode, "forward");configuration.addProperty (ReasonerVocabulary.PROPruleSet, "family.rules");Reasoner domainReasoner =GenericRuleReasonerFactory.theInstance().create(configuration);InfModel domaininfmodel =ModelFactory.createInfModel(domainReasoner, infmodel);StmtIterator happyIter =domaininfmodel.listStatements(wife, type, (RDFNode) null);
  32. 32. SPARQL(SPARQL Protocol and RDF Query Language) an RDF query language, that is, a query language for databases to retrieve and manipulate data stored in Resource Description Framework formatSimple Example PREFIX foaf: <> SELECT ?name ?email WHERE { ?person rdf:type foaf:Person. ?person foaf:name ?name. ?person foaf:mbox ?email. }
  33. 33. family.sparqlPREFIX rdf: <> .PREFIX base: <>SELECT ?xWHERE { ?x rdf:type base:Female. base:MyungjinLee base:hasWife ?x.}
  34. 34. try { // make a query string from SPARQL file FileReader fr = new FileReader("family.sparql"); BufferedReader br = new BufferedReader(fr); StringBuffer queryString = new StringBuffer(); String temp; while ((temp = br.readLine()) != null) { queryString.append(temp); } Query query = QueryFactory.create(queryString.toString()); // create a object for query QueryExecution qexec = QueryExecutionFactory.create(query,domaininfmodel); ResultSet results = qexec.execSelect(); // execute SPARQL query while(results.hasNext()) { QuerySolution soln = results.nextSolution(); RDFNode r = soln.get("x"); // get a result System.out.println(r.toString()); }} catch (Exception e) { e.printStackTrace();}