diff --git a/src/fr/inrialpes/exmo/ontowrap/LoadedOntology.java b/src/fr/inrialpes/exmo/ontowrap/LoadedOntology.java index 9b5660964911aaf275977c1fe791e592626a834c..103f391a25b04ae010764cd61702880b613d4a27 100644 --- a/src/fr/inrialpes/exmo/ontowrap/LoadedOntology.java +++ b/src/fr/inrialpes/exmo/ontowrap/LoadedOntology.java @@ -23,6 +23,11 @@ package fr.inrialpes.exmo.ontowrap; import java.net.URI; import java.util.Set; +import org.semanticweb.owl.model.OWLEntity; +import org.semanticweb.owl.model.OWLException; + +import fr.inrialpes.exmo.ontowrap.util.FilteredSet; + public interface LoadedOntology<O> extends Ontology<O> { public Object getEntity( URI u ) throws OntowrapException; @@ -120,6 +125,10 @@ public interface LoadedOntology<O> extends Ontology<O> { public boolean isObjectProperty( Object o ); public boolean isIndividual( Object o ); + /** + * Returns all named entities having URI beginning with the ontology URI + * @return the set of entities + */ public Set<? extends Object> getEntities(); public Set<? extends Object> getClasses(); public Set<? extends Object> getProperties(); diff --git a/src/fr/inrialpes/exmo/ontowrap/jena25/JENAOntology.java b/src/fr/inrialpes/exmo/ontowrap/jena25/JENAOntology.java index a5e3f2bf007e28773a54afecd6d47bcf2c4f5fd8..071bfb1c6953f78d7f6c9ad4355157da813e1376 100644 --- a/src/fr/inrialpes/exmo/ontowrap/jena25/JENAOntology.java +++ b/src/fr/inrialpes/exmo/ontowrap/jena25/JENAOntology.java @@ -21,11 +21,10 @@ package fr.inrialpes.exmo.ontowrap.jena25; import java.net.URI; -import java.util.AbstractSet; import java.util.HashSet; import java.util.Iterator; import java.util.Set; -import java.util.NoSuchElementException; + import com.hp.hpl.jena.ontology.DatatypeProperty; import com.hp.hpl.jena.ontology.Individual; @@ -35,13 +34,15 @@ 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.ontology.impl.AnnotationPropertyImpl; import com.hp.hpl.jena.rdf.model.Literal; import com.hp.hpl.jena.rdf.model.NodeIterator; import com.hp.hpl.jena.rdf.model.Property; import com.hp.hpl.jena.rdf.model.RDFNode; +import com.hp.hpl.jena.rdf.model.Resource; +import com.hp.hpl.jena.rdf.model.Statement; +import com.hp.hpl.jena.rdf.model.StmtIterator; -import com.hp.hpl.jena.util.iterator.ExtendedIterator; -import com.hp.hpl.jena.util.iterator.Filter; import com.hp.hpl.jena.util.iterator.Map1; import com.hp.hpl.jena.rdf.model.impl.LiteralImpl; @@ -49,6 +50,7 @@ import com.hp.hpl.jena.rdf.model.impl.LiteralImpl; import fr.inrialpes.exmo.ontowrap.BasicOntology; import fr.inrialpes.exmo.ontowrap.LoadedOntology; import fr.inrialpes.exmo.ontowrap.OntowrapException; +import fr.inrialpes.exmo.ontowrap.util.EntityFilter; public class JENAOntology extends BasicOntology<OntModel> implements LoadedOntology<OntModel>{ @@ -59,11 +61,32 @@ public class JENAOntology extends BasicOntology<OntModel> implements LoadedOntol return onto.getOntResource(u.toString()); } - public void getEntityAnnotations( Object o, Set<String> annots ) throws OntowrapException { + public void getEntityAnnotations( Object o, Set<String> annots, String lang) throws OntowrapException { + + StmtIterator stmtIt = onto.listStatements((Resource)o,null,(RDFNode)null); + while (stmtIt.hasNext()) { + Statement st = stmtIt.next(); + + if ( st.getPredicate().canAs(AnnotationProperty.class)) { + RDFNode obj= st.getObject(); + if (obj.isLiteral()) { + Literal l =obj.as(Literal.class); + if (lang==null || lang.equals(l.getLanguage())) { + annots.add(l.getLexicalForm()); + } + else if (obj.isResource()) { + getEntityAnnotations(obj, annots, lang); + } + } + } + } + // THIS SHOULD BE STATIC NOW!! - Iterator<AnnotationProperty> z = onto.listAnnotationProperties(); + /*Iterator<AnnotationProperty> z = onto.listAnnotationProperties(); while (z.hasNext()) { - NodeIterator j = ((OntResource)o).listPropertyValues((Property)z.next()); + AnnotationProperty ap = z.next(); + // System.out.println("hjhjhjh"+ap); + NodeIterator j = ((OntResource)o).listPropertyValues((Property) ap); while (j.hasNext()) { RDFNode n = j.nextNode(); if (n.isResource()) @@ -72,15 +95,21 @@ public class JENAOntology extends BasicOntology<OntModel> implements LoadedOntol annots.add( ((Literal)n.as(Literal.class)).getLexicalForm() ); } } - } + }*/ } public Set<String> getEntityAnnotations(Object o) throws OntowrapException { Set<String> annots = new HashSet<String>(); - getEntityAnnotations(o,annots); + getEntityAnnotations(o,annots,null); return annots; } + public Set<String> getEntityAnnotations( Object o, String lang ) throws OntowrapException { + Set<String> annots = new HashSet<String>(); + getEntityAnnotations(o,annots,lang); + return annots; + } + public Set<String> getEntityComments(Object o, String lang) throws OntowrapException { Set<String> comments = new HashSet<String>(); Iterator<RDFNode> it = ((OntResource)o).listComments(lang); @@ -133,13 +162,13 @@ public class JENAOntology extends BasicOntology<OntModel> implements LoadedOntol } } - /** + /* * This strange structure, as well as the corresponding JENAEntityIt * is there only because Jena may return unammed entities that have to * be filtered out from the sets. * */ - protected <R extends OntResource> Set<R> getEntitySet(final ExtendedIterator<R> i) { + /*protected <R extends OntResource> Set<R> getEntitySet(final ExtendedIterator<R> i) { return new AbstractSet<R>() { private int size=-1; public Iterator<R> iterator() { @@ -154,50 +183,42 @@ public class JENAOntology extends BasicOntology<OntModel> implements LoadedOntol return size; } }; - } + }*/ public Set<OntClass> getClasses() { - return onto.listNamedClasses().toSet(); + return new EntityFilter<OntClass>(onto.listNamedClasses().toSet(),this); } public Set<DatatypeProperty> getDataProperties() { - return getEntitySet(onto.listDatatypeProperties()); + return new EntityFilter<DatatypeProperty>(onto.listDatatypeProperties().toSet(),this); + //return getEntitySet(onto.listDatatypeProperties()); } - static Map1 myMap = new Map1 () { public OntResource map1 ( Object o ) { return (OntResource)o; } }; - + protected final static Map1<OntProperty,OntResource> mapProperty = new Map1<OntProperty,OntResource> () { public OntResource map1 ( OntProperty o ) { return o; } }; + protected final static Map1<OntClass,OntResource> mapClass = new Map1<OntClass,OntResource> () { public OntResource map1 ( OntClass o ) { return o; } }; + protected final static Map1<Individual,OntResource> mapInd = new Map1<Individual,OntResource> () { public OntResource map1 ( Individual o ) { return o; } }; + + public Set<OntResource> getEntities() { - //ExtendedIterator<OntResource> a = onto.listObjectProperties(); - return getEntitySet(onto.listObjectProperties().mapWith( myMap ) - .andThen(onto.listDatatypeProperties().mapWith( myMap )) - .andThen(onto.listNamedClasses().mapWith( myMap )) - .andThen(onto.listIndividuals().mapWith( myMap )) - ); - //.andThen(((ExtendedIterator<OntResource>)(onto.listDatatypeProperties())))); - /* - return getEntitySet( onto.listOntProperties().filterKeep( new Filter () { - public boolean accept( Object o ) { return (o instanceof DatatypeProperty) || - (o instanceof ObjectProperty) || (o instanceof Individual) || (o instanceof OntClass); } - }) ); - return getEntitySet(onto.listObjectProperties()); - .andThen(onto.listDatatypeProperties()) - .andThen(onto.listIndividuals()) - .andThen(onto.listClasses())); - */ + return new EntityFilter<OntResource>((onto.listAllOntProperties().mapWith( mapProperty ) + .andThen(onto.listNamedClasses().mapWith( mapClass )) + .andThen(onto.listIndividuals().mapWith( mapInd )).toSet()), + this); } public Set<Individual> getIndividuals() { - return getEntitySet(onto.listIndividuals()); + return new EntityFilter<Individual>(onto.listIndividuals().toSet(),this); } public Set<ObjectProperty> getObjectProperties() { - return getEntitySet(onto.listObjectProperties()); + return new EntityFilter<ObjectProperty>(onto.listObjectProperties().toSet(),this); } public Set<OntProperty> getProperties() { - return getEntitySet( onto.listAllOntProperties().filterDrop( new Filter () { + return new EntityFilter<OntProperty>(onto.listAllOntProperties().toSet(),this); + /*return getEntitySet( onto.listAllOntProperties().filterDrop( new Filter () { public boolean accept( Object o ) { return (o instanceof AnnotationProperty); } - }) ); + }) );*/ //return getEntitySet(onto.listObjectProperties().andThen(onto.listDatatypeProperties())); } @@ -256,7 +277,7 @@ public class JENAOntology extends BasicOntology<OntModel> implements LoadedOntol } -class JENAEntityIt<R extends OntResource> implements Iterator<R> { +/*class JENAEntityIt<R extends OntResource> implements Iterator<R> { private ExtendedIterator<R> it; private R current; @@ -295,4 +316,4 @@ class JENAEntityIt<R extends OntResource> implements Iterator<R> { public void remove() { throw new UnsupportedOperationException(); } -} +}*/ diff --git a/src/fr/inrialpes/exmo/ontowrap/owlapi10/OWLAPIOntology.java b/src/fr/inrialpes/exmo/ontowrap/owlapi10/OWLAPIOntology.java index 19fa7b3c4bd0b7e750ba66949ed0eb28c70d9844..e6e235433b7cfd454cffee25ed282fca4d52b80a 100644 --- a/src/fr/inrialpes/exmo/ontowrap/owlapi10/OWLAPIOntology.java +++ b/src/fr/inrialpes/exmo/ontowrap/owlapi10/OWLAPIOntology.java @@ -43,11 +43,10 @@ import fr.inrialpes.exmo.ontowrap.OntologyFactory; import fr.inrialpes.exmo.ontowrap.HeavyLoadedOntology; import fr.inrialpes.exmo.ontowrap.BasicOntology; import fr.inrialpes.exmo.ontowrap.OntowrapException; +import fr.inrialpes.exmo.ontowrap.util.EntityFilter; import org.semanticweb.owl.io.vocabulary.RDFSVocabularyAdapter; -import org.semanticweb.owl.model.OWLAnnotationInstance; import org.semanticweb.owl.model.OWLDataType; -import org.semanticweb.owl.model.OWLDataValue; import org.semanticweb.owl.model.OWLOntology; import org.semanticweb.owl.model.OWLProperty; import org.semanticweb.owl.model.OWLClass; @@ -59,8 +58,6 @@ import org.semanticweb.owl.model.OWLRestriction; import org.semanticweb.owl.model.OWLDescription; import org.semanticweb.owl.model.OWLNaryBooleanDescription; import org.semanticweb.owl.model.OWLCardinalityRestriction; -import org.semanticweb.owl.model.OWLDataAllRestriction; -import org.semanticweb.owl.model.OWLObjectAllRestriction; import org.semanticweb.owl.model.OWLException; import org.semanticweb.owl.model.helper.OWLEntityCollector; @@ -212,7 +209,16 @@ public class OWLAPIOntology extends BasicOntology<OWLOntology> implements HeavyL } catch (OWLException oex) { return null; } - }; + } + + public Set<String> getEntityAnnotations(Object o, String lang) throws OntowrapException { + try { + return getAnnotations(((OWLEntity) o),lang,null); + } catch (OWLException oex) { + return null; + } + } + public boolean isEntity( Object o ){ return ( o instanceof OWLEntity ); @@ -237,17 +243,19 @@ public class OWLAPIOntology extends BasicOntology<OWLOntology> implements HeavyL // We should solve this issue, is it better to go this way or tu use the OWL API? // JD: allows to retrieve some specific entities by giving their class // This is not part of the interface... + @SuppressWarnings("unchecked") protected Set<?> getEntities(Class<? extends OWLEntity> c) throws OWLException{ OWLEntityCollector ec = new OWLEntityCollector(); onto.accept(ec); - Set<Object> entities = new HashSet<Object>(); + return new EntityFilter<OWLEntity>((Set<OWLEntity>) ec.entities(),this); + /*Set<Object> entities = new HashSet<Object>(); for (Object obj : ec.entities()) { // JD: OWLEntitytCollector seems to return anonymous entities :&& ((OWLEntity)obj).getURI()!=null if (c.isInstance(obj) && ((OWLEntity) obj).getURI()!=null) { // &&((OWLEntity) obj).getURI().toString().startsWith(onto.getURI().toString()) ){ entities.add(obj); } } - return entities; + return entities;*/ } // Here it shoud be better to report exception @@ -262,8 +270,8 @@ public class OWLAPIOntology extends BasicOntology<OWLOntology> implements HeavyL public Set<?> getClasses() { try { - return onto.getClasses(); // [W:unchecked] - //return getEntities(OWLClass.class); + //return onto.getClasses(); // [W:unchecked] + return getEntities(OWLClass.class); } catch (OWLException ex) { return null; } @@ -282,8 +290,8 @@ public class OWLAPIOntology extends BasicOntology<OWLOntology> implements HeavyL try { // This first method returns also Properties not defined in the Onto // i.e. properties having an namespace different from the ontology uri - return onto.getDataProperties(); // [W:unchecked] - //return getEntities(OWLDataProperty.class); + //return onto.getDataProperties(); // [W:unchecked] + return getEntities(OWLDataProperty.class); } catch (OWLException ex) { return null; } @@ -294,9 +302,9 @@ public class OWLAPIOntology extends BasicOntology<OWLOntology> implements HeavyL // [Warning:unchecked] due to OWL API not serving generic types // This first method returns also Properties not defined in the Onto // i.e. properties having an namespace different from the ontology uri - return onto.getObjectProperties(); // [W:unchecked] + //return onto.getObjectProperties(); // [W:unchecked] // This method works better (??) - //return getEntities(OWLObjectProperty.class); + return getEntities(OWLObjectProperty.class); } catch (OWLException ex) { return null; } @@ -304,8 +312,8 @@ public class OWLAPIOntology extends BasicOntology<OWLOntology> implements HeavyL public Set<?> getIndividuals() { try { - return onto.getIndividuals(); // [W:unchecked] - //return getEntities(OWLIndividual.class); + //return onto.getIndividuals(); // [W:unchecked] + return getEntities(OWLIndividual.class); } catch (OWLException ex) { return null; } @@ -313,18 +321,14 @@ public class OWLAPIOntology extends BasicOntology<OWLOntology> implements HeavyL public int nbEntities() { if ( nbentities != -1 ) return nbentities; - nbentities = nbClasses()+nbProperties()+nbIndividuals(); + nbentities = getEntities().size();//nbClasses()+nbProperties()+nbIndividuals(); return nbentities; } public int nbClasses() { if ( nbclasses != -1 ) return nbclasses; - try { - nbclasses = onto.getClasses().size(); - return nbclasses; - } catch (OWLException oex) { - return 0; - } + nbclasses = getClasses().size(); + return nbclasses; } public int nbProperties() { @@ -335,32 +339,23 @@ public class OWLAPIOntology extends BasicOntology<OWLOntology> implements HeavyL public int nbObjectProperties() { if ( nbobjectproperties != -1 ) return nbobjectproperties; - try { - nbobjectproperties = onto.getObjectProperties().size(); - return nbobjectproperties; - } catch (OWLException oex) { - return 0; - } + nbobjectproperties = getObjectProperties().size(); + return nbobjectproperties; + } public int nbDataProperties() { if ( nbdataproperties != -1 ) return nbdataproperties; - try { - nbdataproperties = onto.getDataProperties().size(); - return nbdataproperties; - } catch (OWLException oex) { - return 0; - } + nbdataproperties = getDataProperties().size(); + return nbdataproperties; + } public int nbIndividuals() { if ( nbindividuals != -1 ) return nbindividuals; - try { - nbindividuals = onto.getIndividuals().size(); - return nbindividuals; - } catch (OWLException oex) { - return 0; - } + nbindividuals = getIndividuals().size(); + return nbindividuals; + } public void unload() { @@ -581,4 +576,6 @@ public class OWLAPIOntology extends BasicOntology<OWLOntology> implements HeavyL } } + + } diff --git a/src/fr/inrialpes/exmo/ontowrap/owlapi30/OWLAPI3Ontology.java b/src/fr/inrialpes/exmo/ontowrap/owlapi30/OWLAPI3Ontology.java index b12d6f58ce926b6be6afff17bacde1479a7c17f1..4d0faa7295fd05d2e99f54dc00b24c3e32acc7ad 100644 --- a/src/fr/inrialpes/exmo/ontowrap/owlapi30/OWLAPI3Ontology.java +++ b/src/fr/inrialpes/exmo/ontowrap/owlapi30/OWLAPI3Ontology.java @@ -21,18 +21,17 @@ package fr.inrialpes.exmo.ontowrap.owlapi30; import java.net.URI; -import java.util.AbstractSet; -import java.util.ArrayList; -import java.util.Collection; import java.util.HashSet; import java.util.NoSuchElementException; import java.util.Set; -import java.util.Iterator; +import org.semanticweb.owlapi.apibinding.OWLManager; import org.semanticweb.owlapi.model.IRI; import org.semanticweb.owlapi.model.OWLAnnotation; +import org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom; import org.semanticweb.owlapi.model.OWLAnnotationValue; import org.semanticweb.owlapi.model.OWLAnnotationProperty; +import org.semanticweb.owlapi.model.OWLAxiom; import org.semanticweb.owlapi.model.OWLDatatype; import org.semanticweb.owlapi.model.OWLClass; import org.semanticweb.owlapi.model.OWLLiteral; @@ -40,6 +39,7 @@ import org.semanticweb.owlapi.model.OWLDataProperty; import org.semanticweb.owlapi.model.OWLEntity; import org.semanticweb.owlapi.model.OWLIndividual; import org.semanticweb.owlapi.model.OWLNamedIndividual; +import org.semanticweb.owlapi.model.OWLObject; import org.semanticweb.owlapi.model.OWLObjectProperty; import org.semanticweb.owlapi.model.OWLOntology; import org.semanticweb.owlapi.model.OWLProperty; @@ -55,19 +55,9 @@ import fr.inrialpes.exmo.ontowrap.BasicOntology; import fr.inrialpes.exmo.ontowrap.OntologyFactory; import fr.inrialpes.exmo.ontowrap.HeavyLoadedOntology; import fr.inrialpes.exmo.ontowrap.OntowrapException; -import fr.inrialpes.exmo.ontowrap.util.FilteredSet; +import fr.inrialpes.exmo.ontowrap.util.EntityFilter; -public class OWLAPI3Ontology extends BasicOntology<OWLOntology> implements - HeavyLoadedOntology<OWLOntology> { - - private static class BuilInFilter<T extends OWLEntity> extends FilteredSet<T> { - public BuilInFilter(Set<T> s) { - super(s); - } - protected boolean isFiltered(OWLEntity obj) { - return obj.isBuiltIn()&&!obj.isTopEntity(); - } - } +public class OWLAPI3Ontology extends BasicOntology<OWLOntology> implements HeavyLoadedOntology<OWLOntology> { public OWLAPI3Ontology() { } @@ -75,17 +65,17 @@ public class OWLAPI3Ontology extends BasicOntology<OWLOntology> implements public Set<? extends Object> getClasses() { //return onto.getReferencedClasses(); //return onto.getClassesInSignature(); - return new BuilInFilter<OWLClass>(onto.getClassesInSignature()); + return new EntityFilter<OWLClass>(onto.getClassesInSignature(),this); } public Set<? extends Object> getDataProperties() { - return new BuilInFilter<OWLDataProperty>(onto.getDataPropertiesInSignature()); + return new EntityFilter<OWLDataProperty>(onto.getDataPropertiesInSignature(),this); //return onto.getDataPropertiesInSignature(); } public Set<? extends Object> getEntities() { //return onto.getSignature(); - return new BuilInFilter<OWLEntity>(onto.getSignature()) { + return new EntityFilter<OWLEntity>(onto.getSignature(),this) { protected final boolean isFiltered(OWLEntity obj) { return super.isFiltered(obj) || obj instanceof OWLAnnotationProperty || obj instanceof OWLDatatype; } @@ -109,7 +99,27 @@ public class OWLAPI3Ontology extends BasicOntology<OWLOntology> implements * type and lang can be null */ protected Set<String> getEntityAnnotations( Object o, URI type, String lang ) { + // We cannot retreive encapsulated annotations with this API + /*OWLEntity entity = (OWLEntity) o; + HashSet<String> annotations = new HashSet<String>(); + for (OWLAnnotationAssertionAxiom annot :entity.getAnnotationAssertionAxioms(onto)) { + OWLAnnotationValue c = annot.getValue(); + if ( c instanceof OWLLiteral ) { + if ( lang == null || ((OWLLiteral)c).hasLang(lang) ) { + annotations.add( ((OWLLiteral)c).getLiteral() ); + } + } + else if (c instanceof IRI) { + //System.out.println(c.getClass()+ " : "+c); + IRI i = (IRI) c; + OWLAnnotationProperty ann = OWLManager.getOWLDataFactory().getOWLAnnotationProperty(i); + System.out.println(ann); + } + } + return annotations; + */ OWLEntity entity = (OWLEntity) o; + Set<String> annotations = new HashSet<String>(); // JE: This had to be rewritten for OWL API 3. Too bad. for ( OWLAnnotation annot : entity.getAnnotations( onto ) ) { @@ -130,6 +140,10 @@ public class OWLAPI3Ontology extends BasicOntology<OWLOntology> implements public Set<String> getEntityAnnotations(Object o) throws OntowrapException { return getEntityAnnotations(o, null, null); } + + public Set<String> getEntityAnnotations( Object o, String lang ) throws OntowrapException { + return getEntityAnnotations(o, null, lang); + } public Set<String> getEntityComments(Object o, String lang) throws OntowrapException { @@ -182,17 +196,17 @@ public class OWLAPI3Ontology extends BasicOntology<OWLOntology> implements public Set<? extends Object> getIndividuals() { //return onto.getIndividualsInSignature(); - return new BuilInFilter<OWLNamedIndividual>(onto.getIndividualsInSignature()); + return new EntityFilter<OWLNamedIndividual>(onto.getIndividualsInSignature(),this); } public Set<? extends Object> getObjectProperties() { //System.err.println ( "ONTO: "+onto ); //return onto.getObjectPropertiesInSignature(); - return new BuilInFilter<OWLObjectProperty>(onto.getObjectPropertiesInSignature()); + return new EntityFilter<OWLObjectProperty>(onto.getObjectPropertiesInSignature(),this); } public Set<? extends Object> getProperties() { - return new BuilInFilter<OWLEntity>(onto.getSignature()) { + return new EntityFilter<OWLEntity>(onto.getSignature(),this) { protected final boolean isFiltered(OWLEntity obj) { return super.isFiltered(obj) || !(obj instanceof OWLObjectProperty || obj instanceof OWLDataProperty); } diff --git a/src/fr/inrialpes/exmo/ontowrap/util/EntityFilter.java b/src/fr/inrialpes/exmo/ontowrap/util/EntityFilter.java new file mode 100644 index 0000000000000000000000000000000000000000..766c97a3ef3e0e3ad5cf13c6f15bdb62c079aca8 --- /dev/null +++ b/src/fr/inrialpes/exmo/ontowrap/util/EntityFilter.java @@ -0,0 +1,52 @@ +/* + * $Id: EntityFilter.java 896 2010-03-26 14:45:46Z jdavid $ + * + * Copyright (C) INRIA, 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.util; + +import java.net.URI; +import java.util.Set; + +import fr.inrialpes.exmo.ontowrap.LoadedOntology; +import fr.inrialpes.exmo.ontowrap.OntowrapException; + +public class EntityFilter<T> extends FilteredSet<T> { + private String ontoURI=null; + private LoadedOntology<?> onto=null; + public EntityFilter(Set<T> s, LoadedOntology<?> onto) { + super(s); + ontoURI = onto.getURI().toString().replaceFirst("#.*", ""); + this.onto=onto; + } + + /** + * filter ontology entities which have no URI or external URI + * ontoURI.equals(entURI.toString()) is for OWL API 1 : it seems that anonymous entities have their URI = ontology URI + */ + protected boolean isFiltered(T obj) { + try { + URI entURI=onto.getEntityURI(obj); + //System.out.println(entURI +" - "+ontoURI); + return (entURI.getAuthority()!=null) && (entURI==null || ontoURI.equals(entURI.toString()) || !entURI.toString().startsWith(ontoURI)); + } + catch (OntowrapException e) { + // e.printStackTrace(); + } + return true; + } +}