Processing OWL Ontologies using Java
Jena Ontology API

Raji GHAWI

30/03/2010
Outline



What is Jena ?
Reading an Existing Ontology







Classes
Properties
Individuals

Creating a New Ontology




30/03/2010

Classes
Properties
Individuales

2
What is Jena ?


Jena is a Java framework



for building Semantic Web applications
includes









30/03/2010

an RDF API
reading and writing RDF in RDF/XML, N3 and N-Triples
an OWL API
in-memory and persistent storage
SPARQL query engine

http://jena.sourceforge.net/

3
Example Ontology

Person
name
age
email

Professor

Student

Class

studentNumber

subClassOf
Object property

taughtBy

teach

Datatype property

enrolledIn
hasModule

Module
moduleName

30/03/2010

Diploma
diplomaName

4
Outline



What is Jena ?
Reading an Existing Ontology







Classes
Properties
Individuals

Creating a New Ontology




30/03/2010

Classes
Properties
Individuales

5
Create Ontology Model


Jena provides an ontology model that allows to specify:




Ontology language
Storage model
Inference mode

OntModel model = ModelFactory.createOntologyModel();



default settings:




30/03/2010

OWL-Full language
in-memory storage
RDFS inference

6
OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM);

OntModelSpec

Language profile

Storage model

Reasoner

OWL_MEM

OWL full

in-memory

none

OWL_MEM_TRANS_INF

OWL full

in-memory

transitive class-hierarchy inference

OWL_MEM_RULE_INF

OWL full

in-memory

rule-based reasoner with OWL rules

OWL_MEM_MICRO_RULE_INF

OWL full

in-memory

optimised rule-based reasoner with OWL rules

OWL_MEM_MINI_RULE_INF

OWL full

in-memory

rule-based reasoner with subset of OWL rules

OWL_DL_MEM

OWL DL

in-memory

none

OWL_DL_MEM_RDFS_INF

OWL DL

in-memory

rule reasoner with RDFS-level entailment-rules

OWL_DL_MEM_TRANS_INF

OWL DL

in-memory

transitive class-hierarchy inference

OWL_DL_MEM_RULE_INF

OWL DL

in-memory

rule-based reasoner with OWL rules

OWL_LITE_MEM

OWL Lite

in-memory

none

OWL_LITE_MEM_TRANS_INF

OWL Lite

in-memory

transitive class-hierarchy inference

OWL_LITE_MEM_RDFS_INF

OWL Lite

in-memory

rule reasoner with RDFS-level entailment-rules

OWL_LITE_MEM_RULES_INF

OWL Lite

in-memory

rule-based reasoner with OWL rules

DAML_MEM

DAML+OIL

in-memory

none

DAML_MEM_TRANS_INF

DAML+OIL

in-memory

transitive class-hierarchy inference

DAML_MEM_RDFS_INF

DAML+OIL

in-memory

rule reasoner with RDFS-level entailment-rules

DAML_MEM_RULE_INF

DAML+OIL

in-memory

rule-based reasoner with DAML rules

RDFS_MEM

RDFS

in-memory

none

RDFS_MEM_TRANS_INF

RDFS

in-memory

transitive class-hierarchy inference

RDFS_MEM_RDFS_INF

RDFS

in-memory

rule reasoner with RDFS-level entailment-rules

30/03/2010

7
Read a File into Ontology Model
String fileName = "univ.owl";
try {
File file = new File(fileName);
FileReader reader = new FileReader(file);
OntModel model = ModelFactory
.createOntologyModel(OntModelSpec.OWL_DL_MEM);
model.read(reader,null);
model.write(System.out,"RDF/XML-ABBREV");
} catch (Exception e) {
e.printStackTrace();
}

30/03/2010

8
Reading an Existing Ontology
Classes

Creating a New Ontology
Properties

Individuales

Retrieve Ontology Classes
Iterator classIter = model.listClasses();
while (classIter.hasNext()) {
OntClass ontClass = (OntClass) classIter.next();
String uri = ontClass.getURI();
if(uri != null)
System.out.println(uri);
}



We can also use ontClass.getLocalName() to get the class name only.
If a class has no name (e.g. a restriction class), then ontClass.getURI()
returns null.
http://www.something.com/myontology#Professor
http://www.something.com/myontology#Module
http://www.something.com/myontology#Diploma
http://www.something.com/myontology#Person
http://www.something.com/myontology#Student

30/03/2010

output

9
Reading an Existing Ontology
Classes

Creating a New Ontology
Properties

Individuales

Retrieve a Specified Class


A specifed class is called by its URI

String classURI = "http://www.something.com/myontology#Professor";
OntClass professor = model.getOntClass(classURI );
// ...



If we know the class name only, we can get its URI by concatenating the
ontology URI with the class name:

ClassURI = OntologyURI + ‘#’ + ClassName

30/03/2010

10
Reading an Existing Ontology
Classes

Creating a New Ontology
Properties

Individuales

Get the Ontology URI
String ontologyURI = null;
Iterator iter = model.listOntologies();
if(iter.hasNext()){
Ontology onto = (Ontology) iter.next();
ontologyURI = onto.getURI();
System.out.println("Ontology URI = "+ontologyURI);
}

String className = "Professor";
String classURI = ontologyURI+"#"+className;
OntClass professor = model.getOntClass(classURI );

30/03/2010

11
Reading an Existing Ontology
Classes

Creating a New Ontology
Properties

Individuales

Class Hierarchy
OntClass student = model.getOntClass(uri+"#Student");
System.out.println(student.getSuperClass());
System.out.println(student.getSubClass());
http://www.something.com/myontology#Person
null

OntClass person = model.getOntClass(uri+"#Person");
System.out.println(person.getSuperClass());
System.out.println(person.getSubClass());
null
http://www.something.com/myontology#Professor

30/03/2010

12
Reading an Existing Ontology
Classes

Creating a New Ontology
Properties

Individuales

