From 11cfdaf9fb11a3560da83138fa8c8cb5e55dfa05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20David?= <Jerome.David@univ-grenoble-alpes.fr> Date: Tue, 29 Apr 2008 08:32:08 +0000 Subject: [PATCH] In Ontlogy interface, Set<Object> have been replaced by Set<?> Some corrections in JENAOntology API and in OWLAPIOntology concerning the annotations methods --- .../exmo/align/onto/LoadedOntology.java | 12 ++--- .../exmo/align/onto/jena25/JENAOntology.java | 44 +++++-------------- .../onto/jena25/JENAOntologyFactory.java | 11 ++++- .../align/onto/owlapi10/OWLAPIOntology.java | 42 ++++++++---------- .../onto/owlapi10/OWLAPIOntologyFactory.java | 12 ++--- 5 files changed, 52 insertions(+), 69 deletions(-) diff --git a/src/fr/inrialpes/exmo/align/onto/LoadedOntology.java b/src/fr/inrialpes/exmo/align/onto/LoadedOntology.java index 0d022729..6a690aa9 100644 --- a/src/fr/inrialpes/exmo/align/onto/LoadedOntology.java +++ b/src/fr/inrialpes/exmo/align/onto/LoadedOntology.java @@ -45,12 +45,12 @@ public interface LoadedOntology<O> extends Ontology<O> { public boolean isObjectProperty( Object o ); public boolean isIndividual( Object o ); - public Set<Object> getEntities(); - public Set<Object> getClasses(); - public Set<Object> getProperties(); - public Set<Object> getObjectProperties(); - public Set<Object> getDataProperties(); - public Set<Object> getIndividuals(); + public Set<?> getEntities(); + public Set<?> getClasses(); + public Set<?> getProperties(); + public Set<?> getObjectProperties(); + public Set<?> getDataProperties(); + public Set<?> getIndividuals(); public int nbClasses(); public int nbProperties(); diff --git a/src/fr/inrialpes/exmo/align/onto/jena25/JENAOntology.java b/src/fr/inrialpes/exmo/align/onto/jena25/JENAOntology.java index 626f8558..1ecfc676 100644 --- a/src/fr/inrialpes/exmo/align/onto/jena25/JENAOntology.java +++ b/src/fr/inrialpes/exmo/align/onto/jena25/JENAOntology.java @@ -1,9 +1,6 @@ package fr.inrialpes.exmo.align.onto.jena25; import java.net.URI; -import java.net.URISyntaxException; -import java.util.Collection; -import java.util.Collections; import java.util.HashSet; import java.util.Iterator; import java.util.Set; @@ -17,7 +14,6 @@ import com.hp.hpl.jena.ontology.OntClass; import com.hp.hpl.jena.ontology.OntModel; import com.hp.hpl.jena.ontology.OntProperty; import com.hp.hpl.jena.ontology.OntResource; -import com.hp.hpl.jena.rdf.model.Statement; import com.hp.hpl.jena.rdf.model.impl.LiteralImpl; import fr.inrialpes.exmo.align.onto.BasicOntology; @@ -26,12 +22,12 @@ import fr.inrialpes.exmo.align.onto.LoadedOntology; public class JENAOntology extends BasicOntology<OntModel> implements LoadedOntology<OntModel>{ - protected Set<Object> listResources(Class<? extends OntResource> type) { + protected Set<?> listResources(Class<? extends OntResource> type) { Set<Object> resources = new HashSet<Object>(); Iterator i = onto.listObjects(); while (i.hasNext()) { Object r = i.next(); - if (type.isInstance(r)) + if (type.isInstance(r) && ((OntResource)r).getURI()!=null) resources.add(r); } return resources; @@ -39,11 +35,11 @@ public class JENAOntology extends BasicOntology<OntModel> implements LoadedOntol } - public Set<Object> getClasses() { + public Set<?> getClasses() { return onto.listNamedClasses().toSet(); } - public Set<Object> getDataProperties() { + public Set<?> getDataProperties() { return onto.listDatatypeProperties().toSet(); } @@ -120,34 +116,18 @@ public class JENAOntology extends BasicOntology<OntModel> implements LoadedOntol } } - public Set<Object> getIndividuals() { - /*Set individuals = new HashSet(); - Iterator i = onto.listIndividuals(); - while (i.hasNext()) { - Individual ind = (Individual) i.next(); - individuals.add(ind); - Iterator j = ind.listProperties(); - while (j.hasNext()) { - Statement s = (Statement) j.next(); - if (s.getPredicate().canAs(ObjectProperty.class)) { - System.out.println(s.getObject()); - individuals.add(s.getObject()); - } - } - } - return individuals;*/ - //return listResources(Individual.class); - return onto.listIndividuals().toSet(); + public Set<?> getIndividuals() { + return onto.listIndividuals().toSet(); } - public Set<Object> getObjectProperties() { - return onto.listObjectProperties().toSet(); + public Set<?> getObjectProperties() { + return (Set<? extends Object>) onto.listObjectProperties().toSet(); } - public Set<Object> getProperties() { - Set properties = new HashSet(); - properties.addAll(onto.listDatatypeProperties().toSet()); - properties.addAll(onto.listObjectProperties().toSet()); + public Set<?> getProperties() { + Set<Object> properties = new HashSet<Object>(); + properties.addAll((Set<? extends Object>) onto.listDatatypeProperties().toSet()); + properties.addAll((Set<? extends Object>) onto.listObjectProperties().toSet()); return properties; } diff --git a/src/fr/inrialpes/exmo/align/onto/jena25/JENAOntologyFactory.java b/src/fr/inrialpes/exmo/align/onto/jena25/JENAOntologyFactory.java index dd844434..bce9791d 100644 --- a/src/fr/inrialpes/exmo/align/onto/jena25/JENAOntologyFactory.java +++ b/src/fr/inrialpes/exmo/align/onto/jena25/JENAOntologyFactory.java @@ -1,7 +1,7 @@ package fr.inrialpes.exmo.align.onto.jena25; import java.net.URI; -import java.util.Iterator; +import java.util.NoSuchElementException; import org.semanticweb.owl.align.AlignmentException; @@ -23,7 +23,14 @@ public class JENAOntologyFactory extends OntologyFactory{ JENAOntology onto = new JENAOntology(); onto.setFile(uri); // to be checked : why several ontologies in a model ??? - onto.setURI(new URI(((Ontology)m.listOntologies().next()).getURI())); + // If no URI can be extracted from ontology, then we use the physical URI + try { + onto.setURI(new URI(((Ontology)m.listOntologies().next()).getURI())); + } + catch (NoSuchElementException nse) { + onto.setURI(new URI(m.getNsPrefixURI(""))); + //onto.setFile(uri); + } //onto.setURI(new URI(m.listOntologies()getOntology(null).getURI())); onto.setOntology(m); return onto; diff --git a/src/fr/inrialpes/exmo/align/onto/owlapi10/OWLAPIOntology.java b/src/fr/inrialpes/exmo/align/onto/owlapi10/OWLAPIOntology.java index fd7d96dc..4509c678 100644 --- a/src/fr/inrialpes/exmo/align/onto/owlapi10/OWLAPIOntology.java +++ b/src/fr/inrialpes/exmo/align/onto/owlapi10/OWLAPIOntology.java @@ -30,9 +30,6 @@ package fr.inrialpes.exmo.align.onto.owlapi10; import java.net.URI; import java.util.Set; import java.util.HashSet; -import java.util.Iterator; -import java.util.NoSuchElementException; -import java.lang.UnsupportedOperationException; import org.semanticweb.owl.align.AlignmentException; @@ -115,7 +112,8 @@ public class OWLAPIOntology extends BasicOntology<OWLOntology> implements Loaded protected Set<String> getAnnotations(OWLEntity e , String lang , String typeAnnot ) throws OWLException { Set<String> annots = new HashSet<String>(); - for (OWLAnnotationInstance annot : (Set<OWLAnnotationInstance>) e.getAnnotations(onto)) { + for (Object objAnnot : e.getAnnotations(onto)) { + OWLAnnotationInstance annot = (OWLAnnotationInstance) objAnnot; String annotUri = annot.getProperty().getURI().toString(); if (annotUri.equals(typeAnnot) || typeAnnot==null) { if ( annot.getContent() instanceof OWLDataValue && @@ -197,12 +195,13 @@ public class OWLAPIOntology extends BasicOntology<OWLOntology> implements Loaded }; // JD: allows to retrieve some specific entities by giving their class - protected Set<Object> getEntities(Class<? extends OWLEntity> c) throws OWLException{ + protected Set<?> getEntities(Class<? extends OWLEntity> c) throws OWLException{ OWLEntityCollector ec = new OWLEntityCollector(); onto.accept(ec); Set<Object> entities = new HashSet<Object>(); for (Object obj : ec.entities()) { - if (c.isInstance(obj) ){ + // JD: OWLEntitytCollector seems to return anonymous entities :&& ((OWLEntity)obj).getURI()!=null + if (c.isInstance(obj) ){ entities.add(obj); } } @@ -211,18 +210,15 @@ public class OWLAPIOntology extends BasicOntology<OWLOntology> implements Loaded // Here it shoud be better to report exception // JE: Onto this does not work at all, of course...!!!! - public Set<Object> getEntities() { - OWLEntityCollector ec = new OWLEntityCollector(); - try { - onto.accept(ec); - return ec.entities(); - - } catch (OWLException e) { - return null; - } + public Set<?> getEntities() { + try { + return getEntities(OWLEntity.class); + } catch (OWLException ex) { + return null; + } } - public Set<Object> getClasses() { + public Set<?> getClasses() { try { return ((OWLOntology)onto).getClasses(); // [W:unchecked] } catch (OWLException ex) { @@ -230,7 +226,7 @@ public class OWLAPIOntology extends BasicOntology<OWLOntology> implements Loaded } } - public Set<Object> getProperties() { + public Set<?> getProperties() { try { //return ((OWLOntology)onto).getProperties(); // [W:unchecked] return getEntities(OWLProperty.class); @@ -242,7 +238,7 @@ public class OWLAPIOntology extends BasicOntology<OWLOntology> implements Loaded // 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 Set<Object> getObjectProperties() { + public Set<?> getObjectProperties() { try { // [Warning:unchecked] due to OWL API not serving generic types return ((OWLOntology)onto).getObjectProperties(); // [W:unchecked] @@ -251,7 +247,7 @@ public class OWLAPIOntology extends BasicOntology<OWLOntology> implements Loaded } } - public Set<Object> getDataProperties() { + public Set<?> getDataProperties() { try { return ((OWLOntology)onto).getDataProperties(); // [W:unchecked] } catch (OWLException ex) { @@ -259,7 +255,7 @@ public class OWLAPIOntology extends BasicOntology<OWLOntology> implements Loaded } } - public Set<Object> getIndividuals() { + public Set<?> getIndividuals() { try { return ((OWLOntology)onto).getIndividuals(); // [W:unchecked] } catch (OWLException ex) { @@ -372,7 +368,7 @@ public class OWLAPIOntology extends BasicOntology<OWLOntology> implements Loaded /* HeavyLoadedOntology specifics */ - public Set<Object> getProperties( OWLDescription desc ) { + public Set<?> getProperties( OWLDescription desc ) { Set<Object> list = new HashSet<Object>(); try { if ( desc instanceof OWLRestriction ){ @@ -381,13 +377,13 @@ public class OWLAPIOntology extends BasicOntology<OWLOntology> implements Loaded // JE: I suspect that this can be a cause for looping!! for ( Object cl : ((OWLClass)desc).getEquivalentClasses((OWLOntology)onto) ){ // JE: strange casting - Set<Object> res = getProperties( (OWLDescription)cl ); + Set<?> res = getProperties( (OWLDescription)cl ); if ( res != null ) list.add( res ); } } else if ( desc instanceof OWLNaryBooleanDescription ) { for ( Object d : ((OWLNaryBooleanDescription)desc).getOperands() ){ // JE: strange casting - Set<Object> res = getProperties( (OWLDescription)d ); + Set<?> res = getProperties( (OWLDescription)d ); if ( res != null ) list.add( res ); } } diff --git a/src/fr/inrialpes/exmo/align/onto/owlapi10/OWLAPIOntologyFactory.java b/src/fr/inrialpes/exmo/align/onto/owlapi10/OWLAPIOntologyFactory.java index 8004be83..766a1394 100644 --- a/src/fr/inrialpes/exmo/align/onto/owlapi10/OWLAPIOntologyFactory.java +++ b/src/fr/inrialpes/exmo/align/onto/owlapi10/OWLAPIOntologyFactory.java @@ -7,12 +7,12 @@ * 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 @@ -66,8 +66,8 @@ public class OWLAPIOntologyFactory extends OntologyFactory { public LoadedOntology loadOntology( URI uri ) throws AlignmentException { OWLConnection connection = null; - Map parameters = new HashMap(); - parameters.put(OWLManager.OWL_CONNECTION, // [W:unchecked] + Map<Object,Object> parameters = new HashMap<Object, Object>(); + parameters.put(OWLManager.OWL_CONNECTION, "org.semanticweb.owl.impl.model.OWLConnectionImpl"); try { connection = OWLManager.getOWLConnection(parameters); @@ -88,7 +88,7 @@ public class OWLAPIOntologyFactory extends OntologyFactory { e.printStackTrace(); } return onto; - } catch (OWLException e) { + } catch (OWLException e) { throw new AlignmentException("Cannot load "+uri, e ); } } @@ -104,7 +104,7 @@ public class OWLAPIOntologyFactory extends OntologyFactory { try { OWLRDFParser parser = new OWLRDFParser(); OWLRDFErrorHandler handler = new OWLRDFErrorHandler(){ - public void owlFullConstruct( int code, String message ) + public void owlFullConstruct( int code, String message ) throws SAXException { } public void owlFullConstruct(int code, String message, Object o) -- GitLab