-
Notifications
You must be signed in to change notification settings - Fork 14
/
OWL2JavaTransformer.java
221 lines (185 loc) · 8.41 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
220
221
/**
* Copyright (C) 2016 Czech Technical University in Prague
*
* 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.
*
* 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.model.SequencesVocabulary;
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 java.io.File;
import java.net.URI;
import java.util.*;
public class OWL2JavaTransformer {
private static final Logger LOG = LoggerFactory.getLogger(OWL2JavaTransformer.class);
public static final String P_IS_INTEGRITY_CONSTRAINT_FOR = "http://krizik.felk.cvut.cz/ontologies/2009/ic.owl#isIntegrityConstraintFor";
private static final ContextDefinition DEFAULT_CONTEXT = new ContextDefinition("<DEFAULT>");
private static final List<IRI> skipped = Arrays
.asList(IRI.create(SequencesVocabulary.c_Collection), IRI.create(SequencesVocabulary.c_List),
IRI.create(SequencesVocabulary.c_OWLSimpleList),
IRI.create(SequencesVocabulary.c_OWLReferencedList));
private OWLOntology ontology;
private Map<String, ContextDefinition> contexts = new HashMap<>();
public Collection<String> listContexts() {
return contexts.keySet();
}
private class ValidContextAnnotationValueVisitor implements OWLAnnotationValueVisitor {
private String name = null;
String getName() {
return name;
}
public void visit(IRI iri) {
}
public void visit(OWLAnonymousIndividual individual) {
}
public void visit(OWLLiteral literal) {
name = literal.getLiteral();
}
}
private final ValidContextAnnotationValueVisitor v = new ValidContextAnnotationValueVisitor();
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.addIRIMapper(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);
m.setSilentMissingImportsHandling(true);
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 (OWLOntologyCreationException e) {
LOG.error(e.getMessage(), e);
throw new IllegalArgumentException("Unable to load ontology " + owlOntologyName, e);
}
}
private void addAxiomToContext(final ContextDefinition ctx, final OWLAxiom axiom) {
for (final OWLEntity e : axiom.getSignature()) {
if (e.isOWLClass() && !skipped.contains(e.getIRI())) {
ctx.classes.add(e.asOWLClass());
}
if (e.isOWLObjectProperty() && !skipped.contains(e.getIRI())) {
ctx.objectProperties.add(e.asOWLObjectProperty());
}
if (e.isOWLDataProperty() && !skipped.contains(e.getIRI())) {
ctx.dataProperties.add(e.asOWLDataProperty());
}
if (e.isOWLAnnotationProperty() && !skipped.contains(e.getIRI())) {
ctx.annotationProperties.add(e.asOWLAnnotationProperty());
}
if (e.isOWLNamedIndividual() && !skipped.contains(e.getIRI())) {
ctx.individuals.add(e.asOWLNamedIndividual());
}
}
ctx.axioms.add(axiom);
}
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");
for (final OWLAxiom a : ontology.getAxioms()) {
addAxiomToContext(DEFAULT_CONTEXT, a);
for (final String icContextName : getContexts(a)) {
ContextDefinition ctx = getContextDefinition(icContextName);
addAxiomToContext(ctx, a);
}
}
DEFAULT_CONTEXT.parser.parse();
for (final ContextDefinition ctx : contexts.values()) {
ctx.parser.parse();
}
LOG.info("Integrity constraints successfully parsed.");
}
private ContextDefinition getContextDefinition(String icContextName) {
ContextDefinition ctx = contexts.get(icContextName);
if (ctx == null) {
ctx = new ContextDefinition(icContextName);
contexts.put(icContextName, ctx);
}
return ctx;
}
private List<String> getContexts(final OWLAxiom a) {
final List<String> contexts = new ArrayList<>();
for (final OWLAnnotation p : a.getAnnotations()) {
LOG.info("Processing annotation : " + p);
if (!p.getProperty().getIRI().toString().equals(P_IS_INTEGRITY_CONSTRAINT_FOR)) {
continue;
}
p.getValue().accept(v);
final String icContextName = v.getName();
LOG.info("CONTEXT:" + icContextName);
if (icContextName == null) {
continue;
}
LOG.debug("Found IC {} for context {}", a, icContextName);
contexts.add(icContextName);
}
return contexts;
}
enum Card {
NO, ONE, MULTIPLE, LIST, SIMPLELIST, REFERENCEDLIST
}
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);
}
}