/*
* 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.ontodetai.metier;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Set;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLAxiom;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLDataProperty;
import org.semanticweb.owlapi.model.OWLException;
import org.semanticweb.owlapi.model.OWLLiteral;
import org.semanticweb.owlapi.model.OWLNamedIndividual;
import org.semanticweb.owlapi.model.OWLObjectProperty;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.reasoner.Node;
import org.semanticweb.owlapi.reasoner.NodeSet;
import org.semanticweb.owlapi.util.DefaultPrefixManager;
import org.semanticweb.owlapi.util.InferredOntologyGenerator;
import org.semanticweb.owlapi.util.SimpleIRIMapper;
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;
/** Auteur: Michel Héon PhD
* Cotechnoe http://www.cotechnoe.com
* Date: 15-jan-2014
*/
/****************************************************************
****************************************************************
**
** Chapitre 10 CommandeFixerTauxImpostion
**
****************************************************************
****************************************************************/
@SuppressWarnings("unused")
public class CommandeFixerTauxImpostion {
private static PelletReasoner reasoner;
private OWLOntologyManager manager;
private IRI ontologyIRI;
private OWLDataFactory factory;
private OWLOntology ontology;
private DefaultPrefixManager prefix;
private OWLNamedIndividual statut_marie;
private OWLNamedIndividual statut_celibataire;
private OWLObjectProperty est_de_etat_matrimonial;
private OWLObjectProperty est_de_la_categorie_de_revenu;
private OWLDataProperty a_pour_revenu;
private OWLDataProperty avec_personne_a_charge;
private OWLDataProperty a_pour_taux_imposition;
private OWLClass owlContribuable;
private OWLNamedIndividual statut_eleve;
private OWLNamedIndividual revenu_moyen;
private OWLNamedIndividual revenu_faible;
private SimpleIRIMapper ontologyMapper;
private OWLNamedIndividual revenu_eleve;
public void init() throws OWLOntologyCreationException{
String CALCULIMPOT_WEB = "http://java-ws.com/ontologie/calcultauximposition.owl";
String CALCULIMPOT_WEB_LOCAL = "http://localhost:8080/com.java-ws.ontologie/calcultauximposition.owl";
String CALCULIMPOT_LOCAL = "file:///C:/JavaWebSemantique/workspace/com.java-ws.ontologie/WebContent/calcultauximposition.owl";
/*
* IRI web , web local et local de l'ontologie du calcul du taux d'imposition
*/
IRI ontologyIRI_LOCAL = IRI.create(CALCULIMPOT_LOCAL);
IRI ontologyIRI_WEB = IRI.create(CALCULIMPOT_WEB);
IRI ontologyIRI_WEB_LOCAL = IRI.create(CALCULIMPOT_WEB_LOCAL);
/*
* Association entre le l'IRI web et l'IRI local de l'ontoloige
*/
manager = OWLManager.createOWLOntologyManager();
ontologyMapper = new SimpleIRIMapper(ontologyIRI_WEB, ontologyIRI_LOCAL);
// Supprimer le commentaire pour utiliser une version publiée sur le serveur http local
// ontologyMapper = new SimpleIRIMapper(ontologyIRI_WEB, ontologyIRI_WEB_LOCAL);
manager.addIRIMapper(ontologyMapper);
factory = manager.getOWLDataFactory();
ontology = manager.loadOntology(ontologyIRI_WEB);
prefix = new DefaultPrefixManager(ontology.getOntologyID().getOntologyIRI()+"#");
statut_marie = factory.getOWLNamedIndividual(":marie", prefix );
statut_celibataire = factory.getOWLNamedIndividual(":celibataire", prefix );
revenu_faible = factory.getOWLNamedIndividual(":revenu_faible", prefix );
revenu_moyen = factory.getOWLNamedIndividual(":revenu_moyen", prefix );
revenu_eleve = factory.getOWLNamedIndividual(":revenu_eleve", prefix );
est_de_etat_matrimonial = factory.getOWLObjectProperty("est_de_etat_matrimonial", prefix);
est_de_la_categorie_de_revenu = factory.getOWLObjectProperty("est_de_la_categorie_de_revenu", prefix);
a_pour_revenu = factory.getOWLDataProperty("a_pour_revenu", prefix);
avec_personne_a_charge = factory.getOWLDataProperty("avec_personne_a_charge", prefix);
a_pour_taux_imposition = factory.getOWLDataProperty("a_pour_taux_imposition", prefix);
owlContribuable = factory.getOWLClass(":Contribuable", prefix );
}
public Contribuable execute (Contribuable contribuable) throws UnsupportedOperationException, OWLException, IOException{
init();
String nom = contribuable.getNom();
OWLNamedIndividual contribuableIndv = factory.getOWLNamedIndividual(nom, prefix);
manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(owlContribuable, contribuableIndv));
manager.addAxiom(ontology, factory.getOWLDataPropertyAssertionAxiom(avec_personne_a_charge, contribuableIndv, contribuable.isAvecPersonneCharge()));
if (contribuable.getStatut().equals(Contribuable.Statut.CELIBATAIRE.name())){
manager.addAxiom(ontology, factory.getOWLObjectPropertyAssertionAxiom(est_de_etat_matrimonial, contribuableIndv, statut_celibataire));
} else {
manager.addAxiom(ontology, factory.getOWLObjectPropertyAssertionAxiom(est_de_etat_matrimonial, contribuableIndv, statut_marie));
}
manager.addAxiom(ontology, factory.getOWLDataPropertyAssertionAxiom(a_pour_revenu, contribuableIndv, contribuable.getSalaire()));
PelletReasoner reasoner = infererOntologie(manager, ontology);
NodeSet catRevenuSet = reasoner.getObjectPropertyValues(contribuableIndv, est_de_la_categorie_de_revenu);
Node catRevenuNode = catRevenuSet.iterator().next();
OWLNamedIndividual catRevenu = catRevenuNode.getEntities().iterator().next();
if (catRevenu.equals(revenu_faible)){
contribuable.setCategorieDeRevenu(Contribuable.Revenu.FAIBLE.name());
} else if (catRevenu.equals(revenu_moyen)){
contribuable.setCategorieDeRevenu(Contribuable.Revenu.MOYEN.name());
} else if (catRevenu.equals(revenu_eleve)){
contribuable.setCategorieDeRevenu(Contribuable.Revenu.ElEVE.name());
}
Set tauxImpositionSet = reasoner.getDataPropertyValues(contribuableIndv, a_pour_taux_imposition);
OWLLiteral tauxImposition = tauxImpositionSet.iterator().next();
contribuable.setTauxImposition(tauxImposition.getLiteral());
return contribuable;
}
public PelletReasoner infererOntologie(OWLOntologyManager manager, OWLOntology ontology) throws UnsupportedOperationException, OWLException, IOException {
PelletExplanation.setup();
PelletReasonerFactory reasonerFactory = PelletReasonerFactory.getInstance();
OWLOntology inferOntology = manager.createOntology();
reasoner = (PelletReasoner) reasonerFactory.createNonBufferingReasoner(ontology);
if (reasoner.isConsistent())
{
InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner);
iog.fillOntology(manager, inferOntology);
} else {
System.err.println("Is consistance ? Rep: "+reasoner.isConsistent());
ManchesterSyntaxExplanationRenderer renderer = new ManchesterSyntaxExplanationRenderer();
PrintWriter out = new PrintWriter( System.err );
renderer.startRendering( out );
PelletExplanation expGen = new PelletExplanation( reasoner );
Set> exp = expGen.getInconsistencyExplanations();
renderer.render( exp );
renderer.endRendering();
}
return reasoner;
}
}