diff --git a/src/fr/inrialpes/exmo/align/onto/BasicOntology.java b/src/fr/inrialpes/exmo/align/onto/BasicOntology.java new file mode 100644 index 0000000000000000000000000000000000000000..f566717de03385b73a3065a2da16b3f9b0aa1972 --- /dev/null +++ b/src/fr/inrialpes/exmo/align/onto/BasicOntology.java @@ -0,0 +1,49 @@ +/* + * $Id: Ontology.java 643 2008-02-23 11:16:09Z euzenat $ + * + * Copyright (C) INRIA Rhône-Alpes, 2007-2008 + * + * 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.align.onto; + +import java.net.URI; + +/** + * Store the information regarding ontologies in a specific structure + */ + +public class BasicOntology<O> implements Ontology<O> { + protected URI uri = null; + protected URI file = null; + protected URI formalismURI = null; + protected String formalism = null; + protected O onto = null; + + public BasicOntology() {}; + + public URI getURI() { return uri; } + public URI getFile() { return file; } + public URI getFormURI() { return formalismURI; } + public String getFormalism() { return formalism; } + public O getOntology() { return onto; } + + public void setURI( URI uri ) { this.uri = uri; } + public void setFile( URI file ) { this.file = file; } + public void setFormURI( URI u ) { formalismURI = u; } + public void setFormalism( String name ) { formalism = name; } + public void setOntology( O o ) { this.onto = o; } +} diff --git a/src/fr/inrialpes/exmo/align/onto/ConcreteOntology.java b/src/fr/inrialpes/exmo/align/onto/ConcreteOntology.java new file mode 100644 index 0000000000000000000000000000000000000000..db46594290359e26a19e7f886448ff71e7dba151 --- /dev/null +++ b/src/fr/inrialpes/exmo/align/onto/ConcreteOntology.java @@ -0,0 +1,46 @@ +/* + * $Id$ + * + * Copyright (C) INRIA Rhône-Alpes, 2003-2008 + * + * 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.align.onto; + +import java.net.URI; +import java.util.Iterator; +import org.xml.sax.SAXException; + +import org.semanticweb.owl.align.AlignmentException; + +/** + * Store the information regarding ontologies in a specific structure + */ + +public abstract class ConcreteOntology<O> extends BasicOntology<O> implements LoadedOntology<O> { + + public abstract Iterator<Object> getObjectProperties() throws AlignmentException; + public abstract Iterator<URI> getClassNames() throws AlignmentException; + public abstract Iterator<URI> getObjectPropertyNames() throws AlignmentException; + public abstract Iterator<URI> getDataPropertyNames() throws AlignmentException; + public abstract Iterator<URI> getInstanceNames() throws AlignmentException; + public abstract int nbClasses() throws AlignmentException; + public abstract int nbDataProperties() throws AlignmentException; + public abstract int nbObjectProperties() throws AlignmentException; + public abstract int nbInstances() throws AlignmentException; + public abstract Object getEntity( URI uri ) throws AlignmentException; + //public abstract void loadOntology( URI ref, OntologyCache ontologies ) throws SAXException, AlignmentException; +} diff --git a/src/fr/inrialpes/exmo/align/onto/LoadedOntology.java b/src/fr/inrialpes/exmo/align/onto/LoadedOntology.java new file mode 100644 index 0000000000000000000000000000000000000000..3ba72b05a30a1ca5959e79fddd1efe6e999da8c8 --- /dev/null +++ b/src/fr/inrialpes/exmo/align/onto/LoadedOntology.java @@ -0,0 +1,34 @@ +/* + * $Id$ + * + * Copyright (C) INRIA Rhône-Alpes, 2008 + * + * 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.align.onto; + +import java.net.URI; +import java.util.Set; + + +public interface LoadedOntology<O> extends Ontology<O> { + + public Set<Entity> getEntities(); + public Set<Entity> getClasses(); + public Set<Entity> getProperties(); + public boolean contains(Entity e); + public void unload(); +} diff --git a/src/fr/inrialpes/exmo/align/onto/Ontology.java b/src/fr/inrialpes/exmo/align/onto/Ontology.java new file mode 100644 index 0000000000000000000000000000000000000000..98dc25a9b63664dc1424a52979d99022ab292964 --- /dev/null +++ b/src/fr/inrialpes/exmo/align/onto/Ontology.java @@ -0,0 +1,40 @@ +/* + * $Id$ + * + * Copyright (C) INRIA Rhône-Alpes, 2008 + * + * 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.align.onto; + +import java.net.URI; +import java.util.Set; + + +public interface Ontology<O> { + + public URI getURI(); + public URI getFile(); + public URI getFormURI(); // Can be null + public String getFormalism(); // Can be null + public O getOntology(); + + public void setURI( URI uri ); + public void setFile( URI file ); + public void setFormURI( URI u ); + public void setFormalism( String name ); + public void setOntology( O o ); +} diff --git a/src/fr/inrialpes/exmo/align/onto/OntologyAdapter.java b/src/fr/inrialpes/exmo/align/onto/OntologyAdapter.java new file mode 100644 index 0000000000000000000000000000000000000000..700409d8a8c710b828ca9796d7ef08f0446933a3 --- /dev/null +++ b/src/fr/inrialpes/exmo/align/onto/OntologyAdapter.java @@ -0,0 +1,84 @@ +/* + * $Id$ + * + * Copyright (C) INRIA Rhône-Alpes, 2008 + * + * 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.align.onto; + + +import java.net.URI; +import java.util.Collections; +import java.util.HashSet; +import java.util.IdentityHashMap; +import java.util.Map; +import java.util.Set; + +public abstract class OntologyAdapter<O> extends ConcreteOntology<O> { + + protected O ontology; + protected URI uri; + + protected Set<Entity> classes; + protected Set<Entity> properties; + protected Set<Entity> entities; + public Set<Entity> individuals; + + protected OntologyAdapter() {} + + public OntologyAdapter(O ont, Set<Entity> c, Set<Entity> p, URI uri) { + ontology=ont; + classes = c; + properties = p; + entities = new HashSet<Entity>(c); + entities.addAll(p); + this.uri = uri; + } + + public boolean contains(Entity e) { + return classes.contains(e) || properties.contains(e); + } + + public Set<Entity> getClasses() { + return Collections.unmodifiableSet(classes); + } + + public Set<Entity> getEntities() { + return Collections.unmodifiableSet(entities); + } + + public O getOntology() { + return ontology; + } + + public void setOntology( O o ) { + ontology = o; + } + + public Set<Entity> getProperties() { + return Collections.unmodifiableSet(properties); + } + + public URI getURI() { + return uri; + } + + public String toString() { + return uri.toString(); + } + +} diff --git a/src/fr/inrialpes/exmo/align/onto/OntologyCache.java b/src/fr/inrialpes/exmo/align/onto/OntologyCache.java new file mode 100644 index 0000000000000000000000000000000000000000..5c8b2326c60a09abe52e0e548e01e98faeb5f25c --- /dev/null +++ b/src/fr/inrialpes/exmo/align/onto/OntologyCache.java @@ -0,0 +1,75 @@ +/* + * $Id: OntologyCache.java 670 2008-03-02 00:06:16Z euzenat $ + * + * Copyright (C) INRIA Rhône-Alpes, 2007-2008 + * + * 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.align.onto; + +// import java classes +import java.util.Hashtable; +import java.util.Enumeration; +import java.net.URI; + +/** + * This caches the loaded ontologies so that it is possible to share them between alignments + * as well as to unload them if necessary. + * + * NOTE[3.2]: This class may be obsoleted and its code go to OntolofyFactory + * + * @author Jérôme Euzenat + * @version $Id: OntologyCache.java 670 2008-03-02 00:06:16Z euzenat $ + */ + +public class OntologyCache { + + /** The list of currently loaded ontologies as a function: + * URI --> Ontology + * This is the ontology URI, NOT its filename + */ + Hashtable<URI,LoadedOntology> ontologies = null; + + public OntologyCache() { + ontologies = new Hashtable<URI,LoadedOntology>(); + } + + public void recordOntology( URI uri, LoadedOntology ontology ){ + ontologies.put(uri,ontology); + } + + public Ontology getOntology( URI uri ){ + return ontologies.get( uri ); + } + + public void unloadOntology( URI uri, LoadedOntology ontology ){ + LoadedOntology o = ontologies.get(uri); + o.unload(); + ontologies.remove( uri ); + } + + public void clear(){ + // JE: Onto [Is this correct ??] + //for ( Enumeration e = ontologies.elements() ; e.hasMoreElements(); ){ + // Ontology o = (Ontology)e.nextElement(); + for ( LoadedOntology o : ontologies.values() ){ + o.unload(); + } + ontologies.clear(); + } + +} diff --git a/src/fr/inrialpes/exmo/align/onto/OntologyFactory.java b/src/fr/inrialpes/exmo/align/onto/OntologyFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..06a7f946d01d5513e6ee6ad223d394d1aa3033d8 --- /dev/null +++ b/src/fr/inrialpes/exmo/align/onto/OntologyFactory.java @@ -0,0 +1,81 @@ +/* + * $Id$ + * + * Copyright (C) INRIA Rhône-Alpes, 2008 + * + * 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.align.onto; + +import java.net.URI; +import java.util.HashMap; +import java.util.Map; + +import java.lang.reflect.InvocationTargetException; + +public abstract class OntologyFactory { + + protected static OntologyFactory instance; + + private static String API_NAME="fr.inrialpes.exmo.align.onto.owlapi10.OWLAPIOntologyFactory"; + + protected static Map<URI,Ontology> loadedOntos= new HashMap<URI,Ontology>(); + protected static Map<URI,Ontology> loadedOntosLogical= new HashMap<URI,Ontology>(); + public static OntologyFactory newInstance() { + return newInstance(API_NAME); + } + + public static OntologyFactory newInstance(String apiName) { + try { + Class ofClass = Class.forName(apiName); + Class[] cparams = {}; + java.lang.reflect.Constructor ofConstructor = ofClass.getConstructor(cparams); + Object[] mparams = {}; + instance = (OntologyFactory)ofConstructor.newInstance(mparams); + } catch (ClassNotFoundException cnfex ) { + cnfex.printStackTrace(); // better raise errors + } catch (NoSuchMethodException nsmex) { + nsmex.printStackTrace(); + } catch (InstantiationException ieex) { + ieex.printStackTrace(); + } catch (IllegalAccessException iaex) { + iaex.printStackTrace(); + } catch (InvocationTargetException itex) { + itex.printStackTrace(); + } + return instance; + } + + public abstract LoadedOntology loadOntology( URI physicalURI ); + + // JE: This is not really usefule since it doubles OntologyCache... + // This may be included as well... + public Ontology getOntologyFromFile(URI physicalURI) { + Ontology onto; + if (loadedOntos.containsKey(physicalURI)) + onto = loadedOntos.get(physicalURI); + else { + onto = loadOntology( physicalURI ); + loadedOntos.put(physicalURI, onto); + loadedOntosLogical.put(onto.getURI(), onto); + } + return onto; + } + + public static Ontology getOntologyFromURI(URI logicalURI) { + return loadedOntosLogical.get(logicalURI); + } +} diff --git a/src/fr/inrialpes/exmo/align/onto/owlapi10/OWLAPIOntology.java b/src/fr/inrialpes/exmo/align/onto/owlapi10/OWLAPIOntology.java new file mode 100644 index 0000000000000000000000000000000000000000..1ebaa4a79dc01573cde8a383990b8a4745ef951d --- /dev/null +++ b/src/fr/inrialpes/exmo/align/onto/owlapi10/OWLAPIOntology.java @@ -0,0 +1,233 @@ +/* + * $Id$ + * + * Copyright (C) INRIA Rhône-Alpes, 2007-2008 + * + * 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.align.onto.owlapi10; + +import java.net.URI; +import java.util.Set; +import java.util.Iterator; +import java.util.NoSuchElementException; +import java.lang.UnsupportedOperationException; + +import org.semanticweb.owl.align.AlignmentException; + +import fr.inrialpes.exmo.align.onto.ConcreteOntology; +import fr.inrialpes.exmo.align.onto.LoadedOntology; +import fr.inrialpes.exmo.align.onto.Entity; + +import org.semanticweb.owl.model.OWLOntology; +import org.semanticweb.owl.model.OWLProperty; +import org.semanticweb.owl.model.OWLClass; +import org.semanticweb.owl.model.OWLObjectProperty; +import org.semanticweb.owl.model.OWLDataProperty; +import org.semanticweb.owl.model.OWLIndividual; +import org.semanticweb.owl.model.OWLEntity; +import org.semanticweb.owl.model.OWLException; + +/** + * Store the information regarding ontologies in a specific structure + * Acts as an interface with regard to an ontology APY + */ + +//private class OWLAPIOntology extends OntologyAdapter<OWLOntology> { +// private OWLAPIOntology() {super();} +//} + + +public class OWLAPIOntology extends ConcreteOntology<OWLOntology> implements LoadedOntology<OWLOntology> { + public OWLAPIOntology() { + setFormalism( "OWL1.0" ); + try { + setFormURI( new URI("http://www.w3.org/2002/07/owl#") ); + } catch (Exception e) {}; // does not happen + }; + + public OWLOntology getOntology() { return onto; } + + public void setOntology( OWLOntology o ) { this.onto = o; } + + // Here it shoud be better to report exception + // JE: Onto this does not work at all, of course... + public Set<Entity> getEntities() { + try { + return (Set<Entity>)((OWLOntology)onto).getClasses(); // [W:unchecked] + } catch (OWLException ex) { + return null; + } + } + + public Set<Entity> getClasses() { + try { + return (Set<Entity>)((OWLOntology)onto).getClasses(); // [W:unchecked] + } catch (OWLException ex) { + return null; + } + } + + public Set<Entity> getProperties() { + try { + return (Set<Entity>)((OWLOntology)onto).getClasses(); // [W:unchecked] + } catch (OWLException ex) { + return null; + } + } + + public Set<Entity> getIndividuals() { + try { + return (Set<Entity>)((OWLOntology)onto).getIndividuals(); // [W:unchecked] + } catch (OWLException ex) { + return null; + } + } + + // The only point is if I should return OWLProperties or names... + // I guess that I will return names (ns+name) because otherwise I will need a full + // Abstract ontology interface and this is not the rule... + public Iterator<Object> getObjectProperties() throws AlignmentException { + try { + // [Warning:unchecked] due to OWL API not serving generic types + return ((OWLOntology)onto).getObjectProperties().iterator(); // [W:unchecked] + } catch (OWLException ex) { + throw new AlignmentException( "Cannot get object properties", ex ); + } + } + + // JE: these are really specific methods I guess + public Iterator<URI> getObjectPropertyNames() throws AlignmentException { + try { + return new URIIterator(((OWLOntology)onto).getObjectProperties().iterator()); + } catch (OWLException ex) { + throw new AlignmentException( "Cannot get object properties", ex ); + } + } + + public Iterator<URI> getDataPropertyNames() throws AlignmentException { + try { + return new URIIterator(((OWLOntology)onto).getDataProperties().iterator()); + } catch (OWLException ex) { + throw new AlignmentException( "Cannot get object properties", ex ); + } + } + + public Iterator<URI> getClassNames() throws AlignmentException { + try { + return new URIIterator(((OWLOntology)onto).getClasses().iterator()); + } catch (OWLException ex) { + throw new AlignmentException( "Cannot get object properties", ex ); + } + } + + public Iterator<URI> getInstanceNames() throws AlignmentException { + try { + return new URIIterator(((OWLOntology)onto).getIndividuals().iterator()); + } catch (OWLException ex) { + throw new AlignmentException( "Cannot get object properties", ex ); + } + } + + public int nbClasses() throws AlignmentException { + try { + return ((OWLOntology)onto).getClasses().size(); + } catch (OWLException ex) { + throw new AlignmentException( "Cannot get class number" ); + } + } + + public int nbObjectProperties() throws AlignmentException { + try { + return ((OWLOntology)onto).getObjectProperties().size(); + } catch (OWLException ex) { + throw new AlignmentException( "Cannot get object property number" ); + } + } + + public int nbDataProperties() throws AlignmentException { + try { + return ((OWLOntology)onto).getDataProperties().size(); + } catch (OWLException ex) { + throw new AlignmentException( "Cannot get data property number" ); + } + } + + public int nbInstances() throws AlignmentException { + try { + return ((OWLOntology)onto).getIndividuals().size(); + } catch (OWLException ex) { + throw new AlignmentException( "Cannot get individual number" ); + } + } + + public boolean contains(Entity e) { + if ( e instanceof OWLEntity ){ + if ( e instanceof OWLClass ) { + for ( Object o : getClasses() ){ + if ( o == e ) return true; + } + } else if ( e instanceof OWLDataProperty ) { + for ( Object o : getProperties() ){ + if ( o == e ) return true; + } + } else if ( e instanceof OWLIndividual ) { + for ( Object o : getIndividuals() ){ + if ( o == e ) return true; + } + } + }; + return false; + } + + public void unload() { + try { + ((OWLOntology)onto).getOWLConnection().notifyOntologyDeleted( ((OWLOntology)onto) ); + } catch (OWLException ex) { System.err.println(ex); }; + } + + public Object getEntity( URI uri ) throws AlignmentException { + try { + OWLEntity result = ((OWLOntology)onto).getClass( uri ); + if ( result == null ) result = ((OWLOntology)onto).getDataProperty( uri ); + if ( result == null ) result = ((OWLOntology)onto).getObjectProperty( uri ); + if ( result == null ) result = ((OWLOntology)onto).getIndividual( uri ); + return result; + } catch (OWLException ex) { + throw new AlignmentException( "Cannot dereference URI : "+uri ); + } + } + +} + +final class URIIterator implements Iterator<URI> { + private Iterator it = null; + + public URIIterator ( Iterator i ){ + it = i; + } + public boolean hasNext() { return it.hasNext(); } + public URI next() throws NoSuchElementException { + try{ + return ((OWLEntity)it.next()).getURI(); + } catch (OWLException ex) { + throw new NoSuchElementException(ex.toString()); // cannot encapsulate! + } + } + public void remove() throws UnsupportedOperationException { + throw new UnsupportedOperationException(); + } +} diff --git a/src/fr/inrialpes/exmo/align/onto/owlapi10/OWLAPIOntologyFactory.java b/src/fr/inrialpes/exmo/align/onto/owlapi10/OWLAPIOntologyFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..e4886eab68f38b48b476e3bcc54342a6b5c37b32 --- /dev/null +++ b/src/fr/inrialpes/exmo/align/onto/owlapi10/OWLAPIOntologyFactory.java @@ -0,0 +1,257 @@ +/* + * $Id$ + * + * Copyright (C) INRIA Rhône-Alpes, 2008 + * + * 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.align.onto.owlapi10; + +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import org.apache.log4j.Logger; +import org.apache.log4j.Level; + +import org.xml.sax.SAXException; + +import org.semanticweb.owl.align.AlignmentException; + +import fr.inrialpes.exmo.align.onto.OntologyCache; +import fr.inrialpes.exmo.align.onto.OntologyFactory; +import fr.inrialpes.exmo.align.onto.EntityAdapter; +import fr.inrialpes.exmo.align.onto.Entity; +import fr.inrialpes.exmo.align.onto.EntityAdapter.TYPE; +import fr.inrialpes.exmo.align.onto.Ontology; +import fr.inrialpes.exmo.align.onto.ConcreteOntology; +import fr.inrialpes.exmo.align.onto.LoadedOntology; + +import org.semanticweb.owl.io.vocabulary.RDFSVocabularyAdapter; +import org.semanticweb.owl.model.OWLAnnotationInstance; +import org.semanticweb.owl.model.OWLClass; +import org.semanticweb.owl.model.OWLDataValue; +import org.semanticweb.owl.model.OWLEntity; +import org.semanticweb.owl.model.OWLException; +import org.semanticweb.owl.model.OWLIndividual; +import org.semanticweb.owl.model.OWLOntology; +import org.semanticweb.owl.model.OWLProperty; +import org.semanticweb.owl.model.helper.OWLEntityCollector; +import org.semanticweb.owl.util.OWLConnection; +import org.semanticweb.owl.util.OWLManager; +import org.semanticweb.owl.io.owl_rdf.OWLRDFParser; +import org.semanticweb.owl.io.owl_rdf.OWLRDFErrorHandler; + +public class OWLAPIOntologyFactory extends OntologyFactory { + + private URI formalismUri = null; + private String formalismId = "OWL1.0"; + + public OWLAPIOntologyFactory() { + try { + formalismUri = new URI("http://www.w3.org/2002/07/owl#"); + } catch (URISyntaxException ex) { ex.printStackTrace(); } // should not happen + }; + + public static OntologyFactory getInstance() { + if (instance == null || !(instance instanceof OWLAPIOntologyFactory)) + instance = new OWLAPIOntologyFactory(); + return instance; + } + + public LoadedOntology loadOntology( URI physicalURI ) { + OWLConnection connection = null; + Map parameters = new HashMap(); + parameters.put(OWLManager.OWL_CONNECTION, + "org.semanticweb.owl.impl.model.OWLConnectionImpl"); + try { + connection = OWLManager.getOWLConnection(parameters); + Level lev = Logger.getLogger("org.semanticweb.owl").getLevel(); + Logger.getLogger("org.semanticweb.owl").setLevel(Level.ERROR); + OWLOntology ontology = connection.loadOntologyPhysical(physicalURI); + Logger.getLogger("org.semanticweb.owl").setLevel(lev); + OWLAPIOntology onto = new OWLAPIOntology(); + // It may be possible to fill this as final in OWLAPIOntology... + onto.setFormalism( formalismId ); + onto.setFormURI( formalismUri ); + onto.setOntology( ontology ); + onto.setFile( physicalURI ); + try { + onto.setURI( ontology.getLogicalURI() );//getURI(); + } catch (OWLException e) { + // Better put in the AlignmentException of loaded + e.printStackTrace(); + } + return onto; + } catch (OWLException e) { + e.printStackTrace(); + } + return null; + } + + /** Can be used for loading the ontology if it is not available **/ + // JE: Onto: check that the structure is properly filled (see build...) + public Ontology loadOntology( URI ref, OntologyCache ontologies ) throws SAXException, AlignmentException { + Ontology onto = null; + if ( (ontologies != null) && ( ontologies.getOntology( ref ) != null ) ) { + } else if ( onto != null ) { + } else { + OWLOntology parsedOnt = null; + try { + OWLRDFParser parser = new OWLRDFParser(); + OWLRDFErrorHandler handler = new OWLRDFErrorHandler(){ + public void owlFullConstruct( int code, String message ) + throws SAXException { + } + public void owlFullConstruct(int code, String message, Object o) + throws SAXException { + } + public void error( String message ) throws SAXException { + throw new SAXException( message.toString() ); + } + public void warning( String message ) throws SAXException { + System.err.println("WARNING: " + message); + } + }; + Level lev = Logger.getLogger("org.semanticweb.owl").getLevel(); + Logger.getLogger("org.semanticweb.owl").setLevel(Level.ERROR); + parser.setOWLRDFErrorHandler( handler ); + parser.setConnection( OWLManager.getOWLConnection() ); + parsedOnt = parser.parseOntology( ref ); + Logger.getLogger("org.semanticweb.owl").setLevel(lev); + } catch (OWLException ex) { + throw new AlignmentException( "Cannot load ontology "+ref, ex ); + } + // THIS SHOULD NOW BE DONE WHEN CREATING THE ONTOLOGY + //setOntology( parsedOnt ); + //if ( ontologies != null ) ontologies.recordOntology( ref, this ); + //onto = parsedOnt; + } + return onto; + } + + // JE: Onto: I am not sure of the meaning of this "get" + /* + private Ontology getOntology(OWLOntology ont) { + // JE: Onto + //OntologyAdapter<OWLOntology> oModel = new OWLAPIOntology(); + Ontology oModel = new OWLAPIOntology(); + oModel.ontology=ont; + try { + oModel.uri=ont.getLogicalURI();//getURI(); + } catch (OWLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + oModel.classes = getEntities(oModel,OWLClass.class); + oModel.properties = getEntities(oModel,OWLProperty.class); + //oModel.individuals = getEntities(oModel,OWLIndividual.class); + oModel.entities = new HashSet<Entity>(oModel.properties); + oModel.entities.addAll(oModel.classes); + //oModel.entities.addAll(oModel.individuals); + + //oModel.classes = getEntities(oModel,OWLEntity.class); + return oModel; + } + */ + + private Set<Entity> getEntities(LoadedOntology<OWLOntology> ont, Class<? extends OWLEntity> c){ + OWLEntityCollector ec = new OWLEntityCollector(); + try { + ont.getOntology().accept(ec); + Set<Entity> entities = new HashSet<Entity>(); + for (Object obj : ec.entities()) { + if (c.isInstance(obj) && (((OWLEntity) obj).getURI() != null) && + ((OWLEntity) obj).getURI().getSchemeSpecificPart().equals(ont.getURI().getSchemeSpecificPart())){ + //System.out.println(((OWLEntity) obj).getURI().getSchemeSpecificPart()+" - "+ont.getURI().getSchemeSpecificPart()); + //System.out.println(((OWLEntity) obj).getURI()+" : "+ont.getOntology().contains(obj)); + entities.add(new OWLAPIEntity((OWLEntity)obj, ont, ((OWLEntity)obj).getURI() )); + } + } + return entities; + } catch (OWLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; + } + + private void addAnnotations(OWLAPIEntity ent) { + for (String[] a : getAnnotations(ent.getObject(),(OWLOntology)ent.getOntology().getOntology())) { + ent.getAnnotations().add(a[0]); + if (a[1]!=null) { + ent.getLangToAnnot(EntityAdapter.LANGUAGES.valueOf(a[1])).add(a[0]); + } + if (a[2].equals(RDFSVocabularyAdapter.INSTANCE.getComment())) { + ent.getTypeToAnnot(EntityAdapter.TYPE.comment).add(a[0]); + } + else if (a[2].equals(RDFSVocabularyAdapter.INSTANCE.getLabel())) { + ent.getTypeToAnnot(EntityAdapter.TYPE.comment).add(a[0]); + } + } + } + + + private Set<String[]> getAnnotations(OWLEntity e, OWLOntology o) { + Set<String[]> res = new HashSet<String[]>(); + Set annots; + try { + annots = e.getAnnotations(o); + for (Object annot : annots) { + OWLAnnotationInstance annInst = ((OWLAnnotationInstance) annot); + String val; + String lang=null; + String annotUri = annInst.getProperty().getURI().toString(); + if ( annInst.getContent() instanceof OWLDataValue ) { + OWLDataValue odv = (OWLDataValue) annInst.getContent(); + val = odv.toString(); + lang = odv.getLang(); + } + else + val= (String) annInst.getContent(); + res.add(new String[]{val,lang,annotUri}); + } + } + catch (OWLException oe) { + oe.printStackTrace(); + } + return Collections.unmodifiableSet(res); + } + + + private class OWLAPIEntity extends EntityAdapter<OWLEntity> { + //private boolean toLoad=true; + + private OWLAPIEntity() { super(false); } + private OWLAPIEntity(OWLEntity e, Ontology o, URI u ) { + super( e, o, u, false); + } + + public Set<String> getAnnotations(String lang, TYPE type) { + if (this.annotations==null) { + init(); + addAnnotations(this); + //toLoad=false; + } + return super.getAnnotations(lang, type); + } + } + +}