Class Hierarchy
Iterator supIter = person.listSuperClasses();
while (supIter.hasNext()) {
OntClass sup = (OntClass) supIter.next();
System.out.println(sup);
}
System.out.println("----------------");
Iterator subIter = person.listSubClasses();
while (subIter.hasNext()) {
OntClass sub = (OntClass) subIter.next();
System.out.println(sub);
}
---------------http://www.something.com/myontology#Professor
http://www.something.com/myontology#Student

30/03/2010

13
Reading an Existing Ontology

Creating a New Ontology

Classes

Properties

Individuales

Class Hierarchy
true

direct sub-classes

false

all sub-classes (default)

true

direct super-classes

false

all super-classes (default)

listSubClasses(boolean)

listSuperClasses(boolean)

A

B, C

->

B, C, D

D.listSuperClasses(true)

->

C

C
D

30/03/2010

->

A.listSubClasses(false)

B

A.listSubClasses(true)

D.listSuperClasses(false) ->

C, A

14
Reading an Existing Ontology
Classes

Creating a New Ontology
Properties

Individuales

Class Hierarchy
<OntClass>.isHierarchyRoot()


This function returns true if this class is the root of the class
hierarchy in the model:



this class has owl:Thing as a direct super-class,
or it has no declared super-classes
owl:Thing

Professor

Person

30/03/2010

Module

Diploma

Student

15
Reading an Existing Ontology
Classes

Creating a New Ontology
Properties

Individuales

Intersection / Union / Complement
if(ontClass.isIntersectionClass()){
IntersectionClass intersection = ontClass.asIntersectionClass();
RDFList operands = intersection.getOperands();
for (int i = 0; i < operands.size(); i++) {
RDFNode rdfNode = operands.get(i);
...
}
} else if(ontClass.isUnionClass()){
UnionClass union = ontClass.asUnionClass();
RDFList operands = union.getOperands();
...
} else if(ontClass.isComplementClass()){
ComplementClass compl = ontClass.asComplementClass();
RDFList operands = compl.getOperands();
...
}

30/03/2010

16
Reading an Existing Ontology
Classes

Creating a New Ontology
Properties

Individuales

Retrieve the Properties of a Specified Class
OntClass student = model.getOntClass(uri+"#Student");
Iterator propIter = student.listDeclaredProperties();
while (propIter.hasNext()) {
OntProperty property = (OntProperty) propIter.next();
System.out.println(property.getLocalName());
}
output

studentNumber
age
personName
enrolledIn
email

true

directly associated properties

false

all properties (direct + inhereted)

listDeclaredProperties(boolean)
(default)
30/03/2010

17
Reading an Existing Ontology
Classes

Creating a New Ontology
Properties

Individuales

Property Types


1

Two ways to know whether a property is datatype property or
object property:
if(property.isObjectProperty()){
// ... this is an object property
} else if (property.isDatatypeProperty()){
// ... this is an datatype property
}

property.isFunctionalProperty();
property.isSymmetricProperty();
property.isTransitiveProperty();
property.isInverseOf(anotherProperty);

30/03/2010

18
Reading an Existing Ontology
Classes

Creating a New Ontology
Properties

Individuales

Property Types


Two ways to know whether a property is datatype property or
object property:
Resource propertyType = property.getRDFType();
System.out.println(propertyType);

2

if(propertyType.equals(OWL.DatatypeProperty)){
// ... this is an datatype property
} else if(propertyType.equals(OWL.ObjectProperty)){
// ... this is an object property
}

http://www.w3.org/2002/07/owl#DatatypeProperty
http://www.w3.org/2002/07/owl#DatatypeProperty
http://www.w3.org/2002/07/owl#DatatypeProperty
http://www.w3.org/2002/07/owl#ObjectProperty
http://www.w3.org/2002/07/owl#DatatypeProperty

30/03/2010

output

19
Reading an Existing Ontology
Classes

Creating a New Ontology
Properties

Individuales

Property Domain and Range
String propertyName = property.getLocalName();
String dom = "";
String rng = "";
if(property.getDomain()!=null)
dom = property.getDomain().getLocalName();
if(property.getRange()!=null)
rng = property.getRange().getLocalName();
System.out.println(propertyName +": t("+dom+") t -> ("+rng+") ");
listDeclaredProperties(boolean);

true

studentNumber:
enrolledIn:

(Student)
(Student)

false

studentNumber:
age:
personName:
enrolledIn:
email:

(Student)
(Person)
(Person)
(Student)
(Person)

30/03/2010

-> (string)
-> (Diploma)
->
->
->
->
->

output

(string)
(int)
(string)
(Diploma)
(string)
20
Reading an Existing Ontology
Classes

Creating a New Ontology
Properties

Individuales

Datatype Properties


list all datatype properties in the model:

Iterator iter = model.listDatatypeProperties();
while (iter.hasNext()) {
DatatypeProperty prop = (DatatypeProperty) iter.next();
String propName = prop.getLocalName();
String dom = "";
String rng = "";
if(prop.getDomain()!=null)
dom = prop.getDomain().getLocalName();
if(prop.getRange()!=null)
rng = prop.getRange().getLocalName();
System.out.println(propName +": t("+dom+") t -> ("+rng+") ");
}
diplomaName:
studentNumber:
moduleName:
age:
personName:
email:
30/03/2010

(Diploma)
(Student)
(Module)
(Person)
(Person)
(Person)

-> (string)
-> (string)
-> (string)
-> (int)
-> (string)
-> (string)

output

21
Reading an Existing Ontology
Classes

Creating a New Ontology
Properties

Individuales

Object Properties


list all object properties in the model:

Iterator iter = model.listObjectProperties();
while (iter.hasNext()) {
ObjectProperty prop = (ObjectProperty) iter.next();
String propName = prop.getLocalName();
String dom = "";
String rng = "";
if(prop.getDomain()!=null)
dom = prop.getDomain().getLocalName();
if(prop.getRange()!=null)
rng = prop.getRange().getLocalName();
System.out.println(propName +": t("+dom+") t -> ("+rng+") ");
}
enrolledIn:
hasModule:
taughtBy:
teach:

