Skip to content

Package: OWL2JavaTransformer$Card

OWL2JavaTransformer$Card

nameinstructionbranchcomplexitylinemethod
static {...}
M: 0 C: 64
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.owl2java;
16:
17: import cz.cvut.kbss.jopa.model.SequencesVocabulary;
18: import cz.cvut.kbss.jopa.util.MappingFileParser;
19: import org.semanticweb.owlapi.apibinding.OWLManager;
20: import org.semanticweb.owlapi.model.*;
21: import org.semanticweb.owlapi.util.OWLOntologyMerger;
22: import org.slf4j.Logger;
23: import org.slf4j.LoggerFactory;
24:
25: import java.io.File;
26: import java.net.URI;
27: import java.util.*;
28:
29: public class OWL2JavaTransformer {
30:
31: private static final Logger LOG = LoggerFactory.getLogger(OWL2JavaTransformer.class);
32:
33: public static final String P_IS_INTEGRITY_CONSTRAINT_FOR = "http://krizik.felk.cvut.cz/ontologies/2009/ic.owl#isIntegrityConstraintFor";
34:
35: private static final ContextDefinition DEFAULT_CONTEXT = new ContextDefinition("<DEFAULT>");
36:
37: private static final List<IRI> skipped = Arrays
38: .asList(IRI.create(SequencesVocabulary.c_Collection), IRI.create(SequencesVocabulary.c_List),
39: IRI.create(SequencesVocabulary.c_OWLSimpleList),
40: IRI.create(SequencesVocabulary.c_OWLReferencedList));
41:
42: private OWLOntology ontology;
43:
44: private Map<String, ContextDefinition> contexts = new HashMap<>();
45:
46: public Collection<String> listContexts() {
47: return contexts.keySet();
48: }
49:
50: private class ValidContextAnnotationValueVisitor implements OWLAnnotationValueVisitor {
51: private String name = null;
52:
53: String getName() {
54: return name;
55: }
56:
57: public void visit(IRI iri) {
58: }
59:
60: public void visit(OWLAnonymousIndividual individual) {
61: }
62:
63: public void visit(OWLLiteral literal) {
64: name = literal.getLiteral();
65: }
66: }
67:
68: private final ValidContextAnnotationValueVisitor v = new ValidContextAnnotationValueVisitor();
69:
70: private OWLOntology getWholeOntology(final String owlOntologyName, final String mappingFile) {
71: // reader
72: final OWLOntologyManager m = OWLManager.createOWLOntologyManager();
73:
74: if (mappingFile != null) {
75: LOG.info("Using mapping file '{}'.", mappingFile);
76:
77: final Map<URI, URI> map = MappingFileParser.getMappings(new File(mappingFile));
78: m.addIRIMapper(ontologyIRI -> {
79: final URI value = map.get(ontologyIRI.toURI());
80:
81: if (value == null) {
82: return null;
83: } else {
84: return IRI.create(value);
85: }
86: });
87: LOG.info("Mapping file successfully parsed.");
88: }
89:
90: LOG.info("Loading ontology {} ... ", owlOntologyName);
91: m.setSilentMissingImportsHandling(true);
92:
93: try {
94: m.loadOntology(org.semanticweb.owlapi.model.IRI.create(owlOntologyName));
95: return new OWLOntologyMerger(m)
96: .createMergedOntology(m, org.semanticweb.owlapi.model.IRI.create(owlOntologyName + "-generated"));
97: } catch (OWLOntologyCreationException e) {
98: LOG.error(e.getMessage(), e);
99: throw new IllegalArgumentException("Unable to load ontology " + owlOntologyName, e);
100: }
101: }
102:
103: private void addAxiomToContext(final ContextDefinition ctx, final OWLAxiom axiom) {
104: for (final OWLEntity e : axiom.getSignature()) {
105: if (e.isOWLClass() && !skipped.contains(e.getIRI())) {
106: ctx.classes.add(e.asOWLClass());
107: }
108: if (e.isOWLObjectProperty() && !skipped.contains(e.getIRI())) {
109: ctx.objectProperties.add(e.asOWLObjectProperty());
110: }
111: if (e.isOWLDataProperty() && !skipped.contains(e.getIRI())) {
112: ctx.dataProperties.add(e.asOWLDataProperty());
113: }
114: if (e.isOWLAnnotationProperty() && !skipped.contains(e.getIRI())) {
115: ctx.annotationProperties.add(e.asOWLAnnotationProperty());
116: }
117: if (e.isOWLNamedIndividual() && !skipped.contains(e.getIRI())) {
118: ctx.individuals.add(e.asOWLNamedIndividual());
119: }
120: }
121: ctx.axioms.add(axiom);
122: }
123:
124: public void setOntology(final String owlOntologyName,
125: final String mappingFile, boolean includeImports) {
126: ontology = getWholeOntology(owlOntologyName, mappingFile);
127:
128: // this.imports = ontology.getOWLOntologyManager().getOntologies();
129:
130: LOG.info("Parsing integrity constraints");
131:
132: for (final OWLAxiom a : ontology.getAxioms()) {
133: addAxiomToContext(DEFAULT_CONTEXT, a);
134: for (final String icContextName : getContexts(a)) {
135: ContextDefinition ctx = getContextDefinition(icContextName);
136: addAxiomToContext(ctx, a);
137: }
138: }
139:
140: DEFAULT_CONTEXT.parser.parse();
141: for (final ContextDefinition ctx : contexts.values()) {
142: ctx.parser.parse();
143: }
144:
145: LOG.info("Integrity constraints successfully parsed.");
146: }
147:
148: private ContextDefinition getContextDefinition(String icContextName) {
149: ContextDefinition ctx = contexts.get(icContextName);
150: if (ctx == null) {
151: ctx = new ContextDefinition(icContextName);
152: contexts.put(icContextName, ctx);
153: }
154: return ctx;
155: }
156:
157: private List<String> getContexts(final OWLAxiom a) {
158: final List<String> contexts = new ArrayList<>();
159: for (final OWLAnnotation p : a.getAnnotations()) {
160: LOG.info("Processing annotation : " + p);
161: if (!p.getProperty().getIRI().toString().equals(P_IS_INTEGRITY_CONSTRAINT_FOR)) {
162: continue;
163: }
164: p.getValue().accept(v);
165: final String icContextName = v.getName();
166: LOG.info("CONTEXT:" + icContextName);
167: if (icContextName == null) {
168: continue;
169: }
170: LOG.debug("Found IC {} for context {}", a, icContextName);
171: contexts.add(icContextName);
172: }
173: return contexts;
174: }
175:
176: enum Card {
177: NO, ONE, MULTIPLE, LIST, SIMPLELIST, REFERENCEDLIST
178: }
179:
180: private void verifyContextExistence(String context) {
181: if (!contexts.containsKey(context)) {
182: throw new IllegalArgumentException(
183: "Context " + context + " not found. Existing contexts: " + listContexts());
184: }
185: }
186:
187: public void transform(String context, String pkg, String targetDir, boolean withOWLAPI) {
188: LOG.info("Transforming context ...");
189: if (context == null) {
190: LOG.info(" - for all axioms");
191: } else {
192: LOG.info(" - for context '{}'.", context);
193: verifyContextExistence(context);
194: }
195:
196: ContextDefinition def = context == null ? DEFAULT_CONTEXT : contexts.get(context);
197: new JavaTransformer().generateModel(ontology, def, pkg, targetDir, withOWLAPI);
198: LOG.info("Transformation SUCCESSFUL.");
199: }
200:
201: /**
202: * Generates only vocabulary of the loaded ontology.
203: *
204: * @param context Integrity constraints context, if null is supplied, the whole ontology is interpreted as integrity constraints.
205: * @param targetDir Directory into which the vocabulary file will be generated
206: * @param pkg Package
207: * @param withOWLAPI Whether OWLAPI-based IRIs of the generated vocabulary items should be created as well
208: */
209: public void generateVocabulary(String context, String pkg, String targetDir, boolean withOWLAPI) {
210: LOG.info("Generating vocabulary ...");
211: if (context == null) {
212: LOG.info(" - for all axioms");
213: } else {
214: LOG.info(" - for context '{}'.", context);
215: verifyContextExistence(context);
216: }
217: ContextDefinition def = (context == null) ? DEFAULT_CONTEXT : contexts.get(context);
218: new JavaTransformer().
219: generateVocabulary(ontology, def, pkg, targetDir, withOWLAPI);
220: }
221: }