java and owl

43
Processing OWL Ontologies using Java Raji GHAWI 30/03/2010 Jena Ontology API

Post on 19-Oct-2014

493 views

Category:

Technology


4 download

DESCRIPTION

A tutorial on how to use Java to process (read, write) OWL1 ontologies using Jena API.

TRANSCRIPT

Page 1: Java and OWL

Processing OWL Ontologies using Java

Raji GHAWI

30/03/2010

Jena Ontology API

Page 2: Java and OWL

230/03/2010

Outline

What is Jena ? Reading an Existing Ontology

Classes Properties Individuals

Creating a New Ontology Classes Properties Individuales

Page 3: Java and OWL

330/03/2010

What is Jena ?

Jena is a Java framework for building Semantic Web applications includes

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/

Page 4: Java and OWL

430/03/2010

hasModule

Person

Object property

subClassOf

Class

Datatype property

StudentProfessor

DiplomaModule

name

age

moduleName diplomaName

studentNumber

enrolledIn

email

taughtBy teach

Example Ontology

Page 5: Java and OWL

530/03/2010

Outline

What is Jena ? Reading an Existing Ontology

Classes Properties Individuals

Creating a New Ontology Classes Properties Individuales

Page 6: Java and OWL

630/03/2010

Create Ontology Model

OntModel model = ModelFactory.createOntologyModel();OntModel model = ModelFactory.createOntologyModel();

Jena provides an ontology model that allows to specify: Ontology language Storage model Inference mode

default settings: OWL-Full language in-memory storage RDFS inference

Page 7: Java and OWL

730/03/2010

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

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

Page 8: Java and OWL

830/03/2010

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();}

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();}

Page 9: Java and OWL

930/03/2010

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);}

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#Professorhttp://www.something.com/myontology#Modulehttp://www.something.com/myontology#Diplomahttp://www.something.com/myontology#Personhttp://www.something.com/myontology#Student

output

Reading an Existing Ontology Creating a New Ontology

Classes Properties Individuales

Page 10: Java and OWL

1030/03/2010

Retrieve a Specified Class

String classURI = "http://www.something.com/myontology#Professor";

OntClass professor = model.getOntClass(classURI );// ...

String classURI = "http://www.something.com/myontology#Professor";

OntClass professor = model.getOntClass(classURI );// ...

A specifed class is called by its URI

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

ClassURI = OntologyURI + ‘#’ + ClassName

Reading an Existing Ontology Creating a New Ontology

Classes Properties Individuales

Page 11: Java and OWL

1130/03/2010

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

String className = "Professor";String classURI = ontologyURI+"#"+className;

OntClass professor = model.getOntClass(classURI );

Reading an Existing Ontology Creating a New Ontology

Classes Properties Individuales

Page 12: Java and OWL

1230/03/2010

http://www.something.com/myontology#Personnull

nullhttp://www.something.com/myontology#Professor

OntClass student = model.getOntClass(uri+"#Student");System.out.println(student.getSuperClass());System.out.println(student.getSubClass());

OntClass student = model.getOntClass(uri+"#Student");System.out.println(student.getSuperClass());System.out.println(student.getSubClass());

OntClass person = model.getOntClass(uri+"#Person");System.out.println(person.getSuperClass());System.out.println(person.getSubClass());

OntClass person = model.getOntClass(uri+"#Person");System.out.println(person.getSuperClass());System.out.println(person.getSubClass());

Class Hierarchy

Reading an Existing Ontology Creating a New Ontology

Classes Properties Individuales

Page 13: Java and OWL

1330/03/2010

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);}

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);}

Class Hierarchy

----------------http://www.something.com/myontology#Professorhttp://www.something.com/myontology#Student

Reading an Existing Ontology Creating a New Ontology

Classes Properties Individuales

Page 14: Java and OWL

1430/03/2010

Class Hierarchy

listSubClasses(boolean) true direct sub-classes

false all sub-classes (default)

listSuperClasses(boolean) true direct super-classes

false all super-classes (default)

A

B C

D

A.listSubClasses(true) -> B, C

A.listSubClasses(false) -> B, C, D

D.listSuperClasses(true) -> C

D.listSuperClasses(false) -> C, A

Reading an Existing Ontology Creating a New Ontology

Classes Properties Individuales

Page 15: Java and OWL

1530/03/2010

Class Hierarchy

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

<OntClass>.isHierarchyRoot()

Person Student

Module Diploma

owl:Thing

Professor

Reading an Existing Ontology Creating a New Ontology

