Sunteți pe pagina 1din 37

From Ideas to Ontologies

From Ideas to Ontologies

Daniele Francesco Santamaria

Department of Mathematics and Computer Science, University of Catania

Lecture for the course of Web Reasoning, 2018


Part III - Coding

Daniele Francesco Santamaria - Course of Web Reasoning Department of Maths and CS - Univ. of Catania 1 / 37
Motivation
From Ideas to Ontologies
Get Started

Motivation

Programmatically creating ontologies.


Programmatically modifying ontologies.
Programmatically querying ontologies.
Integrating ontologies with (web) applications.

Daniele Francesco Santamaria - Course of Web Reasoning Department of Maths and CS - Univ. of Catania 2 / 37
Motivation
From Ideas to Ontologies
Get Started

Scenario

Daniele Francesco Santamaria - Course of Web Reasoning Department of Maths and CS - Univ. of Catania 3 / 37
Motivation
From Ideas to Ontologies
Get Started

JAVA OWL-API

What is needed.
Java 8.
Java Editor.
OWL API 5.1.4.
Basic knowledge of Software Project Management and
Comprehension (SPMCF) system (Maven) and of a Distributed
Version Controm (DVC) system (GIT) is HIGHLY encouraged.

Daniele Francesco Santamaria - Course of Web Reasoning Department of Maths and CS - Univ. of Catania 4 / 37
Motivation
From Ideas to Ontologies
Get Started

Maven POM

Daniele Francesco Santamaria - Course of Web Reasoning Department of Maths and CS - Univ. of Catania 5 / 37
Motivation
From Ideas to Ontologies
Get Started

JAVA OWL-API

Ontology manipulation in OWL-API is all about:


OWLEntity interface, anything that can be identified with an
IRI:
OWLIndividual;
OWLAnonymousIndividual;
OWLClassExpression;
OWLPropertyExpression;
OWLAnnotation;
OWLAnnotationProperty;
OWLAnnotationValue;

Daniele Francesco Santamaria - Course of Web Reasoning Department of Maths and CS - Univ. of Catania 6 / 37
Motivation
From Ideas to Ontologies
Get Started

JAVA OWL-API

Ontology manipulation in OWL-API is all about:


OWLAxiom class, the basic unity.
OWLOntology interface.
It models a set of logical and non-logical OWLAxioms, with a
name (an IRI), an (optional) physical location and
convenience methods to retrieve such axioms.
OWLOntologyManager interface.
It is used to manage ontologies.
OWLDataFactory interface.
It is used to manipulate axioms.

Daniele Francesco Santamaria - Course of Web Reasoning Department of Maths and CS - Univ. of Catania 7 / 37
Motivation
From Ideas to Ontologies
Get Started

JAVA OWL-API

Let’s start by creating an ontology manager.


OWLOntologyManager manager;
manager=OWLManager.createOWLOntologyManager();

Daniele Francesco Santamaria - Course of Web Reasoning Department of Maths and CS - Univ. of Catania 8 / 37
Motivation
From Ideas to Ontologies
Get Started

JAVA OWL-API

Load an OWLOntology.
From File:
OWLOntology ontology;
ontology=manager.loadOntologyFromOntologyDocument(
new File(''ontology.owl''));
Remember to manage the exceptions
FileNotFoundException and
OWLOntologyCreationException.

Daniele Francesco Santamaria - Course of Web Reasoning Department of Maths and CS - Univ. of Catania 9 / 37
Motivation
From Ideas to Ontologies
Get Started

JAVA OWL-API

Load an OWLOntology.
From Web:
String iri=
''http://www.w3.org/2009/08/ontology.owl'';
IRI docIRI = IRI.create(iri);
OWLOntology ontology =
manager.loadOntologyFromOntologyDocument(docIRI);
Remember to manage the exception
OWLOntologyCreationException.

Create an empty ontology:


OWLOntology ontology=manager.createOntology(iri);
or
OWLOntologyID id=new OWLOntologyID(iri, versionIRI));
OWLOntology ontology=manager.createOntology(id);
Daniele Francesco Santamaria - Course of Web Reasoning Department of Maths and CS - Univ. of Catania 10 / 37
Motivation
From Ideas to Ontologies
Get Started

