From fa42fc5c985e9be45e7faa897aff4a299775b8a9 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, 16 Jul 2013 15:10:38 +0000
Subject: [PATCH] Added onlyLocalEntities option to ontologyfactory. This is
 only implemented for Jena and OWL3API

---
 .../exmo/ontowrap/OntologyFactory.java        | 18 +++++++-
 .../exmo/ontowrap/jena25/JENAOntology.java    | 43 ++++++++++++++-----
 .../ontowrap/jena25/JENAOntologyFactory.java  | 10 +++--
 .../owlapi10/OWLAPIOntologyFactory.java       |  4 +-
 .../ontowrap/owlapi30/OWLAPI3Ontology.java    | 33 +++++++++++---
 .../owlapi30/OWLAPI3OntologyFactory.java      |  8 ++--
 .../ontowrap/skosapi/SKOSOntologyFactory.java |  4 +-
 .../skoslite/SKOSLiteOntologyFactory.java     |  4 +-
 .../exmo/ontowrap/util/EntityFilter.java      |  1 +
 9 files changed, 91 insertions(+), 34 deletions(-)

diff --git a/src/fr/inrialpes/exmo/ontowrap/OntologyFactory.java b/src/fr/inrialpes/exmo/ontowrap/OntologyFactory.java
index 8d8f5b71..f80d48df 100644
--- a/src/fr/inrialpes/exmo/ontowrap/OntologyFactory.java
+++ b/src/fr/inrialpes/exmo/ontowrap/OntologyFactory.java
@@ -144,13 +144,27 @@ public abstract class OntologyFactory {
      * Encapsulate an ontology already in the environment
      * These methods should rather be in a LoadableOntologyFactory
      */
-    public abstract LoadedOntology newOntology( Object onto ) throws OntowrapException;
+    public abstract LoadedOntology newOntology( Object onto , boolean onlyLocalEntities ) throws OntowrapException;
+    
+    public  LoadedOntology newOntology( Object onto ) throws OntowrapException {
+	return newOntology(onto,false);
+    }
+    
+    
 
     /**
      * Load an ontology, cache enabled
      * These methods should rather be in a LoadableOntologyFactory
      */
-    public abstract LoadedOntology loadOntology( URI uri ) throws OntowrapException;
+    public LoadedOntology loadOntology( URI uri ) throws OntowrapException {
+	return loadOntology(uri,true);
+    }
+    
+    /**
+     * Load an ontology, cache enabled
+     * These methods should rather be in a LoadableOntologyFactory
+     */
+    public abstract LoadedOntology loadOntology( URI uri , boolean onlyLocalEntities) throws OntowrapException;
 
     /**
      * Load an ontology, cache enabled if true, disabled otherwise
diff --git a/src/fr/inrialpes/exmo/ontowrap/jena25/JENAOntology.java b/src/fr/inrialpes/exmo/ontowrap/jena25/JENAOntology.java
index 1bb6bbcf..f7bec973 100644
--- a/src/fr/inrialpes/exmo/ontowrap/jena25/JENAOntology.java
+++ b/src/fr/inrialpes/exmo/ontowrap/jena25/JENAOntology.java
@@ -55,8 +55,15 @@ import fr.inrialpes.exmo.ontowrap.util.EntityFilter;
 
 public class JENAOntology extends BasicOntology<OntModel> implements HeavyLoadedOntology<OntModel>{
 
+    private boolean onlyLocalEntities=true;
     // JE: this is not very Java 1.5...
     // This is because of the version of Jena we use apparently
+    
+    
+
+    public JENAOntology(boolean onlyLocalEntities) {
+	this.onlyLocalEntities=onlyLocalEntities;
+    }
 
     public Object getEntity(URI u) throws OntowrapException {
 	return onto.getOntResource(u.toString());
@@ -205,13 +212,22 @@ public class JENAOntology extends BasicOntology<OntModel> implements HeavyLoaded
 	    }
 	};
     }*/
+    
+    private <K> Set<K> getFilteredOrNot(Set<K> s) {
+	if (onlyLocalEntities)
+	    return new EntityFilter<K>(s,this);
+	else 
+	    return s;
+    }
 
     public Set<OntClass> getClasses() {
-	return new EntityFilter<OntClass>(onto.listNamedClasses().toSet(),this);
+	
+	    return getFilteredOrNot(onto.listNamedClasses().toSet());
+	
     }
 
     public Set<DatatypeProperty> getDataProperties() {
-	return new EntityFilter<DatatypeProperty>(onto.listDatatypeProperties().toSet(),this);
+	return getFilteredOrNot(onto.listDatatypeProperties().toSet());
 	//return getEntitySet(onto.listDatatypeProperties());
     }
 
@@ -222,22 +238,21 @@ public class JENAOntology extends BasicOntology<OntModel> implements HeavyLoaded
     
     
     public Set<OntResource> getEntities() {
-	return new EntityFilter<OntResource>((onto.listAllOntProperties().mapWith( mapProperty )
+	return getFilteredOrNot((onto.listAllOntProperties().mapWith( mapProperty )
 		    .andThen(onto.listNamedClasses().mapWith( mapClass ))
-		    .andThen(onto.listIndividuals().mapWith( mapInd )).toSet()),
-		    this);
+		    .andThen(onto.listIndividuals().mapWith( mapInd )).toSet()));
     }
 
     public Set<Individual> getIndividuals() {
-	return new EntityFilter<Individual>(onto.listIndividuals().toSet(),this);
+	return getFilteredOrNot(onto.listIndividuals().toSet());
     }
 
     public Set<ObjectProperty> getObjectProperties() {
-	return new EntityFilter<ObjectProperty>(onto.listObjectProperties().toSet(),this);
+	return getFilteredOrNot(onto.listObjectProperties().toSet());
     }
 
     public Set<OntProperty> getProperties() {
-	return new EntityFilter<OntProperty>(onto.listAllOntProperties().toSet(),this);
+	return getFilteredOrNot(onto.listAllOntProperties().toSet());
 	/*return getEntitySet( onto.listAllOntProperties().filterDrop( new Filter () {
 		public boolean accept( Object o ) { return (o instanceof AnnotationProperty); }
 	    }) );*/
@@ -308,7 +323,10 @@ public class JENAOntology extends BasicOntology<OntModel> implements HeavyLoaded
     public Set<OntProperty> getDataProperties(Object c, int local, int asserted, int named) {
 	return new EntityFilter<OntProperty>( ((OntClass) c).listDeclaredProperties(asserted==OntologyFactory.DIRECT).toSet(),this) {
 	    protected boolean isFiltered(OntProperty obj) {
-		return super.isFiltered(obj) && !obj.isDatatypeProperty();
+		if (JENAOntology.this.onlyLocalEntities)
+		    return super.isFiltered(obj) && !obj.isDatatypeProperty();
+		else 
+		    return !obj.isDatatypeProperty();
 	    }
 	    
 	};
@@ -329,12 +347,15 @@ public class JENAOntology extends BasicOntology<OntModel> implements HeavyLoaded
     public Set<OntProperty> getObjectProperties(Object c, int local, int asserted, int named) {
 	return new EntityFilter<OntProperty>( ((OntClass) c).listDeclaredProperties(asserted==OntologyFactory.DIRECT).toSet(),this) {
 	    protected boolean isFiltered( OntProperty obj ) {
-		return super.isFiltered(obj) && !obj.isObjectProperty();
+		if (JENAOntology.this.onlyLocalEntities)
+		    return super.isFiltered(obj) && !obj.isObjectProperty();
+		else
+		    return !obj.isObjectProperty();
 	    }  
 	};
     }
     public Set<OntProperty> getProperties(Object c, int local, int asserted, int named) {
-	return new EntityFilter<OntProperty>( ((OntClass) c).listDeclaredProperties(asserted==OntologyFactory.DIRECT).toSet(),this);
+	return getFilteredOrNot(((OntClass) c).listDeclaredProperties(asserted==OntologyFactory.DIRECT).toSet());
     }
 
     public Set<? extends OntResource> getRange(Object p, int asserted) {
diff --git a/src/fr/inrialpes/exmo/ontowrap/jena25/JENAOntologyFactory.java b/src/fr/inrialpes/exmo/ontowrap/jena25/JENAOntologyFactory.java
index 4b6ca350..08e4286a 100644
--- a/src/fr/inrialpes/exmo/ontowrap/jena25/JENAOntologyFactory.java
+++ b/src/fr/inrialpes/exmo/ontowrap/jena25/JENAOntologyFactory.java
@@ -48,9 +48,9 @@ public class JENAOntologyFactory extends OntologyFactory {
 	} catch (URISyntaxException ex) { ex.printStackTrace(); } // should not happen
     }
 
-    public JENAOntology newOntology( Object ontology ) throws OntowrapException {
+    public JENAOntology newOntology( Object ontology , boolean onlyLocalEntities ) throws OntowrapException {
 	if ( ontology instanceof OntModel ) {
-	    JENAOntology onto = new JENAOntology();
+	    JENAOntology onto = new JENAOntology(onlyLocalEntities);
 	    onto.setFormalism( formalismId );
 	    onto.setFormURI( formalismUri );
 	    onto.setOntology( (OntModel)ontology );
@@ -70,12 +70,14 @@ public class JENAOntologyFactory extends OntologyFactory {
 	    }
 	    cache.recordOntology( onto.getURI(), onto );
 	    return onto;
+
 	} else {
 	    throw new OntowrapException( "Argument is not an OntModel: "+ontology );
 	}
+
     }
 
-    public JENAOntology loadOntology( URI uri ) throws OntowrapException {
+    public JENAOntology loadOntology( URI uri , boolean onlyLocalEntities) throws OntowrapException {
 	JENAOntology onto = null;
 	onto = cache.getOntologyFromURI( uri );
 	if ( onto != null ) return onto;
@@ -84,7 +86,7 @@ public class JENAOntologyFactory extends OntologyFactory {
 	try {
 	    OntModel m = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM, null );
 	    m.read(uri.toString());
-	    onto = new JENAOntology();
+	    onto = new JENAOntology(onlyLocalEntities);
 	    onto.setFile(uri);
 	    // to be checked : why several ontologies in a model ???
 	    // If no URI can be extracted from ontology, then we use the physical URI
diff --git a/src/fr/inrialpes/exmo/ontowrap/owlapi10/OWLAPIOntologyFactory.java b/src/fr/inrialpes/exmo/ontowrap/owlapi10/OWLAPIOntologyFactory.java
index fc7ee6ea..3039bd37 100644
--- a/src/fr/inrialpes/exmo/ontowrap/owlapi10/OWLAPIOntologyFactory.java
+++ b/src/fr/inrialpes/exmo/ontowrap/owlapi10/OWLAPIOntologyFactory.java
@@ -56,7 +56,7 @@ public class OWLAPIOntologyFactory extends OntologyFactory {
 	cache.clear();
     }
 
-    public OWLAPIOntology newOntology( Object ontology ) throws OntowrapException {
+    public OWLAPIOntology newOntology( Object ontology, boolean onlyLocalEntities ) throws OntowrapException {
 	if ( ontology instanceof OWLOntology ) {
 	    OWLAPIOntology onto = new OWLAPIOntology();
 	    onto.setFormalism( formalismId );
@@ -76,7 +76,7 @@ public class OWLAPIOntologyFactory extends OntologyFactory {
 	}
     }
 
-    public OWLAPIOntology loadOntology( URI uri ) throws OntowrapException {
+    public OWLAPIOntology loadOntology( URI uri, boolean onlyLocalEntities) throws OntowrapException {
 	OWLAPIOntology onto = null;
 	onto = cache.getOntologyFromURI( uri );
 	if ( onto != null ) return onto;
diff --git a/src/fr/inrialpes/exmo/ontowrap/owlapi30/OWLAPI3Ontology.java b/src/fr/inrialpes/exmo/ontowrap/owlapi30/OWLAPI3Ontology.java
index fd3041d8..99377df6 100644
--- a/src/fr/inrialpes/exmo/ontowrap/owlapi30/OWLAPI3Ontology.java
+++ b/src/fr/inrialpes/exmo/ontowrap/owlapi30/OWLAPI3Ontology.java
@@ -62,25 +62,40 @@ import fr.inrialpes.exmo.ontowrap.util.EntityFilter;
 
 public class OWLAPI3Ontology extends BasicOntology<OWLOntology> implements HeavyLoadedOntology<OWLOntology> {
     
-    public OWLAPI3Ontology() {
+    private boolean onlyLocalEntities=true;
+    public OWLAPI3Ontology(boolean onlyLocalEntities) {
+	this.onlyLocalEntities=onlyLocalEntities;
     }
 
+    private <K> Set<K> getFilteredOrNot(Set<K> s) {
+	if (onlyLocalEntities)
+	    return new EntityFilter<K>(s,this);
+	else 
+	    return s;
+    }
+    
     public Set<? extends Object> getClasses() {
 	//return onto.getReferencedClasses();
 	//return onto.getClassesInSignature();
-	return new EntityFilter<OWLClass>(onto.getClassesInSignature(),this);
+	return getFilteredOrNot(onto.getClassesInSignature());
     }
 
     public Set<? extends Object> getDataProperties() {
-	return new EntityFilter<OWLDataProperty>(onto.getDataPropertiesInSignature(),this);
+	return getFilteredOrNot(onto.getDataPropertiesInSignature());
 	//return onto.getDataPropertiesInSignature();
     }
+    
+    
+    
 
     public Set<? extends Object> getEntities() {
 	//return 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;
+		if (onlyLocalEntities)
+		    return super.isFiltered(obj) || obj instanceof OWLAnnotationProperty || obj instanceof OWLDatatype;
+		else
+		    return obj instanceof OWLAnnotationProperty || obj instanceof OWLDatatype;
 	    }  
 	};
     }
@@ -219,19 +234,23 @@ public class OWLAPI3Ontology extends BasicOntology<OWLOntology> implements Heavy
 
     public Set<? extends Object> getIndividuals() {
 	//return onto.getIndividualsInSignature();
-	return new EntityFilter<OWLNamedIndividual>(onto.getIndividualsInSignature(),this);
+	return getFilteredOrNot(onto.getIndividualsInSignature());
     }
 
     public Set<? extends Object> getObjectProperties() {
 	//System.err.println ( "ONTO: "+onto );
 	//return onto.getObjectPropertiesInSignature();
-	return new EntityFilter<OWLObjectProperty>(onto.getObjectPropertiesInSignature(),this);
+	return getFilteredOrNot(onto.getObjectPropertiesInSignature());
     }
 
     public Set<? extends Object> getProperties() {
 	return new EntityFilter<OWLEntity>(onto.getSignature(),this) {
 	    protected final boolean isFiltered(OWLEntity obj) {
-		return super.isFiltered(obj) || !(obj instanceof OWLObjectProperty || obj instanceof OWLDataProperty);
+		if (onlyLocalEntities)
+		    return super.isFiltered(obj) || !(obj instanceof OWLObjectProperty || obj instanceof OWLDataProperty);
+		else {
+		    return !(obj instanceof OWLObjectProperty || obj instanceof OWLDataProperty);
+		}
 	    }  
 	};
 	
diff --git a/src/fr/inrialpes/exmo/ontowrap/owlapi30/OWLAPI3OntologyFactory.java b/src/fr/inrialpes/exmo/ontowrap/owlapi30/OWLAPI3OntologyFactory.java
index 81382dc9..788996dc 100644
--- a/src/fr/inrialpes/exmo/ontowrap/owlapi30/OWLAPI3OntologyFactory.java
+++ b/src/fr/inrialpes/exmo/ontowrap/owlapi30/OWLAPI3OntologyFactory.java
@@ -58,9 +58,9 @@ public class OWLAPI3OntologyFactory extends OntologyFactory {
     }
 
     @Override
-    public OWLAPI3Ontology newOntology( Object ontology ) throws OntowrapException {
+    public OWLAPI3Ontology newOntology( Object ontology , boolean onlyLocalEntities) throws OntowrapException {
 	if ( ontology instanceof OWLOntology ) {
-	    OWLAPI3Ontology onto = new OWLAPI3Ontology();
+	    OWLAPI3Ontology onto = new OWLAPI3Ontology(onlyLocalEntities);
 	    onto.setFormalism( formalismId );
 	    onto.setFormURI( formalismUri );
 	    onto.setOntology( (OWLOntology)ontology );
@@ -74,7 +74,7 @@ public class OWLAPI3OntologyFactory extends OntologyFactory {
     }
 
     @Override
-    public HeavyLoadedOntology loadOntology( URI uri ) throws OntowrapException {
+    public HeavyLoadedOntology loadOntology( URI uri , boolean onlyLocalEntities) throws OntowrapException {
 	OWLAPI3Ontology onto = null;
 	//System.err.println( " Loading ontology "+uri );
 	// Cache seems to be implemented in API 3.0 anyway
@@ -108,7 +108,7 @@ public class OWLAPI3OntologyFactory extends OntologyFactory {
 	    oocex.printStackTrace();
 	    throw new OntowrapException("Cannot load " + uri, oocex );
 	}
-	onto = new OWLAPI3Ontology();
+	onto = new OWLAPI3Ontology(onlyLocalEntities);
 	onto.setFormalism( formalismId );
 	onto.setFormURI( formalismUri );
 	onto.setOntology( ontology );
diff --git a/src/fr/inrialpes/exmo/ontowrap/skosapi/SKOSOntologyFactory.java b/src/fr/inrialpes/exmo/ontowrap/skosapi/SKOSOntologyFactory.java
index 4d59f7e8..fa9fdf95 100644
--- a/src/fr/inrialpes/exmo/ontowrap/skosapi/SKOSOntologyFactory.java
+++ b/src/fr/inrialpes/exmo/ontowrap/skosapi/SKOSOntologyFactory.java
@@ -57,7 +57,7 @@ public class SKOSOntologyFactory extends OntologyFactory {
     }
 
     @Override
-    public SKOSThesaurus newOntology( Object ontology ) throws OntowrapException {
+    public SKOSThesaurus newOntology( Object ontology , boolean onlyLocalEntities) throws OntowrapException {
 	if ( ontology instanceof SKOSDataset ) {
 	    SKOSThesaurus onto = null;
 	    onto = new SKOSThesaurus();
@@ -76,7 +76,7 @@ public class SKOSOntologyFactory extends OntologyFactory {
     }
 
     @Override
-    public SKOSThesaurus loadOntology( URI uri ) throws OntowrapException {
+    public SKOSThesaurus loadOntology( URI uri , boolean onlyLocalEntities) throws OntowrapException {
 	//System.err.println(" Loading "+uri );
 	SKOSThesaurus onto = null;
 	onto = cache.getOntologyFromURI( uri );
diff --git a/src/fr/inrialpes/exmo/ontowrap/skoslite/SKOSLiteOntologyFactory.java b/src/fr/inrialpes/exmo/ontowrap/skoslite/SKOSLiteOntologyFactory.java
index 1c9ab621..aefc3594 100644
--- a/src/fr/inrialpes/exmo/ontowrap/skoslite/SKOSLiteOntologyFactory.java
+++ b/src/fr/inrialpes/exmo/ontowrap/skoslite/SKOSLiteOntologyFactory.java
@@ -50,7 +50,7 @@ public class SKOSLiteOntologyFactory extends OntologyFactory {
     }
 
     @Override
-    public SKOSLiteThesaurus loadOntology(URI uri) throws OntowrapException {
+    public SKOSLiteThesaurus loadOntology(URI uri, boolean onlyLocalEntities) throws OntowrapException {
 	SKOSLiteThesaurus onto = cache.getOntologyFromURI( uri );
 	if ( onto != null ) return onto;
 	onto = cache.getOntology( uri );
@@ -66,7 +66,7 @@ public class SKOSLiteOntologyFactory extends OntologyFactory {
     }
 
     @Override
-    public SKOSLiteThesaurus newOntology(Object m) throws OntowrapException {
+    public SKOSLiteThesaurus newOntology(Object m, boolean onlyLocalEntities) throws OntowrapException {
 	if ( m instanceof Model ) {
 	    SKOSLiteThesaurus onto = new SKOSLiteThesaurus((Model) m);
 	    onto.setFormalism( formalismId );
diff --git a/src/fr/inrialpes/exmo/ontowrap/util/EntityFilter.java b/src/fr/inrialpes/exmo/ontowrap/util/EntityFilter.java
index c9895e3f..d99f8982 100644
--- a/src/fr/inrialpes/exmo/ontowrap/util/EntityFilter.java
+++ b/src/fr/inrialpes/exmo/ontowrap/util/EntityFilter.java
@@ -29,6 +29,7 @@ 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("#.*", "");
-- 
GitLab