-
Notifications
You must be signed in to change notification settings - Fork 14
/
OWL2JavaTransformer.java
219 lines (184 loc) · 8.35 KB
/
OWL2JavaTransformer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/**
* Copyright (C) 2016 Czech Technical University in Prague
* <p>
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
* version.
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.jopa.owl2java;
import cz.cvut.kbss.jopa.owl2java.config.TransformationConfiguration;
import cz.cvut.kbss.jopa.owl2java.exception.OWL2JavaException;
import cz.cvut.kbss.jopa.util.MappingFileParser;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.*;
import org.semanticweb.owlapi.search.EntitySearcher;
import org.semanticweb.owlapi.util.OWLOntologyMerger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Nonnull;
import java.io.File;
import java.net.URI;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Stream;
public class OWL2JavaTransformer {
private static final Logger LOG = LoggerFactory.getLogger(OWL2JavaTransformer.class);
private OWLOntology ontology;
private final ValidContextAnnotationValueVisitor v = new ValidContextAnnotationValueVisitor();
private final ContextDefinition defaultContext = new ContextDefinition();
private Map<String, ContextDefinition> contexts = new HashMap<>();
private boolean ignoreMissingImports;
public Collection<String> listContexts() {
return contexts.keySet();
}
public void ignoreMissingImports(boolean ignore) {
this.ignoreMissingImports = ignore;
}
private OWLOntology getWholeOntology(final String owlOntologyName, final String mappingFile) {
// reader
final OWLOntologyManager m = OWLManager.createOWLOntologyManager();
if (mappingFile != null) {
LOG.info("Using mapping file '{}'.", mappingFile);
final Map<URI, URI> map = MappingFileParser.getMappings(new File(mappingFile));
m.getIRIMappers().add(ontologyIRI -> {
final URI value = map.get(ontologyIRI.toURI());
if (value == null) {
return null;
} else {
return IRI.create(value);
}
});
LOG.info("Mapping file successfully parsed.");
}
LOG.info("Loading ontology {}... ", owlOntologyName);
if (ignoreMissingImports) {
m.getOntologyConfigurator().setMissingImportHandlingStrategy(MissingImportHandlingStrategy.SILENT);
m.addMissingImportListener(e -> LOG.warn("Unable to import ontology {}.", e.getImportedOntologyURI()));
}
try {
m.loadOntology(IRI.create(owlOntologyName));
return new OWLOntologyMerger(m).createMergedOntology(m, IRI.create(owlOntologyName + "-generated"));
} catch (OWLException | OWLRuntimeException e) {
LOG.error(e.getMessage(), e);
throw new OWL2JavaException("Unable to load ontology " + owlOntologyName, e);
}
}
public void setOntology(final String owlOntologyName, final String mappingFile) {
ontology = getWholeOntology(owlOntologyName, mappingFile);
LOG.debug("Parsing integrity constraints");
ontology.axioms().forEach(a -> {
defaultContext.addAxiom(a);
for (final String icContextName : getContexts(a)) {
ContextDefinition ctx = getContextDefinition(icContextName);
ctx.addAxiom(a);
}
});
registerEntitiesInContexts();
defaultContext.parse();
for (final ContextDefinition ctx : contexts.values()) {
ctx.parse();
}
LOG.debug("Integrity constraints successfully parsed.");
}
private void registerEntitiesInContexts() {
final Consumer<Stream<? extends OWLEntity>> consumer = (stream) -> stream.forEach(e -> {
defaultContext.add(e);
for (final String context : getContexts(e)) {
getContextDefinition(context).add(e);
}
});
consumer.accept(ontology.classesInSignature());
consumer.accept(ontology.objectPropertiesInSignature());
consumer.accept(ontology.dataPropertiesInSignature());
consumer.accept(ontology.annotationPropertiesInSignature());
}
private ContextDefinition getContextDefinition(String icContextName) {
return contexts.computeIfAbsent(icContextName, name -> new ContextDefinition());
}
private List<String> getContexts(final OWLAxiom a) {
final List<String> icContexts = new ArrayList<>();
a.annotations().filter(p -> p.getProperty().getIRI().toString().equals(Constants.P_IS_INTEGRITY_CONSTRAINT_FOR))
.forEach(p -> {
LOG.trace("Processing annotation : " + p);
p.getValue().accept(v);
final String icContextName = v.getName();
LOG.trace("CONTEXT:" + icContextName);
if (icContextName == null) {
return;
}
LOG.debug("Found IC {} for context {}", a, icContextName);
icContexts.add(icContextName);
});
return icContexts;
}
private List<String> getContexts(final OWLEntity entity) {
final List<String> icContexts = new ArrayList<>();
EntitySearcher.getAnnotations(entity, ontology)
.filter(p -> p.getProperty().getIRI().toString().equals(Constants.P_IS_INTEGRITY_CONSTRAINT_FOR))
.forEach(p -> {
LOG.trace("Processing annotation : " + p);
p.getValue().accept(v);
final String icContextName = v.getName();
LOG.trace("CONTEXT:" + icContextName);
if (icContextName == null) {
return;
}
LOG.debug("Found OWLEntity declaration {} for context {}", entity, icContextName);
icContexts.add(icContextName);
});
return icContexts;
}
private void verifyContextExistence(String context) {
if (!contexts.containsKey(context)) {
throw new IllegalArgumentException(
"Context " + context + " not found. Existing contexts: " + listContexts());
}
}
public void transform(TransformationConfiguration transformConfig) {
final ContextDefinition def = getValidContext(transformConfig);
LOG.info("Transforming context ...");
new JavaTransformer(transformConfig).generateModel(ontology, def);
LOG.info("Transformation SUCCESSFUL.");
}
private ContextDefinition getValidContext(TransformationConfiguration configuration) {
if (configuration.areAllAxiomsIntegrityConstraints()) {
LOG.info(" - for all axioms");
return defaultContext;
}
final String context = configuration.getContext();
verifyContextExistence(context);
LOG.info(" - for context '{}'.", context);
return contexts.get(context);
}
/**
* Generates only vocabulary of the loaded ontology.
*
* @param transformConfig Configuration of the generation process
*/
public void generateVocabulary(TransformationConfiguration transformConfig) {
LOG.info("Generating vocabulary ...");
ContextDefinition def = getValidContext(transformConfig);
new JavaTransformer(transformConfig).generateVocabulary(ontology, def);
}
private class ValidContextAnnotationValueVisitor implements OWLAnnotationValueVisitor {
private String name = null;
String getName() {
return name;
}
@Override
public void visit(@Nonnull IRI iri) {
}
@Override
public void visit(@Nonnull OWLAnonymousIndividual individual) {
}
@Override
public void visit(@Nonnull OWLLiteral literal) {
name = literal.getLiteral();
}
}
}