JAVA OWL-API

Save a local copy of the ontology.


File file = new File(''copy.owl '');
manager.saveOntology(ontology, IRI.create(file.toURI()));
Remember to manage the exception
OWLOntologyStorageException.
The ontology is saved in the same format from which it is
loaded.
Change the format to OWL/XML:
OWLOntologyFormat format =
new OWLXMLOntologyFormat();
manager.saveOntology(ontology, format);

Daniele Francesco Santamaria - Course of Web Reasoning Department of Maths and CS - Univ. of Catania 11 / 37
Motivation
From Ideas to Ontologies
Get Started

JAVA OWL-API

Save a local copy of the ontology.


File file = new File(''copy.owl '');
manager.saveOntology(ontology, IRI.create(file.toURI()));
Remember to manage the exception
OWLOntologyStorageException.
The ontology is saved in the same format from which it is
loaded.
Change the format to OWL/XML:
OWLOntologyFormat format =
new OWLXMLOntologyFormat();
manager.saveOntology(ontology, format);
Remove the ontology:
manager.remove(ontology);

Daniele Francesco Santamaria - Course of Web Reasoning Department of Maths and CS - Univ. of Catania 12 / 37
Motivation
From Ideas to Ontologies
Get Started

JAVA OWL-API

OWLOntology represents an OWL 2 Ontology.


It consists of a possibly empty set of OWLAxioms and a
possibly empty set of OWLAnnotations.
An ontology can have an ontology IRI which can be used to
identify the ontology.
If it has an ontology IRI then it may also have an ontology
version IRI.

Daniele Francesco Santamaria - Course of Web Reasoning Department of Maths and CS - Univ. of Catania 13 / 37
Motivation
From Ideas to Ontologies
Get Started

JAVA OWL-API

Some OWLOntology tool.


getOWLOntologyManager() retrieves the associated manager.
imports() gets the transitive closure of directly import
relation.
isDeclared(OWLEntity owlEntity) checks if owlEntity is
declared.
addAxiom(OWLAxiom axiom) adds the given axiom.
removeAxiom(OWLAxiom axiom) removes the given axioms.
getOntoloyID() retrieves the ID of the ontology.

Daniele Francesco Santamaria - Course of Web Reasoning Department of Maths and CS - Univ. of Catania 14 / 37
Motivation
From Ideas to Ontologies
Get Started

JAVA OWL-API

Create the data-factory associated to the manager.


OWLDataFactory datafact=manager.getOWLDataFactory();
It is an interface for manipulating every aspect of the ontology.
For example, for creating entities, class expressions and
axioms,
And for managing the set of ontology imports.

Daniele Francesco Santamaria - Course of Web Reasoning Department of Maths and CS - Univ. of Catania 15 / 37
Motivation
From Ideas to Ontologies
Get Started

JAVA OWL-API

Import ontology from IRI.


OWLImportsDeclaration importDeclaration=
datafact.getOWLImportsDeclaration(IRI.create(ontoIRI));
//ontoIRI is the IRI of the ontology to be added.
m.applyChange(new AddImport(ontology, importDeclaration));
Or from a local file:
OWLImportsDeclaration importDeclaration=
datafact.getOWLImportsDeclaration(onto2.getOntologyID().
getOntologyIRI().get());
Retrieve imports set
Stream <OWLImportsDeclaration>impDecs=
ontology.importsDeclarations();
impDecs.forEach(a->System.out.println(
a.getIRI().toString()));
Java 8 lambdas (and Stream)!

Daniele Francesco Santamaria - Course of Web Reasoning Department of Maths and CS - Univ. of Catania 16 / 37
Motivation
From Ideas to Ontologies
Get Started

Java Labdas

Daniele Francesco Santamaria - Course of Web Reasoning Department of Maths and CS - Univ. of Catania 17 / 37
Motivation
From Ideas to Ontologies
Get Started

Java Labdas

