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.service.data;
23  
24  import cz.zcu.mre.dao.OntologyRepository;
25  import java.util.ArrayList;
26  import java.util.HashMap;
27  import java.util.Iterator;
28  import java.util.List;
29  import java.util.Map;
30  import org.apache.jena.ontology.OntClass;
31  import org.apache.jena.ontology.OntModel;
32  import org.apache.jena.ontology.OntProperty;
33  import org.apache.jena.ontology.OntResource;
34  import org.apache.jena.rdf.model.RDFNode;
35  import org.apache.jena.util.iterator.ExtendedIterator;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  import org.springframework.beans.factory.annotation.Autowired;
39  import org.springframework.context.annotation.Scope;
40  import org.springframework.context.annotation.ScopedProxyMode;
41  
42  /**
43   * Ontology Service.
44   *
45   * @author Petr Vcelak (vcelak@kiv.zcu.cz)
46   */
47  //@Service
48  //@Repository
49  @Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)
50  public class OntologyServiceImpl implements OntologyService {
51  
52      private static final Logger LOG = LoggerFactory.getLogger(OntologyServiceImpl.class);
53  
54      private OntologyRepository ontologyRepository;
55  
56      @Autowired
57      public void setOntologyRepository(OntologyRepository ontologyRepository) {
58          this.ontologyRepository = ontologyRepository;
59      }
60  
61      /**
62       * Default constructor.
63       *
64       */
65      public OntologyServiceImpl() {
66      }
67  
68      @Override
69      public OntModel getOntology() {
70          return ontologyRepository.getOntology();
71      }
72  
73      @Override
74      public Map<OntProperty, String> mapOfPropertyAndLabel(String prefLanguages) {
75  
76          LOG.info("Get map of property and label for prefered language: {}", prefLanguages);
77          Map<OntProperty, String> map = new HashMap<>();
78          String[] langs = prefLanguages.split(",");
79  
80          getOntology().listAllOntProperties().toList().forEach((prop) -> {
81              String label;
82  
83              for (String lang : langs) {
84                  Iterator<RDFNode> it = prop.listLabels(lang);
85                  if (it.hasNext()) {
86                      RDFNode node = it.next();
87                      label = node.asLiteral().getString();
88  
89                      if (label != null && !label.isEmpty()) {
90                          LOG.debug("Property {} has label '{}' in '{}'.", prop, label, lang);
91                          map.put(prop, label);
92                          return;
93                      }
94                  }
95              }
96          });
97          LOG.debug("Get map of property and label for prefered language '{}' has {} items", prefLanguages, map.size());
98          return map;
99      }
100 
101     @Deprecated
102     @Override
103     public Map<String, String> mapOfLocalNameAndLabel(String prefLanguage) {
104 
105         Map<String, String> map = new HashMap<>();
106 
107         mapOfPropertyAndLabel(prefLanguage).forEach((property, label) -> {
108 
109             // put only the first occurence for each key
110             if (!map.containsKey(property.getLocalName())) {
111                 map.put(property.getLocalName(), label);
112             }
113         });
114 
115         return map;
116     }
117 
118     @Override
119     public List<OntClass> getVocabularyList() {
120         List<OntClass> vocabularies = new ArrayList<>();
121 
122         ExtendedIterator<OntClass> it = getOntology().listClasses();
123         while (it.hasNext()) {
124             OntClass ontClass = it.next();
125             if (ontClass.getLocalName() != null
126                     && ontClass.getLocalName().endsWith("Vocab")) {
127                 vocabularies.add(ontClass);
128             }
129             // TODO can add any other list of vocabularies
130             //use the form:isVocabulary annotation property
131         }
132 
133         return vocabularies;
134     }
135 
136     /**
137      * List all available properties in loaded ontology.
138      *
139      * @return List of OntProperty
140      */
141     @Override
142     public ExtendedIterator<OntProperty> listProperties() {
143 
144         return ontologyRepository.getOntology().listAllOntProperties();
145     }
146 
147     @Override
148     public ExtendedIterator<OntProperty> listProperties(OntClass ontClass) {
149 
150         ExtendedIterator<OntProperty> it = ontologyRepository.getOntology().listAllOntProperties().filterKeep((OntProperty t) -> {
151             ExtendedIterator it2 = t.listDomain();
152             while (it2.hasNext()) {
153                 OntResource r = (OntResource) it2.next();
154                 if (r.equals(ontClass)) {
155                     return true;
156                 }
157             }
158 
159             return false;
160         });
161 
162         return it;
163     }
164 }