From 72b06498ed2f32d12999a6991e4e46e776841360 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Euzenat?= <Jerome.Euzenat@inria.fr>
Date: Thu, 3 Jun 2010 08:32:52 +0000
Subject: [PATCH] - checkin of the SKOS API support

---
 .../exmo/ontowrap/skosapi/SKOSOntology.java   | 339 ++++++++++++++++++
 .../ontowrap/skosapi/SKOSOntologyFactory.java | 107 ++++++
 2 files changed, 446 insertions(+)
 create mode 100644 src/fr/inrialpes/exmo/ontowrap/skosapi/SKOSOntology.java
 create mode 100644 src/fr/inrialpes/exmo/ontowrap/skosapi/SKOSOntologyFactory.java

diff --git a/src/fr/inrialpes/exmo/ontowrap/skosapi/SKOSOntology.java b/src/fr/inrialpes/exmo/ontowrap/skosapi/SKOSOntology.java
new file mode 100644
index 00000000..e8e6e9cc
--- /dev/null
+++ b/src/fr/inrialpes/exmo/ontowrap/skosapi/SKOSOntology.java
@@ -0,0 +1,339 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) INRIA, 2009-2010
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+ */
+
+package fr.inrialpes.exmo.ontowrap.skosapi;
+
+import java.net.URI;
+import java.util.AbstractSet;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+
+import org.semanticweb.skos.SKOSDataFactory;
+import org.semanticweb.skos.SKOSDataset;
+import org.semanticweb.skos.SKOSAnnotation;
+import org.semanticweb.skos.SKOSEntity;
+import org.semanticweb.skos.SKOSConcept;
+import org.semanticweb.skos.SKOSLiteral;
+import org.semanticweb.skos.SKOSUntypedLiteral;
+import org.semanticweb.skos.SKOSDataProperty;
+
+import fr.inrialpes.exmo.ontowrap.BasicOntology;
+import fr.inrialpes.exmo.ontowrap.LoadedOntology;
+import fr.inrialpes.exmo.ontowrap.OntowrapException;
+
+public class SKOSOntology extends BasicOntology<SKOSDataset> implements LoadedOntology<SKOSDataset>{
+
+    SKOSDataFactory factory;
+
+    public SKOSOntology () {
+    }
+
+    public void setFactory( SKOSDataFactory df ){
+	factory = df; // new SKOSDataFactoryImpl();
+    }
+
+    /**
+     * JE: I had not followed for a while but now SKOS is an OWL ontology that 
+     * can be interpreted in many ways. Hence a SKOS terminology is an OWL Full
+     * ontology (the S is for Simple).
+     *
+     * OK So what is in the SKOS Data model, since this is the only one that we
+     * will take into account...
+     * [SPEC: ]: 12/07/2009
+     * Object properties: skos:semanticRelation, skos:broader, skos:narrower, skos:related, skos:broaderTransitive and skos:narrowerTransitive (the transitive are obtained by the transitive closure of the "direct") braoder is the inverse of narrower.
+     * Data properties (all with lang): skos:notation
+     * Annotation properties: skos:hiddenLabel, skos:prefLabel, skos:altLabel
+     * skos:note, skos:changeNote, skos:definition, skos:editorialNote, skos:example, skos:historyNote and skos:scopeNote
+     * Other object relations: skos:mappingRelation, skos:closeMatch, skos:exactMatch, skos:broadMatch, skos:narrowMatch and skos:relatedMatch
+    **/
+
+    /**
+
+onto.getSKOSDataRelationAssertions(concept)
+assertion.getSKOSObject();
+if (literal.isTyped()) {
+SKOSTypedLiteral typedLiteral = literal.getAsSKOSTypedLiteral();
+System.out.println("\t\t" + assertion.getSKOSProperty().getURI().getFragment() + " " + literal.getLiteral() + " Type:" + typedLiteral.getDataType().getURI() );
+} else {
+SKOSUntypedLiteral untypedLiteral = literal.getAsSKOSUntypedLiteral();
+if (untypedLiteral.hasLang()) {
+lang = untypedLiteral.getLang();
+}}
+     */
+
+
+    public void getDataValues( SKOSConcept o, SKOSDataProperty p, Set<String> result ){
+	for ( SKOSLiteral lit : onto.getSKOSDataRelationByProperty( o, p ) ){
+                result.add( lit.toString() );
+	}
+    }
+		  
+    public void getDataValues( SKOSConcept o, SKOSDataProperty p, Set<String> result, String lang ){
+	for ( SKOSLiteral lit : onto.getSKOSDataRelationByProperty( o, p ) ){
+	    if ( !lit.isTyped() ) {
+		SKOSUntypedLiteral l = lit.getAsSKOSUntypedLiteral();
+		if ( l.hasLang() && l.getLang().equals( lang ) )
+		    result.add( lit.toString() );
+	    }
+	}
+    }
+		  
+    /**
+       We should document:
+
+       name < names 
+       annotations
+       comments(lang) < comments
+     **/
+
+    // NEARLY DONE
+    /**
+     * returns one of the prefLabel property values for a given SKOS concept.
+     * @param o the entity
+     * @return a label
+     * @throws OntowrapException
+     * JE// This is not satisfying because in case of several PrefLabels it will return the first one...
+     */
+    public String getEntityName( Object o ) throws OntowrapException {
+	Set<String> result = new HashSet<String>();
+	getDataValues( (SKOSConcept)o, factory.getSKOSPrefLabelProperty(), result );
+	if ( result.size() > 0 ) return result.iterator().next();
+	else return getFragmentAsLabel( getEntityURI( o ) );
+    }
+
+    /**
+     * returns one of the prefLabel property values for a given SKOS concept in a given language.
+     * @param o the entity
+     * @param lang the code of the language ("en", "fr", "es", etc.) 
+     * @return a label
+     * @throws OntowrapException
+     * JE// This is not satisfying because in case of several PrefLabels it will return the first one...
+     */
+    public String getEntityName( Object o, String lang ) throws OntowrapException {
+	Set<String> result = new HashSet<String>();
+	getDataValues( (SKOSConcept)o, factory.getSKOSPrefLabelProperty(), result, lang );
+	if ( result.size() > 0 ) return result.iterator().next();
+	else return getEntityName( o );
+    }
+
+    // DONE
+    /**
+     * Returns the values of the prefLabel and altLabel properties in a given language.
+     * @param o the entity
+     * @param lang the code of the language ("en", "fr", "es", etc.) 
+     * @return the set of labels
+     * @throws OntowrapException
+     */
+    public Set<String> getEntityNames( Object o, String lang ) throws OntowrapException {
+	Set<String> result = new HashSet<String>();
+	getDataValues( (SKOSConcept)o, factory.getSKOSPrefLabelProperty(), result, lang );
+	getDataValues( (SKOSConcept)o, factory.getSKOSAltLabelProperty(), result, lang );
+	getDataValues( (SKOSConcept)o, factory.getSKOSHiddenLabelProperty(), result, lang );
+	return result;
+    }
+
+    /**
+     * Returns the values of the prefLabel and altLabel properties.
+     * @param o the concept
+     * @return the set of labels
+     * @throws OntowrapException
+     */
+    // DONE
+    public Set<String> getEntityNames(Object o) throws OntowrapException {
+	Set<String> result = new HashSet<String>();
+	getDataValues( (SKOSConcept)o, factory.getSKOSPrefLabelProperty(), result );
+	getDataValues( (SKOSConcept)o, factory.getSKOSAltLabelProperty(), result );
+	getDataValues( (SKOSConcept)o, factory.getSKOSHiddenLabelProperty(), result );
+	if ( result.size() == 0 ) result.add( getFragmentAsLabel( getEntityURI( o ) ) );
+	return result;
+    }
+
+    // TODO
+    /**
+     * Returns the values of the "rdfs:comment" property for a given entity and for a given natural language (attribute xml:lang).
+     * @param o the entity
+     * @param lang the code of the language ("en", "fr", "es", etc.) 
+     * @return the set of comments
+     * @throws OntowrapException
+     */
+    public Set<String> getEntityComments(Object o, String lang) throws OntowrapException {
+	Set<String> comments = new HashSet<String>();
+	/*
+	OntResource or = (OntResource) o;
+	Iterator<?> i = or.listComments(lang);
+	while (i.hasNext()) {
+	    String comment = ((LiteralImpl) i.next()).getLexicalForm();
+	    comments.add(comment);
+	}
+	*/
+	return comments;
+    }
+
+    // TODO
+    /**
+     * Returns all the values of the "rdfs:comment" property for a given entity
+     * @param o the entity
+     * @return the set of comments
+     * @throws OntowrapException
+     */
+    public Set<String> getEntityComments( Object o ) throws OntowrapException {
+	return getEntityComments(o,null);
+    }
+
+    // DONE
+    /**
+     * Returns all the values of the "owl:AnnotationProperty" property for a given entity. 
+     * These annotations are those predefined in owl (owl:versionInfo, rdfs:label, rdfs:comment, rdfs:seeAlso and rdfs:isDefinedBy)
+     * In SKOS, they also are:  skos:notation
+     * Annotation properties: 
+     * skos:note, skos:changeNote, skos:definition, skos:editorialNote, 
+     * skos:example, skos:historyNote and skos:scopeNote
+     * but also all other defined annotation properties which are subClass of "owl:AnnotationProperty"
+     * @param o the entity
+     * @return the set of annotation values
+     * @throws OntowrapException
+     */
+    public Set<String> getEntityAnnotations( Object o ) throws OntowrapException {
+	Set<String> annots = new HashSet<String>();
+	for( SKOSAnnotation ann : ((SKOSConcept)o).getSKOSAnnotations( onto ) ){
+	    if( ann.isAnnotationByConstant() ){
+		SKOSLiteral lit = ann.getAnnotationValueAsConstant();
+		if( (lit != null) && !(lit.isTyped()) ){
+		    annots.add( lit.getAsSKOSUntypedLiteral().toString() );
+		}
+	    }
+	}
+	return annots;
+    }
+
+    // DONE
+    /**
+     * There is no languages on annotations in SKOS API
+     * Hence we return all of them
+     */
+    public Set<String> getEntityAnnotations( Object o, String lang ) throws OntowrapException {
+	return getEntityAnnotations( o );
+    }
+
+    // DONE
+    public Object getEntity( URI u ) throws OntowrapException {
+	return factory.getSKOSConcept( u );
+    }
+    
+    public URI getEntityURI( Object o ) throws OntowrapException {
+	if ( o instanceof SKOSConcept ) {
+	    return ((SKOSConcept)o).getURI();
+	} else {
+	    throw new OntowrapException( o+" is not a SKOSConcept" );
+	}
+    }
+
+    /* all done here */
+
+    //@SuppressWarnings("unchecked")
+    public Set<?> getClasses() {
+	return onto.getSKOSConcepts();
+    }
+
+    //@SuppressWarnings("unchecked")
+    public Set<?> getDataProperties() {
+	return null; // null or new HashSet()
+    }
+
+    //@SuppressWarnings("unchecked")
+    public Set<?> getEntities() {
+	return getClasses();
+    }
+
+    //@SuppressWarnings("unchecked")
+    public Set<?> getIndividuals() {
+	return null;
+    }
+
+    //@SuppressWarnings("unchecked")
+    public Set<?> getObjectProperties() {
+	return null;
+    }
+
+    //@SuppressWarnings("unchecked")
+    public Set<?> getProperties() {
+	return null;
+    }
+
+    public boolean isClass(Object o) {
+	return o instanceof SKOSConcept;
+    }
+
+    public boolean isDataProperty(Object o) {
+	return false;
+    }
+
+    public boolean isEntity(Object o) {
+	return isClass(o);
+    }
+
+    public boolean isIndividual(Object o) {
+	return false;
+    }
+
+    public boolean isObjectProperty(Object o) {
+	return false;
+    }
+
+    public boolean isProperty(Object o) {
+	return false;
+    }
+
+    public int nbEntities() {
+	return this.getEntities().size();
+    }
+
+    public int nbClasses() {
+	return this.getClasses().size();
+    }
+
+    public int nbDataProperties() {
+	return 0;
+    }
+
+    public int nbIndividuals() {
+	return 0;
+    }
+
+    public int nbObjectProperties() {
+	return 0;
+    }
+
+    public int nbProperties() {
+	return 0;
+    }
+
+    public void unload() {
+    }
+
+    /** THESE ARE HEAVY LOADED PRIMITIVES
+	o.getSKOSBroaderConcepts( onto )
+	o.getSKOSBroaderTransitiveConcepts( onto )
+	o.getSKOSNarrowerConcepts( onto )
+	o.getSKOSNarrowerTransitiveConcepts( onto )
+     **/
+
+}
diff --git a/src/fr/inrialpes/exmo/ontowrap/skosapi/SKOSOntologyFactory.java b/src/fr/inrialpes/exmo/ontowrap/skosapi/SKOSOntologyFactory.java
new file mode 100644
index 00000000..3bf34356
--- /dev/null
+++ b/src/fr/inrialpes/exmo/ontowrap/skosapi/SKOSOntologyFactory.java
@@ -0,0 +1,107 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) INRIA, 2009-2010
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+ */
+
+package fr.inrialpes.exmo.ontowrap.skosapi;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.NoSuchElementException;
+
+import org.semanticweb.skosapibinding.SKOSManager;
+import org.semanticweb.skos.SKOSCreationException;
+import org.semanticweb.skos.SKOSDataset;
+import org.semanticweb.skos.SKOSDataFactory;
+
+import fr.inrialpes.exmo.ontowrap.LoadedOntology;
+import fr.inrialpes.exmo.ontowrap.OntologyFactory;
+import fr.inrialpes.exmo.ontowrap.OntologyCache;
+import fr.inrialpes.exmo.ontowrap.OntowrapException;
+
+public class SKOSOntologyFactory extends OntologyFactory {
+
+    private static URI formalismUri = null;
+    private static String formalismId = "SKOS1.0";
+    private static OntologyCache<SKOSOntology> cache = null;
+
+    private SKOSManager manager;
+    private SKOSDataFactory factory;
+
+    public SKOSOntologyFactory() throws OntowrapException {
+	try { 
+	    formalismUri = new URI("http://www.w3.org/2004/02/skos/core#");
+	} catch (URISyntaxException ex) { ex.printStackTrace(); } // should not happen
+	try {
+	    manager = new SKOSManager();
+	} catch (SKOSCreationException sce) {
+	    throw new OntowrapException( "Cannot initialise SKOSManager ", sce);
+	}
+	factory = manager.getSKOSDataFactory();
+    }
+
+    @Override
+    public SKOSOntology newOntology( Object ontology ) throws OntowrapException {
+	if ( ontology instanceof SKOSDataset ) {
+	    SKOSOntology onto = null;
+	    onto = new SKOSOntology();
+	    onto.setFormalism( formalismId );
+	    onto.setFormURI( formalismUri );
+	    onto.setOntology( (SKOSDataset)ontology );
+	    onto.setFactory( factory );
+	    // This is the URI of the corresponding OWL API Ontology
+	    URI uri = ((SKOSDataset)ontology).getURI();
+	    onto.setURI( uri );
+	    cache.recordOntology( uri, onto );
+	    return onto;
+	} else {
+	    throw new OntowrapException( "Argument is not an SKOSDataset: "+ontology );
+	}
+    }
+
+    @Override
+    public SKOSOntology loadOntology( URI uri ) throws OntowrapException {
+	SKOSOntology onto = null;
+	onto = cache.getOntologyFromURI( uri );
+	if ( onto != null ) return onto;
+	onto = cache.getOntology( uri );
+	if ( onto != null ) return onto;
+	SKOSDataset dataset = null;
+	try {
+	    dataset = manager.loadDataset( uri );
+	} catch (SKOSCreationException sce) {
+	    throw new OntowrapException( "Cannot load ontology: "+uri, sce);
+	}
+	onto = new SKOSOntology();
+	onto.setFormalism( formalismId );
+	onto.setFormURI( formalismUri );
+	onto.setOntology( dataset );
+	onto.setFactory( factory );
+	onto.setFile( uri );
+	// This is the URI of the corresponding OWL API Ontology
+	onto.setURI( dataset.getURI() );
+	cache.recordOntology( uri, onto );
+	return onto;
+    }
+
+    @Override
+    public void clearCache() {
+	cache.clear();
+    }
+
+}
-- 
GitLab