Daniele Francesco Santamaria - Course of Web Reasoning Department of Maths and CS - Univ. of Catania 18 / 37
Motivation
From Ideas to Ontologies
Get Started

JAVA OWL-API

IRI mapper can be used to provide a redirection mechanism


among local and remote ontologies.
This means that ontologies can be loaded as if they were
located on the web.
File output = new File( ''ontologie/localOntology.owl'');
IRI documentIRI = IRI.create(output);
IRI remoteIRI= IRI.create(remoteIRI);
SimpleIRIMapper mapper = new SimpleIRIMapper(remoteIRI,
documentIRI);
manager.getIRIMappers().add(mapper);
OWLOntology ontology = manager.loadOntology(remoteIRI);
manager.saveOntology(ontology,
new OWLXMLDocumentFormat());

Daniele Francesco Santamaria - Course of Web Reasoning Department of Maths and CS - Univ. of Catania 19 / 37
Motivation
From Ideas to Ontologies
Get Started

JAVA OWL-API

Creating entities
Creating Classes
OWLClass person = df.getOWLClass(IOR+ ''#Person'');
OWLDeclarationAxiom da = df.getOWLDeclarationAxiom(
person);
ChangeApplied ca=o.add(da);
or
manager.addAxiom(ontology, da);
ChangeApplied is used to check the change.
Getting axioms and logical axioms:
ontology.axioms();
ontology.logicalAxioms();
manager.remove(axiom) removes the axiom.

Daniele Francesco Santamaria - Course of Web Reasoning Department of Maths and CS - Univ. of Catania 20 / 37
Motivation
From Ideas to Ontologies
Get Started

JAVA OWL-API

Creating entities
Creating Properties
OWLObjectProperty r = df.getOWLObjectProperty(IOR +
''#R'');
OWLDataProperty p= df.getOWLDataProperty(IOR+ ''#P'');
Creating Individuals
OWLIndividual i= df.getOWLNamedIndividual(IOR+ ''#I '');
o.add(df.getOWLDeclarationAxiom( (OWLEntity) i));
Class assertions
df.getOWLClassAssertionAxiom(classexpr, ind);
Creating Annotations
OWLAnnotation c= df.getOWLAnnotation(
df.getRDFSComment(), df.getOWLLiteral(''c'', ''en ''));
OWLAxiom a = df.getOWLAnnotationAssertionAxiom(
p.getIRI(), c);
Daniele Francesco Santamaria - Course of Web Reasoning Department of Maths and CS - Univ. of Catania 21 / 37
Motivation
From Ideas to Ontologies
Get Started

JAVA OWL-API

Creating subclasses
OWLSubClassOfAxiom sca=df.getOWLSubClassOfAxiom(sub,
sup);
Creating subproperty
OWLSubObjectPropertyOfAxiom
sop=df.getOWLSubObjectPropertyOfAxiom(sub, sup);
Domain restriction
df.getOWLObjectPropertyDomainAxiom(prop, class);
Data range restriction
df.getOWLDataPropertyRangeAxiom(p,
df.getStringOWLDatatype());
OWLDataRange rg= df.getOWLDataOneOf(
df.getOWLLiteral(''a''), df.getOWLLiteral(''b''));

Daniele Francesco Santamaria - Course of Web Reasoning Department of Maths and CS - Univ. of Catania 22 / 37
Motivation
From Ideas to Ontologies
Get Started

JAVA OWL-API

Restrictions
OWLSubClassOfAxiom ax = df.getOWLSubClassOfAxiom(
df.getOWLObjectSomeValuesFrom(R, A),
df.getOWLObjectSomeValuesFrom(S, B));

Daniele Francesco Santamaria - Course of Web Reasoning Department of Maths and CS - Univ. of Catania 23 / 37
Motivation
From Ideas to Ontologies
Get Started

JAVA OWL-API

Exploring ontology
o.axioms()
Several options admitted.
o.axioms(AxiomtType T).
o.getLogicalAxioms().
Only logical axioms.
Axioms managing, more control
AddAxiom ac = new AddAxiom(o, axiom);
ac.isAxiomChange, isAddAxiom ...

Daniele Francesco Santamaria - Course of Web Reasoning Department of Maths and CS - Univ. of Catania 24 / 37
Motivation
From Ideas to Ontologies
Get Started

OWL-API and Reasoners

Hermit Reasoner.
Configuration config= new Configuration();
Reasoner reasoner= new Reasoner(config, ontology);
reasoner.classifyClasses();
reasoner.classifyDataProperties();
reasoner.classifyObjectProperties();
System.out.println(reasoner.isConsistent());

Daniele Francesco Santamaria - Course of Web Reasoning Department of Maths and CS - Univ. of Catania 25 / 37
Motivation
From Ideas to Ontologies
Get Started

OWL-API and Reasoners

Pellet Reasoner can be used with the last version of OWL API
via an open-source fork of the project called ''openllet-pellint''
OpenlletReasoner reasoner = OpenlletReasonerFac-
tory.getInstance().createReasoner(ontology);
System.out.println(reasonerP.isConsistent());
reasoner.getKB().realize();
reasoner.getKB().printClassTree();

Daniele Francesco Santamaria - Course of Web Reasoning Department of Maths and CS - Univ. of Catania 26 / 37
Motivation
From Ideas to Ontologies
Get Started

OWL-API and Reasoners

Get the inferences set


InferredOntologyGenerator iog = new
InferredOntologyGenerator (reasonerP);
OWLOntology inferredOntology =
manager.createOntology(aNewIri);
iog.fillOntology(datafactory, inferredOntology);
manager.saveOntology(inferredOntology, new
OWLXMLDocumentFormat(), IRI.create(infFile.toURI()));

Daniele Francesco Santamaria - Course of Web Reasoning Department of Maths and CS - Univ. of Catania 27 / 37
Motivation
From Ideas to Ontologies
Get Started

OWL-API and Reasoners

Hermit Reasoner:
Configuration configuration = new Configuration();
...
ReasonerFactory rf = new ReasonerFactory();
OWLReasoner reasoner = rf.createReasoner(ontology,
configuration);
List <InferredAxiomGenerator <? extends OWLAxiom>>
generators = new ArrayList <>();
generators.add...
List <InferredIndividualAxiomGenerator <? extends
OWLIndividualAxiom >>individualAxioms =
new ArrayList <>();
generators.addAll(individualAxioms);
InferredOntologyGenerator iog = ...

Daniele Francesco Santamaria - Course of Web Reasoning Department of Maths and CS - Univ. of Catania 28 / 37
Motivation
From Ideas to Ontologies
Get Started

OWL-API and Reasoners

In case you prefer a non-compatible reasoner.


Use OWL-API
Write the ontology in any serialization.
Check the ontology consistency with the reasoner.
Export all inferences.
Reload the inferred ontology with OWL-API.
In most of the cases it is sufficient.

Daniele Francesco Santamaria - Course of Web Reasoning Department of Maths and CS - Univ. of Catania 29 / 37
Motivation
From Ideas to Ontologies
Get Started

JENA

JENA
OntModel m = ModelFactory.createOntologyModel(
OntModelSpec.OWL MEM, null);
OntDocumentManager dm = m.getDocumentManager();
FileManager fm=dm.getFileManager();
OntClass students = m.createClass( unictIRI + ''Students '');
ObjectProperty attendsCourse = m.createObjectProperty(
unictIRI + ''attendsCourse'');
DatatypeProperty hasSurname = m.createDatatypeProperty(
unictIRI + ''hasSurname'');
attendsLecture.addRange(courses);
Individual webreasoning= m.createIndividual(unictIRI +
''webreasoning'', courses);
m.add(dfsantamaria, hasSurname,
ResourceFactory.createStringLiteral(''Santamaria''));

Daniele Francesco Santamaria - Course of Web Reasoning Department of Maths and CS - Univ. of Catania 30 / 37
Motivation
From Ideas to Ontologies
Get Started

JENA

JENA
SomeValuesFromRestriction gst =
m.createSomeValuesFromRestriction(
null, attendsCourse, courses);
OntClass goodStudent= m.createClass(unictIRI +
''goodStudent'');
goodStudents.addSuperClass(gst);
goodStudents.addSuperClass(students);

Daniele Francesco Santamaria - Course of Web Reasoning Department of Maths and CS - Univ. of Catania 31 / 37
Motivation
From Ideas to Ontologies
Get Started

JENA

SPARQL Query
Query query = QueryFactory.create(queryString);
QueryExecution qexec = QueryExecutionFactory.create
(query, m);
ResultSet rs = qexec.execSelect() ;
while(rs.hasNext()){
QuerySolution qs = rs.nextSolution();
...
RDFNode nodeRes = qs.get(variableName);
...
String s=nodeRes.asLiteral().toString();
}
Resource z = (Resource) qs.getResource(variableName);
Resource z = nodeRes.asResource();
SPARQL Update
UpdateAction.parseExecute(updateQ, model);

Daniele Francesco Santamaria - Course of Web Reasoning Department of Maths and CS - Univ. of Catania 32 / 37
Motivation
From Ideas to Ontologies
Get Started

SPARQL Query
Evaluating SPARQL queries in OWL-API is not
straightforward.
The SPARQL is very firmly rooted in RDF (as Jena API).
OWL-API is OWL-centric.
OWL can be serialized using RDF since OWL builds on top of
the (revised) RDF specifications, so it’s feasible to create a
wrapper layer over an RDF graph.
At the time of writing, Jena supports only OWL 1.1.
At the time of writing, there is no SPARQL implementation
over OWL-API.
The structural (OWL) and graph (Jena) model do not coexist.
How vs What.
From the structural model (under the implementation of
OWL-API) to the graph model (under the implementation of
Jena) and vice-versa can be a solution.
Shortly, it is matter of implementation, not theory.
ONT-API is an effort in this sense.
Daniele Francesco Santamaria - Course of Web Reasoning Department of Maths and CS - Univ. of Catania 33 / 37
Motivation
From Ideas to Ontologies
Get Started

ONTAPI

Version 1.1.0 available (at time of writing).


What do you need?
MAVEN (of course) with the following artifacts.
owlapi, v 5.4.1
owlapi-parsers, v. 5.4.1.
jena-arq, v. 3.6.0.
JENA/OWL compatible reasoner (depending on your needs).

Daniele Francesco Santamaria - Course of Web Reasoning Department of Maths and CS - Univ. of Catania 34 / 37
Motivation
From Ideas to Ontologies
Get Started

ONTAPI

Interacting with OWL-API.


OWLOntologyManager manager =
OntManagers.createONT();
OWLDataFactory factory = manager.getOWLDataFactory();
OWLOntology ontology=
manager.loadOntologyFromOntologyDocument(
new File(''ont.owl''));
Then, with JENA.
Model model = ((OntologyModel)ontology).asGraphModel();
OntologyManager managerJ = OntManagers.createONT();
OntGraphModel modelJ = OntModelFactory.createModel();

Daniele Francesco Santamaria - Course of Web Reasoning Department of Maths and CS - Univ. of Catania 35 / 37
Motivation
From Ideas to Ontologies
Get Started

ONTAPI

OWL-API and SPARQL.


SPARQL Select
String query= ...
QueryExecution qexec = QueryExecutionFactory.create
(QueryFactory.create (query), model)
String query2= ...
UpdateAction.parseExecute(query2,model);

Daniele Francesco Santamaria - Course of Web Reasoning Department of Maths and CS - Univ. of Catania 36 / 37
Motivation
From Ideas to Ontologies
Get Started

ONTAPI

Interacting with JENA.


OntologyManager manager= OntManagers.createONT();
OntGraphModel model = OntModelFactory.createModel();
Then with OWL-API
OntologyModel ontology =
manager.addOntology(model.getBaseGraph());
OWLOntology owlOnt=(OWLOntology) ontology;

Daniele Francesco Santamaria - Course of Web Reasoning Department of Maths and CS - Univ. of Catania 37 / 37

S-ar putea să vă placă și