Skip to content

Package: OWL2JavaTransformer$ValidContextAnnotationValueVisitor

OWL2JavaTransformer$ValidContextAnnotationValueVisitor

nameinstructionbranchcomplexitylinemethod
OWL2JavaTransformer.ValidContextAnnotationValueVisitor(OWL2JavaTransformer)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getName()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
visit(IRI)
M: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
visit(OWLAnonymousIndividual)
M: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
visit(OWLLiteral)
M: 0 C: 5
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: * <p>
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: * <p>
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: }
118: ctx.axioms.add(axiom);
119: }
120:
121: public void setOntology(final String owlOntologyName,
122: final String mappingFile, boolean includeImports) {
123: ontology = getWholeOntology(owlOntologyName, mappingFile);
124:
125: // this.imports = ontology.getOWLOntologyManager().getOntologies();
126:
127: LOG.info("Parsing integrity constraints");
128:
129: for (final OWLAxiom a : ontology.getAxioms()) {
130: addAxiomToContext(DEFAULT_CONTEXT, a);
131: for (final String icContextName : getContexts(a)) {
132: ContextDefinition ctx = getContextDefinition(icContextName);
133: addAxiomToContext(ctx, a);
134: }
135: }
136:
137: DEFAULT_CONTEXT.parser.parse();
138: for (final ContextDefinition ctx : contexts.values()) {
139: ctx.parser.parse();
140: }
141:
142: LOG.info("Integrity constraints successfully parsed.");
143: }
144:
145: private ContextDefinition getContextDefinition(String icContextName) {
146: ContextDefinition ctx = contexts.get(icContextName);
147: if (ctx == null) {
148: ctx = new ContextDefinition(icContextName);
149: contexts.put(icContextName, ctx);
150: }
151: return ctx;
152: }
153:
154: private List<String> getContexts(final OWLAxiom a) {
155: final List<String> contexts = new ArrayList<>();
156: for (final OWLAnnotation p : a.getAnnotations()) {
157: LOG.info("Processing annotation : " + p);
158: if (!p.getProperty().getIRI().toString().equals(P_IS_INTEGRITY_CONSTRAINT_FOR)) {
159: continue;
160: }
161: p.getValue().accept(v);
162: final String icContextName = v.getName();
163: LOG.info("CONTEXT:" + icContextName);
164: if (icContextName == null) {
165: continue;
166: }
167: LOG.debug("Found IC {} for context {}", a, icContextName);
168: contexts.add(icContextName);
169: }
170: return contexts;
171: }
172:
173: enum Card {
174: NO, ONE, MULTIPLE, LIST, SIMPLELIST, REFERENCEDLIST
175: }
176:
177: private void verifyContextExistence(String context) {
178: if (!contexts.containsKey(context)) {
179: throw new IllegalArgumentException(
180: "Context " + context + " not found. Existing contexts: " + listContexts());
181: }
182: }
183:
184: public void transform(String context, String pkg, String targetDir, boolean withOWLAPI) {
185: LOG.info("Transforming context ...");
186: if (context == null) {
187: LOG.info(" - for all axioms");
188: } else {
189: LOG.info(" - for context '{}'.", context);
190: verifyContextExistence(context);
191: }
192:
193: ContextDefinition def = context == null ? DEFAULT_CONTEXT : contexts.get(context);
194: new JavaTransformer().generateModel(ontology, def, pkg, targetDir, withOWLAPI);
195: LOG.info("Transformation SUCCESSFUL.");
196: }
197:
198: /**
199: * Generates only vocabulary of the loaded ontology.
200: *
201: * @param context Integrity constraints context, if null is supplied, the whole ontology is interpreted as integrity constraints.
202: * @param targetDir Directory into which the vocabulary file will be generated
203: * @param pkg Package
204: * @param withOWLAPI Whether OWLAPI-based IRIs of the generated vocabulary items should be created as well
205: */
206: public void generateVocabulary(String context, String pkg, String targetDir, boolean withOWLAPI) {
207: LOG.info("Generating vocabulary ...");
208: if (context == null) {
209: LOG.info(" - for all axioms");
210: } else {
211: LOG.info(" - for context '{}'.", context);
212: verifyContextExistence(context);
213: }
214: ContextDefinition def = (context == null) ? DEFAULT_CONTEXT : contexts.get(context);
215: new JavaTransformer().
216: generateVocabulary(ontology, def, pkg, targetDir, withOWLAPI);
217: }
218: }