3131
3232public class OntologyParser {
3333 private final static Logger log = LoggerFactory .getLogger (OntologyParser .class .getName ());
34+ public static final String USE_IMPORTS_IRI = "http://omv.ontoware.org/2005/05/ontology#useImports" ;
3435 public static final String VERSION_SUBJECT = "http://bioportal.bioontology.org/ontologies/versionSubject" ;
3536
3637 protected ParserInvocation parserInvocation = null ;
@@ -169,6 +170,33 @@ private void addOntologyIRI(OWLOntology sourceOnt) {
169170 }
170171 }
171172
173+ /**
174+ * Get ontology imports and them to <ONTOLOGY_URI>
175+ * omv:useImports <import_URI>
176+ *
177+ * @param fact
178+ * @param sourceOnt
179+ */
180+ private void addOntologyImports (OWLDataFactory fact , OWLOntology sourceOnt ){
181+ Optional <IRI > sub = sourceOnt .getOntologyID ().getOntologyIRI ();
182+
183+ if (!sub .isPresent ()){
184+ return ;
185+ }
186+
187+ IRI ontologyIRI = sub .get ();
188+
189+ // Get imports and add them as omv:useImports
190+ OWLAnnotationProperty useImportProp = fact .getOWLAnnotationProperty (IRI .create (USE_IMPORTS_IRI ));
191+ for (OWLOntology imported : sourceOnt .getImports ()) {
192+ if (!imported .getOntologyID ().isAnonymous ()) {
193+ log .info ("useImports: " + imported .getOntologyID ().getOntologyIRI ().get ());
194+ OWLAnnotationAssertionAxiom useImportAxiom = fact .getOWLAnnotationAssertionAxiom (useImportProp , ontologyIRI , imported .getOntologyID ().getOntologyIRI ().get ());
195+ this .targetOwlManager .addAxiom (targetOwlOntology , useImportAxiom );
196+ }
197+ }
198+ }
199+
172200 /**
173201 * Copies ontology-level annotation axioms from the source ontology to the target ontology.
174202 * <p>
@@ -188,6 +216,7 @@ private void addGroundMetadata(IRI documentIRI, OWLDataFactory factory, OWLOntol
188216 }
189217
190218 for (OWLAnnotation annotation : sourceOntology .getAnnotations ()) {
219+
191220 IRI subjectIRI = ontologyID .getOntologyIRI ().get ();
192221 OWLAnnotationProperty annotationProperty = annotation .getProperty ();
193222 OWLAnnotationValue annotationValue = annotation .getValue ();
@@ -212,6 +241,7 @@ private boolean buildOWLOntology(OWLOntology masterOntology, boolean isOBO) {
212241 try {
213242 targetOwlOntology = targetOwlManager .createOntology ();
214243 addOntologyIRI (masterOntology );
244+ addOntologyImports (fact , masterOntology );
215245 } catch (OWLOntologyCreationException e ) {
216246 log .error (e .getMessage ());
217247 parserLog .addError (ParserError .OWL_CREATE_ONTOLOGY_EXCEPTION , "Error buildOWLOntology" + e .getMessage ());
@@ -232,51 +262,52 @@ private boolean buildOWLOntology(OWLOntology masterOntology, boolean isOBO) {
232262
233263 boolean isPrefixedOWL = sourceOwlManager .getOntologyFormat (sourceOnt ).isPrefixOWLOntologyFormat ();
234264 log .info ("isPrefixOWLOntologyFormat: {}" , isPrefixedOWL );
235- if (isPrefixedOWL == true && !isOBO ) {
265+ if (isPrefixedOWL && !isOBO ) {
236266 generateSKOSInOwl (allAxioms , fact , sourceOnt );
237267 }
238268 }
239269
240- targetOwlManager .addAxioms (targetOwlOntology , allAxioms );
241- for (OWLAnnotation ann : targetOwlOntology .getAnnotations ()) {
270+
271+ targetOwlManager .addAxioms (targetOwlOntology , allAxioms );
272+ for (OWLAnnotation ann : targetOwlOntology .getAnnotations ()) {
273+ AddOntologyAnnotation addAnn = new AddOntologyAnnotation (targetOwlOntology , ann );
274+ targetOwlManager .applyChange (addAnn );
275+ }
276+
277+ if (isOBO ) {
278+ String oboVersion = parserInvocation .getOBOVersion ();
279+ if (oboVersion != null ) {
280+ log .info ("Adding version: {}" , oboVersion );
281+ OWLAnnotationProperty prop = fact .getOWLAnnotationProperty (IRI .create (OWLRDFVocabulary .OWL_VERSION_INFO .toString ()));
282+ IRI versionSubjectIRI = IRI .create (VERSION_SUBJECT );
283+ OWLAnnotationAssertionAxiom annVersion = fact .getOWLAnnotationAssertionAxiom (prop , versionSubjectIRI , fact .getOWLLiteral (oboVersion ));
284+ targetOwlManager .addAxiom (targetOwlOntology , annVersion );
285+ }
286+ }
287+
288+ addOntologyAnnotations (masterOntology );
289+ escapeXMLLiterals (targetOwlOntology );
290+
291+ OWLReasonerFactory reasonerFactory = new StructuralReasonerFactory ();
292+ OWLReasoner reasoner = reasonerFactory .createReasoner (targetOwlOntology );
293+ InferredSubClassAxiomGenerator isc = new InferredSubClassAxiomGenerator ();
294+ Set <OWLSubClassOfAxiom > subAxs = isc .createAxioms (targetOwlOntology .getOWLOntologyManager ().getOWLDataFactory (), reasoner );
295+ targetOwlManager .addAxioms (targetOwlOntology , subAxs );
296+ deprecateBranch ();
297+
298+ log .info ("isOBO: {}" , isOBO );
299+ if (isOBO ) {
300+ replicateHierarchyAsTreeview (fact );
301+ }
302+ return true ;
303+ }
304+
305+ private void addOntologyAnnotations (OWLOntology masterOntology ){
306+ for (OWLAnnotation ann : masterOntology .getAnnotations ()) {
242307 AddOntologyAnnotation addAnn = new AddOntologyAnnotation (targetOwlOntology , ann );
243308 targetOwlManager .applyChange (addAnn );
244309 }
245-
246- if (isOBO ) {
247- String oboVersion = parserInvocation .getOBOVersion ();
248- if (oboVersion != null ) {
249- log .info ("Adding version: {}" , oboVersion );
250- OWLAnnotationProperty prop = fact .getOWLAnnotationProperty (IRI .create (OWLRDFVocabulary .OWL_VERSION_INFO .toString ()));
251- IRI versionSubjectIRI = IRI .create (VERSION_SUBJECT );
252- OWLAnnotationAssertionAxiom annVersion = fact .getOWLAnnotationAssertionAxiom (prop , versionSubjectIRI , fact .getOWLLiteral (oboVersion ));
253- targetOwlManager .addAxiom (targetOwlOntology , annVersion );
254- }
255- }
256-
257- for (OWLOntology sourceOnt : sourceOwlManager .getOntologies ()) {
258- for (OWLAnnotation ann : sourceOnt .getAnnotations ()) {
259- AddOntologyAnnotation addAnn = new AddOntologyAnnotation (targetOwlOntology , ann );
260- targetOwlManager .applyChange (addAnn );
261- }
262- }
263-
264- escapeXMLLiterals (targetOwlOntology );
265-
266- OWLReasonerFactory reasonerFactory = new StructuralReasonerFactory ();
267- OWLReasoner reasoner = reasonerFactory .createReasoner (targetOwlOntology );
268- InferredSubClassAxiomGenerator isc = new InferredSubClassAxiomGenerator ();
269- Set <OWLSubClassOfAxiom > subAxs = isc .createAxioms (targetOwlOntology .getOWLOntologyManager ().getOWLDataFactory (), reasoner );
270- targetOwlManager .addAxioms (targetOwlOntology , subAxs );
271- deprecateBranch ();
272-
273- log .info ("isOBO: {}" , isOBO );
274- if (isOBO ) {
275- replicateHierarchyAsTreeview (fact );
276- }
277- return true ;
278310 }
279-
280311 private void replicateHierarchyAsTreeview (OWLDataFactory fact ) {
281312 Set <OWLAxiom > treeViewAxs = new HashSet <OWLAxiom >();
282313 for (OWLAxiom axiom : targetOwlOntology .getAxioms ()) {
@@ -449,7 +480,7 @@ private void escapeXMLLiterals(OWLOntology target) {
449480 targetOwlManager .addAxiom (target , annAsse );
450481 Set <OWLAnnotationAssertionAxiom > del = new HashSet <OWLAnnotationAssertionAxiom >();
451482 del .add (ann );
452- targetOwlManager .removeAxioms (target ,del );
483+ targetOwlManager .removeAxioms (target ,del );
453484 }
454485 }
455486 }
0 commit comments