/*
* 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.File;
import java.util.Collections;
import org.coode.owlapi.turtle.TurtleOntologyFormat;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.io.StreamDocumentTarget;
import org.semanticweb.owlapi.io.StringDocumentSource;
import org.semanticweb.owlapi.model.AddImport;
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.OWLDeclarationAxiom;
import org.semanticweb.owlapi.model.OWLEntity;
import org.semanticweb.owlapi.model.OWLImportsDeclaration;
import org.semanticweb.owlapi.model.OWLNamedIndividual;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyFormat;
import org.semanticweb.owlapi.model.OWLOntologyLoaderConfiguration;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.model.OWLOntologyStorageException;
import org.semanticweb.owlapi.model.OWLSubClassOfAxiom;
import org.semanticweb.owlapi.model.PrefixManager;
import org.semanticweb.owlapi.model.UnknownOWLOntologyException;
import org.semanticweb.owlapi.util.AutoIRIMapper;
import org.semanticweb.owlapi.util.DefaultPrefixManager;
import org.semanticweb.owlapi.util.OWLEntityRemover;
import org.semanticweb.owlapi.util.SimpleIRIMapper;
import org.semanticweb.owlapi.vocab.PrefixOWLOntologyFormat;
import com.java_ws.owlapi.exemple.util.JavawsHelper;
@SuppressWarnings("unused")
/** Auteur: Michel Héon PhD
* Cotechnoe http://www.cotechnoe.com
* Date: 15-jan-2014
*/
/****************************************************************
****************************************************************
**
** Chapitre 4
**
****************************************************************
****************************************************************/
public class ChapIV {
private static final String JAVAWS_RACINE = "/JavaWebSemantique/workspace/";
private static final String JAVAWS_RACINE_IRI = "file:///C:"+JAVAWS_RACINE;
public static void main(String[] args) throws OWLOntologyCreationException, OWLOntologyStorageException {
// Section 3 créer un IRI
if (false) creer_IRI();
// Section 5.1 Créer une ontologie
if (false) creer_ontologie();
// Section 5.2 Charger une ontologie
if (false) charger_ontologie_FICHIER();
if (false) charger_ontologie_WEB();
// Section 5.3 Charger la ressource locale correspondant à la l'IRI web
if (false) charger_Ontologie_correspondance_WEB_LOCALE();
// Section 6. Imprimer le contenu d’une ontologie
if (false) imprimer_ontologie();
// section 7 Charger une ontologie d’un document ontologique
if (false) charger_ontologie_DocumentSource();
// Section 8 créer des entités dans une ontologie
if (false) creer_une_entite_ontologique();
// Section 9 Manipuler l’entête du document ontologique
if (false) importer_une_ontologie();
if (false) gerer_les_prefix();
// Section 11 Créer l'ontologie et ses éléments
if (false) ajout_recherche_suppression_entites();
}
/****************************************************************
****************************************************************
**
** Section 3. Créer une IRI
**
****************************************************************
****************************************************************/
public static void creer_IRI() {
IRI pizza_http_iri = IRI.create("http://owl.cs.manchester.ac.uk/co-ode-files/ontologies/pizza.owl");
IRI pizza_file_iri = IRI.create("file:///C:/JavaWebSemantique/workspace/com.javaws.ontologie/WebContent/pizza.owl");
IRI pizza_file_full_iri = IRI.create("pizza", "http://owl.cs.manchester.ac.uk/co-ode-files/ontologies/pizza.owl");
System.out.println(pizza_http_iri.toString());
System.out.println(pizza_file_iri.toString());
System.out.println(pizza_file_full_iri.toString());
}
/****************************************************************
****************************************************************
**
** Section 5.1 Créer une ontologie
** Créer un objet de type OWLOntology
**
****************************************************************
****************************************************************/
public static void creer_ontologie() throws OWLOntologyCreationException, OWLOntologyStorageException {
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
IRI myFamilyIRI = IRI.create("http://java-ws.com/ontologies/myfamily");
OWLOntology myFamilyOntology = manager.createOntology(myFamilyIRI);
JavawsHelper.printXMLOntology(manager, myFamilyOntology);
JavawsHelper.printFunctionalOWL(manager, myFamilyOntology);
JavawsHelper.printManchesterOWL(manager, myFamilyOntology);
JavawsHelper.printRDFXML(manager, myFamilyOntology);
JavawsHelper.printTurtle(manager, myFamilyOntology);
}
/****************************************************************
****************************************************************
**
** Section 5.2 Charger la ressource à partir d'un fichier
**
****************************************************************
****************************************************************/
public static void charger_ontologie_FICHIER() throws OWLOntologyCreationException, OWLOntologyStorageException{
String PIZZA = JAVAWS_RACINE_IRI + "com.javaws.ontologie/WebContent/pizza.owl";
IRI pizzaIRI = IRI.create(PIZZA);
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLOntology pizzaOntology = manager.loadOntology(pizzaIRI);
JavawsHelper.printXMLOntology(manager, pizzaOntology);
}
/****************************************************************
****************************************************************
**
** Section 5.2 Charger la ressource locale correspondant à la l'IRI web
** Créer un objet de type OWLOntology
**
****************************************************************
****************************************************************/
public static void charger_ontologie_WEB() throws OWLOntologyCreationException, OWLOntologyStorageException{
String PIZZA = "http://130.88.198.11/co-ode-files/ontologies/pizza.owl";
IRI pizzaIRI = IRI.create(PIZZA);
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLOntology pizzaOntology = manager.loadOntology(pizzaIRI);
JavawsHelper.printTurtle(manager, pizzaOntology);
}
/****************************************************************
****************************************************************
**
** Section 5.3 Charger la ressource locale correspondant à la l'IRI web
**
****************************************************************
****************************************************************/
public static void charger_Ontologie_correspondance_WEB_LOCALE() throws OWLOntologyCreationException, OWLOntologyStorageException{
/*
* Nom local et web de l'ontologie
*/
String FAMILIES_WEB = "http://example.com/owl/families";
String FAMILIES_LOCALE = JAVAWS_RACINE_IRI + "com.javaws.ontologie/WebContent/families.owl";
/*
* IRI web et local de l'ontologie families
*/
IRI familiesIRILocal = IRI.create(FAMILIES_LOCALE);
IRI familiesIRIWeb = IRI.create(FAMILIES_WEB);
/*
* Association entre le l'IRI web et l'IRI local de l'ontoloige families
*/
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
SimpleIRIMapper familiesMapper = new SimpleIRIMapper(familiesIRIWeb, familiesIRILocal);
manager.addIRIMapper(familiesMapper);
/*
* Chargement et impression de l'ontologie families
*/
OWLOntology familiesOntology = manager.loadOntology(familiesIRIWeb);
OWLOntologyFormat ontologySyntaxFormat = new TurtleOntologyFormat();
manager.saveOntology(familiesOntology, ontologySyntaxFormat, new StreamDocumentTarget(System.out));
}
/****************************************************************
****************************************************************
**
** Section 6 Imprimer le contenu d’une ontologie
**
****************************************************************
****************************************************************/
public static void imprimer_ontologie() throws OWLOntologyCreationException, UnknownOWLOntologyException, OWLOntologyStorageException{
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
String FAMILIES_LOCALE = JAVAWS_RACINE_IRI + "com.javaws.ontologie/WebContent/families.owl";
IRI familiesIRI = IRI.create(FAMILIES_LOCALE);
OWLOntology ont = manager.loadOntology(familiesIRI);
JavawsHelper.printTurtle(manager,ont );
JavawsHelper.printFunctionalOWL(manager,ont );
JavawsHelper.printManchesterOWL(manager,ont );
JavawsHelper.printRDFXML(manager,ont );
JavawsHelper.printXMLOntology(manager,ont );
}
/****************************************************************
****************************************************************
**
** section 7 Charger une ontologie d’un document ontologique
**
****************************************************************
****************************************************************/
public static void charger_ontologie_DocumentSource() throws OWLOntologyCreationException, OWLOntologyStorageException{
String myFamilyStringOnto =
"@prefix : //java-ws.com/ontologie/myfamily#> . \n" +
"@prefix owl: //www.w3.org/2002/07/owl#> . \n" +
"@prefix rdf: //www.w3.org/1999/02/22-rdf-syntax-ns#> . \n" +
"@prefix xml: //www.w3.org/XML/1998/namespace> . \n" +
"@prefix xsd: //www.w3.org/2001/XMLSchema#> . \n" +
"@prefix rdfs: //www.w3.org/2000/01/rdf-schema#> . \n" +
"@base //java-ws.com/ontologie/myfamily> . \n" +
"//java-ws.com/ontologie/myfamily> rdf:type owl:Ontology . \n" +
":Mary rdf:type owl:NamedIndividual . \n" ;
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
StringDocumentSource myFamilyDocString = new StringDocumentSource(myFamilyStringOnto);
OWLOntology myFamilyOntology = manager.loadOntologyFromOntologyDocument(myFamilyDocString);
JavawsHelper.printFunctionalOWL(manager, myFamilyOntology);
}
/****************************************************************
****************************************************************
**
** Section 8 créer des entités dans une ontologie
**
****************************************************************
****************************************************************/
public static void creer_une_entite_ontologique() throws OWLOntologyCreationException, OWLOntologyStorageException{
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
IRI myFamilyIRI = IRI.create("http://java-ws.com/ontologie/myfamily");
OWLOntology myFamilyOntology = manager.createOntology(myFamilyIRI);
OWLDataFactory factory = manager.getOWLDataFactory();
OWLNamedIndividual mary = factory.getOWLNamedIndividual(IRI.create(myFamilyIRI + "#Mary"));
OWLDeclarationAxiom maryDeclarationAxiom = factory.getOWLDeclarationAxiom((OWLEntity) mary);
manager.addAxiom(myFamilyOntology, maryDeclarationAxiom);
OWLOntologyFormat ontologySyntaxFormat = new TurtleOntologyFormat();
manager.saveOntology(myFamilyOntology, ontologySyntaxFormat, new StreamDocumentTarget(System.out));
}
/****************************************************************
****************************************************************
**
** Section 9 Manipuler l’entête du document ontologique
**
****************************************************************
****************************************************************/
public static void importer_une_ontologie() throws OWLOntologyCreationException, OWLOntologyStorageException{
//
// Indiquer l'emplacement des ontolologies
//
String MY_FAMILY_LOCAL_IRI = JAVAWS_RACINE ;
String MY_FAMILY_IRI = "http://java-ws.com/ontologie/myfamily"; //nom de l'ontologie factuelle
String FAMILIES_IRI = "http://java-ws.com/ontologie/families"; //nom de l'ontologie conceptuelle
File ontologiesDirectory=new File(MY_FAMILY_LOCAL_IRI);
IRI familiesIRI = IRI.create(FAMILIES_IRI);
IRI myFamilyIRI = IRI.create(MY_FAMILY_IRI);
//
// Associer le nom de base de l'ontologie avec son nom d'emplacement local
//
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
AutoIRIMapper iriMapper = new AutoIRIMapper(ontologiesDirectory,true);
manager.addIRIMapper(iriMapper);
//
// Créer l'ontologie courante et importer l'ontologie conceptuelle
//
OWLOntology myFamilyOntology = manager.createOntology(myFamilyIRI);
OWLOntology familiesOntology = manager.loadOntology(familiesIRI);
//
// Créer la déclaration de l'import et appliquer le chancement
//
OWLDataFactory factory = manager.getOWLDataFactory();
OWLImportsDeclaration importDecl = factory.getOWLImportsDeclaration(familiesIRI);
AddImport addImport = new AddImport(myFamilyOntology, importDecl);
manager.applyChange(addImport);
manager.makeLoadImportRequest(importDecl, new OWLOntologyLoaderConfiguration());
//
// Créer l'ontologie factuelle
//
OWLNamedIndividual mary = factory.getOWLNamedIndividual(IRI.create(myFamilyIRI + "#Mary"));
OWLDeclarationAxiom maryDeclarationAxiom = factory.getOWLDeclarationAxiom((OWLEntity) mary);
OWLClass womanDeclAxiom = factory.getOWLClass(IRI.create(familiesIRI+"#Woman"));
OWLClassAssertionAxiom maryIsAWoman = factory.getOWLClassAssertionAxiom(womanDeclAxiom, mary);
manager.addAxiom(myFamilyOntology, maryIsAWoman);
manager.addAxiom(myFamilyOntology, maryDeclarationAxiom);
//
// Imprimer le contenu de l'ontologie factuelle et le contenu de l'ontologie conceptuelle importée
//
JavawsHelper.printTurtle(manager, myFamilyOntology);
for (OWLOntology importedOntology : myFamilyOntology.getImports()) {
System.out.println("IMPORTED ONTOLOGY");
JavawsHelper.printTurtle(manager, importedOntology);
}
}
/****************************************************************
****************************************************************
**
** Section 9.1.6 Ajuster les Préfix
**
****************************************************************
****************************************************************/
public static void gerer_les_prefix() throws OWLOntologyCreationException, OWLOntologyStorageException{
String MY_FAMILY_LOCAL_IRI = JAVAWS_RACINE ;
String MY_FAMILY_IRI = "http://java-ws.com/ontologie/myfamily"; //nom de l'ontologie factuelle
String FAMILIES_IRI = "http://java-ws.com/ontologie/families"; //nom de l'ontologie conceptuelle
File ontologiesDirectory=new File(MY_FAMILY_LOCAL_IRI);
IRI familiesIRI = IRI.create(FAMILIES_IRI);
IRI myFamilyIRI = IRI.create(MY_FAMILY_IRI);
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLDataFactory factory = manager.getOWLDataFactory();
AutoIRIMapper iriMapper = new AutoIRIMapper(ontologiesDirectory,true);
manager.addIRIMapper(iriMapper);
OWLOntology myFamilyOntology = manager.createOntology(myFamilyIRI);
OWLOntology familiesOntology = manager.loadOntology(familiesIRI);
OWLOntologyLoaderConfiguration config = new OWLOntologyLoaderConfiguration();
config=config.setStrict(true);
config.setLoadAnnotationAxioms(true);
OWLImportsDeclaration importDecl = factory.getOWLImportsDeclaration(familiesIRI);
AddImport addImport = new AddImport(myFamilyOntology, importDecl);
manager.applyChange(addImport);
manager.makeLoadImportRequest(importDecl, config);
OWLNamedIndividual mary = factory.getOWLNamedIndividual(IRI.create(myFamilyIRI + "#Mary"));
OWLDeclarationAxiom maryDeclarationAxiom = factory.getOWLDeclarationAxiom((OWLEntity) mary);
OWLClass womanDeclAxiom = factory.getOWLClass(IRI.create(familiesIRI+"#Woman"));
OWLClassAssertionAxiom maryIsAWoman = factory.getOWLClassAssertionAxiom(womanDeclAxiom, mary);
manager.addAxiom(myFamilyOntology, maryIsAWoman);
manager.addAxiom(myFamilyOntology, maryDeclarationAxiom);
PrefixOWLOntologyFormat familyOntologyFormat = new TurtleOntologyFormat();
familyOntologyFormat.setDefaultPrefix(MY_FAMILY_IRI + "#");
familyOntologyFormat.setPrefix( "myFamily",MY_FAMILY_IRI + "#");
familyOntologyFormat.setPrefix("families", FAMILIES_IRI + "#");
manager.saveOntology(myFamilyOntology, familyOntologyFormat, new StreamDocumentTarget(System.out));
for (OWLOntology importedOntology : myFamilyOntology.getImports()) {
System.out.println("IMPORTED ONTOLOGY");
manager.saveOntology(importedOntology, familyOntologyFormat, new StreamDocumentTarget(System.out));
}
}
/****************************************************************
****************************************************************
**
** Section 11 Ajouter - rechercher et supprimer des entités ontologiques
**
****************************************************************
****************************************************************/
public static void ajout_recherche_suppression_entites() throws OWLOntologyCreationException, OWLOntologyStorageException {
/*
* Créer le gestionnaire, la fabrique et l'ontologie
*/
String myFamilyName = "http://java-ws.com/ontologies/myfamily";
IRI myFamilyIRI = IRI.create(myFamilyName);
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLOntology myFamilyOntology = manager.createOntology(myFamilyIRI);
OWLDataFactory factory = manager.getOWLDataFactory();
/*
* Créer les entités ontologiques
*/
OWLClass woman = factory.getOWLClass(IRI.create(myFamilyIRI+"#Woman"));
OWLNamedIndividual mary = factory.getOWLNamedIndividual(IRI.create(myFamilyIRI + "#Mary"));
PrefixManager myFamPrefix = new DefaultPrefixManager(
myFamilyName +"#");
OWLClass person = factory.getOWLClass(":Person", myFamPrefix);
/*
* Créer la axiomes
*/
OWLDeclarationAxiom maryDeclarationAxiom = factory.getOWLDeclarationAxiom((OWLEntity) mary);
OWLDeclarationAxiom womanDeclarationAxiom = factory.getOWLDeclarationAxiom((OWLEntity) woman);
OWLDeclarationAxiom personDeclarationAxiom = factory.getOWLDeclarationAxiom((OWLEntity) person);
OWLSubClassOfAxiom womanKindOfPersonAxiom = factory.getOWLSubClassOfAxiom(woman, person);
OWLClassAssertionAxiom maryIsAWoman = factory.getOWLClassAssertionAxiom(woman, mary);
/*
* Ajouter les axiomes à l'ontologie
*/
manager.addAxiom(myFamilyOntology, maryDeclarationAxiom);
manager.addAxiom(myFamilyOntology, womanDeclarationAxiom);
manager.addAxiom(myFamilyOntology, personDeclarationAxiom);
manager.addAxiom(myFamilyOntology, maryIsAWoman);
manager.addAxiom(myFamilyOntology, womanKindOfPersonAxiom);
JavawsHelper.printTurtle(manager,myFamilyOntology );
/*
* Rechercher un élément ontologique et le supprimer de l'ontologie
*/
System.out.println("Ontology content BEFORE remove ");
OWLEntityRemover remover = new OWLEntityRemover(manager, Collections.singleton(myFamilyOntology));
for (OWLEntity entity : myFamilyOntology.getSignature()) {
System.out.println("entity IRI " + entity.getIRI());
if ( entity.getIRI().equals(IRI.create(myFamilyIRI+"#Woman"))) {
System.out.println("entity IRI to remove " + entity.getIRI());
entity.accept(remover);
}
}
for (OWLAxiom axiom : myFamilyOntology.getAxioms()) {
System.out.println("axiom " + axiom.toString());
}
manager.applyChanges(remover.getChanges());
System.out.println("\nOntology content AFTER remove ");
for (OWLEntity entity : myFamilyOntology.getSignature()) {
System.out.println("entity IRI " + entity.getIRI());
}
for (OWLAxiom axiom : myFamilyOntology.getAxioms()) {
System.out.println("axiom " + axiom.toString());
}
remover.reset();
JavawsHelper.printTurtle(manager,myFamilyOntology );
}
}