/*
* 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 org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.AddOntologyAnnotation;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLAnnotation;
import org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom;
import org.semanticweb.owlapi.model.OWLAnnotationProperty;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLDeclarationAxiom;
import org.semanticweb.owlapi.model.OWLLiteral;
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.OWLSubAnnotationPropertyOfAxiom;
import org.semanticweb.owlapi.vocab.OWLRDFVocabulary;
import com.java_ws.owlapi.exemple.util.JavawsHelper;
/** Auteur: Michel Héon PhD
* Cotechnoe http://www.cotechnoe.com
* Date: 15-jan-2014
*/
@SuppressWarnings("unused")
public class ChapIX {
/****************************************************************
****************************************************************
**
** Chapitre 9
**
****************************************************************
****************************************************************/
public static void main(String[] args) throws OWLOntologyCreationException, OWLOntologyStorageException {
// Section 2. Annotation d’ontologie
if (true) annoterOntologie();
// Section 3. Axiomes d’annotation et assertions
if (false) annoterUneEntite();
// Section 4. Recherche d’annotations
if (false) rechercherLesAnnotations();
// Section 5. Création d’une propriété d’annotation
if (false) creerUneAnnotation();
}
/****************************************************************
****************************************************************
**
** Section 2. Annotation d’ontologie
**
****************************************************************
****************************************************************/
public static void annoterOntologie() 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();
// ajouter un commentaire
OWLLiteral ontoLit = factory.getOWLLiteral("Ontologie de la famille", "fr");
OWLAnnotation ontoLabel = factory.getOWLAnnotation(
factory.getOWLAnnotationProperty(OWLRDFVocabulary.RDFS_COMMENT.getIRI()),
ontoLit);
manager.applyChange(new AddOntologyAnnotation(ontology, ontoLabel));
// ajouter une annotation de version
OWLAnnotation versionAnno = factory.getOWLAnnotation(
factory.getOWLAnnotationProperty(OWLRDFVocabulary.OWL_VERSION_INFO.getIRI()),
factory.getOWLLiteral("Version initiale", "fr"));
manager.applyChange(new AddOntologyAnnotation(ontology, versionAnno));
// Ajouter une étiquette
OWLAnnotation labelAnnoFr = factory.getOWLAnnotation(
factory.getOWLAnnotationProperty(OWLRDFVocabulary.RDFS_LABEL.getIRI()),
factory.getOWLLiteral("famille", "fr"));
manager.applyChange(new AddOntologyAnnotation(ontology, labelAnnoFr));
OWLAnnotation labelAnnoEn = factory.getOWLAnnotation(
factory.getRDFSLabel(), factory.getOWLLiteral("families", "en"));
manager.applyChange(new AddOntologyAnnotation(ontology, labelAnnoEn));
// Indiquer la source de l'information
OWLAnnotation isDefinedByAnno = factory.getOWLAnnotation(
factory.getRDFSIsDefinedBy(), factory.getOWLLiteral("Cotechnoe inc. http://www.cotechnoe.com", "fr"));
manager.applyChange(new AddOntologyAnnotation(ontology, isDefinedByAnno));
JavawsHelper.printTurtle(manager, ontology);
JavawsHelper.printFunctionalOWL(manager, ontology);
}
/****************************************************************
****************************************************************
**
** Section 3. Axiomes d’annotation et assertions
**
****************************************************************
****************************************************************/
public static OWLOntology annoterUneEntite() 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();
OWLClass person = factory.getOWLClass(IRI.create(ontologyIRI + "#Person"));
OWLDeclarationAxiom personAxiom = factory.getOWLDeclarationAxiom(person);
OWLAnnotation annotationEn = factory.getOWLAnnotation(factory.getRDFSComment(),
factory.getOWLLiteral("Represents the set of all people", "en"));
OWLAnnotation annotationFr = factory.getOWLAnnotation(factory.getRDFSComment(),
factory.getOWLLiteral("Représente l'ensemble de tous les humains", "fr"));
OWLAnnotation labelAnno = factory.getOWLAnnotation(factory.getRDFSLabel(),
factory.getOWLLiteral("Personne", "fr"));
OWLAnnotation seeAlsoFr = factory.getOWLAnnotation(factory.getRDFSSeeAlso(),
factory.getOWLLiteral("Voir aussi humain", "fr"));
OWLAnnotationAssertionAxiom annotationAxiomEn = factory.getOWLAnnotationAssertionAxiom(person.getIRI(), annotationEn);
OWLAnnotationAssertionAxiom annotationAxiomfr = factory.getOWLAnnotationAssertionAxiom(person.getIRI(), annotationFr);
OWLAnnotationAssertionAxiom labelAxiomfr = factory.getOWLAnnotationAssertionAxiom(person.getIRI(), labelAnno);
OWLAnnotationAssertionAxiom seeAlsoFrAxiom = factory.getOWLAnnotationAssertionAxiom(person.getIRI(), seeAlsoFr);
manager.addAxiom(ontology, personAxiom);
manager.addAxiom(ontology, annotationAxiomEn);
manager.addAxiom(ontology, annotationAxiomfr);
manager.addAxiom(ontology, labelAxiomfr);
manager.addAxiom(ontology, seeAlsoFrAxiom);
JavawsHelper.printTurtle(manager, ontology);
JavawsHelper.printFunctionalOWL(manager, ontology);
return ontology;
}
/****************************************************************
****************************************************************
**
** Section 4. Recherche d’annotations
**
****************************************************************
****************************************************************/
public static void rechercherLesAnnotations() throws OWLOntologyCreationException, OWLOntologyStorageException {
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLDataFactory factory = manager.getOWLDataFactory();
OWLOntology ontology = annoterUneEntite();
OWLAnnotationProperty label = factory.getOWLAnnotationProperty(OWLRDFVocabulary.RDFS_LABEL.getIRI());
OWLAnnotationProperty comment = factory.getRDFSComment();
for (OWLClass cls : ontology.getClassesInSignature()) {
for (OWLAnnotation annotation : cls.getAnnotations(ontology, label)) {
if (annotation.getValue() instanceof OWLLiteral) {
OWLLiteral val = (OWLLiteral) annotation.getValue();
if (val.hasLang("fr")) {
System.out.println(cls + " -label-> " + val.getLiteral());
}
}
}
for (OWLAnnotation annotation : cls.getAnnotations(ontology, comment)) {
if (annotation.getValue() instanceof OWLLiteral) {
OWLLiteral val = (OWLLiteral) annotation.getValue();
if (val.hasLang("fr")) {
System.out.println(cls + " -comment-> " + val.getLiteral());
}
}
}
}
}
/****************************************************************
****************************************************************
**
** Section 5. Création d’une propriété d’annotation
**
****************************************************************
****************************************************************/
public static void creerUneAnnotation() 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();
OWLClass person = factory.getOWLClass(IRI.create(ontologyIRI + "#Person"));
OWLDeclarationAxiom personAxiom = factory.getOWLDeclarationAxiom(person);
manager.addAxiom(ontology, personAxiom);
OWLAnnotationProperty name = factory.getOWLAnnotationProperty(IRI.create(ontologyIRI + "#name"));
OWLAnnotation namePersonne = factory.getOWLAnnotation(name, factory.getOWLLiteral("Personne", "fr"));
OWLSubAnnotationPropertyOfAxiom nameProp = factory.getOWLSubAnnotationPropertyOfAxiom(name, factory.getRDFSLabel());
OWLAnnotationAssertionAxiom annotationAxiomFr = factory.getOWLAnnotationAssertionAxiom(person.getIRI(), namePersonne);
manager.addAxiom(ontology, annotationAxiomFr);
manager.addAxiom(ontology, nameProp);
JavawsHelper.printTurtle(manager, ontology);
JavawsHelper.printFunctionalOWL(manager, ontology);
}
}