/*
* Michel Héon PhD: Web sémantique et modélisation ontologique - Guide du développeur Java sous Eclipse
* This file is part of the book:
*
* Michel Héon
* Web sémantique et modélisation ontologique - Guide du développeur Java sous Eclipse
* 2014
* Editions ENI
* ISBN : 978-2-7460-8869-6
* EAN : 9782746088696
* France
*
* The contents of this file are subject to the LGPL License, Version 3.0.
*
* Copyright (C) 2014, Cotechnoe inc. http://www.cotechnoe.com, http://java-ws.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*
*
* Alternatively, the contents of this file may be used under the terms of the Apache License, Version 2.0
* in which case, the provisions of the Apache License Version 2.0 are applicable instead of those above.
*
* Copyright (C) 2014, Cotechnoe inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.java_ws.owlapi.exemple;
import org.coode.owlapi.turtle.TurtleOntologyFormat;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.io.StreamDocumentTarget;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLClassAssertionAxiom;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLDataProperty;
import org.semanticweb.owlapi.model.OWLDataPropertyAssertionAxiom;
import org.semanticweb.owlapi.model.OWLDataPropertyDomainAxiom;
import org.semanticweb.owlapi.model.OWLDataPropertyRangeAxiom;
import org.semanticweb.owlapi.model.OWLDatatype;
import org.semanticweb.owlapi.model.OWLDisjointClassesAxiom;
import org.semanticweb.owlapi.model.OWLEquivalentClassesAxiom;
import org.semanticweb.owlapi.model.OWLIndividual;
import org.semanticweb.owlapi.model.OWLLiteral;
import org.semanticweb.owlapi.model.OWLNegativeDataPropertyAssertionAxiom;
import org.semanticweb.owlapi.model.OWLNegativeObjectPropertyAssertionAxiom;
import org.semanticweb.owlapi.model.OWLObjectProperty;
import org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom;
import org.semanticweb.owlapi.model.OWLObjectPropertyDomainAxiom;
import org.semanticweb.owlapi.model.OWLObjectPropertyRangeAxiom;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.model.OWLOntologyStorageException;
import org.semanticweb.owlapi.model.OWLSubClassOfAxiom;
import org.semanticweb.owlapi.model.OWLSubObjectPropertyOfAxiom;
import org.semanticweb.owlapi.model.PrefixManager;
import org.semanticweb.owlapi.reasoner.ConsoleProgressMonitor;
import org.semanticweb.owlapi.reasoner.OWLReasonerConfiguration;
import org.semanticweb.owlapi.reasoner.OWLReasonerFactory;
import org.semanticweb.owlapi.reasoner.SimpleConfiguration;
import org.semanticweb.owlapi.util.DefaultPrefixManager;
import org.semanticweb.owlapi.util.InferredOntologyGenerator;
import org.semanticweb.owlapi.vocab.PrefixOWLOntologyFormat;
import com.clarkparsia.pellet.owlapiv3.PelletReasoner;
import com.clarkparsia.pellet.owlapiv3.PelletReasonerFactory;
import com.java_ws.owlapi.exemple.util.JavawsHelper;
/** Auteur: Michel Héon PhD
* Cotechnoe http://www.cotechnoe.com
* Date: 15-jan-2014
*/
public class ChapVI_Demo {
/****************************************************************
****************************************************************
**
** Chapitre 6
**
****************************************************************
****************************************************************/
@SuppressWarnings("unused")
public static void main(String[] args) throws OWLOntologyCreationException, OWLOntologyStorageException {
if (true) structureDeDemo();
if (false) hierarchieDeClasse();
if (false) objectPropertyAssertion();
if (false) dataPropertyAssertion();
}
/****************************************************************
****************************************************************
**
** Section 2 : Notions de la modélisation ontologique
**
****************************************************************
****************************************************************/
public static void structureDeDemo() throws OWLOntologyCreationException, OWLOntologyStorageException {
/***********************************************************
*
* ENTETE DU PROGRAMME
*
***********************************************************/
//
// Attribuer les dénominations: IRI et Préfix
//
String FAMILIES_IRI = "http://java-ws.com/ontologie/families";
PrefixOWLOntologyFormat familiesOntologyFormat = new TurtleOntologyFormat();
familiesOntologyFormat.setDefaultPrefix(FAMILIES_IRI + "#");
familiesOntologyFormat.setPrefix( "myFamily",FAMILIES_IRI + "#");
PrefixManager familiesPrefix = new DefaultPrefixManager(FAMILIES_IRI +"#");
//
// Instancier le gestionnaire, et les fabriques
//
IRI familiesIRI = IRI.create(FAMILIES_IRI);
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLDataFactory factory = manager.getOWLDataFactory();
OWLReasonerFactory reasonerFactory = PelletReasonerFactory.getInstance();
//
// Instancier le gestionnaire, et les fabriques
//
OWLOntology familiesOntology = manager.createOntology(familiesIRI);
OWLOntology inferFamilyOntology = manager.createOntology();
ConsoleProgressMonitor progressMonitor = new ConsoleProgressMonitor();
OWLReasonerConfiguration config = new SimpleConfiguration(progressMonitor);
PelletReasoner reasoner = (PelletReasoner) reasonerFactory.createNonBufferingReasoner(familiesOntology, config);
/***********************************************************
*
* CORPS DU PROGRAMME (La description du modèle conceptuel)
*
***********************************************************/
// Construire les entités ontologiques
OWLClass woman = factory.getOWLClass(":Woman", familiesPrefix);
OWLIndividual mary = factory.getOWLNamedIndividual(":Mary", familiesPrefix );
// construire les axiomes
OWLClassAssertionAxiom maryIsAWoman = factory.getOWLClassAssertionAxiom(woman, mary);
/***********************************************************
*
* PIED DU PROGRAMME
*
***********************************************************/
// Ajouter les axiomes à l’ontologie
manager.addAxiom(familiesOntology, maryIsAWoman);
// Imprimer l’ontologie descriptive
JavawsHelper.printTurtle(manager,familiesOntology );
//Inférer l'ontologie descriptive
InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner);
iog.fillOntology(manager, inferFamilyOntology);
// Imprimer l'ontologie inférée
manager.saveOntology(inferFamilyOntology, familiesOntologyFormat, new StreamDocumentTarget(System.out));
}
/****************************************************************
****************************************************************
**
** Section 5 Hiérarchie de classes
**
****************************************************************
****************************************************************/
public static void hierarchieDeClasse() throws OWLOntologyCreationException, OWLOntologyStorageException {
/***********************************************************
*
* ENTETE DU PROGRAMME
*
***********************************************************/
//
// Attribuer les dénominations: IRI et Préfix
//
String FAMILIES_IRI = "http://java-ws.com/ontologie/families";
PrefixOWLOntologyFormat familiesOntologyFormat = new TurtleOntologyFormat();
familiesOntologyFormat.setDefaultPrefix(FAMILIES_IRI + "#");
familiesOntologyFormat.setPrefix( "myFamily",FAMILIES_IRI + "#");
PrefixManager familiesPrefix = new DefaultPrefixManager(FAMILIES_IRI +"#");
//
// Instancier le gestionnaire, et les fabriques
//
IRI familiesIRI = IRI.create(FAMILIES_IRI);
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLDataFactory factory = manager.getOWLDataFactory();
OWLReasonerFactory reasonerFactory = PelletReasonerFactory.getInstance();
//
// Instancier le gestionnaire, et les fabriques
//
OWLOntology familiesOntology = manager.createOntology(familiesIRI);
OWLOntology inferFamilyOntology = manager.createOntology();
ConsoleProgressMonitor progressMonitor = new ConsoleProgressMonitor();
OWLReasonerConfiguration config = new SimpleConfiguration(progressMonitor);
/***********************************************************
*
* CORPS DU PROGRAMME (La description du modèle conceptuel)
*
***********************************************************/
PelletReasoner reasoner = (PelletReasoner) reasonerFactory.createNonBufferingReasoner(familiesOntology, config);
/*
* Construire l'ontologie
*/
OWLClass human = factory.getOWLClass(":Human", familiesPrefix);
OWLClass person = factory.getOWLClass(":Person", familiesPrefix);
OWLClass woman = factory.getOWLClass(":Woman", familiesPrefix);
OWLClass man = factory.getOWLClass(":Man", familiesPrefix);
OWLIndividual mary = factory.getOWLNamedIndividual(":Mary", familiesPrefix );
OWLIndividual john = factory.getOWLNamedIndividual(":John", familiesPrefix);
OWLIndividual jim = factory.getOWLNamedIndividual(":Jim", familiesPrefix);
OWLSubClassOfAxiom womanKindOfPerson = factory.getOWLSubClassOfAxiom(woman, person);
OWLSubClassOfAxiom manKindOfPerson = factory.getOWLSubClassOfAxiom(man, person);
OWLClassAssertionAxiom maryIsAWoman = factory.getOWLClassAssertionAxiom(woman, mary);
OWLClassAssertionAxiom johnIsAMan = factory.getOWLClassAssertionAxiom(man, john);
OWLClassAssertionAxiom jimIsAPerson = factory.getOWLClassAssertionAxiom(person, jim);
OWLEquivalentClassesAxiom humansArePersons = factory.getOWLEquivalentClassesAxiom(human, person);
OWLDisjointClassesAxiom mansAreNotWomans = factory.getOWLDisjointClassesAxiom(man,woman);
/***********************************************************
*
* PIED DU PROGRAMME
*
***********************************************************/
// Ajouter les axiomes à l’ontologie
manager.addAxiom(familiesOntology, maryIsAWoman);
manager.addAxiom(familiesOntology, johnIsAMan);
manager.addAxiom(familiesOntology, jimIsAPerson);
manager.addAxiom(familiesOntology, womanKindOfPerson);
manager.addAxiom(familiesOntology, manKindOfPerson);
manager.addAxiom(familiesOntology, humansArePersons);
manager.addAxiom(familiesOntology, mansAreNotWomans);
// Imprimer l’ontologie descriptive
JavawsHelper.printTurtle(manager,familiesOntology );
//Inférer l'ontologie descriptive
InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner);
iog.fillOntology(manager, inferFamilyOntology);
// Imprimer l'ontologie inférée
manager.saveOntology(inferFamilyOntology, familiesOntologyFormat, new StreamDocumentTarget(System.out));
}
/****************************************************************
****************************************************************
**
** Section 6 Propriété d'objets
**
****************************************************************
****************************************************************/
public static void objectPropertyAssertion() throws OWLOntologyCreationException, OWLOntologyStorageException {
/***********************************************************
*
* ENTETE DU PROGRAMME
*
***********************************************************/
//
// Attribuer les dénominations: IRI et Préfix
//
String FAMILIES_IRI = "http://java-ws.com/ontologie/families";
PrefixOWLOntologyFormat familiesOntologyFormat = new TurtleOntologyFormat();
familiesOntologyFormat.setDefaultPrefix(FAMILIES_IRI + "#");
familiesOntologyFormat.setPrefix( "myFamily",FAMILIES_IRI + "#");
PrefixManager familiesPrefix = new DefaultPrefixManager(FAMILIES_IRI +"#");
IRI familiesIRI = IRI.create(FAMILIES_IRI);
//
// Instancier le gestionnaire, et les fabriques
//
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLDataFactory factory = manager.getOWLDataFactory();
OWLReasonerFactory reasonerFactory = PelletReasonerFactory.getInstance();
//
// Instancier les ontologies
//
OWLOntology familiesOntology = manager.createOntology(familiesIRI);
OWLOntology inferFamilyOntology = manager.createOntology();
//
// Instancier le raisonneur et la console de mesure de la progression
//
ConsoleProgressMonitor progressMonitor = new ConsoleProgressMonitor();
OWLReasonerConfiguration config = new SimpleConfiguration(progressMonitor);
PelletReasoner reasoner = (PelletReasoner) reasonerFactory.createNonBufferingReasoner(familiesOntology, config);
/***********************************************************
*
* CORPS DU PROGRAMME (La description du modèle conceptuel)
*
***********************************************************/
OWLClass woman = factory.getOWLClass(":Woman", familiesPrefix);
OWLClass man = factory.getOWLClass(":Man", familiesPrefix);
OWLIndividual mary = factory.getOWLNamedIndividual(":Mary", familiesPrefix);
OWLIndividual john = factory.getOWLNamedIndividual(":John", familiesPrefix);
OWLIndividual bill = factory.getOWLNamedIndividual(":Bill", familiesPrefix);
OWLObjectProperty hasWife = factory.getOWLObjectProperty(":hasWife", familiesPrefix);
OWLObjectProperty hasSpouse = factory.getOWLObjectProperty(":hasSpouse", familiesPrefix);
OWLObjectPropertyRangeAxiom hasWifeRangeWoman = factory.getOWLObjectPropertyRangeAxiom(hasWife, woman);
OWLObjectPropertyDomainAxiom haWifeDomainMan = factory.getOWLObjectPropertyDomainAxiom(hasWife, man);
OWLObjectPropertyAssertionAxiom johnHasWifeMary = factory.getOWLObjectPropertyAssertionAxiom(hasWife, john, mary);
OWLSubObjectPropertyOfAxiom hasWifeKindOfHasSpouse = factory.getOWLSubObjectPropertyOfAxiom(hasWife, hasSpouse);
OWLNegativeObjectPropertyAssertionAxiom billNOTHasWifeMary = factory.getOWLNegativeObjectPropertyAssertionAxiom(hasWife, bill, mary);
/***********************************************************
*
* PIED DU PROGRAMME
*
***********************************************************/
// Ajouter les axiomes à l’ontologie
manager.addAxiom(familiesOntology, hasWifeRangeWoman);
manager.addAxiom(familiesOntology, haWifeDomainMan);
manager.addAxiom(familiesOntology, johnHasWifeMary);
manager.addAxiom(familiesOntology, hasWifeKindOfHasSpouse);
manager.addAxiom(familiesOntology, billNOTHasWifeMary);
// Imprimer l’ontologie descriptive
JavawsHelper.printTurtle(manager,familiesOntology );
//Inférer l'ontologie descriptive
InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner);
iog.fillOntology(manager, inferFamilyOntology);
// Imprimer l'ontologie inférée
manager.saveOntology(inferFamilyOntology, familiesOntologyFormat, new StreamDocumentTarget(System.out));
}
/****************************************************************
****************************************************************
**
** Section 6 Propriété sur les types de données
**
****************************************************************
****************************************************************/
public static void dataPropertyAssertion() throws OWLOntologyCreationException, OWLOntologyStorageException {
/***********************************************************
*
* ENTETE DU PROGRAMME
*
***********************************************************/
//
// Attribuer les dénominations: IRI et Préfix
//
String FAMILIES_IRI = "http://java-ws.com/ontologie/families";
PrefixOWLOntologyFormat familiesOntologyFormat = new TurtleOntologyFormat();
familiesOntologyFormat.setDefaultPrefix(FAMILIES_IRI + "#");
familiesOntologyFormat.setPrefix( "myFamily",FAMILIES_IRI + "#");
PrefixManager familiesPrefix = new DefaultPrefixManager(FAMILIES_IRI +"#");
//
// Instancier le gestionnaire, et les fabriques
//
IRI familiesIRI = IRI.create(FAMILIES_IRI);
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLDataFactory factory = manager.getOWLDataFactory();
OWLReasonerFactory reasonerFactory = PelletReasonerFactory.getInstance();
//
// Instancier le gestionnaire, et les fabriques
//
OWLOntology familiesOntology = manager.createOntology(familiesIRI);
OWLOntology inferFamilyOntology = manager.createOntology();
ConsoleProgressMonitor progressMonitor = new ConsoleProgressMonitor();
OWLReasonerConfiguration config = new SimpleConfiguration(progressMonitor);
/***********************************************************
*
* CORPS DU PROGRAMME (La description du modèle conceptuel)
*
***********************************************************/
PelletReasoner reasoner = (PelletReasoner) reasonerFactory.createNonBufferingReasoner(familiesOntology, config);
/*
* Construire l'ontologie
*/
OWLClass person = factory.getOWLClass(":Person", familiesPrefix);
OWLIndividual john = factory.getOWLNamedIndividual(":John", familiesPrefix);
OWLIndividual jack = factory.getOWLNamedIndividual(":Jack", familiesPrefix);
OWLDataProperty hasAge = factory.getOWLDataProperty(":hasAge", familiesPrefix);
OWLLiteral age = factory.getOWLLiteral(53);
OWLDatatype integerDatatype = factory.getIntegerOWLDatatype();
OWLDataPropertyRangeAxiom hasAgeRange = factory.getOWLDataPropertyRangeAxiom(hasAge, integerDatatype);
OWLDataPropertyDomainAxiom personHasAge = factory.getOWLDataPropertyDomainAxiom(hasAge, person);
OWLDataPropertyAssertionAxiom johnHasAge51 = factory.getOWLDataPropertyAssertionAxiom(hasAge, john, 51);
OWLNegativeDataPropertyAssertionAxiom jackNotHasAge = factory.getOWLNegativeDataPropertyAssertionAxiom(hasAge, jack, age);
/***********************************************************
*
* PIED DU PROGRAMME
*
***********************************************************/
// Ajouter les axiomes à l’ontologie
manager.addAxiom(familiesOntology, hasAgeRange);
manager.addAxiom(familiesOntology, personHasAge);
manager.addAxiom(familiesOntology, johnHasAge51);
manager.addAxiom(familiesOntology, jackNotHasAge);
// Imprimer l’ontologie descriptive
JavawsHelper.printTurtle(manager,familiesOntology );
//Inférer l'ontologie descriptive
InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner);
iog.fillOntology(manager, inferFamilyOntology);
// Imprimer l'ontologie inférée
manager.saveOntology(inferFamilyOntology, familiesOntologyFormat, new StreamDocumentTarget(System.out));
}
}