(Student) -> (Diploma)
(Diploma) -> (Module)
(Module) -> (Professor)
(Professor) -> (Module)

getInverse()
30/03/2010

→

output

(OntProperty)
22
Reading an Existing Ontology
Classes

Creating a New Ontology
Properties

Individuales

Other Types of Properties


Using the same way, we may list (iterate) other properties:

model.listFunctionalProperties()

model.listInverseFunctionalProperties()

model.listSymmetricProperties()

model.listTransitiveProperties()

30/03/2010

23
Reading an Existing Ontology
Classes

Creating a New Ontology
Properties

Individuales

Restrictions

if(ontClass.isRestriction()){
Restriction rest = ontClass.asRestriction();
OntProperty onProperty = rest.getOnProperty();
...
}

30/03/2010

24
Reading an Existing Ontology
Classes

Creating a New Ontology
Properties

Individuales

AllValuesFrom / SomeValuesFrom

if(rest.isAllValuesFromRestriction()){
AllValuesFromRestriction avfr = rest.asAllValuesFromRestriction();
Resource avf = avfr.getAllValuesFrom();
...
}
if(rest.isSomeValuesFromRestriction()){
SomeValuesFromRestriction svfr = rest.asSomeValuesFromRestriction();
Resource svf = svfr.getSomeValuesFrom();
...
}

30/03/2010

25
Reading an Existing Ontology
Classes

Creating a New Ontology
Properties

Individuales

HasValue

if(rest.isHasValueRestriction()){
HasValueRestriction hvr = rest.asHasValueRestriction();
RDFNode hv = hvr.getHasValue();
...
}

30/03/2010

26
Reading an Existing Ontology
Classes

Creating a New Ontology
Properties

Individuales

Cardinality / MinCardinality / MaxCardinality
if(rest.isCardinalityRestriction()){
CardinalityRestriction cr = rest.asCardinalityRestriction();
int card = cr.getCardinality();
...
}
if(rest.isMinCardinalityRestriction()){
MinCardinalityRestriction mcr = rest.asMinCardinalityRestriction();
int minCard = minCR.getMinCardinality();
...
}
if(rest.isMaxCardinalityRestriction()){
MaxCardinalityRestriction mcr = rest.asMaxCardinalityRestriction();
int maxCard = maxCR.getMaxCardinality();
...
}

30/03/2010

27
Reading an Existing Ontology
Classes

Creating a New Ontology
Properties

Individuales

Individuals


list all individuals in the model

Iterator individuals = model.listIndividuals();
while (individuals.hasNext()) {
Individual individual = (Individual) individuals.next();
...
}



list individuals of a specific class

Iterator iter = ontClass.listInstances();
while (iter.hasNext()) {
Individual individual = (Individual) iter.next();
...
}

30/03/2010

28
Reading an Existing Ontology
Classes

Creating a New Ontology
Properties

Individuales

Individuals


list properties and property values of an individual

Iterator props = individual.listProperties();
while (props.hasNext()) {
Property property = (Property) props.next();
RDFNode value = individual.getPropertyValue(property);
...
}



list values of a specific property of an individual

Iterator values = individual.listPropertyValues(property);
while (values.hasNext()) {
RDFNode value = values.next();
...
}

30/03/2010

29
Reading an Existing Ontology
Classes

Creating a New Ontology
Properties

Individuales

Individuals


get the class to which an individual belongs

Resource rdfType = individual.getRDFType();
OntClass ontClass = model.getOntClass(rdfType.getURI());



in recent versions of Jena

OntClass ontClass = individual.getOntClass();

30/03/2010

30
Outline



What is Jena ?
Reading an Existing Ontology







Classes
Properties
Individuals

Creating a New Ontology




30/03/2010

Classes
Properties
Individuales

31
Creating a New Ontology

Reading an Existing Ontology
Classes

Properties

Individuales

Create the ontology model
OntModel model =
ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM);

String uriBase = "http://www.something.com/myontology";
model.createOntology(uriBase);

30/03/2010

32
Creating a New Ontology

Reading an Existing Ontology
Classes

Properties

Individuales

Create Classes
//create
OntClass
OntClass
OntClass
OntClass
OntClass

30/03/2010

classes
person = model.createClass(uriBase+"#Person");
module = model.createClass(uriBase+"#Module");
diploma = model.createClass(uriBase+"#Diploma");
student = model.createClass(uriBase+"#Student");
professor = model.createClass(uriBase+"#Professor");

33
Creating a New Ontology

Reading an Existing Ontology
Classes

Properties

Individuales

Set Sub-Classes

//set sub-classes
person.addSubClass(student);
person.addSubClass(professor);

30/03/2010

34
Creating a New Ontology

Reading an Existing Ontology
Classes

Properties

Individuales

Intersection / Union / Complement

RDFNode[] classes = new RDFNode[] {class1, class2, ...};
RDFList list = model.createList(classes);
IntersectionClass intersection =
model.createIntersectionClass(uri, list);
UnionClass union = model.createUnionClass(uri, list);
ComplementClass compl =
model.createComplementClass(uri, resource);

30/03/2010

35
Creating a New Ontology

Reading an Existing Ontology
Classes

Properties

Individuales

Create Datatype Properties

//create datatype properties
DatatypeProperty personName =
model.createDatatypeProperty(uriBase+"#personName");
personName.setDomain(person);
personName.setRange(XSD.xstring);
DatatypeProperty age = model.createDatatypeProperty(uriBase+"#age");
age.setDomain(person);
age.setRange(XSD.integer);
//

... ...

30/03/2010

36
Creating a New Ontology

Reading an Existing Ontology
Classes

Properties

Individuales

Create Object Properties
//create object properties
ObjectProperty teach = model.createObjectProperty(uriBase+"#teach");
teach.setDomain(professor);
teach.setRange(module);
ObjectProperty taughtBy =
model.createObjectProperty(uriBase+"#taughtBy");
taughtBy.setDomain(module);
taughtBy.setRange(professor);
ObjectProperty enrolledIn =
model.createObjectProperty(uriBase+"#enrolledIn");
enrolledIn.setDomain(student);
enrolledIn.setRange(diploma);
// ... ...
30/03/2010

