1 Knowledge Representation XII – IKT437 Knowledge Representation XII – IKT437 Part III Div Jan Pettersen Nytun, UiA Apache Jena.

Slides:



Advertisements
Similar presentations
CH-4 Ontologies, Querying and Data Integration. Introduction to RDF(S) RDF stands for Resource Description Framework. RDF is a standard for describing.
Advertisements

GridVine: Building Internet-Scale Semantic Overlay Networks By Lan Tian.
Master Informatique 1 Semantic Technologies Part 4Jena Werner Nutt.
JENA –A SEMANTIC WEB TOOL by Ranjani Sankaran & krishna Priyanka Chebrolu.
1 More on Decisions Overview l The selection Operator l Grouping of Statements l Multiple branches (if else if) l Switch Statement.
Jena --- A Java API for RDF Jing deng CSCI 7818 Web Technologies Computer Science Department University of Colorado at Boulder Wednesday, October 31, 2001.
Internet Technologies1 RDF Bibliography: “XML How To Program” by Deitel, Deitel, Nieto, Lin and Sadhu. “XML Bible” by Elliotte Rusty Harold. HP’s Jena.
SCOPE & I/O CSC 171 FALL 2004 LECTURE 5. CSC171 Room Change Thursday, September 23. CSB 209 THERE WILL BE A (group) QUIZ! - topic: the CS department at.
Internet Technologies 1 Master of Information System Management Internet Technologies The Resource Description Framework (RDF)
SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end.
Jena 4/17/2017.
1 Repetition structures [cont’d] Overview l Nested Loops l Sentinel-Controlled loop l Avoiding Number Format exception.
COMP 14: Intro. to Intro. to Programming May 23, 2000 Nick Vallidis.
16-Aug-15 Java Puzzlers From the book Java Puzzlers by Joshua Bloch and Neal Gafter.
CSC3170 Introduction to Database Systems
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Java Programming Exception Handling. The exception handling is one of the powerful mechanism provided in java. It provides the mechanism to handle the.
The Jena RDF Framework Konstantinos Tzonas. Contents What is Jena Capabilities of Jena Basic notions RDF concepts in Jena Persistence Ontology management.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
Ontology Query. What is an Ontology Ontologies resemble faceted taxonomies but use richer semantic relationships among terms and attributes, as well as.
Presentation : Konstantinos Kanaris.  What is Jena?  Usage of Jena  Main Concepts  Main Components  Storage Models  OWL API  RDF API  Reasoning.
From C++ to Java A whirlwind tour of Java for C++ programmers.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
Overriding toString()
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
State Design Pattern. Behavioral Pattern Allows object to alter its behavior when internal state changes Uses Polymorphism to define different behaviors.
Intelligent Space 國立台灣大學資訊工程研究所 智慧型空間實驗室 Jena: A Semantic Web Framework for Java Reporter C.F.Liao ( 廖峻鋒 ) May 17,2007.
Chapter 5 : Methods Part 2. Returning a Value from a Method  Data can be passed into a method by way of the parameter variables. Data may also be returned.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
Java Server Sockets ServerSocket : Object to listen for client connection requests Throws IOException accept() method to take the client connection. Returns.
Programming Principles Operators and Expressions.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
I/O in java and Revision. 2 I/O in Java Many packages and libraries associated with Java provide sophisticated ways to input and output information, e.g.:
CHAPTER 8 – Jena:RDF in Java 박형우 SNU OOPSLA Lab. August 27, 2004.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
1 Knowledge Representation XI – IKT437 Knowledge Representation XI – IKT437 Part I RDF Jan Pettersen Nytun, UiA Apache Jena.
Reading Parameters CSCI 201 Principles of Software Development Jeffrey Miller, Ph.D.
1 RDF Storage and Retrieval Systems Jan Pettersen Nytun, UiA.
Knowledge Representation XI – IKT437 Part II OWL, SPARQL, …
Jena Ontology API 4/14/2018.
GC211 Data structure Lecture 3 Sara Alhajjam.
Threads in Java Two ways to start a thread
Jena 6/8/2018.
Lecture 2: Data Types, Variables, Operators, and Expressions
CS1101: Programming Methodology Recitation 7 – Exceptions
Knowledge Representation Part II
Jan Pettersen Nytun, UiA
Going from C++ to Java Jayden Navarro.
null, true, and false are also reserved.
Introduction to Java Programming
An Introduction to Java – Part I, language basics
An overview of Java, Data types and variables
Chapter 1: Computer Systems
Code Animation Examples
Exception Handling in Java
JavaScript Reserved Words
The Jena RDF Framework Konstantinos Tzonas.
Prof. Bhushan Trivedi Director GLS Institute of Computer Technology
class PrintOnetoTen { public static void main(String args[]) {
Module 2 - Part 1 Variables, Assignment, and Data Types
JDBC Example.
Instructor: Alexander Stoytchev
Agenda Types and identifiers Practice Assignment Keywords in Java
Knowledge Representation Part III
State Design Pattern.
A type is a collection of values
Exceptions and Exception Handling
Presentation transcript:

1 Knowledge Representation XII – IKT437 Knowledge Representation XII – IKT437 Part III Div Jan Pettersen Nytun, UiA Apache Jena

S O P Example 1 Jan Pettersen Nytun, UIA, page 2

S O P Ancestor An ancestor is a parent or recursively the parent of an ancestor (i.e., any person from whom one is descending). In mathematical terms: The ancestral relation is transitive. Jan Pettersen Nytun, UIA, page 3 Human1 Human2Human3 Human4Human5 hasAncestor E.g.: Human3 has ancestors: Human2 and Human1

S O P package ancestor; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.List; import org.apache.jena.ontology.Individual; import org.apache.jena.ontology.ObjectProperty; import org.apache.jena.ontology.OntClass; import org.apache.jena.ontology.OntModel; import org.apache.jena.ontology.OntModelSpec; import org.apache.jena.rdf.model.Model; import org.apache.jena.rdf.model.ModelFactory; import org.apache.jena.rdf.model.Property; import org.apache.jena.rdf.model.RDFNode; import org.apache.jena.rdf.model.Resource; import org.apache.jena.rdf.model.Statement; import org.apache.jena.rdf.model.StmtIterator; public class AncesterApp { private static String defaultNamespace = " public static void main(String[] args){…} private static void printTriplesInDefaultNamespace(OntModel ontModel) {…} public static List getSortedTriples(Model model) {…} } Jan Pettersen Nytun, UIA, page 4 Overview

public static void main(String[] args){ … OntModel ancesterModel = ModelFactory.createOntologyModel( OntModelSpec.OWL_DL_MEM_RULE_INF); ancesterModel.setNsPrefix("", defaultNamespace); System.out.println("OntModel created."); System.out.println("Language profile: OWL DL"); System.out.println("Storage model: in-memory"); System.out.println("Reasoner: rule-based reasoner with OWL rules"); System.out.println(“ \n"); … Jan Pettersen Nytun, UIA, page 5 OntModel created. Language profile: OWL DL Storage model: in-memory Reasoner: rule-based reasoner with OWL rules Output:

S O P public static void main(String[] args){ … OntClass humanClass = ancesterModel. createClass (defaultNamespace+"Human"); System.out.println("Class Human created:"); printTriplesInDefaultNamespace(ancesterModel); … Jan Pettersen Nytun, UIA, page 6 Class Human created: Human equivalentClass Human. Human subClassOf Human. Human subClassOf Resource. Human subClassOf Thing. Human type Class. Human type Resource.

Individual human1 = ancesterModel.createIndividual( defaultNamespace + "Human1", humanClass ); Individual human2 = ancesterModel.createIndividual( defaultNamespace + "Human2", humanClass ); Individual human3 = ancesterModel.createIndividual( defaultNamespace + "Human3", humanClass ); Individual human4 = ancesterModel.createIndividual( defaultNamespace + "Human4", humanClass ); Individual human5 = ancesterModel.createIndividual( defaultNamespace + "Human5", humanClass ); System.out.println("5 human created:"); printTriplesInDefaultNamespace(ancesterModel); Jan Pettersen Nytun, UIA, page 7 5 human created: Human equivalentClass Human. … Human1 sameAs Human1. Human1 type Human. Human1 type Resource. Human1 type Thing. Human2 sameAs Human2. …

ObjectProperty hasAncesterObjectProperty = ancesterModel.createTransitiveProperty( defaultNamespace + "hasAncestor", false); System.out.println("Transitive property hasAncestor created:"); printTriplesInDefaultNamespace(ancesterModel); Jan Pettersen Nytun, UIA, page 8 Transitive property hasAncestor created: … hasAncestor subPropertyOf hasAncestor. hasAncestor type ObjectProperty. hasAncestor type Property. hasAncestor type Resource. hasAncestor type TransitiveProperty boolean functional

human3.setPropertyValue(hasAncesterObjectProperty, human2); human2.setPropertyValue(hasAncesterObjectProperty, human1); human5.setPropertyValue(hasAncesterObjectProperty, human4); human4.setPropertyValue(hasAncesterObjectProperty, human1); System.out.println("Two ancester cains has been created:"); System.out.println(":Human3 :hasAncestor :Human2. :Human2 :hasAncestor :Human1"); System.out.println(":Human5 :hasAncestor :Human4. :Human4 :hasAncestor :Human1"); System.out.println("After adding the transitive properties:"); printTriplesInDefaultNamespace(ancesterModel); Jan Pettersen Nytun, UIA, page 9 Two ancestor cains have been created: :Human3 :hasAncestor :Human2. :Human2 :hasAncestor :Human1 :Human5 :hasAncestor :Human4. :Human4 :hasAncestor :Human1 After adding the transitive properties: … Human2 hasAncestor Human1. … Human3 hasAncestor Human1. Human3 hasAncestor Human2. …

BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String cmd; while (true) { System.out.println("\nList of all humans:"); System.out.println(" 1: Human1"); System.out.println(" 2: Human2"); System.out.println(" 3: Human3"); System.out.println(" 4: Human4"); System.out.println(" 5: Human5"); System.out.println("\nGet ancestors of which human (give a number; 0 gives exit):"); try { cmd = in.readLine(); int cmdAsNumber = Integer.parseInt(cmd); if (cmdAsNumber 5) break; Jan Pettersen Nytun, UIA, page 10

// decide URI for selected human String humanLocalName = "Human" + cmdAsNumber; String humanURI = defaultNamespace + humanLocalName; Resource oneHuman = ancesterModel.getResource(humanURI); StmtIterator iterAncestors = oneHuman.listProperties(hasAncesterObjectProperty); if (!iterAncestors.hasNext()){ System.out.println("\nHuman: " + humanLocalName + " has not registred any ancestors."); } else { System.out.println("\nHuman: " + humanLocalName + " has the following ancestors:"); while (iterAncestors.hasNext()) { System.out.println(" " + iterAncestors.nextStatement().getObject().asResource().getLocalName()); } } catch (IOException e) { e.printStackTrace(); break; } Jan Pettersen Nytun, UIA, page 11

Jan Pettersen Nytun, UIA, page 12 List of all humans: 1: Human1 2: Human2 3: Human3 4: Human4 5: Human5 Get ancestors of which human (give a number; 0 gives exit): 1 Human: Human1 has not registered any ancestors. List of all humans: 1: Human1 2: Human2 3: Human3 4: Human4 5: Human5 Get ancestors of which human (give a number; 0 gives exit): 2 Human: Human2 has the following ancestors: Human1

Jan Pettersen Nytun, UIA, page 13 List of all humans: 1: Human1 2: Human2 3: Human3 4: Human4 5: Human5 Get ancestors of which human (give a number; 0 gives exit): 3 Human: Human3 has the following ancestors: Human2 Human1

S O P private static void printTriplesInDefaultNamespace(OntModel ontModel) { List triples = getSortedTriples(ontModel); System.out.println(" "); for (String oneTriple : triples) System.out.println(oneTriple); System.out.println(" \n"); } Jan Pettersen Nytun, UIA, page 14

S O P public static List getSortedTriples(Model model){ List triples = new ArrayList (); StmtIterator iter = model.listStatements(); String format = "%-40s"; String defaultNamespace = model.getNsPrefixMap().get(""); if (defaultNamespace == null) return triples; while (iter.hasNext()) { String tripleAsString = ""; Statement stmt = iter.nextStatement(); // get next statement Resource subject = stmt.getSubject(); // get the subject Property predicate = stmt.getPredicate(); // get the predicate RDFNode object = stmt.getObject(); // get the object if (subject.asNode().isBlank()) { tripleAsString = String.format(format, "_:" + subject.asNode().getBlankNodeId()); } else { String namespace = subject.getNameSpace(); if (!namespace.equals(defaultNamespace)) continue; tripleAsString = String.format(format, subject.getLocalName().toString()); } tripleAsString = tripleAsString + String.format(format, predicate.getLocalName().toString()); if (object instanceof Resource) { if (object.asNode().isBlank()) { tripleAsString = tripleAsString + "_:" + object.asNode().getBlankNodeId() + "."; } else { tripleAsString = tripleAsString + object.asResource().getLocalName().toString() + "."; } } else { // object is a literal tripleAsString = tripleAsString + " \"" + object.toString() + "\"" + "."; } triples.add(tripleAsString); } Collections.sort(triples); return triples; } Jan Pettersen Nytun, UIA, page 15 Rest of code

S O P Example 2 Jan Pettersen Nytun, UIA, page 16

S O P Extending The Ancestor Application … OntModel ancesterModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_RULE_INF); … OntClass humanClass = ancesterModel.createClass(defaultNamespace+ "Human"); … DatatypeProperty hasName = ancesterModel.createDatatypeProperty(defaultNamespace + "hasName"); DatatypeProperty hasAddress = ancesterModel.createDatatypeProperty(defaultNamespace + "hasAddress"); … Jan Pettersen Nytun, UIA, page 17

… createHuman( ancesterModel, defaultNamespace, "Bob Dylan", "California", defaultNamespace + "HumanBob“ ); createHuman( ancesterModel, defaultNamespace, "Tom", "Norway", defaultNamespace + "HumanTom“ ); String address = getAddress( ancesterModel, defaultNamespace, "Bob Dylan" ); updateAddress( ancesterModel, defaultNamespace, "Bob Dylan", "Grimstad" ); … Jan Pettersen Nytun, UIA, page 18

public static void createHuman(OntModel ontModel, String namespace, // same namespace is used for person and class Human String personName, String address, String personUri ) { String queryRequest = "PREFIX rdfs: " + " PREFIX ns: " + " " + " INSERT DATA " + "{" + " " + " rdfs:type " + "ns:Human ;" + "ns:hasName " + "\"" + personName +"\"" + " ;"+ "ns:hasAddress " + "\"" + address +"\"."+ "}"; System.out.println("queryRequest:" + queryRequest); UpdateAction.parseExecute(queryRequest, ontModel.getGraph()); } Jan Pettersen Nytun, UIA, page 19 Output Example, queryRequest: PREFIX rdfs: PREFIX ns: INSERT DATA { rdfs:type ns:Human ; ns:hasName "Bob Dylan" ; ns:hasAddress "California".}

public static String getAddress( OntModel ontModel, String namespace, String personName ) { String address = ""; String personUri = ""; String queryRequest = "PREFIX rdfs: " + " PREFIX ns: " + " " + " SELECT ?address ?personUri " + "WHERE {" + "?personUri " + " rdfs:type " + "ns:Human ;" + "ns:hasName " + "\"" + personName +"\"" + " ; " + "ns:hasAddress " + "?address."+ "}"; … Jan Pettersen Nytun, UIA, page 20 PREFIX rdfs: PREFIX ns: INSERT DATA { rdfs:type ns:Human ; ns:hasName "Tom" ; ns:hasAddress "Norway".}

Query query; QueryExecution qexec; try { query = QueryFactory.create(queryRequest); qexec = QueryExecutionFactory.create(query, ontModel); } catch(Exception e){ System.out.println(e); return null; } //Run Select ResultSet response = null; try { response = qexec.execSelect(); Jan Pettersen Nytun, UIA, page 21

while( response.hasNext()){ // OBS. Only interested in the first result… QuerySolution QuerySolution = response.nextSolution(); RDFNode addressNode = QuerySolution.get("?address"); if( addressNode != null ){ address = addressNode.toString(); } RDFNode personUriNode = QuerySolution.get("?personUri"); // This only to show how //to handle several variables if( personUriNode != null ){ personUri = personUriNode.toString(); break; } } finally {qexec.close();} return address; } Jan Pettersen Nytun, UIA, page 22

public static void updateAddress( OntModel ontModel, String namespace, String personName, String newAddress ) { String queryRequest = "PREFIX rdfs: " + "PREFIX ns: " + " " + "DELETE { ?personUri " + "ns:hasAddress ?address } " + "INSERT { ?personUri ns:hasAddress " + "\"" + newAddress + "\"} " + "WHERE { ?personUri ns:hasName " + "\"" + personName + "\"} "; UpdateAction.parseExecute(queryRequest, ontModel.getGraph()); } Jan Pettersen Nytun, UIA, page 23