View Javadoc
1   /*
2    * Copyright 2018-2022 Medical Information Systems Research Group (https://medical.zcu.cz),
3    * Department of Computer Science and Engineering, University of West Bohemia.
4    * Address: Univerzitni 8, 306 14 Plzen, Czech Republic.
5    *
6    * Author Petr Vcelak (vcelak@kiv.zcu.cz).
7    *
8    * This file is part of MRECore project.
9    *
10   * MRECore is free software: you can redistribute it and/or modify
11   * it under the terms of the GNU General Public License as published by
12   * the Free Software Foundation, either version 3 of the License.
13   *
14   * MRECore is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17   * GNU General Public License for more details.
18   *
19   * You should have received a copy of the GNU General Public License
20   * along with MRECore. If not, see <http://www.gnu.org/licenses/>.
21   */
22  package cz.zcu.mre.dao;
23  
24  import java.io.File;
25  import org.apache.jena.ontology.OntDocumentManager;
26  import org.apache.jena.ontology.OntModel;
27  import org.apache.jena.ontology.OntModelSpec;
28  import org.apache.jena.rdf.model.ModelFactory;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  /**
33   * 
34   * @author Petr Vcelak (vcelak@kiv.zcu.cz)
35   */
36  public class OntologyRepositoryImpl implements OntologyRepository {
37  
38      private static final Logger LOG = LoggerFactory.getLogger(OntologyRepositoryImpl.class);
39  
40      /**
41       * Local copy of tdb.owl file (backup).
42       */
43      private OntModelSpec ontModelSpec = OntModelSpec.OWL_MEM;
44  
45      /**
46       * Ontology model.
47       */
48      private OntModel ontology;
49  
50      /**
51       * Default constructor.
52       */
53      public OntologyRepositoryImpl() {
54          initialize(ontModelSpec);
55      }
56  
57      /**
58       * Method initialize instance. It is called automatically in constructor
59       * with default OntModelSpec.OWL_MEM.
60       *
61       * @param ontModelSpec User-defined OntModelSpec value.
62       */
63      public final void initialize(OntModelSpec ontModelSpec) {
64  
65          this.ontModelSpec = ontModelSpec;
66  
67          ontology = ModelFactory.createOntologyModel(this.ontModelSpec);
68      }
69  
70      /**
71       * Insert ontology to this ontology repository.
72       *
73       * @param ontologyURI The ontology URI as String
74       * @param ontologyLocalFileCopy Path to the local file - should be null.
75       */
76      @Override
77      public void addOntology(String ontologyURI, String ontologyLocalFileCopy) {
78  
79          LOG.info("Load ontology {} (local file {})", ontologyURI, ontologyLocalFileCopy);
80          try {
81              // add alternative copy of the ontology
82              OntDocumentManager dm = ontology.getDocumentManager();
83              if (ontologyLocalFileCopy != null && new File(ontologyLocalFileCopy).isFile()) {
84                  dm.addAltEntry(ontologyURI, ontologyLocalFileCopy);
85              }
86  
87              // read ontology
88              ontology.read(ontologyURI);
89              LOG.info("Loaded ontology {}", ontologyURI);
90  
91          } catch (ExceptionInInitializerError e) {
92              LOG.info("Can't read ontology {} (used local file {}). Is the ontology availaible?", ontologyURI, ontologyLocalFileCopy);
93          }
94      }
95  
96      /**
97       * Get ontology model.
98       *
99       * @return The OntModel instance.
100      */
101     @Override
102     public synchronized OntModel getOntology() {
103         return ontology;
104     }
105 
106 }