37
Creating a New Ontology

Reading an Existing Ontology
Classes

Properties

Individuales

Inverse-of / Functional


set inverse properties

// set inverse properties
teach.setInverseOf(taughtBy);



set functional properties

// set functional properties
enrolledIn.setRDFType(OWL.FunctionalProperty);

30/03/2010

38
Creating a New Ontology

Reading an Existing Ontology
Classes

Properties

Individuales

Restrictions
usually, null

usually, a class

SomeValuesFromRestriction svfr =
model.createSomeValuesFromRestriction(uri, property, resource);
AllValuesFromRestriction avfr =
model.createAllValuesFromRestriction(uri, property, resource);
HasValueRestriction hvr =
model.createHasValueRestriction(uri, property, rdfNode);
CardinalityRestriction cr =
model.createCardinalityRestriction(uri, property, int);
MinCardinalityRestriction min_cr =
model.createMinCardinalityRestriction(uri, property, int);
MaxCardinalityRestriction max_cr =
model.createMaxCardinalityRestriction(uri, property, int);
30/03/2010

39
Creating a New Ontology

Reading an Existing Ontology
Classes

Properties

Individuales

Individuales


create an individual
Individual indv = ontClass.createIndividual(uri);

or
Individual indv = model.createIndividual(uri, ontClass);



set a property value of an individual
indv.setPropertyValue(property, rdfNode);

30/03/2010

40
Write Ontology Model


write the model to standard output

// write out the model
model.write(System.out,"RDF/XML-ABBREV");



save the model to a string

StringWriter sw = new StringWriter();
model.write(sw, "RDF/XML-ABBREV");
String owlCode = sw.toString();

30/03/2010

41
Save the Model to a File

File file = new File(filePath);
try{
FileWriter fw = new FileWriter(file);
fw.write(owlCode);
fw.close();
} catch(FileNotFoundException fnfe){
fnfe.printStackTrace();
} catch(IOException ioe){
ioe.printStackTrace();
}

30/03/2010

42
References


Jena Ontology API




http://jena.sourceforge.net/ontology/index.html

RDFS and OWL Reasoning Capabilities:


http://www-ksl.stanford.edu/software/jtp/doc/owl-reasoning.html

30/03/2010

43

More Related Content

PPTX
Java and SPARQL
PPTX
PDF
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
PDF
Python Tutorial For Beginners | Python Crash Course - Python Programming Lang...
PPTX
Data Structures in Python
PDF
Enumeration in Java Explained | Java Tutorial | Edureka
PDF
Oop basic overview
PPTX
6. static keyword
Java and SPARQL
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
Python Tutorial For Beginners | Python Crash Course - Python Programming Lang...
Data Structures in Python
Enumeration in Java Explained | Java Tutorial | Edureka
Oop basic overview
6. static keyword

What's hot (20)

PDF
Selecting with SPARQL
PDF
React js t2 - jsx
PDF
Programming the Semantic Web
PDF
Python Course | Python Programming | Python Tutorial | Python Training | Edureka
PPT
OS-20210426203801.ppt
PDF
Python Class | Python Programming | Python Tutorial | Edureka
PPT
Java packages
PPT
DOM and SAX
PPT
Asynchronous Programming in C# - Part 1
PPT
Oop java
PDF
C++ STATEMENTS
PPT
L11 array list
PDF
Java 8 Default Methods
PPTX
Multithreading in java
PPT
Java oops and fundamentals
PDF
Object-oriented Programming in Python
PPTX
Conditional Statements.pptx
PDF
Python For Data Analysis | Python Pandas Tutorial | Learn Python | Python Tra...
PDF
A Prelude of Purity: Scaling Back ZIO
Selecting with SPARQL
React js t2 - jsx
Programming the Semantic Web
Python Course | Python Programming | Python Tutorial | Python Training | Edureka
OS-20210426203801.ppt
Python Class | Python Programming | Python Tutorial | Edureka
Java packages
DOM and SAX
Asynchronous Programming in C# - Part 1
Oop java
C++ STATEMENTS
L11 array list
Java 8 Default Methods
Multithreading in java
Java oops and fundamentals
Object-oriented Programming in Python
Conditional Statements.pptx
Python For Data Analysis | Python Pandas Tutorial | Learn Python | Python Tra...
A Prelude of Purity: Scaling Back ZIO
Ad

Viewers also liked (7)

PDF
PyCon 2012: Militarizing Your Backyard: Computer Vision and the Squirrel Hordes
PDF
Face Recognition with OpenCV and scikit-learn
PPTX
Face Recognition using OpenCV
PDF
Semantic Web - Ontology 101
PDF
Introduction to OpenCV (with Java)
PDF
Introduction to OpenCV 3.x (with Java)
PPTX
Jena Programming
PyCon 2012: Militarizing Your Backyard: Computer Vision and the Squirrel Hordes
Face Recognition with OpenCV and scikit-learn
Face Recognition using OpenCV
Semantic Web - Ontology 101
Introduction to OpenCV (with Java)
Introduction to OpenCV 3.x (with Java)
Jena Programming
Ad

Similar to Java and OWL (20)

PPT
Object and Classes in Java
DOCX
javaopps concepts
PPTX
Object Oriented Programming with C#
PPTX
Lecture 4
PPTX
Design patterns(red)
PPT
C++ Programming Course
PPT
Ccourse 140618093931-phpapp02
PPTX
classes-objects in oops java-201023154255.pptx
PPTX
Java basics
DOCX
Question and answer Programming
PDF
Object Oriented Programming using JAVA Notes
PPT
Inheritance, polymorphisam, abstract classes and composition)
PPTX
Chapter 05 classes and objects
PPTX
Unit3 part1-class
PPTX
OOPS 46 slide Python concepts .pptx
PPTX
Inner Classes & Multi Threading in JAVA
PDF
PPT OF JAWA (1).pdf
PDF
CS8392-OOPS-Printed-Notes-All-Units.pdf for students
PDF
Python - object oriented
Object and Classes in Java
javaopps concepts
Object Oriented Programming with C#
Lecture 4
Design patterns(red)
C++ Programming Course
Ccourse 140618093931-phpapp02
classes-objects in oops java-201023154255.pptx
Java basics
Question and answer Programming
Object Oriented Programming using JAVA Notes
Inheritance, polymorphisam, abstract classes and composition)
Chapter 05 classes and objects
Unit3 part1-class
OOPS 46 slide Python concepts .pptx
Inner Classes & Multi Threading in JAVA
PPT OF JAWA (1).pdf
CS8392-OOPS-Printed-Notes-All-Units.pdf for students
Python - object oriented

