2121import com .hp .hpl .jena .ontology .OntModel ;
2222import com .hp .hpl .jena .ontology .OntModelSpec ;
2323import com .hp .hpl .jena .rdf .model .*;
24+ import com .hp .hpl .jena .shared .CannotCreateException ;
2425import com .hp .hpl .jena .shared .JenaException ;
2526import org .apache .commons .io .FileUtils ;
2627import org .apache .commons .lang3 .StringUtils ;
@@ -50,22 +51,30 @@ public class OntologyLoader {
5051 private static final String OLD_CACHE_SUFFIX = ".old" ;
5152 private static final String TMP_CACHE_SUFFIX = ".tmp" ;
5253
54+ public static OntModel loadMemoryModel ( InputStream is , String url ) {
55+ return loadMemoryModel ( is , url , true );
56+ }
57+
5358 /**
5459 * Load an ontology into memory. Use this type of model when fast access is critical and memory is available.
5560 */
56- public static OntModel loadMemoryModel ( InputStream is , String url ) {
57- OntModel model = getMemoryModel ( url );
61+ public static OntModel loadMemoryModel ( InputStream is , String url , boolean processImports ) {
62+ OntModel model = getMemoryModel ( url , processImports );
5863 model .read ( is , null );
5964 return model ;
6065 }
6166
6267 /**
6368 * Load an ontology into memory. Use this type of model when fast access is critical and memory is available.
6469 *
65- * @see #loadMemoryModel(String, String)
70+ * @see #loadMemoryModel(String, String, boolean )
6671 */
6772 public static OntModel loadMemoryModel ( String url ) {
68- return loadMemoryModel ( url , null );
73+ return loadMemoryModel ( url , null , true );
74+ }
75+
76+ public static OntModel loadMemoryModel ( String url , @ Nullable String cacheName ) {
77+ return loadMemoryModel ( url , cacheName , true );
6978 }
7079
7180 /**
@@ -77,10 +86,10 @@ public static OntModel loadMemoryModel( String url ) {
7786 * @param url a URL where the OWL file is stored
7887 * @param cacheName unique name of this ontology, will be used to load from disk in case of failed url connection
7988 */
80- public static OntModel loadMemoryModel ( String url , @ Nullable String cacheName ) {
89+ public static OntModel loadMemoryModel ( String url , @ Nullable String cacheName , boolean processImports ) {
8190 StopWatch timer = new StopWatch ();
8291 timer .start ();
83- OntModel model = getMemoryModel ( url );
92+ OntModel model = getMemoryModel ( url , processImports );
8493
8594 URLConnection urlc = openConnection ( url );
8695
@@ -187,12 +196,12 @@ public static void deleteOldCache( String cacheName ) {
187196 /**
188197 * Get model that is entirely in memory.
189198 */
190- private static OntModel getMemoryModel ( String url ) {
199+ private static OntModel getMemoryModel ( String url , boolean processImports ) {
191200 OntModelSpec spec = new OntModelSpec ( OntModelSpec .OWL_MEM_TRANS_INF );
192201 ModelMaker maker = ModelFactory .createMemModelMaker ();
193202 Model base = maker .createModel ( url , false );
194203 spec .setImportModelMaker ( maker );
195- spec .getDocumentManager ().setProcessImports ( true );
204+ spec .getDocumentManager ().setProcessImports ( processImports );
196205 spec .setImportModelGetter ( new ModelGetter () {
197206 @ Override
198207 public Model getModel ( String URL ) {
@@ -207,7 +216,7 @@ public Model getModel( String URL, ModelReader loadIfAbsent ) {
207216 try ( InputStream in = urlc .getInputStream () ) {
208217 return model .read ( in , URL );
209218 } catch ( JenaException | IOException e ) {
210- log . error ( String .format ( "Failed to resolve import %s for %s: %s ." , URL , url , e . getMessage () ) );
219+ throw new CannotCreateException ( String .format ( "Failed to resolve import %s for %s." , URL , url ), e );
211220 }
212221 }
213222 return loadIfAbsent .readModel ( model , URL );
0 commit comments