Classes Properties Individuales

Page 16: Java and OWL

1630/03/2010

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(); ...}

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(); ...}

Intersection / Union / Complement

Reading an Existing Ontology Creating a New Ontology

Classes Properties Individuales

Page 17: Java and OWL

1730/03/2010

OntClass student = model.getOntClass(uri+"#Student");

Iterator propIter = student.listDeclaredProperties();while (propIter.hasNext()) { OntProperty property = (OntProperty) propIter.next(); System.out.println(property.getLocalName());}

OntClass student = model.getOntClass(uri+"#Student");

Iterator propIter = student.listDeclaredProperties();while (propIter.hasNext()) { OntProperty property = (OntProperty) propIter.next(); System.out.println(property.getLocalName());}

Retrieve the Properties of a Specified Class

studentNumberagepersonNameenrolledInemail

output

listDeclaredProperties(boolean)

true directly associated properties

false all properties (direct + inhereted)

(default)

Reading an Existing Ontology Creating a New Ontology

Classes Properties Individuales

Page 18: Java and OWL

1830/03/2010

Property Types

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}

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

1

Reading an Existing Ontology Creating a New Ontology

Classes Properties Individuales

Page 19: Java and OWL

1930/03/2010

Property Types

Two ways to know whether a property is datatype property or object property:

Resource propertyType = property.getRDFType();System.out.println(propertyType);

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

Resource propertyType = property.getRDFType();System.out.println(propertyType);

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

2

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

output

Reading an Existing Ontology Creating a New Ontology

Classes Properties Individuales

Page 20: Java and OWL

2030/03/2010

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+") ");

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+") ");

studentNumber: (Student) -> (string) enrolledIn: (Student) -> (Diploma)

output

listDeclaredProperties(boolean);

studentNumber: (Student) -> (string) age: (Person) -> (int) personName: (Person) -> (string) enrolledIn: (Student) -> (Diploma) email: (Person) -> (string)

true

false

Reading an Existing Ontology Creating a New Ontology

Classes Properties Individuales

Page 21: Java and OWL

2130/03/2010

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+") ");}

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: (Diploma) -> (string) studentNumber: (Student) -> (string) moduleName: (Module) -> (string) age: (Person) -> (int) personName: (Person) -> (string) email: (Person) -> (string)

output

Reading an Existing Ontology Creating a New Ontology

Classes Properties Individuales

Page 22: Java and OWL

2230/03/2010

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+") ");}

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: (Student) -> (Diploma) hasModule: (Diploma) -> (Module) taughtBy: (Module) -> (Professor) teach: (Professor) -> (Module)

output

→ (OntProperty)

Reading an Existing Ontology Creating a New Ontology

Classes Properties Individuales

getInverse()

Page 23: Java and OWL

2330/03/2010

Other Types of Properties

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

Reading an Existing Ontology Creating a New Ontology

Classes Properties Individuales

model.listFunctionalProperties()model.listFunctionalProperties()

model.listInverseFunctionalProperties()model.listInverseFunctionalProperties()

model.listSymmetricProperties()model.listSymmetricProperties()

model.listTransitiveProperties()model.listTransitiveProperties()

Page 24: Java and OWL

2430/03/2010

Restrictions

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

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

Reading an Existing Ontology Creating a New Ontology

Classes Properties Individuales

Page 25: Java and OWL

2530/03/2010

AllValuesFrom / SomeValuesFrom

if(rest.isAllValuesFromRestriction()){ AllValuesFromRestriction avfr = rest.asAllValuesFromRestriction(); Resource avf = avfr.getAllValuesFrom(); ...}

if(rest.isSomeValuesFromRestriction()){ SomeValuesFromRestriction svfr = rest.asSomeValuesFromRestriction(); Resource svf = svfr.getSomeValuesFrom(); ...}

if(rest.isAllValuesFromRestriction()){ AllValuesFromRestriction avfr = rest.asAllValuesFromRestriction(); Resource avf = avfr.getAllValuesFrom(); ...}

if(rest.isSomeValuesFromRestriction()){ SomeValuesFromRestriction svfr = rest.asSomeValuesFromRestriction(); Resource svf = svfr.getSomeValuesFrom(); ...}

Reading an Existing Ontology Creating a New Ontology

Classes Properties Individuales

Page 26: Java and OWL

2630/03/2010

HasValue

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

}

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

}

Reading an Existing Ontology Creating a New Ontology

Classes Properties Individuales

Page 27: Java and OWL

