• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Java and OWL
 

Java and OWL

on

  • 302 views

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

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

Statistics

Views

Total Views
302
Views on SlideShare
302
Embed Views
0

Actions

Likes
2
Downloads
30
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Java and OWL Java and OWL Presentation Transcript

    • 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