/*
* 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.Set;
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.OWLAxiom;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLClassAssertionAxiom;
import org.semanticweb.owlapi.model.OWLDataAllValuesFrom;
import org.semanticweb.owlapi.model.OWLDataComplementOf;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLDataIntersectionOf;
import org.semanticweb.owlapi.model.OWLDataOneOf;
import org.semanticweb.owlapi.model.OWLDataProperty;
import org.semanticweb.owlapi.model.OWLDataPropertyAssertionAxiom;
import org.semanticweb.owlapi.model.OWLDataSomeValuesFrom;
import org.semanticweb.owlapi.model.OWLDatatype;
import org.semanticweb.owlapi.model.OWLDatatypeDefinitionAxiom;
import org.semanticweb.owlapi.model.OWLDatatypeRestriction;
import org.semanticweb.owlapi.model.OWLDeclarationAxiom;
import org.semanticweb.owlapi.model.OWLEntity;
import org.semanticweb.owlapi.model.OWLEquivalentClassesAxiom;
import org.semanticweb.owlapi.model.OWLException;
import org.semanticweb.owlapi.model.OWLFacetRestriction;
import org.semanticweb.owlapi.model.OWLFunctionalDataPropertyAxiom;
import org.semanticweb.owlapi.model.OWLIndividual;
import org.semanticweb.owlapi.model.OWLLiteral;
import org.semanticweb.owlapi.model.OWLObjectIntersectionOf;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.reasoner.ConsoleProgressMonitor;
import org.semanticweb.owlapi.reasoner.OWLReasonerConfiguration;
import org.semanticweb.owlapi.reasoner.SimpleConfiguration;
import org.semanticweb.owlapi.util.DefaultPrefixManager;
import org.semanticweb.owlapi.util.InferredOntologyGenerator;
import org.semanticweb.owlapi.vocab.OWLFacet;
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.java_ws.owlapi.exemple.util.JavawsHelper;
/** Auteur: Michel Héon PhD
* Cotechnoe http://www.cotechnoe.com
* Date: 15-jan-2014
*/
public class ChapVIII_Demo {
@SuppressWarnings("unused")
/****************************************************************
****************************************************************
**
** Chapitre 8 DEMO
**
****************************************************************
****************************************************************/
public static void main(String[] args) throws UnsupportedOperationException, OWLException, IOException {
ChapVIII_Demo demo = new ChapVIII_Demo();
demo.ontologieConceptuelle();
// 7.2 Première expérimentation : la classification d’individus selon l’âge et selon le paradigme du monde ouvert
if (false) demo.baseDeFaitsDeLaFamille();
// 7.3 Deuxième expérimentation : classification selon le paradigme du monde fermé
if (true) demo.scenario_IncoherenceDOupsBody();
demo.infererEtImprimer();
System.out.println("-->FIN<--");
}
private String FAMILIES_IRI;
private IRI familiesIRI;
private OWLOntologyManager manager;
private OWLDataFactory factory;
private PelletReasonerFactory reasonerFactory;
private OWLOntology ontology;
private OWLDatatype intDatatype;
private OWLDatatype personAge;
private OWLDataProperty hasAge;
private OWLClass teenager;
private OWLClass adult;
private OWLClass child;
private OWLClass toddler;
private OWLFacetRestriction age0;
private OWLFacetRestriction age150;
private OWLDatatypeRestriction personAgeRestriction;
private OWLFacetRestriction ageMin13;
private OWLFacetRestriction ageMax19;
private OWLDatatypeRestriction teenagerAgeRestriction;
private OWLDataSomeValuesFrom teenagerHasAgeRestriction;
private OWLEquivalentClassesAxiom teenagerHasAgeRestrictionAxiom;
private OWLDatatypeRestriction minorAgeRestriction;
private OWLDataComplementOf notMinorAgeRestriction;
private OWLDataSomeValuesFrom childHasAgeRestriction;
private OWLEquivalentClassesAxiom childtHasAgeRestrictionAxiom;
private OWLLiteral age1;
private OWLLiteral age2;
private OWLDatatype toddlerAge;
private OWLDataSomeValuesFrom toddlerHasAgeRestriction;
private OWLEquivalentClassesAxiom toddleHasAgeRestrictionAxiom;
private TurtleOntologyFormat familiesOntologyFormat;
private DefaultPrefixManager prefix;
private OWLDatatypeDefinitionAxiom personAgeRestrictionAxiom;
private OWLDataIntersectionOf adultAgeRestriction;
private OWLEquivalentClassesAxiom adultAxiom;
private OWLDataSomeValuesFrom adultRestriction;
private OWLFunctionalDataPropertyAxiom functionalHasAgeAxiom;
private OWLDatatype minorAge;
private OWLDatatypeDefinitionAxiom minorAgeRestrictionAxiom;
private OWLDataOneOf toddlerAgeRestriction;
private OWLDatatypeDefinitionAxiom toddlerAgeRestrictionAxiom;
private OWLDatatype majorAge;
private OWLDatatypeDefinitionAxiom majorAgeRestrictionAxiom;
/****************************************************************
****************************************************************
**
** Section 7.1.2 Code Java OWL-API du cadre conceptuel de l’ontologie des âges
**
****************************************************************
****************************************************************/
public void ontologieConceptuelle() throws UnsupportedOperationException, OWLException, IOException {
/***********************************************************
*
* ENTETE DU PROGRAMME
*
***********************************************************/
//
// Attribuer les dénominations: IRI et Préfix
//
FAMILIES_IRI = "http://java-ws.com/ontologie/families";
familiesOntologyFormat = new TurtleOntologyFormat();
familiesOntologyFormat.setDefaultPrefix(FAMILIES_IRI + "#");
familiesOntologyFormat.setPrefix( "myFamily",FAMILIES_IRI + "#");
prefix = new DefaultPrefixManager(FAMILIES_IRI +"#");
//
// Instancier le gestionnaire, et les fabriques
//
familiesIRI = IRI.create(FAMILIES_IRI);
manager = OWLManager.createOWLOntologyManager();
factory = manager.getOWLDataFactory();
reasonerFactory = PelletReasonerFactory.getInstance();
//
// Instancier le gestionnaire, et les fabriques
//
ontology = manager.createOntology(familiesIRI);
/***********************************************************
*
* CORPS DU PROGRAMME (La description du modèle conceptuel)
*
***********************************************************/
/*
* Définition de hasAge
*/
hasAge = factory.getOWLDataProperty(":hasAge", prefix);
functionalHasAgeAxiom = factory.getOWLFunctionalDataPropertyAxiom(hasAge);
/*
* Définition de personAge
*/
personAge = factory.getOWLDatatype(":personAge", prefix);
intDatatype = factory.getIntegerOWLDatatype();
age0 = factory.getOWLFacetRestriction(OWLFacet.MIN_INCLUSIVE, 0);
age150 = factory.getOWLFacetRestriction(OWLFacet.MAX_INCLUSIVE, 150);
personAgeRestriction = factory.getOWLDatatypeRestriction(intDatatype, age0, age150);
personAgeRestrictionAxiom = factory.getOWLDatatypeDefinitionAxiom(personAge, personAgeRestriction);
/*
* Définition de minorAge
*/
minorAge = factory.getOWLDatatype(":minorAge", prefix);
ageMax19 = factory.getOWLFacetRestriction(OWLFacet.MAX_INCLUSIVE, 19);
minorAgeRestriction = factory.getOWLDatatypeRestriction(intDatatype,age0, ageMax19);
minorAgeRestrictionAxiom = factory.getOWLDatatypeDefinitionAxiom(minorAge, minorAgeRestriction);
/*
* Définition du toddlerAge
*/
toddlerAge = factory.getOWLDatatype(":toddlerAge", prefix);
age1 = factory.getOWLLiteral("1", intDatatype);
age2 = factory.getOWLLiteral("2", intDatatype);
toddlerAgeRestriction = factory.getOWLDataOneOf(age1, age2);
toddlerAgeRestrictionAxiom = factory.getOWLDatatypeDefinitionAxiom(toddlerAge, toddlerAgeRestriction);
/*
* Définition de majorAge
*/
majorAge = factory.getOWLDatatype(":majorAge", prefix);
notMinorAgeRestriction = factory.getOWLDataComplementOf(minorAgeRestriction);
adultAgeRestriction = factory.getOWLDataIntersectionOf(personAgeRestriction, notMinorAgeRestriction);
majorAgeRestrictionAxiom = factory.getOWLDatatypeDefinitionAxiom(majorAge, adultAgeRestriction);
/*
* Définition d'un teenager
*/
teenager = factory.getOWLClass(":Teenager", prefix);
ageMin13 = factory.getOWLFacetRestriction(OWLFacet.MIN_EXCLUSIVE, 12);
teenagerAgeRestriction = factory.getOWLDatatypeRestriction(intDatatype, ageMin13, ageMax19);
teenagerHasAgeRestriction = factory.getOWLDataSomeValuesFrom(hasAge, teenagerAgeRestriction);
teenagerHasAgeRestrictionAxiom = factory.getOWLEquivalentClassesAxiom(teenager, teenagerHasAgeRestriction);
/*
* Définition de adult
*/
adult = factory.getOWLClass(":Adult", prefix);
adultRestriction = factory.getOWLDataSomeValuesFrom(hasAge,majorAge);
adultAxiom = factory.getOWLEquivalentClassesAxiom(adult, adultRestriction);
/*
* Définition de Child
*/
child = factory.getOWLClass(":Child", prefix);
childHasAgeRestriction = factory.getOWLDataSomeValuesFrom(hasAge, minorAge);
childtHasAgeRestrictionAxiom = factory.getOWLEquivalentClassesAxiom(child, childHasAgeRestriction);
/*
* Définition d'un toddler
*/
toddler = factory.getOWLClass(":Toddler", prefix);
toddlerHasAgeRestriction = factory.getOWLDataSomeValuesFrom(hasAge, toddlerAge);
toddleHasAgeRestrictionAxiom = factory.getOWLEquivalentClassesAxiom(toddler, toddlerHasAgeRestriction);
/***********************************************************
*
* PIED DE LA MÉTHODE
*
***********************************************************/
/*
* Construire les axiomes et les intégrer à l'ontologie
*/
manager.addAxiom(ontology, functionalHasAgeAxiom);
manager.addAxiom(ontology, teenagerHasAgeRestrictionAxiom);
manager.addAxiom(ontology, adultAxiom);
manager.addAxiom(ontology, childtHasAgeRestrictionAxiom);
manager.addAxiom(ontology, toddleHasAgeRestrictionAxiom);
manager.addAxiom(ontology, personAgeRestrictionAxiom);
manager.addAxiom(ontology, minorAgeRestrictionAxiom);
manager.addAxiom(ontology, toddlerAgeRestrictionAxiom);
manager.addAxiom(ontology, majorAgeRestrictionAxiom);
}
/****************************************************************
****************************************************************
**
** Méthode utilitaire qui implémente le raisonneur et qui imprime l'ontologie déclarative et inférée. En cas
** d'incohérence, la méthode présente l'explication de l'incohérence
**
****************************************************************
****************************************************************/
public void infererEtImprimer() throws UnsupportedOperationException, OWLException, IOException {
// Imprimer l’ontologie descriptive
JavawsHelper.printTurtle(manager,ontology );
PelletExplanation.setup();
OWLOntology inferOntology = manager.createOntology();
ConsoleProgressMonitor progressMonitor = new ConsoleProgressMonitor();
OWLReasonerConfiguration config = new SimpleConfiguration(progressMonitor);
PelletReasoner reasoner = (PelletReasoner) reasonerFactory.createNonBufferingReasoner(ontology, config);
System.out.println("Is consistance ? Rep: "+reasoner.isConsistent());
if (reasoner.isConsistent())
{
InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner);
iog.fillOntology(manager, inferOntology);
// Imprimer l'ontologie inférée
manager.saveOntology(inferOntology, familiesOntologyFormat, new StreamDocumentTarget(System.out));
}
ManchesterSyntaxExplanationRenderer renderer = new ManchesterSyntaxExplanationRenderer();
// The writer used for the explanation rendered
PrintWriter out = new PrintWriter( System.out );
renderer.startRendering( out );
PelletExplanation expGen = new PelletExplanation( reasoner );
Set> exp = expGen.getInconsistencyExplanations();
renderer.render( exp );
renderer.endRendering();
}
/****************************************************************
****************************************************************
**
** Section 7.2 Première expérimentation : la classification d’individus selon l’âge et selon le paradigme du monde ouvert
**
****************************************************************
****************************************************************/
public void baseDeFaitsDeLaFamille() throws UnsupportedOperationException, OWLException, IOException {
OWLIndividual john = factory.getOWLNamedIndividual(":John", prefix);
OWLIndividual bill = factory.getOWLNamedIndividual(":Bill", prefix);
OWLIndividual jack = factory.getOWLNamedIndividual(":Jack", prefix);
OWLIndividual mary = factory.getOWLNamedIndividual(":Mary", prefix);
OWLIndividual sophie = factory.getOWLNamedIndividual(":Sophie", prefix);
OWLIndividual steve = factory.getOWLNamedIndividual(":Steve", prefix);
OWLIndividual oupsBody = factory.getOWLNamedIndividual(":oupsBody", prefix);
OWLDeclarationAxiom jackAxiom = factory.getOWLDeclarationAxiom((OWLEntity) jack);
OWLLiteral thirtyThree = factory.getOWLLiteral("33", intDatatype);
OWLDataPropertyAssertionAxiom johnAxiom = factory.getOWLDataPropertyAssertionAxiom(hasAge, john, thirtyThree);
OWLDataPropertyAssertionAxiom maryAxiom = factory.getOWLDataPropertyAssertionAxiom(hasAge, mary, 31);
OWLDataPropertyAssertionAxiom billAxiom = factory.getOWLDataPropertyAssertionAxiom(hasAge, bill, 13);
OWLDataPropertyAssertionAxiom sophieAxiom = factory.getOWLDataPropertyAssertionAxiom(hasAge, sophie, 8);
OWLDataPropertyAssertionAxiom steveAxiom = factory.getOWLDataPropertyAssertionAxiom(hasAge, steve, 1);
OWLDataPropertyAssertionAxiom oupsBodyAxiom = factory.getOWLDataPropertyAssertionAxiom(hasAge, oupsBody, 151);
/*
* Construire les axiomes et les intégrer à l'ontologie
*/
manager.addAxiom(ontology, johnAxiom);
manager.addAxiom(ontology, billAxiom);
manager.addAxiom(ontology, maryAxiom);
manager.addAxiom(ontology, sophieAxiom);
manager.addAxiom(ontology, steveAxiom);
manager.addAxiom(ontology, jackAxiom);
manager.addAxiom(ontology, oupsBodyAxiom);
}
/****************************************************************
****************************************************************
**
** Section 7.3 Deuxième expérimentation : classification selon le paradigme du monde fermé
**
****************************************************************
****************************************************************/
public void scenario_IncoherenceDOupsBody() throws UnsupportedOperationException, OWLException, IOException {
OWLIndividual oupsBody = factory.getOWLNamedIndividual(":oupsBody", prefix);
OWLDataAllValuesFrom personHasAgeRestriction = factory.getOWLDataAllValuesFrom(hasAge, personAgeRestriction);
OWLClassAssertionAxiom oupsBodyAxiom = factory.getOWLClassAssertionAxiom(personHasAgeRestriction, oupsBody);
OWLDataPropertyAssertionAxiom oupsBodyHasAge151Axiom = factory.getOWLDataPropertyAssertionAxiom(hasAge, oupsBody, 151);
manager.addAxiom(ontology, oupsBodyAxiom);
manager.addAxiom(ontology, oupsBodyHasAge151Axiom);
}
}