2730/03/2010

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(); ...}

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(); ...}

Reading an Existing Ontology Creating a New Ontology

Classes Properties Individuales

Page 28: Java and OWL

2830/03/2010

Individuals

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

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

Iterator iter = ontClass.listInstances();while (iter.hasNext()) {

Individual individual = (Individual) iter.next();...

}

Iterator iter = ontClass.listInstances();while (iter.hasNext()) {

Individual individual = (Individual) iter.next();...

}

list all individuals in the model

list individuals of a specific class

Reading an Existing Ontology Creating a New Ontology

Classes Properties Individuales

Page 29: Java and OWL

2930/03/2010

Individuals

Iterator props = individual.listProperties();while (props.hasNext()) {

Property property = (Property) props.next();RDFNode value = individual.getPropertyValue(property);...

}

Iterator props = individual.listProperties();while (props.hasNext()) {

Property property = (Property) props.next();RDFNode value = individual.getPropertyValue(property);...

}

list properties and property values of an individual

Iterator values = individual.listPropertyValues(property);while (values.hasNext()) {

RDFNode value = values.next();...

}

Iterator values = individual.listPropertyValues(property);while (values.hasNext()) {

RDFNode value = values.next();...

}

list values of a specific property of an individual

Reading an Existing Ontology Creating a New Ontology

Classes Properties Individuales

Page 30: Java and OWL

3030/03/2010

Individuals

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

get the class to which an individual belongs

OntClass ontClass = individual.getOntClass();OntClass ontClass = individual.getOntClass();

in recent versions of Jena

Reading an Existing Ontology Creating a New Ontology

Classes Properties Individuales

Page 31: Java and OWL

3130/03/2010

Outline

What is Jena ? Reading an Existing Ontology

Classes Properties Individuals

Creating a New Ontology Classes Properties Individuales

Page 32: Java and OWL

3230/03/2010

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

String uriBase = "http://www.something.com/myontology";

model.createOntology(uriBase);

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

String uriBase = "http://www.something.com/myontology";

model.createOntology(uriBase);

Reading an Existing Ontology Creating a New OntologyClasses Properties Individuales

Create the ontology model

Page 33: Java and OWL

3330/03/2010

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

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

Reading an Existing Ontology Creating a New OntologyClasses Properties Individuales

Create Classes

Page 34: Java and OWL

3430/03/2010

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

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

Reading an Existing Ontology Creating a New OntologyClasses Properties Individuales

Set Sub-Classes

Page 35: Java and OWL

3530/03/2010

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

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

Intersection / Union / Complement

Reading an Existing Ontology Creating a New OntologyClasses Properties Individuales

Page 36: Java and OWL

3630/03/2010

//create datatype propertiesDatatypeProperty 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);

// ... ...

//create datatype propertiesDatatypeProperty 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);

// ... ...

Reading an Existing Ontology Creating a New OntologyClasses Properties Individuales

Create Datatype Properties

Page 37: Java and OWL

3730/03/2010

//create object propertiesObjectProperty 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);

// ... ...

//create object propertiesObjectProperty 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);

// ... ...

Reading an Existing Ontology Creating a New OntologyClasses Properties Individuales

Create Object Properties

Page 38: Java and OWL

3830/03/2010

set inverse properties

// set inverse propertiesteach.setInverseOf(taughtBy);// set inverse propertiesteach.setInverseOf(taughtBy);

// set functional propertiesenrolledIn.setRDFType(OWL.FunctionalProperty);// set functional propertiesenrolledIn.setRDFType(OWL.FunctionalProperty);

set functional properties

Reading an Existing Ontology Creating a New OntologyClasses Properties Individuales

Inverse-of / Functional

Page 39: Java and OWL

3930/03/2010

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

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

Restrictions

usually, null usually, a class

Reading an Existing Ontology Creating a New OntologyClasses Properties Individuales

Page 40: Java and OWL

4030/03/2010

Individual indv = ontClass.createIndividual(uri);Individual indv = ontClass.createIndividual(uri);

Individuales

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

indv.setPropertyValue(property, rdfNode);indv.setPropertyValue(property, rdfNode);

create an individual

or

set a property value of an individual

Reading an Existing Ontology Creating a New OntologyClasses Properties Individuales

Page 41: Java and OWL

4130/03/2010

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

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

write the model to standard output

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

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

save the model to a string

Write Ontology Model

Page 42: Java and OWL

4230/03/2010

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();}

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();}

Save the Model to a File

Page 43: Java and OWL

4330/03/2010

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