/*
* 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 java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Set;
import org.coode.owlapi.turtle.TurtleOntologyFormat;
import org.mindswap.pellet.KnowledgeBase;
import org.mindswap.pellet.jena.ModelExtractor;
import org.mindswap.pellet.jena.PelletInfGraph;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.io.StreamDocumentTarget;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLAxiom;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLClassAssertionAxiom;
import org.semanticweb.owlapi.model.OWLClassAxiom;
import org.semanticweb.owlapi.model.OWLClassExpression;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLDisjointClassesAxiom;
import org.semanticweb.owlapi.model.OWLEquivalentClassesAxiom;
import org.semanticweb.owlapi.model.OWLException;
import org.semanticweb.owlapi.model.OWLIndividual;
import org.semanticweb.owlapi.model.OWLNamedIndividual;
import org.semanticweb.owlapi.model.OWLObjectIntersectionOf;
import org.semanticweb.owlapi.model.OWLObjectUnionOf;
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.reasoner.ConsoleProgressMonitor;
import org.semanticweb.owlapi.reasoner.InferenceType;
import org.semanticweb.owlapi.reasoner.OWLReasonerConfiguration;
import org.semanticweb.owlapi.reasoner.OWLReasonerFactory;
import org.semanticweb.owlapi.reasoner.SimpleConfiguration;
import org.semanticweb.owlapi.util.InferredOntologyGenerator;
import org.semanticweb.owlapi.vocab.PrefixOWLOntologyFormat;
import com.clarkparsia.owlapi.explanation.PelletExplanation;
import com.clarkparsia.owlapi.explanation.io.manchester.ManchesterSyntaxExplanationRenderer;
import com.clarkparsia.pellet.owlapiv3.PelletReasoner;
import com.clarkparsia.pellet.owlapiv3.PelletReasonerFactory;
import com.hp.hpl.jena.rdf.model.InfModel;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.java_ws.owlapi.exemple.util.JavawsHelper;
@SuppressWarnings("unused")
/** Auteur: Michel Héon PhD
* Cotechnoe http://www.cotechnoe.com
* Date: 15-jan-2014
*/
public class ChapV {
private static String FAMILIES_IRI;
private static OWLOntologyManager manager;
private static IRI ontologyIRI;
private static TurtleOntologyFormat familiesOntologyFormat;
private static OWLDataFactory factory;
private static OWLOntology ontology;
private static OWLClass woman;
private static OWLClass parent;
private static OWLClass mother;
private static OWLNamedIndividual mary;
private static OWLObjectIntersectionOf womanParent;
private static OWLEquivalentClassesAxiom motherEqWomanParentAxiome;
private static OWLClassAssertionAxiom assertionAxiome;
private static OWLClass father;
private static OWLClass man;
private static OWLClass person;
private static OWLObjectIntersectionOf fatherExpr;
private static OWLClassAxiom fatherAxiom;
private static OWLObjectIntersectionOf motherExpr;
private static OWLClassAxiom motherAxiom;
private static OWLObjectUnionOf personExpr;
private static OWLClassAxiom personAxiom;
private static OWLClassAxiom parentKindOfpersonAxiom;
private static OWLDisjointClassesAxiom womanNotMan;
/****************************************************************
****************************************************************
**
** Chapitre 5
**
****************************************************************
****************************************************************/
public static void main(String[] args) throws UnsupportedOperationException, OWLException, IOException {
// Section 4. Scénario de référence
if (false) {
init_modele_de_base();
JavawsHelper.printManchesterOWL(manager, ontology);
}
// Section 5.1. Inférer une ontologie
if (false) infererUneOntologie();
// Section 5.2. Suivre la progression de l’inférence
if (false) suivreLaProgession();
// Section 5.3. Synchroniser l’amorce du raisonnement à la manipulation de l’ontologie
if (false) attacherUnListener();
// Section 6.1 Valider la cohérence et expliquez l’incohérence
if (false) ontologieIncoherente();
// Section 6.1 b Valider la cohérence et expliquez l’incohérence
if (false) expliquerOntologieIncoherente();
// Section 6.2. Éprouver la vérifiabilité
if (false) eprouverLaVerifiabilite();
// Section 7.1. Évaluer une condition nécessaire selon l’OWA
if (false) conditionNecessaire();
// Section 7.2. Évaluer une condition nécessaire et suffisante selon l’OWA
if (false) conditionNecessaireEtSuffisante();
}
/****************************************************************
****************************************************************
**
** Section 4. Scénario de référence
**
****************************************************************
****************************************************************/
public static void init_modele_de_base() throws OWLOntologyStorageException, OWLOntologyCreationException {
/*
* Configurer les parametres du document ontologique
*/
FAMILIES_IRI = "http://java-ws.com/ontologie/families";
manager = OWLManager.createOWLOntologyManager();
ontologyIRI = IRI.create(FAMILIES_IRI);
familiesOntologyFormat = new TurtleOntologyFormat();
familiesOntologyFormat.setDefaultPrefix(FAMILIES_IRI + "#");
familiesOntologyFormat.setPrefix( "myFamily",FAMILIES_IRI + "#");
/*
* Construire le corps de l'ontologie
*/
factory = manager.getOWLDataFactory();
ontology = manager.createOntology(ontologyIRI);
father = factory.getOWLClass(IRI.create(ontologyIRI + "#Father"));
man = factory.getOWLClass(IRI.create(ontologyIRI + "#Man"));
mother = factory.getOWLClass(IRI.create(ontologyIRI + "#Mother"));
woman = factory.getOWLClass(IRI.create(ontologyIRI + "#Woman"));
parent = factory.getOWLClass(IRI.create(ontologyIRI + "#Parent"));
person = factory.getOWLClass(IRI.create(ontologyIRI + "#Person"));
womanParent = factory.getOWLObjectIntersectionOf(woman, parent);
motherExpr = factory.getOWLObjectIntersectionOf(woman, parent);
personExpr = factory.getOWLObjectUnionOf(man, woman);
fatherExpr = factory.getOWLObjectIntersectionOf(man, parent);
motherAxiom = factory.getOWLSubClassOfAxiom( mother, motherExpr);
personAxiom = factory.getOWLEquivalentClassesAxiom(personExpr, person);
womanNotMan = factory.getOWLDisjointClassesAxiom(woman, man);
fatherAxiom = factory.getOWLSubClassOfAxiom(father, fatherExpr);
manager.addAxiom(ontology, motherAxiom);
manager.addAxiom(ontology, personAxiom);
manager.addAxiom(ontology, womanNotMan);
manager.addAxiom(ontology, fatherAxiom);
}
/****************************************************************
****************************************************************
**
** Section 5.1. Inférer une ontologie
**
****************************************************************
****************************************************************/
public static void infererUneOntologie() throws UnsupportedOperationException, OWLException, IOException {
// Créer l'ontologie du modèle conceptuel
init_modele_de_base();
// Créer la base de faits
OWLIndividual mary = factory.getOWLNamedIndividual(IRI.create(FAMILIES_IRI + "#Mary"));
manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(mother, mary));
// Instancier le raisonneur
PelletReasoner reasoner = PelletReasonerFactory.getInstance().createReasoner( ontology );
// Créer une ontologie contenant les inférences
OWLOntologyManager infManager = OWLManager.createOWLOntologyManager();
OWLOntology infOnt = infManager.createOntology(ontologyIRI);
// Inférer et mettre les résultats dans l'ontologie des inférences
InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner);
iog.fillOntology(infManager, infOnt);
// Imprimer les ontologies
JavawsHelper.printManchesterOWL(manager, ontology);
JavawsHelper.printManchesterOWL(infManager, infOnt);
}
/****************************************************************
****************************************************************
**
** Section 5.2. Suivre la progression de l’inférence
**
****************************************************************
****************************************************************/
public static void suivreLaProgession() throws UnsupportedOperationException, OWLException, IOException {
init_modele_de_base();
OWLIndividual mary = factory.getOWLNamedIndividual(IRI.create(FAMILIES_IRI + "#Mary"));
manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(mother, mary));
ConsoleProgressMonitor progressMonitor = new ConsoleProgressMonitor();
OWLReasonerConfiguration config = new SimpleConfiguration(progressMonitor);
// Instancier le raisonneur
PelletReasoner reasoner = PelletReasonerFactory.getInstance().createReasoner( ontology, config );
System.out.println("CLASSIFICATION DE L'ONTOLOGIE");
reasoner.getKB().classify();
System.out.println("REALISATION DE L'ONTOLOGIE");
reasoner.getKB().realize();
System.out.println("STRUCTURE DE L'ONTOLOGIE");
reasoner.getKB().printClassTree();
}
/****************************************************************
****************************************************************
**
** Section 5.3. Synchroniser l’amorce du raisonnement à la manipulation de l’ontologie
**
****************************************************************
****************************************************************/
public static void attacherUnListener() throws UnsupportedOperationException, OWLException, IOException {
init_modele_de_base();
// Associer un écouteur à l'ontologie
PelletReasoner reasoner = PelletReasonerFactory.getInstance().createNonBufferingReasoner( ontology );
manager.addOntologyChangeListener( reasoner );
System.out.println( "Cohérence initiale de l'ontologie ? " +reasoner.isConsistent() );
// Ajouter l'individu Mary
OWLIndividual mary = factory.getOWLNamedIndividual(IRI.create(FAMILIES_IRI + "#Mary"));
// Associer mary à la classe woman
System.out.println( "Ajout de l'assertion : Mary est un Woman" );
manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(woman, mary));
System.out.println( "Cohérent ? " +reasoner.isConsistent() );
// Associer mary à la classe man (incohérence)
System.out.println( "Ajout de l'assertion : Mary est un Man" );
manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(man, mary));
System.out.println( "Cohérent ? " +reasoner.isConsistent() );
}
/****************************************************************
****************************************************************
**
** Section 6.1 a) Valider la cohérence et expliquez l’incohérence
**
****************************************************************
****************************************************************/
public static void ontologieIncoherente() throws UnsupportedOperationException, OWLException, IOException {
// Créer l'ontologie du modèle conceptuel
init_modele_de_base();
// Créer la base de faits
OWLIndividual mary = factory.getOWLNamedIndividual(IRI.create(FAMILIES_IRI + "#Mary"));
manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(mother, mary));
manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(father, mary));
// Instancier le raisonneur
PelletReasoner reasoner = PelletReasonerFactory.getInstance().createReasoner( ontology );
// Créer une ontologie contenant les inférences
OWLOntologyManager infManager = OWLManager.createOWLOntologyManager();
OWLOntology infOnt = infManager.createOntology(ontologyIRI);
// Inférer et mettre les résultats dans l'ontologie des inférences
InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner);
System.err.println("L'appel à cette méthode va générer une Exception");
iog.fillOntology(infManager, infOnt);
// Imprimer les ontologies
JavawsHelper.printTurtle(manager, ontology);
JavawsHelper.printTurtle(infManager, infOnt);
}
/****************************************************************
****************************************************************
**
** Section 6.1 Valider la cohérence et expliquez l’incohérence
** Méthode qui explique l'incohérence de l'ontologie
**
****************************************************************
****************************************************************/
public static void expliquerOntologieIncoherente() throws UnsupportedOperationException, OWLException, IOException {
// Créer l'ontologie du modèle conceptuel
init_modele_de_base();
// Créer la base de faits
OWLIndividual mary = factory.getOWLNamedIndividual(IRI.create(FAMILIES_IRI + "#Mary"));
manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(mother, mary));
manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(father, mary));
// Instancier le raisonneur
PelletExplanation.setup();
PelletReasoner reasoner = PelletReasonerFactory.getInstance().createReasoner( ontology );
if ( !reasoner.isConsistent() ){
// obtenir l'explication de l'incohérence
PelletExplanation expGen = new PelletExplanation( reasoner );
Set> exp = expGen.getInconsistencyExplanations();
JavawsHelper.imprimerExplication(exp);
// ManchesterSyntaxExplanationRenderer renderer = new ManchesterSyntaxExplanationRenderer();
// PrintWriter out = new PrintWriter( System.out );
// renderer.startRendering( out );
// renderer.render( exp );
// renderer.endRendering();
} else {
// Créer une ontologie contenant les inférences
OWLOntologyManager infManager = OWLManager.createOWLOntologyManager();
OWLOntology infOnt = infManager.createOntology(ontologyIRI);
// Inférer et mettre les résultats dans l'ontologie des inférences
InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner);
iog.fillOntology(infManager, infOnt);
// Imprimer les ontologies
JavawsHelper.printTurtle(manager, ontology);
JavawsHelper.printTurtle(infManager, infOnt);
}
}
/****************************************************************
****************************************************************
**
** Section 6.2. Éprouver la vérifiabilité
**
****************************************************************
****************************************************************/
public static void eprouverLaVerifiabilite() throws UnsupportedOperationException, OWLException, IOException {
// Créer l'ontologie du modèle conceptuel
init_modele_de_base();
// Créer la base de faits
OWLClass oupsParent = factory.getOWLClass(IRI.create(ontologyIRI + "#OupsParent"));
OWLObjectIntersectionOf womanAndMann = factory.getOWLObjectIntersectionOf(woman, man);
OWLSubClassOfAxiom oupsParentKindOfWomanAndMan = factory.getOWLSubClassOfAxiom(oupsParent, womanAndMann);
manager.addAxiom(ontology, oupsParentKindOfWomanAndMan);
PelletExplanation.setup();
PelletReasoner reasoner = PelletReasonerFactory.getInstance().createReasoner( ontology );
// Obtenir la liste des classes non vérifiables
// en excluant la classe owl:Nothing
Set unsatisfiableClasses = reasoner.getUnsatisfiableClasses().getEntitiesMinusBottom();
for (Iterator iterator = unsatisfiableClasses.iterator(); iterator.hasNext();) {
OWLClass uClass = (OWLClass) iterator.next();
// Obtenir l'explication de la classe non vérifiable
PelletExplanation expGen = new PelletExplanation( reasoner );
Set> exp = expGen.getUnsatisfiableExplanations( uClass );
JavawsHelper.imprimerExplication(exp);
}
}
/****************************************************************
****************************************************************
**
** Section 7.1. Évaluer une condition nécessaire selon l’OWA
**
****************************************************************
****************************************************************/
public static void conditionNecessaire() throws UnsupportedOperationException, OWLException, IOException {
// Créer l'ontologie du modèle conceptuel
init_modele_de_base();
// Créer la base de faits
fatherExpr = factory.getOWLObjectIntersectionOf(man, parent);
fatherAxiom = factory.getOWLSubClassOfAxiom(father, fatherExpr);
OWLNamedIndividual john = factory.getOWLNamedIndividual(IRI.create(FAMILIES_IRI + "#John"));
OWLClassAssertionAxiom johsKindOfMan = factory.getOWLClassAssertionAxiom(man, john);
OWLClassAssertionAxiom johsKindOfParent = factory.getOWLClassAssertionAxiom(parent, john);
manager.addAxiom(ontology, fatherAxiom);
manager.addAxiom(ontology, johsKindOfMan);
manager.addAxiom(ontology, johsKindOfParent);
PelletExplanation.setup();
PelletReasoner reasoner = PelletReasonerFactory.getInstance().createReasoner( ontology );
PelletExplanation expGen = new PelletExplanation( reasoner );
JavawsHelper.imprimerExplication(expGen.getEntailmentExplanations(fatherAxiom));
JavawsHelper.imprimerExplication(expGen.getEntailmentExplanations(personAxiom));
Set assertedClasses = john.getTypes(ontology);
for (OWLClass clazz : reasoner.getTypes( john, false).getFlattened()) {
boolean asserted = assertedClasses.contains(clazz);
System.out.println((asserted ? "asserted" : "inferred") + " class for john: " + clazz.getIRI());
}
// JavawsHelper.printTurtle(manager, ontology);
}
/****************************************************************
****************************************************************
**
** Section 7.2. Évaluer une condition nécessaire et suffisante selon l’OWA
**
****************************************************************
****************************************************************/
public static void conditionNecessaireEtSuffisante() throws UnsupportedOperationException, OWLException, IOException {
// Créer l'ontologie du modèle conceptuel
init_modele_de_base();
// Créer la base de faits
fatherExpr = factory.getOWLObjectIntersectionOf(man, parent);
fatherAxiom = factory.getOWLEquivalentClassesAxiom(father, fatherExpr);
OWLNamedIndividual john = factory.getOWLNamedIndividual(IRI.create(FAMILIES_IRI + "#John"));
OWLClassAssertionAxiom johsKindOfMan = factory.getOWLClassAssertionAxiom(man, john);
OWLClassAssertionAxiom johsKindOfParent = factory.getOWLClassAssertionAxiom(parent, john);
manager.addAxiom(ontology, johsKindOfMan);
manager.addAxiom(ontology, johsKindOfParent);
manager.addAxiom(ontology, fatherAxiom);
PelletExplanation.setup();
PelletReasoner reasoner = PelletReasonerFactory.getInstance().createReasoner( ontology );
PelletExplanation expGen = new PelletExplanation( reasoner );
JavawsHelper.imprimerExplication(expGen.getEntailmentExplanations(fatherAxiom));
JavawsHelper.imprimerExplication(expGen.getEntailmentExplanations(personAxiom));
Set assertedClasses = john.getTypes(ontology);
for (OWLClass clazz : reasoner.getTypes( john, false).getFlattened()) {
boolean asserted = assertedClasses.contains(clazz);
System.out.println((asserted ? "asserted" : "inferred") + " class for john: " + clazz.getIRI());
}
}
}