More from Raji Ghawi (11)

PPTX
Database Programming Techniques
PPTX
Java and XML Schema
PPTX
Java and XML
PPTX
SPARQL
PPTX
XQuery
PPTX
XPath
PPT
Ontology-based Cooperation of Information Systems
PPT
OWSCIS: Ontology and Web Service based Cooperation of Information Sources
PPT
Coopération des Systèmes d'Informations basée sur les Ontologies
PPT
Building Ontologies from Multiple Information Sources
PPT
Database-to-Ontology Mapping Generation for Semantic Interoperability
Database Programming Techniques
Java and XML Schema
Java and XML
SPARQL
XQuery
XPath
Ontology-based Cooperation of Information Systems
OWSCIS: Ontology and Web Service based Cooperation of Information Sources
Coopération des Systèmes d'Informations basée sur les Ontologies
Building Ontologies from Multiple Information Sources
Database-to-Ontology Mapping Generation for Semantic Interoperability

Recently uploaded (20)

PPTX
CRM(Customer Relationship Managmnet) Presentation
PDF
Advancements in abstractive text summarization: a deep learning approach
PPTX
AQUEEL MUSHTAQUE FAKIH COMPUTER CENTER .
PDF
TicketRoot: Event Tech Solutions Deck 2025
PPTX
From XAI to XEE through Influence and Provenance.Controlling model fairness o...
PDF
“Introduction to Designing with AI Agents,” a Presentation from Amazon Web Se...
PPTX
From Curiosity to ROI — Cost-Benefit Analysis of Agentic Automation [3/6]
PDF
Human Computer Interaction Miterm Lesson
PDF
Fitaura: AI & Machine Learning Powered Fitness Tracker
PDF
Secure Java Applications against Quantum Threats
PDF
TrustArc Webinar - Data Minimization in Practice_ Reducing Risk, Enhancing Co...
PDF
State of AI in Business 2025 - MIT NANDA
PDF
Child-friendly e-learning for artificial intelligence education in Indonesia:...
PDF
ELLIE29.pdfWETWETAWTAWETAETAETERTRTERTER
PDF
Uncertainty-aware contextual multi-armed bandits for recommendations in e-com...
PDF
CCUS-as-the-Missing-Link-to-Net-Zero_AksCurious.pdf
PPTX
Report in SIP_Distance_Learning_Technology_Impact.pptx
PPTX
Strategic Picks — Prioritising the Right Agentic Use Cases [2/6]
PDF
Decision Optimization - From Theory to Practice
PDF
EGCB_Solar_Project_Presentation_and Finalcial Analysis.pdf
CRM(Customer Relationship Managmnet) Presentation
Advancements in abstractive text summarization: a deep learning approach
AQUEEL MUSHTAQUE FAKIH COMPUTER CENTER .
TicketRoot: Event Tech Solutions Deck 2025
From XAI to XEE through Influence and Provenance.Controlling model fairness o...
“Introduction to Designing with AI Agents,” a Presentation from Amazon Web Se...
From Curiosity to ROI — Cost-Benefit Analysis of Agentic Automation [3/6]
Human Computer Interaction Miterm Lesson
Fitaura: AI & Machine Learning Powered Fitness Tracker
Secure Java Applications against Quantum Threats
TrustArc Webinar - Data Minimization in Practice_ Reducing Risk, Enhancing Co...
State of AI in Business 2025 - MIT NANDA
Child-friendly e-learning for artificial intelligence education in Indonesia:...
ELLIE29.pdfWETWETAWTAWETAETAETERTRTERTER
Uncertainty-aware contextual multi-armed bandits for recommendations in e-com...
CCUS-as-the-Missing-Link-to-Net-Zero_AksCurious.pdf
Report in SIP_Distance_Learning_Technology_Impact.pptx
Strategic Picks — Prioritising the Right Agentic Use Cases [2/6]
Decision Optimization - From Theory to Practice
EGCB_Solar_Project_Presentation_and Finalcial Analysis.pdf

