11/*
22 * The baseCode project
3- *
3+ *
44 * Copyright (c) 2010 University of British Columbia
5- *
5+ *
66 * Licensed under the Apache License, Version 2.0 (the "License");
77 * you may not use this file except in compliance with the License.
88 * You may obtain a copy of the License at
1818 */
1919package ubic .basecode .ontology ;
2020
21- import java .io .BufferedReader ;
22- import java .io .File ;
23- import java .io .FileReader ;
24- import java .io .IOException ;
25- import java .io .InputStream ;
26- import java .io .InputStreamReader ;
27- import java .io .Reader ;
28- import java .net .HttpURLConnection ;
29- import java .net .URL ;
30- import java .net .URLConnection ;
31- import java .nio .file .Files ;
32- import java .nio .file .StandardCopyOption ;
33- import java .util .Collection ;
34- import java .util .HashSet ;
35-
36- import org .apache .commons .io .FileUtils ;
37- import org .apache .commons .lang3 .StringUtils ;
38- import org .apache .commons .lang3 .time .StopWatch ;
39- import org .slf4j .Logger ;
40- import org .slf4j .LoggerFactory ;
41-
42- import com .hp .hpl .jena .ontology .Individual ;
43- import com .hp .hpl .jena .ontology .OntClass ;
4421import com .hp .hpl .jena .ontology .OntModel ;
4522import com .hp .hpl .jena .ontology .OntModelSpec ;
4623import com .hp .hpl .jena .rdf .model .Model ;
4724import com .hp .hpl .jena .rdf .model .ModelFactory ;
4825import com .hp .hpl .jena .rdf .model .ModelMaker ;
49- import com .hp .hpl .jena .util .iterator .ExtendedIterator ;
50-
51- import ubic .basecode .ontology .model .OntologyIndividual ;
52- import ubic .basecode .ontology .model .OntologyIndividualImpl ;
53- import ubic .basecode .ontology .model .OntologyProperty ;
54- import ubic .basecode .ontology .model .OntologyResource ;
55- import ubic .basecode .ontology .model .OntologyTerm ;
56- import ubic .basecode .ontology .model .OntologyTermImpl ;
57- import ubic .basecode .ontology .model .PropertyFactory ;
26+ import org .apache .commons .io .FileUtils ;
27+ import org .apache .commons .lang3 .StringUtils ;
28+ import org .apache .commons .lang3 .time .StopWatch ;
29+ import org .slf4j .Logger ;
30+ import org .slf4j .LoggerFactory ;
5831import ubic .basecode .util .Configuration ;
5932
33+ import java .io .*;
34+ import java .net .HttpURLConnection ;
35+ import java .net .URL ;
36+ import java .net .URLConnection ;
37+ import java .nio .file .Files ;
38+ import java .nio .file .StandardCopyOption ;
39+
6040/**
6141 * Reads ontologies from OWL resources
62- *
63- * @author paul
42+ *
43+ * @author paul
6444 */
6545public class OntologyLoader {
6646
@@ -69,133 +49,37 @@ public class OntologyLoader {
6949 private static final String OLD_CACHE_SUFFIX = ".old" ;
7050 private static final String TMP_CACHE_SUFFIX = ".tmp" ;
7151
72- /**
73- * @param url
74- * @param model
75- * @return
76- */
77- public static Collection <OntologyResource > initialize ( String url , OntModel model ) {
78-
79- Collection <OntologyResource > result = new HashSet <>();
80-
81- ExtendedIterator <OntClass > classIt = model .listClasses ();
82- int count = 0 ;
83- log .debug ( "Reading classes for ontology: " + url );
84- while ( classIt .hasNext () ) {
85- OntClass element = classIt .next ();
86- if ( element .isAnon () ) continue ;
87- OntologyTerm ontologyTerm = new OntologyTermImpl ( element );
88- result .add ( ontologyTerm );
89- if ( ++count % 1000 == 0 ) {
90- log .debug ( "Loaded " + count + " terms, last was " + ontologyTerm );
91- }
92- }
93-
94- log .debug ( "Loaded " + count + " terms" );
95-
96- ExtendedIterator <com .hp .hpl .jena .ontology .ObjectProperty > propIt = model .listObjectProperties ();
97- count = 0 ;
98- log .debug ( "Reading object properties..." );
99- while ( propIt .hasNext () ) {
100- com .hp .hpl .jena .ontology .ObjectProperty element = propIt .next ();
101- OntologyProperty ontologyTerm = PropertyFactory .asProperty ( element );
102- if ( ontologyTerm == null ) continue ; // couldn't be converted for some reason.
103- result .add ( ontologyTerm );
104- if ( ++count % 1000 == 0 ) {
105- log .debug ( "Loaded " + count + " object properties, last was " + ontologyTerm );
106- }
107- }
108-
109- ExtendedIterator <com .hp .hpl .jena .ontology .DatatypeProperty > dtPropIt = model .listDatatypeProperties ();
110- log .debug ( "Reading datatype properties..." );
111- while ( dtPropIt .hasNext () ) {
112- com .hp .hpl .jena .ontology .DatatypeProperty element = dtPropIt .next ();
113- OntologyProperty ontologyTerm = PropertyFactory .asProperty ( element );
114- if ( ontologyTerm == null ) continue ; // couldn't be converted for some reason.
115- result .add ( ontologyTerm );
116- if ( ++count % 1000 == 0 ) {
117- log .debug ( "Loaded " + count + " datatype properties, last was " + ontologyTerm );
118- }
119- }
120-
121- log .debug ( "Loaded " + count + " properties" );
122-
123- ExtendedIterator <Individual > indiIt = model .listIndividuals ();
124- count = 0 ;
125- log .debug ( "Reading individuals..." );
126- while ( indiIt .hasNext () ) {
127- Individual element = indiIt .next ();
128- if ( element .isAnon () ) continue ;
129- OntologyIndividual ontologyTerm = new OntologyIndividualImpl ( element );
130- result .add ( ontologyTerm );
131- if ( ++count % 1000 == 0 ) {
132- log .debug ( "Loaded " + count + " individuals, last was " + ontologyTerm );
133- }
134- }
135- log .debug ( "Loaded " + count + " individuals" );
136- return result ;
137- }
138-
13952 /**
14053 * Load an ontology into memory. Use this type of model when fast access is critical and memory is available.
141- *
142- * @param is
143- * @param url, used as a key
144- * @param spec
145- * @return
14654 */
147- public static OntModel loadMemoryModel ( InputStream is , String url , OntModelSpec spec ) {
148- OntModel model = getMemoryModel ( url , spec );
55+ public static OntModel loadMemoryModel ( InputStream is , String url ) {
56+ OntModel model = getMemoryModel ( url );
14957 model .read ( is , null );
15058 return model ;
15159 }
15260
153- /**
154- * Load an ontology into memory. Use this type of model when fast access is critical and memory is available. Uses
155- * OWL_MEM_TRANS_INF
156- *
157- * @param url
158- * @return
159- */
160- public static OntModel loadMemoryModel ( String url ) {
161- return loadMemoryModel ( url , OntModelSpec .OWL_MEM_TRANS_INF );
162- }
163-
164- /**
165- * Load an ontology into memory. Use this type of model when fast access is critical and memory is available. Uses
166- * OWL_MEM_TRANS_INF
167- * If load from URL fails, attempt to load from disk cache under @cacheName.
168- *
169- * @param url
170- * @return
171- */
172- public static OntModel loadMemoryModel ( String url , String cacheName ) {
173- return loadMemoryModel ( url , OntModelSpec .OWL_MEM_TRANS_INF , cacheName );
174- }
175-
17661 /**
17762 * Load an ontology into memory. Use this type of model when fast access is critical and memory is available.
178- *
179- * @param url
180- * @return
63+ *
64+ * @see #loadMemoryModel(String, String)
18165 */
182- public static OntModel loadMemoryModel ( String url , OntModelSpec spec ) {
183- return loadMemoryModel ( url , spec , null );
66+ public static OntModel loadMemoryModel ( String url ) {
67+ return loadMemoryModel ( url , null );
18468 }
18569
18670 /**
18771 * Load an ontology into memory. Use this type of model when fast access is critical and memory is available.
18872 * If load from URL fails, attempt to load from disk cache under @cacheName.
189- *
190- * @param url
191- * @param spec e.g. OWL_MEM_TRANS_INF
192- * @param cacheName unique name of this ontology, will be used to load from disk in case of failed url connection
193- * @return
73+ * <p>
74+ * Uses {@link OntModelSpec#OWL_MEM_TRANS_INF}.
75+ *
76+ * @param url a URL where the OWL file is stored
77+ * @param cacheName unique name of this ontology, will be used to load from disk in case of failed url connection
19478 */
195- public static OntModel loadMemoryModel ( String url , OntModelSpec spec , String cacheName ) {
79+ public static OntModel loadMemoryModel ( String url , String cacheName ) {
19680 StopWatch timer = new StopWatch ();
19781 timer .start ();
198- OntModel model = getMemoryModel ( url , spec );
82+ OntModel model = getMemoryModel ( url );
19983
20084 URLConnection urlc = null ;
20185 int tries = 0 ;
@@ -242,7 +126,7 @@ public static OntModel loadMemoryModel( String url, OntModelSpec spec, String ca
242126 }
243127
244128 if ( urlc != null ) {
245- try (InputStream in = urlc .getInputStream ();) {
129+ try ( InputStream in = urlc .getInputStream (); ) {
246130 Reader reader ;
247131 if ( cacheName != null ) {
248132 // write tmp to disk
@@ -261,7 +145,7 @@ public static OntModel loadMemoryModel( String url, OntModelSpec spec, String ca
261145 }
262146
263147 assert reader != null ;
264- try (BufferedReader buf = new BufferedReader ( reader );) {
148+ try ( BufferedReader buf = new BufferedReader ( reader ); ) {
265149 model .read ( buf , url );
266150 }
267151
@@ -286,7 +170,7 @@ public static OntModel loadMemoryModel( String url, OntModelSpec spec, String ca
286170 }
287171
288172 if ( f .exists () && !f .isDirectory () ) {
289- try (BufferedReader buf = new BufferedReader ( new FileReader ( f ) );) {
173+ try ( BufferedReader buf = new BufferedReader ( new FileReader ( f ) ); ) {
290174 model .read ( buf , url );
291175 // We successfully loaded the cached ontology. Copy the loaded ontology to oldFile
292176 // so that we don't recreate indices during initialization based on a false change in
@@ -359,25 +243,14 @@ public static boolean deleteOldCache( String cacheName ) {
359243 return false ;
360244 }
361245
362- /**
363- * Get model that is entirely in memory with default OntModelSpec.OWL_MEM_RDFS_INF.
364- *
365- * @param url
366- * @return
367- */
368- static OntModel getMemoryModel ( String url ) {
369- return getMemoryModel ( url , OntModelSpec .OWL_MEM_RDFS_INF );
370- }
371-
372246 /**
373247 * Get model that is entirely in memory.
374- *
375- * @param url
376- * @param specification
248+ *
249+ * @param url
377250 * @return
378251 */
379- static OntModel getMemoryModel ( String url , OntModelSpec specification ) {
380- OntModelSpec spec = new OntModelSpec ( specification );
252+ private static OntModel getMemoryModel ( String url ) {
253+ OntModelSpec spec = new OntModelSpec ( OntModelSpec . OWL_MEM_TRANS_INF );
381254 ModelMaker maker = ModelFactory .createMemModelMaker ();
382255 Model base = maker .createModel ( url , false );
383256 spec .setImportModelMaker ( maker );
@@ -389,7 +262,7 @@ static OntModel getMemoryModel( String url, OntModelSpec specification ) {
389262 }
390263
391264 /**
392- * @param name
265+ * @param name
393266 * @return
394267 */
395268 public static File getDiskCachePath ( String name ) {
0 commit comments