-
Notifications
You must be signed in to change notification settings - Fork 14
/
OWL2JavaTransformer.java
188 lines (157 loc) · 7.17 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
/**
* 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.exception.OWL2JavaException;
import cz.cvut.kbss.jopa.util.MappingFileParser;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.*;
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.*;
public class OWL2JavaTransformer {
private static final Logger LOG = LoggerFactory.getLogger(OWL2JavaTransformer.class);
private static final ContextDefinition DEFAULT_CONTEXT = new ContextDefinition("<DEFAULT>");
private final ValidContextAnnotationValueVisitor v = new ValidContextAnnotationValueVisitor();
private OWLOntology ontology;
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(org.semanticweb.owlapi.model.IRI.create(owlOntologyName));
return new OWLOntologyMerger(m)
.createMergedOntology(m, org.semanticweb.owlapi.model.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, boolean includeImports) {
ontology = getWholeOntology(owlOntologyName, mappingFile);
// this.imports = ontology.getOWLOntologyManager().getOntologies();
LOG.info("Parsing integrity constraints");
ontology.axioms().forEach(a -> {
DEFAULT_CONTEXT.addAxiom(a);
for (final String icContextName : getContexts(a)) {
ContextDefinition ctx = getContextDefinition(icContextName);
ctx.addAxiom(a);
}
});
DEFAULT_CONTEXT.parse();
for (final ContextDefinition ctx : contexts.values()) {
ctx.parse();
}
LOG.info("Integrity constraints successfully parsed.");
}
private ContextDefinition getContextDefinition(String icContextName) {
return contexts.computeIfAbsent(icContextName, ContextDefinition::new);
}
private List<String> getContexts(final OWLAxiom a) {
final List<String> contexts = new ArrayList<>();
a.annotations().filter(p -> p.getProperty().getIRI().toString().equals(Constants.P_IS_INTEGRITY_CONSTRAINT_FOR))
.forEach(p -> {
LOG.info("Processing annotation : " + p);
p.getValue().accept(v);
final String icContextName = v.getName();
LOG.info("CONTEXT:" + icContextName);
if (icContextName == null) {
return;
}
LOG.debug("Found IC {} for context {}", a, icContextName);
contexts.add(icContextName);
});
return contexts;
}
private void verifyContextExistence(String context) {
if (!contexts.containsKey(context)) {
throw new IllegalArgumentException(
"Context " + context + " not found. Existing contexts: " + listContexts());
}
}
public void transform(String context, String pkg, String targetDir, boolean withOWLAPI) {
LOG.info("Transforming context ...");
if (context == null) {
LOG.info(" - for all axioms");
} else {
LOG.info(" - for context '{}'.", context);
verifyContextExistence(context);
}
ContextDefinition def = context == null ? DEFAULT_CONTEXT : contexts.get(context);
new JavaTransformer().generateModel(ontology, def, pkg, targetDir, withOWLAPI);
LOG.info("Transformation SUCCESSFUL.");
}
/**
* Generates only vocabulary of the loaded ontology.
*
* @param context Integrity constraints context, if null is supplied, the whole ontology is interpreted as integrity constraints.
* @param targetDir Directory into which the vocabulary file will be generated
* @param pkg Package
* @param withOWLAPI Whether OWLAPI-based IRIs of the generated vocabulary items should be created as well
*/
public void generateVocabulary(String context, String pkg, String targetDir, boolean withOWLAPI) {
LOG.info("Generating vocabulary ...");
if (context == null) {
LOG.info(" - for all axioms");
} else {
LOG.info(" - for context '{}'.", context);
verifyContextExistence(context);
}
ContextDefinition def = (context == null) ? DEFAULT_CONTEXT : contexts.get(context);
new JavaTransformer().generateVocabulary(ontology, def, pkg, targetDir, withOWLAPI);
}
private class ValidContextAnnotationValueVisitor implements OWLAnnotationValueVisitor {
private String name = null;
String getName() {
return name;
}
public void visit(@Nonnull IRI iri) {
}
public void visit(@Nonnull OWLAnonymousIndividual individual) {
}
public void visit(@Nonnull OWLLiteral literal) {
name = literal.getLiteral();
}
}
}