Java and OWL

  • 1. Processing OWL Ontologies using Java Jena Ontology API Raji GHAWI 30/03/2010
  • 2. Outline   What is Jena ? Reading an Existing Ontology     Classes Properties Individuals Creating a New Ontology    30/03/2010 Classes Properties Individuales 2
  • 3. What is Jena ?  Jena is a Java framework   for building Semantic Web applications includes       30/03/2010 an RDF API reading and writing RDF in RDF/XML, N3 and N-Triples an OWL API in-memory and persistent storage SPARQL query engine http://jena.sourceforge.net/ 3
  • 5. Outline   What is Jena ? Reading an Existing Ontology     Classes Properties Individuals Creating a New Ontology    30/03/2010 Classes Properties Individuales 5
  • 6. Create Ontology Model  Jena provides an ontology model that allows to specify:    Ontology language Storage model Inference mode OntModel model = ModelFactory.createOntologyModel();  default settings:    30/03/2010 OWL-Full language in-memory storage RDFS inference 6
  • 7. OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM); OntModelSpec Language profile Storage model Reasoner OWL_MEM OWL full in-memory none OWL_MEM_TRANS_INF OWL full in-memory transitive class-hierarchy inference OWL_MEM_RULE_INF OWL full in-memory rule-based reasoner with OWL rules OWL_MEM_MICRO_RULE_INF OWL full in-memory optimised rule-based reasoner with OWL rules OWL_MEM_MINI_RULE_INF OWL full in-memory rule-based reasoner with subset of OWL rules OWL_DL_MEM OWL DL in-memory none OWL_DL_MEM_RDFS_INF OWL DL in-memory rule reasoner with RDFS-level entailment-rules OWL_DL_MEM_TRANS_INF OWL DL in-memory transitive class-hierarchy inference OWL_DL_MEM_RULE_INF OWL DL in-memory rule-based reasoner with OWL rules OWL_LITE_MEM OWL Lite in-memory none OWL_LITE_MEM_TRANS_INF OWL Lite in-memory transitive class-hierarchy inference OWL_LITE_MEM_RDFS_INF OWL Lite in-memory rule reasoner with RDFS-level entailment-rules OWL_LITE_MEM_RULES_INF OWL Lite in-memory rule-based reasoner with OWL rules DAML_MEM DAML+OIL in-memory none DAML_MEM_TRANS_INF DAML+OIL in-memory transitive class-hierarchy inference DAML_MEM_RDFS_INF DAML+OIL in-memory rule reasoner with RDFS-level entailment-rules DAML_MEM_RULE_INF DAML+OIL in-memory rule-based reasoner with DAML rules RDFS_MEM RDFS in-memory none RDFS_MEM_TRANS_INF RDFS in-memory transitive class-hierarchy inference RDFS_MEM_RDFS_INF RDFS in-memory rule reasoner with RDFS-level entailment-rules 30/03/2010 7
  • 8. Read a File into Ontology Model String fileName = "univ.owl"; try { File file = new File(fileName); FileReader reader = new FileReader(file); OntModel model = ModelFactory .createOntologyModel(OntModelSpec.OWL_DL_MEM); model.read(reader,null); model.write(System.out,"RDF/XML-ABBREV"); } catch (Exception e) { e.printStackTrace(); } 30/03/2010 8
  • 9. Reading an Existing Ontology Classes Creating a New Ontology Properties Individuales Retrieve Ontology Classes Iterator classIter = model.listClasses(); while (classIter.hasNext()) { OntClass ontClass = (OntClass) classIter.next(); String uri = ontClass.getURI(); if(uri != null) System.out.println(uri); }   We can also use ontClass.getLocalName() to get the class name only. If a class has no name (e.g. a restriction class), then ontClass.getURI() returns null. http://www.something.com/myontology#Professor http://www.something.com/myontology#Module http://www.something.com/myontology#Diploma http://www.something.com/myontology#Person http://www.something.com/myontology#Student 30/03/2010 output 9
  • 10. Reading an Existing Ontology Classes Creating a New Ontology Properties Individuales Retrieve a Specified Class  A specifed class is called by its URI String classURI = "http://www.something.com/myontology#Professor"; OntClass professor = model.getOntClass(classURI ); // ...  If we know the class name only, we can get its URI by concatenating the ontology URI with the class name: ClassURI = OntologyURI + ‘#’ + ClassName 30/03/2010 10
  • 11. Reading an Existing Ontology Classes Creating a New Ontology Properties Individuales Get the Ontology URI String ontologyURI = null; Iterator iter = model.listOntologies(); if(iter.hasNext()){ Ontology onto = (Ontology) iter.next(); ontologyURI = onto.getURI(); System.out.println("Ontology URI = "+ontologyURI); } String className = "Professor"; String classURI = ontologyURI+"#"+className; OntClass professor = model.getOntClass(classURI ); 30/03/2010 11
  • 12. Reading an Existing Ontology Classes Creating a New Ontology Properties Individuales Class Hierarchy OntClass student = model.getOntClass(uri+"#Student"); System.out.println(student.getSuperClass()); System.out.println(student.getSubClass()); http://www.something.com/myontology#Person null OntClass person = model.getOntClass(uri+"#Person"); System.out.println(person.getSuperClass()); System.out.println(person.getSubClass()); null http://www.something.com/myontology#Professor 30/03/2010 12
  • 13. Reading an Existing Ontology Classes Creating a New Ontology Properties Individuales Class Hierarchy Iterator supIter = person.listSuperClasses(); while (supIter.hasNext()) { OntClass sup = (OntClass) supIter.next(); System.out.println(sup); } System.out.println("----------------"); Iterator subIter = person.listSubClasses(); while (subIter.hasNext()) { OntClass sub = (OntClass) subIter.next(); System.out.println(sub); } ---------------http://www.something.com/myontology#Professor http://www.something.com/myontology#Student 30/03/2010 13
  • 14. Reading an Existing Ontology Creating a New Ontology Classes Properties Individuales Class Hierarchy true direct sub-classes false all sub-classes (default) true direct super-classes false all super-classes (default) listSubClasses(boolean) listSuperClasses(boolean) A B, C -> B, C, D D.listSuperClasses(true) -> C C D 30/03/2010 -> A.listSubClasses(false) B A.listSubClasses(true) D.listSuperClasses(false) -> C, A 14
  • 15. Reading an Existing Ontology Classes Creating a New Ontology Properties Individuales Class Hierarchy <OntClass>.isHierarchyRoot()  This function returns true if this class is the root of the class hierarchy in the model:   this class has owl:Thing as a direct super-class, or it has no declared super-classes owl:Thing Professor Person 30/03/2010 Module Diploma Student 15
  • 16. Reading an Existing Ontology Classes Creating a New Ontology Properties Individuales Intersection / Union / Complement if(ontClass.isIntersectionClass()){ IntersectionClass intersection = ontClass.asIntersectionClass(); RDFList operands = intersection.getOperands(); for (int i = 0; i < operands.size(); i++) { RDFNode rdfNode = operands.get(i); ... } } else if(ontClass.isUnionClass()){ UnionClass union = ontClass.asUnionClass(); RDFList operands = union.getOperands(); ... } else if(ontClass.isComplementClass()){ ComplementClass compl = ontClass.asComplementClass(); RDFList operands = compl.getOperands(); ... } 30/03/2010 16
  • 17. Reading an Existing Ontology Classes Creating a New Ontology Properties Individuales Retrieve the Properties of a Specified Class OntClass student = model.getOntClass(uri+"#Student"); Iterator propIter = student.listDeclaredProperties(); while (propIter.hasNext()) { OntProperty property = (OntProperty) propIter.next(); System.out.println(property.getLocalName()); } output studentNumber age personName enrolledIn email true directly associated properties false all properties (direct + inhereted) listDeclaredProperties(boolean) (default) 30/03/2010 17
  • 18. Reading an Existing Ontology Classes Creating a New Ontology Properties Individuales Property Types  1 Two ways to know whether a property is datatype property or object property: if(property.isObjectProperty()){ // ... this is an object property } else if (property.isDatatypeProperty()){ // ... this is an datatype property } property.isFunctionalProperty(); property.isSymmetricProperty(); property.isTransitiveProperty(); property.isInverseOf(anotherProperty); 30/03/2010 18
  • 19. Reading an Existing Ontology Classes Creating a New Ontology Properties Individuales Property Types  Two ways to know whether a property is datatype property or object property: Resource propertyType = property.getRDFType(); System.out.println(propertyType); 2 if(propertyType.equals(OWL.DatatypeProperty)){ // ... this is an datatype property } else if(propertyType.equals(OWL.ObjectProperty)){ // ... this is an object property } http://www.w3.org/2002/07/owl#DatatypeProperty http://www.w3.org/2002/07/owl#DatatypeProperty http://www.w3.org/2002/07/owl#DatatypeProperty http://www.w3.org/2002/07/owl#ObjectProperty http://www.w3.org/2002/07/owl#DatatypeProperty 30/03/2010 output 19
  • 20. Reading an Existing Ontology Classes Creating a New Ontology Properties Individuales Property Domain and Range String propertyName = property.getLocalName(); String dom = ""; String rng = ""; if(property.getDomain()!=null) dom = property.getDomain().getLocalName(); if(property.getRange()!=null) rng = property.getRange().getLocalName(); System.out.println(propertyName +": t("+dom+") t -> ("+rng+") "); listDeclaredProperties(boolean); true studentNumber: enrolledIn: (Student) (Student) false studentNumber: age: personName: enrolledIn: email: (Student) (Person) (Person) (Student) (Person) 30/03/2010 -> (string) -> (Diploma) -> -> -> -> -> output (string) (int) (string) (Diploma) (string) 20
  • 21. Reading an Existing Ontology Classes Creating a New Ontology Properties Individuales Datatype Properties  list all datatype properties in the model: Iterator iter = model.listDatatypeProperties(); while (iter.hasNext()) { DatatypeProperty prop = (DatatypeProperty) iter.next(); String propName = prop.getLocalName(); String dom = ""; String rng = ""; if(prop.getDomain()!=null) dom = prop.getDomain().getLocalName(); if(prop.getRange()!=null) rng = prop.getRange().getLocalName(); System.out.println(propName +": t("+dom+") t -> ("+rng+") "); } diplomaName: studentNumber: moduleName: age: personName: email: 30/03/2010 (Diploma) (Student) (Module) (Person) (Person) (Person) -> (string) -> (string) -> (string) -> (int) -> (string) -> (string) output 21
  • 22. Reading an Existing Ontology Classes Creating a New Ontology Properties Individuales Object Properties  list all object properties in the model: Iterator iter = model.listObjectProperties(); while (iter.hasNext()) { ObjectProperty prop = (ObjectProperty) iter.next(); String propName = prop.getLocalName(); String dom = ""; String rng = ""; if(prop.getDomain()!=null) dom = prop.getDomain().getLocalName(); if(prop.getRange()!=null) rng = prop.getRange().getLocalName(); System.out.println(propName +": t("+dom+") t -> ("+rng+") "); } enrolledIn: hasModule: taughtBy: teach: (Student) -> (Diploma) (Diploma) -> (Module) (Module) -> (Professor) (Professor) -> (Module) getInverse() 30/03/2010 → output (OntProperty) 22
  • 23. Reading an Existing Ontology Classes Creating a New Ontology Properties Individuales Other Types of Properties  Using the same way, we may list (iterate) other properties: model.listFunctionalProperties() model.listInverseFunctionalProperties() model.listSymmetricProperties() model.listTransitiveProperties() 30/03/2010 23
  • 24. Reading an Existing Ontology Classes Creating a New Ontology Properties Individuales Restrictions if(ontClass.isRestriction()){ Restriction rest = ontClass.asRestriction(); OntProperty onProperty = rest.getOnProperty(); ... } 30/03/2010 24
  • 25. Reading an Existing Ontology Classes Creating a New Ontology Properties Individuales AllValuesFrom / SomeValuesFrom if(rest.isAllValuesFromRestriction()){ AllValuesFromRestriction avfr = rest.asAllValuesFromRestriction(); Resource avf = avfr.getAllValuesFrom(); ... } if(rest.isSomeValuesFromRestriction()){ SomeValuesFromRestriction svfr = rest.asSomeValuesFromRestriction(); Resource svf = svfr.getSomeValuesFrom(); ... } 30/03/2010 25
  • 26. Reading an Existing Ontology Classes Creating a New Ontology Properties Individuales HasValue if(rest.isHasValueRestriction()){ HasValueRestriction hvr = rest.asHasValueRestriction(); RDFNode hv = hvr.getHasValue(); ... } 30/03/2010 26
  • 27. Reading an Existing Ontology Classes Creating a New Ontology Properties Individuales Cardinality / MinCardinality / MaxCardinality if(rest.isCardinalityRestriction()){ CardinalityRestriction cr = rest.asCardinalityRestriction(); int card = cr.getCardinality(); ... } if(rest.isMinCardinalityRestriction()){ MinCardinalityRestriction mcr = rest.asMinCardinalityRestriction(); int minCard = minCR.getMinCardinality(); ... } if(rest.isMaxCardinalityRestriction()){ MaxCardinalityRestriction mcr = rest.asMaxCardinalityRestriction(); int maxCard = maxCR.getMaxCardinality(); ... } 30/03/2010 27
  • 28. Reading an Existing Ontology Classes Creating a New Ontology Properties Individuales Individuals  list all individuals in the model Iterator individuals = model.listIndividuals(); while (individuals.hasNext()) { Individual individual = (Individual) individuals.next(); ... }  list individuals of a specific class Iterator iter = ontClass.listInstances(); while (iter.hasNext()) { Individual individual = (Individual) iter.next(); ... } 30/03/2010 28
  • 29. Reading an Existing Ontology Classes Creating a New Ontology Properties Individuales Individuals  list properties and property values of an individual Iterator props = individual.listProperties(); while (props.hasNext()) { Property property = (Property) props.next(); RDFNode value = individual.getPropertyValue(property); ... }  list values of a specific property of an individual Iterator values = individual.listPropertyValues(property); while (values.hasNext()) { RDFNode value = values.next(); ... } 30/03/2010 29
  • 30. Reading an Existing Ontology Classes Creating a New Ontology Properties Individuales Individuals  get the class to which an individual belongs Resource rdfType = individual.getRDFType(); OntClass ontClass = model.getOntClass(rdfType.getURI());  in recent versions of Jena OntClass ontClass = individual.getOntClass(); 30/03/2010 30
  • 31. Outline   What is Jena ? Reading an Existing Ontology     Classes Properties Individuals Creating a New Ontology    30/03/2010 Classes Properties Individuales 31
  • 32. Creating a New Ontology Reading an Existing Ontology Classes Properties Individuales Create the ontology model OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM); String uriBase = "http://www.something.com/myontology"; model.createOntology(uriBase); 30/03/2010 32
  • 33. Creating a New Ontology Reading an Existing Ontology Classes Properties Individuales Create Classes //create OntClass OntClass OntClass OntClass OntClass 30/03/2010 classes person = model.createClass(uriBase+"#Person"); module = model.createClass(uriBase+"#Module"); diploma = model.createClass(uriBase+"#Diploma"); student = model.createClass(uriBase+"#Student"); professor = model.createClass(uriBase+"#Professor"); 33
  • 34. Creating a New Ontology Reading an Existing Ontology Classes Properties Individuales Set Sub-Classes //set sub-classes person.addSubClass(student); person.addSubClass(professor); 30/03/2010 34
  • 35. Creating a New Ontology Reading an Existing Ontology Classes Properties Individuales Intersection / Union / Complement RDFNode[] classes = new RDFNode[] {class1, class2, ...}; RDFList list = model.createList(classes); IntersectionClass intersection = model.createIntersectionClass(uri, list); UnionClass union = model.createUnionClass(uri, list); ComplementClass compl = model.createComplementClass(uri, resource); 30/03/2010 35
  • 36. Creating a New Ontology Reading an Existing Ontology Classes Properties Individuales Create Datatype Properties //create datatype properties DatatypeProperty personName = model.createDatatypeProperty(uriBase+"#personName"); personName.setDomain(person); personName.setRange(XSD.xstring); DatatypeProperty age = model.createDatatypeProperty(uriBase+"#age"); age.setDomain(person); age.setRange(XSD.integer); // ... ... 30/03/2010 36
  • 37. Creating a New Ontology Reading an Existing Ontology Classes Properties Individuales Create Object Properties //create object properties ObjectProperty teach = model.createObjectProperty(uriBase+"#teach"); teach.setDomain(professor); teach.setRange(module); ObjectProperty taughtBy = model.createObjectProperty(uriBase+"#taughtBy"); taughtBy.setDomain(module); taughtBy.setRange(professor); ObjectProperty enrolledIn = model.createObjectProperty(uriBase+"#enrolledIn"); enrolledIn.setDomain(student); enrolledIn.setRange(diploma); // ... ... 30/03/2010 37
  • 38. Creating a New Ontology Reading an Existing Ontology Classes Properties Individuales Inverse-of / Functional  set inverse properties // set inverse properties teach.setInverseOf(taughtBy);  set functional properties // set functional properties enrolledIn.setRDFType(OWL.FunctionalProperty); 30/03/2010 38
  • 39. Creating a New Ontology Reading an Existing Ontology Classes Properties Individuales Restrictions usually, null usually, a class SomeValuesFromRestriction svfr = model.createSomeValuesFromRestriction(uri, property, resource); AllValuesFromRestriction avfr = model.createAllValuesFromRestriction(uri, property, resource); HasValueRestriction hvr = model.createHasValueRestriction(uri, property, rdfNode); CardinalityRestriction cr = model.createCardinalityRestriction(uri, property, int); MinCardinalityRestriction min_cr = model.createMinCardinalityRestriction(uri, property, int); MaxCardinalityRestriction max_cr = model.createMaxCardinalityRestriction(uri, property, int); 30/03/2010 39
  • 40. Creating a New Ontology Reading an Existing Ontology Classes Properties Individuales Individuales  create an individual Individual indv = ontClass.createIndividual(uri); or Individual indv = model.createIndividual(uri, ontClass);  set a property value of an individual indv.setPropertyValue(property, rdfNode); 30/03/2010 40
  • 41. Write Ontology Model  write the model to standard output // write out the model model.write(System.out,"RDF/XML-ABBREV");  save the model to a string StringWriter sw = new StringWriter(); model.write(sw, "RDF/XML-ABBREV"); String owlCode = sw.toString(); 30/03/2010 41
  • 42. Save the Model to a File File file = new File(filePath); try{ FileWriter fw = new FileWriter(file); fw.write(owlCode); fw.close(); } catch(FileNotFoundException fnfe){ fnfe.printStackTrace(); } catch(IOException ioe){ ioe.printStackTrace(); } 30/03/2010 42
  • 43. References  Jena Ontology API   http://jena.sourceforge.net/ontology/index.html RDFS and OWL Reasoning Capabilities:  http://www-ksl.stanford.edu/software/jtp/doc/owl-reasoning.html 30/03/2010 43