Web sémantique et modélisation ontologique (avec G-OWL)

Web sémantique et modélisation ontologique (avec G-OWL):

Le guide du développeur Java sous Eclipse

 

 

 

Web sémantique et modélisation ontologique avec G-OWL

ChapVI.java

/*
 * 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.util.HashSet;
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.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.OWLDeclarationAxiom;
import org.semanticweb.owlapi.model.OWLDifferentIndividualsAxiom;
import org.semanticweb.owlapi.model.OWLDisjointClassesAxiom;
import org.semanticweb.owlapi.model.OWLEntity;
import org.semanticweb.owlapi.model.OWLEquivalentClassesAxiom;
import org.semanticweb.owlapi.model.OWLIndividual;
import org.semanticweb.owlapi.model.OWLLiteral;
import org.semanticweb.owlapi.model.OWLNamedIndividual;
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.OWLSameIndividualAxiom;
import org.semanticweb.owlapi.model.OWLSubClassOfAxiom;
import org.semanticweb.owlapi.model.OWLSubObjectPropertyOfAxiom;

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 {

    /**
     * @param args
     * @throws OWLOntologyStorageException 
     * @throws OWLOntologyCreationException 
     */

    @SuppressWarnings("unused")
    /****************************************************************
     ****************************************************************
     **
     **    Chapitre 6
     **
     ****************************************************************
     ****************************************************************/
    public static void main(String[] args) throws OWLOntologyCreationException, OWLOntologyStorageException {
            if (true) declarationDesEntites();
            if (false) classeEtIndividu();
            if (false) sousClasseDe();
            if (false) equivalenceDeCLasses();
            if (false) disjonctionDeClasses();
            if (false) objectPropertyAssertion();
            if (false) negativeObjectPropertyAssertion();
            if (false) subObjectPropertyOf();
            if (false) domaineCodomaine();
            if (false) differentIndividuals();
            if (false) sameIndividuals();
            if (false) dataPropertyAssertion();
            if (false) negativeDataPropertyAssertion();
            if (false) dataPropertyDomainRange();

    }
    /****************************************************************
     ****************************************************************
     **
     **    3. Entité et axiome ontologiques
     **
     ****************************************************************
     ****************************************************************/
    public static void declarationDesEntites() throws OWLOntologyCreationException, OWLOntologyStorageException {
        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        IRI ontologyIRI = IRI.create("http://java-ws.com/ontologies/families");
        OWLOntology ontology = manager.createOntology(ontologyIRI);
        OWLDataFactory factory = manager.getOWLDataFactory();
        OWLNamedIndividual john = factory.getOWLNamedIndividual(IRI.create(ontologyIRI + "#John"));
        OWLClass person = factory.getOWLClass(IRI.create(ontologyIRI + "#Person"));
        OWLObjectProperty hasWife = factory.getOWLObjectProperty(IRI.create(ontologyIRI + "#hasWife"));
        OWLDataProperty hasAge = factory.getOWLDataProperty(IRI.create(ontologyIRI + "#hasAge"));
        OWLDeclarationAxiom johnDeclarationAxiom = factory.getOWLDeclarationAxiom((OWLEntity) john);
        OWLDeclarationAxiom personDeclarationAxiom = factory.getOWLDeclarationAxiom(person);
        OWLDeclarationAxiom hasWifeDeclarationAxiom = factory.getOWLDeclarationAxiom(hasWife);
        OWLDeclarationAxiom hasAgeDeclarationAxiom = factory.getOWLDeclarationAxiom(hasAge);
        Set axioms = new HashSet();
        axioms.add(johnDeclarationAxiom);
        axioms.add(personDeclarationAxiom);
        axioms.add(hasWifeDeclarationAxiom);
        axioms.add(hasAgeDeclarationAxiom);
        manager.addAxioms(ontology, axioms);
        JavawsHelper.printTurtle(manager, ontology);
    }
    /****************************************************************
     ****************************************************************
     **
     **    Section 4.1 Assertion de classes
     **
     ****************************************************************
     ****************************************************************/
    public static void classeEtIndividu() throws OWLOntologyCreationException, OWLOntologyStorageException {
        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        IRI ontologyIRI = IRI.create("http://java-ws.com/ontologies/families");
        OWLOntology ontology = manager.createOntology(ontologyIRI);
        OWLDataFactory factory = manager.getOWLDataFactory();
        
        OWLIndividual mary = factory.getOWLNamedIndividual(IRI.create(ontologyIRI + "#Mary"));
        OWLClass person = factory.getOWLClass(IRI.create(ontologyIRI + "#Person"));
        OWLClassAssertionAxiom axiome = factory.getOWLClassAssertionAxiom(person, mary);
        
        manager.addAxiom(ontology, axiome);
        JavawsHelper.printTurtle(manager, ontology);
    }
    /****************************************************************
     ****************************************************************
     **
     **    Section 5.1 Sous classe de
     **
     ****************************************************************
     ****************************************************************/
    public static void sousClasseDe() throws OWLOntologyCreationException, OWLOntologyStorageException {
        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        IRI ontologyIRI = IRI.create("http://java-ws.com/ontologies/families");
        OWLDataFactory factory = manager.getOWLDataFactory();
        OWLOntology ontology = manager.createOntology(ontologyIRI);
        OWLClass person = factory.getOWLClass(IRI.create(ontologyIRI + "#Person"));
        OWLClass women = factory.getOWLClass(IRI.create(ontologyIRI + "#Woman"));
        OWLSubClassOfAxiom axiome = factory.getOWLSubClassOfAxiom(women, person);
        manager.addAxiom(ontology, axiome);
        JavawsHelper.printTurtle(manager, ontology);
    }
    /****************************************************************
     *
     * 
     *
     ****************************************************************/
    /****************************************************************
     ****************************************************************
     **
     **    Section 5.2 Équivalence de classes
     **
     ****************************************************************
     ****************************************************************/
    public static void equivalenceDeCLasses() throws OWLOntologyCreationException, OWLOntologyStorageException {
        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        IRI ontologyIRI = IRI.create("http://java-ws.com/ontologies/families");
        OWLDataFactory factory = manager.getOWLDataFactory();
        OWLOntology ontology = manager.createOntology(ontologyIRI);
        OWLClass person = factory.getOWLClass(IRI.create(ontologyIRI + "#Person"));
        OWLClass human = factory.getOWLClass(IRI.create(ontologyIRI + "#Human"));
        OWLEquivalentClassesAxiom axiome = factory.getOWLEquivalentClassesAxiom(person, human);
        manager.addAxiom(ontology, axiome);
        JavawsHelper.printTurtle(manager, ontology);
    }
    /****************************************************************
     ****************************************************************
     **
     **    Section 5.3    Classes mutuellement exclusives
     **
     ****************************************************************
     ****************************************************************/
    public static void disjonctionDeClasses() throws OWLOntologyCreationException, OWLOntologyStorageException {
        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        IRI ontologyIRI = IRI.create("http://java-ws.com/ontologies/families");
        OWLDataFactory factory = manager.getOWLDataFactory();
        OWLOntology ontology = manager.createOntology(ontologyIRI);
        OWLClass woman = factory.getOWLClass(IRI.create(ontologyIRI + "#Woman"));
        OWLClass man = factory.getOWLClass(IRI.create(ontologyIRI + "#Man"));
        OWLDisjointClassesAxiom axiome = factory.getOWLDisjointClassesAxiom(woman, man);
        manager.addAxiom(ontology, axiome);
        JavawsHelper.printTurtle(manager, ontology);
    }
    /****************************************************************
     ****************************************************************
     **
     **    Section 6.1.    Assertion de propriétés
     **
     ****************************************************************
     ****************************************************************/
    public static void objectPropertyAssertion() throws OWLOntologyCreationException, OWLOntologyStorageException {
        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        IRI ontologyIRI = IRI.create("http://java-ws.com/ontologies/families");
        OWLDataFactory factory = manager.getOWLDataFactory();
        OWLOntology ontology = manager.createOntology(ontologyIRI);
        OWLIndividual john = factory.getOWLNamedIndividual(IRI.create(ontologyIRI + "#John"));
        OWLIndividual mary = factory.getOWLNamedIndividual(IRI.create(ontologyIRI + "#Mary"));
        OWLObjectProperty hasWife = factory.getOWLObjectProperty(IRI.create(ontologyIRI + "#hasWife"));
        OWLObjectPropertyAssertionAxiom axiome = factory.getOWLObjectPropertyAssertionAxiom(hasWife, john, mary);
        manager.addAxiom(ontology, axiome);
        JavawsHelper.printTurtle(manager, ontology);
    }
    /****************************************************************
     ****************************************************************
     **
     **    Section 6.2.    Négation de propriétés
     **
     ****************************************************************
     ****************************************************************/
    public static void negativeObjectPropertyAssertion() throws OWLOntologyCreationException, OWLOntologyStorageException {
        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        IRI ontologyIRI = IRI.create("http://java-ws.com/ontologies/families");
        OWLDataFactory factory = manager.getOWLDataFactory();
        OWLOntology ontology = manager.createOntology(ontologyIRI);
        OWLIndividual bill = factory.getOWLNamedIndividual(IRI.create(ontologyIRI + "#Bill"));
        OWLIndividual mary = factory.getOWLNamedIndividual(IRI.create(ontologyIRI + "#Mary"));
        OWLObjectProperty hasWife = factory.getOWLObjectProperty(IRI.create(ontologyIRI + "#hasWife"));
        OWLNegativeObjectPropertyAssertionAxiom axiome = factory.getOWLNegativeObjectPropertyAssertionAxiom(hasWife, bill, mary);
        manager.addAxiom(ontology, axiome);
        JavawsHelper.printTurtle(manager, ontology);
    }
    /****************************************************************
     ****************************************************************
     **
     **    Section 6.3.    Hiérarchie de propriétés
     **
     ****************************************************************
     ****************************************************************/
    public static void subObjectPropertyOf() throws OWLOntologyCreationException, OWLOntologyStorageException {
        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        IRI ontologyIRI = IRI.create("http://java-ws.com/ontologies/families");
        OWLDataFactory factory = manager.getOWLDataFactory();
        OWLOntology ontology = manager.createOntology(ontologyIRI);
        OWLObjectProperty hasWife = factory.getOWLObjectProperty(IRI.create(ontologyIRI + "#hasWife"));
        OWLObjectProperty hasSpouse = factory.getOWLObjectProperty(IRI.create(ontologyIRI + "#hasSpouse"));
        OWLSubObjectPropertyOfAxiom axiome = factory.getOWLSubObjectPropertyOfAxiom(hasWife, hasSpouse);
        manager.addAxiom(ontology, axiome);
        JavawsHelper.printTurtle(manager, ontology);
    }
    /****************************************************************
     ****************************************************************
     **
     **    Section 6.4. Restriction de domaine et codomaine
     **
     ****************************************************************
     ****************************************************************/
    public static void domaineCodomaine() throws OWLOntologyCreationException, OWLOntologyStorageException {
        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        IRI ontologyIRI = IRI.create("http://java-ws.com/ontologies/families");
        OWLDataFactory factory = manager.getOWLDataFactory();
        OWLOntology ontology = manager.createOntology(ontologyIRI);
        OWLClass woman = factory.getOWLClass(IRI.create(ontologyIRI + "#Woman"));
        OWLClass man = factory.getOWLClass(IRI.create(ontologyIRI + "#Man"));
        OWLObjectProperty hasWife = factory.getOWLObjectProperty(IRI.create(ontologyIRI + "#hasWife"));
        OWLObjectPropertyRangeAxiom axiomCodomaine = factory.getOWLObjectPropertyRangeAxiom(hasWife, woman);
        OWLObjectPropertyDomainAxiom axiomDomaine = factory.getOWLObjectPropertyDomainAxiom(hasWife, man);
        Set axioms = new HashSet();
        axioms.add(axiomCodomaine);
        axioms.add(axiomDomaine);
        manager.addAxioms(ontology, axioms);
        JavawsHelper.printTurtle(manager, ontology);
    }
    /****************************************************************
     ****************************************************************
     **
     **    Section 7.1. Entités distinctes
     **
     ****************************************************************
     ****************************************************************/
    public static void differentIndividuals() throws OWLOntologyCreationException, OWLOntologyStorageException {
        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        IRI ontologyIRI = IRI.create("http://java-ws.com/ontologies/families");
        OWLDataFactory factory = manager.getOWLDataFactory();
        OWLOntology ontology = manager.createOntology(ontologyIRI);
        OWLIndividual bill = factory.getOWLNamedIndividual(IRI.create(ontologyIRI + "#Bill"));
        OWLIndividual john = factory.getOWLNamedIndividual(IRI.create(ontologyIRI + "#John"));
        OWLDifferentIndividualsAxiom axiom = factory.getOWLDifferentIndividualsAxiom(bill, john);
        manager.addAxiom(ontology, axiom);
        JavawsHelper.printTurtle(manager, ontology);
    }
    /****************************************************************
     ****************************************************************
     **
     **    Section 7.2.    Similitude
     **
     ****************************************************************
     ****************************************************************/
    public static void sameIndividuals() throws OWLOntologyCreationException, OWLOntologyStorageException {
        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        IRI ontologyIRI = IRI.create("http://java-ws.com/ontologies/families");
        OWLDataFactory factory = manager.getOWLDataFactory();
        OWLOntology ontology = manager.createOntology(ontologyIRI);
        OWLIndividual james = factory.getOWLNamedIndividual(IRI.create(ontologyIRI + "#James"));
        OWLIndividual jim = factory.getOWLNamedIndividual(IRI.create(ontologyIRI + "#Jim"));
        OWLSameIndividualAxiom axiom = factory.getOWLSameIndividualAxiom(james, jim);
        manager.addAxiom(ontology, axiom);
        JavawsHelper.printTurtle(manager, ontology);
    }
    /****************************************************************
     ****************************************************************
     **
     **    Section 8.1.    Assertion de propriétée associées à un type de données
     **
     ****************************************************************
     ****************************************************************/
    public static void dataPropertyAssertion() throws OWLOntologyCreationException, OWLOntologyStorageException {
        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        IRI ontologyIRI = IRI.create("http://java-ws.com/ontologies/families");
        OWLDataFactory factory = manager.getOWLDataFactory();
        OWLOntology ontology = manager.createOntology(ontologyIRI);
        OWLIndividual john = factory.getOWLNamedIndividual(IRI.create(ontologyIRI + "#John"));
        OWLDataProperty hasAge = factory.getOWLDataProperty(IRI.create(ontologyIRI + "#hasAge"));
        OWLDataPropertyAssertionAxiom axiom = factory.getOWLDataPropertyAssertionAxiom(hasAge, john, 51);
        manager.addAxiom(ontology, axiom);
        JavawsHelper.printTurtle(manager, ontology);
    }
    /****************************************************************
     ****************************************************************
     **
     **    Section 8.2.    Assertion d’exclusion de données
     **
     ****************************************************************
     ****************************************************************/
    public static void negativeDataPropertyAssertion() throws OWLOntologyCreationException, OWLOntologyStorageException {
        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        IRI ontologyIRI = IRI.create("http://java-ws.com/ontologies/families");
        OWLDataFactory factory = manager.getOWLDataFactory();
        OWLOntology ontology = manager.createOntology(ontologyIRI);
        OWLIndividual jack = factory.getOWLNamedIndividual(IRI.create(ontologyIRI + "#Jack"));
        OWLDataProperty hasAge = factory.getOWLDataProperty(IRI.create(ontologyIRI + "#hasAge"));
        OWLLiteral age = factory.getOWLLiteral(53);
        OWLNegativeDataPropertyAssertionAxiom axiom = factory.getOWLNegativeDataPropertyAssertionAxiom(hasAge, jack, age);
        manager.addAxiom(ontology, axiom);
        JavawsHelper.printTurtle(manager, ontology);
    }
    /****************************************************************
     ****************************************************************
     **
     **    Section 8.3.    Domaine et codomaine d’une propriété de données
     **
     ****************************************************************
     ****************************************************************/
    public static void dataPropertyDomainRange() throws OWLOntologyCreationException, OWLOntologyStorageException {
        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        IRI ontologyIRI = IRI.create("http://java-ws.com/ontologies/families");
        OWLDataFactory factory = manager.getOWLDataFactory();
        OWLOntology ontology = manager.createOntology(ontologyIRI);
        OWLClass person = factory.getOWLClass(IRI.create(ontologyIRI + "#Person"));
        OWLDataProperty hasAge = factory.getOWLDataProperty(IRI.create(ontologyIRI + "#hasAge"));
        OWLDatatype integerDatatype = factory.getIntegerOWLDatatype();
        OWLDataPropertyRangeAxiom hasAgeRange = factory.getOWLDataPropertyRangeAxiom(hasAge, integerDatatype);
        OWLDataPropertyDomainAxiom hasAgeDomain = factory.getOWLDataPropertyDomainAxiom(hasAge, person);
        Set axioms = new HashSet();
        axioms.add(hasAgeRange);
        axioms.add(hasAgeDomain);
        manager.addAxioms(ontology, axioms);
        JavawsHelper.printTurtle(manager, ontology);
    }
}

Copyright © 2018 Michel Héon. Tous droits réservés. www.cotechnoe.com