From 3f0b6c8dabdc816b196f2fc7fb05b33d87f5296e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Euzenat?= <Jerome.Euzenat@inria.fr> Date: Fri, 28 Mar 2008 08:42:33 +0000 Subject: [PATCH] - Suppressed all entity representations --- .../exmo/align/onto/LoadedOntology.java | 5 +- .../exmo/align/onto/OntologyCache.java | 18 ++- .../exmo/align/onto/OntologyFactory.java | 56 +++++++--- .../align/onto/owlapi10/OWLAPIOntology.java | 100 +++++++++++++++-- .../onto/owlapi10/OWLAPIOntologyFactory.java | 103 +++--------------- 5 files changed, 161 insertions(+), 121 deletions(-) diff --git a/src/fr/inrialpes/exmo/align/onto/LoadedOntology.java b/src/fr/inrialpes/exmo/align/onto/LoadedOntology.java index 748a55bf..85ce0d0c 100644 --- a/src/fr/inrialpes/exmo/align/onto/LoadedOntology.java +++ b/src/fr/inrialpes/exmo/align/onto/LoadedOntology.java @@ -35,7 +35,7 @@ public interface LoadedOntology<O> extends Ontology<O> { public boolean isEntity( Object o ); public boolean isClass( Object o ); public boolean isProperty( Object o ); - public boolean isDatatypeProperty( Object o ); + public boolean isDataProperty( Object o ); public boolean isObjectProperty( Object o ); public boolean isIndividual( Object o ); @@ -43,10 +43,11 @@ public interface LoadedOntology<O> extends Ontology<O> { public Set<Object> getClasses(); public Set<Object> getProperties(); public Set<Object> getObjectProperties(); - public Set<Object> getDatatypeProperties(); + public Set<Object> getDataProperties(); public Set<Object> getIndividuals(); public int nbClasses(); + public int nbProperties(); public int nbDataProperties(); public int nbObjectProperties(); public int nbInstances(); diff --git a/src/fr/inrialpes/exmo/align/onto/OntologyCache.java b/src/fr/inrialpes/exmo/align/onto/OntologyCache.java index 5c8b2326..897fd147 100644 --- a/src/fr/inrialpes/exmo/align/onto/OntologyCache.java +++ b/src/fr/inrialpes/exmo/align/onto/OntologyCache.java @@ -43,32 +43,38 @@ public class OntologyCache { * This is the ontology URI, NOT its filename */ Hashtable<URI,LoadedOntology> ontologies = null; + Hashtable<URI,LoadedOntology> ontologyUris = null; public OntologyCache() { ontologies = new Hashtable<URI,LoadedOntology>(); + ontologyUris = new Hashtable<URI,LoadedOntology>(); } public void recordOntology( URI uri, LoadedOntology ontology ){ - ontologies.put(uri,ontology); + ontologies.put( uri, ontology ); + ontologyUris.put( ontology.getURI(), ontology ); } - public Ontology getOntology( URI uri ){ + public LoadedOntology getOntology( URI uri ){ return ontologies.get( uri ); } + public LoadedOntology getOntologyFromURI( URI uri ){ + return ontologyUris.get( uri ); + } + public void unloadOntology( URI uri, LoadedOntology ontology ){ - LoadedOntology o = ontologies.get(uri); + LoadedOntology o = ontologyUris.get(uri); o.unload(); + ontologyUris.remove( uri ); 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(); } + ontologyUris.clear(); ontologies.clear(); } diff --git a/src/fr/inrialpes/exmo/align/onto/OntologyFactory.java b/src/fr/inrialpes/exmo/align/onto/OntologyFactory.java index 06a7f946..bcca9364 100644 --- a/src/fr/inrialpes/exmo/align/onto/OntologyFactory.java +++ b/src/fr/inrialpes/exmo/align/onto/OntologyFactory.java @@ -26,14 +26,22 @@ import java.util.Map; import java.lang.reflect.InvocationTargetException; +import org.semanticweb.owl.align.AlignmentException; + 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 String getDefaultFactory(){ + return API_NAME; + } + + public void setDefaultFactory( String className ){ + API_NAME = className; + } + public static OntologyFactory newInstance() { return newInstance(API_NAME); } @@ -59,23 +67,45 @@ public abstract class OntologyFactory { return instance; } - public abstract LoadedOntology loadOntology( URI physicalURI ); + /** + * Load an ontology, cache enabled + */ + public abstract LoadedOntology loadOntology( URI uri ) throws AlignmentException; + /** + * Load an ontology, cache enabled if true, disabled otherwise + */ + public LoadedOntology loadOntology( URI uri, OntologyCache ontologies ) throws AlignmentException { + LoadedOntology onto = null; + if ( ontologies != null ) { + onto = ontologies.getOntologyFromURI( uri ); + if ( onto != null ) return onto; + onto = ontologies.getOntology( uri ); + if ( onto != null ) return onto; + }; + onto = loadOntology( uri ); + if ( ontologies != null ) ontologies.recordOntology( uri, onto ); + return onto; + }; - // JE: This is not really usefule since it doubles OntologyCache... + /* JE: this is a reimplementation of OntologyCache + // JE: This is not really useful 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); - } + protected static Map<URI,LoadedOntology> loadedOntos = new HashMap<URI,LoadedOntology>(); + protected static Map<URI,LoadedOntology> loadedOntosLogical = new HashMap<URI,LoadedOntology>(); + public LoadedOntology getOntologyFromCache( URI uri ) { + LoadedOntology onto; + if ( loadedOntos.containsKey( uri ) ) { + onto = loadedOntos.get( uri ); + } else { + onto = loadOntology( uri ); + loadedOntos.put( uri, 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 index 903478cc..d4093a9a 100644 --- a/src/fr/inrialpes/exmo/align/onto/owlapi10/OWLAPIOntology.java +++ b/src/fr/inrialpes/exmo/align/onto/owlapi10/OWLAPIOntology.java @@ -40,16 +40,18 @@ import org.semanticweb.owl.model.OWLIndividual; import org.semanticweb.owl.model.OWLEntity; import org.semanticweb.owl.model.OWLException; +/* + * JE: BEWARE, THIS HAS NOT BEEN FULLY IMPLEMENTED SO FAR!!!! + * TO BE CHECKED + * getEntities() and getProperties() ARE INCORRECT + * nbXxxx() ARE INNEFICIENT + */ + /** * 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 BasicOntology<OWLOntology> implements LoadedOntology<OWLOntology> { public OWLAPIOntology() { @@ -59,10 +61,12 @@ public class OWLAPIOntology extends BasicOntology<OWLOntology> implements Loaded } catch (Exception e) {}; // does not happen }; + // Ontology interface public OWLOntology getOntology() { return onto; } public void setOntology( OWLOntology o ) { this.onto = o; } + // LoadedOntology interface public Object getEntity( URI uri ) throws AlignmentException { try { OWLEntity result = ((OWLOntology)onto).getClass( uri ); @@ -83,7 +87,14 @@ public class OWLAPIOntology extends BasicOntology<OWLOntology> implements Loaded } } public String getEntityName( Object o ) throws AlignmentException { - return "Dummt"; + try { + // Try to get labels first... + URI u = ((OWLEntity)o).getURI(); + if ( u != null ) return u.getFragment(); + else return ""; + } catch (OWLException oex) { + return null; + } }; public boolean isEntity( Object o ){ @@ -98,7 +109,7 @@ public class OWLAPIOntology extends BasicOntology<OWLOntology> implements Loaded if ( o instanceof OWLProperty ) return true; else return false; }; - public boolean isDatatypeProperty( Object o ){ + public boolean isDataProperty( Object o ){ if ( o instanceof OWLDataProperty ) return true; else return false; }; @@ -145,17 +156,14 @@ public class OWLAPIOntology extends BasicOntology<OWLOntology> implements Loaded // [Warning:unchecked] due to OWL API not serving generic types return ((OWLOntology)onto).getObjectProperties(); // [W:unchecked] } catch (OWLException ex) { - //throw new AlignmentException( "Cannot get object properties", ex ); return null; } } - public Set<Object> getDatatypeProperties() { + public Set<Object> getDataProperties() { try { - // [Warning:unchecked] due to OWL API not serving generic types return ((OWLOntology)onto).getDataProperties(); // [W:unchecked] } catch (OWLException ex) { - //throw new AlignmentException( "Cannot get object properties", ex ); return null; } } @@ -168,6 +176,72 @@ public class OWLAPIOntology extends BasicOntology<OWLOntology> implements Loaded } } + /* + private Set<Object> getEntities( Class<? extends OWLEntity> c ){ + OWLEntityCollector ec = new OWLEntityCollector(); + try { + getOntology().accept(ec); + Set<Object> entities = new HashSet<Object>(); + 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); + } + */ + // JE: particularly inefficient public int nbClasses() { try { @@ -177,6 +251,10 @@ public class OWLAPIOntology extends BasicOntology<OWLOntology> implements Loaded } } + public int nbProperties() { + return nbObjectProperties()+nbDataProperties(); + } + public int nbObjectProperties() { try { return ((OWLOntology)onto).getObjectProperties().size(); diff --git a/src/fr/inrialpes/exmo/align/onto/owlapi10/OWLAPIOntologyFactory.java b/src/fr/inrialpes/exmo/align/onto/owlapi10/OWLAPIOntologyFactory.java index e4886eab..deeb5cfd 100644 --- a/src/fr/inrialpes/exmo/align/onto/owlapi10/OWLAPIOntologyFactory.java +++ b/src/fr/inrialpes/exmo/align/onto/owlapi10/OWLAPIOntologyFactory.java @@ -31,34 +31,22 @@ 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; +/* + * JE: everything should be asked to the Ontoogy, not to the factory... + * So I suppress many initial methods + */ public class OWLAPIOntologyFactory extends OntologyFactory { private URI formalismUri = null; @@ -76,7 +64,7 @@ public class OWLAPIOntologyFactory extends OntologyFactory { return instance; } - public LoadedOntology loadOntology( URI physicalURI ) { + public LoadedOntology loadOntology( URI uri ) throws AlignmentException { OWLConnection connection = null; Map parameters = new HashMap(); parameters.put(OWLManager.OWL_CONNECTION, @@ -85,28 +73,27 @@ public class OWLAPIOntologyFactory extends OntologyFactory { 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); + OWLOntology ontology = connection.loadOntologyPhysical(uri); 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 ); + onto.setFile( uri ); try { - onto.setURI( ontology.getLogicalURI() );//getURI(); + onto.setURI( ontology.getLogicalURI() ); } catch (OWLException e) { // Better put in the AlignmentException of loaded e.printStackTrace(); } return onto; - } catch (OWLException e) { - e.printStackTrace(); + } catch (OWLException e) { + throw new AlignmentException("Cannot load "+uri, e ); } - return null; } - /** Can be used for loading the ontology if it is not available **/ + /* 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; @@ -146,7 +133,7 @@ public class OWLAPIOntologyFactory extends OntologyFactory { } return onto; } - + */ // JE: Onto: I am not sure of the meaning of this "get" /* private Ontology getOntology(OWLOntology ont) { @@ -172,70 +159,7 @@ public class OWLAPIOntologyFactory extends OntologyFactory { } */ - 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; @@ -253,5 +177,6 @@ public class OWLAPIOntologyFactory extends OntologyFactory { return super.getAnnotations(lang, type); } } + */ } -- GitLab